1
\documentclass{article}
4
\title{\$SPAD/src/macro}
5
\author{The Axiom Team}
19
#include "parse_aux.h"
22
#include "all_hyper_proto.H1"
30
* This routine keeps scanning until it reaches it pops off 1 more
31
* right brace then left brace
40
HDWindow *twin = gWindow;
42
int number_of_left_braces = 1;
45
while (number_of_left_braces) {
46
ret_val = get_token();
47
if (ret_val == EOF && number_of_left_braces) {
48
fprintf(stderr, "Scan_Hypertex: Unexpected End of File\n");
53
fprintf(stderr, "scan_HyperDoc: Unexpected Page Declaration\n");
56
fprintf(stderr, "scan_HyperDoc: Unexpected Macro Declaration\n");
59
number_of_left_braces++;
63
number_of_left_braces--;
88
/* Parse a given macro given the pointer to the unlaoded macro ** */
92
load_macro(MacroStore *macro)
104
save_scanner_state();
105
cfile = find_fp(macro->fpos);
110
/** First thing I should do is make sure that the name is correct ***/
111
get_expected_token(NewCommand);
112
get_expected_token(Lbrace);
113
get_expected_token(Macro);
114
if (strcmp(token.id, macro->name)) {
115
/** WOW, Somehow I had the location of the wrong macro **/
116
fprintf(stderr, "Expected macro name %s got insted %s in load_macro\n",
117
macro->name, token.id);
120
get_expected_token(Rbrace);
122
/** Next I should check to see if I have any parameters **/
124
if (token.type == Lsquarebrace) {
125
/** The person is telling me the number of macros he is going to use **/
126
get_expected_token(Word);
127
if (!number(token.id)) {
128
fprintf(stderr, "load_macro: Expected A Value Instead Got %s\n",
132
/** if it is a number, then I should store it in the parameter number
133
member of the macro structure **/
134
macro->number_parameters = atoi(token.id);
137
"The number of parameters is %d\n", macro->number_parameters);
139
get_expected_token(Rsquarebrace);
143
macro->number_parameters = 0;
145
/*** Now I should be able to check the token, and insure that I have read
146
a leftbrace, then the string will follow ****/
147
if (token.type != Lbrace) {
148
/** The macro is not in a group, uh oh **/
149
fprintf(stderr, "load_macro:Expected a Left Brace got type %d\n",
155
ret_val = fseek(cfile, macro->fpos.pos + start_fpos, 0);
156
size = fpos - start_fpos;
157
macro_buff = (char *) halloc((size + 1) * sizeof(char), "Macro_buf");
158
for (size = 0, trace = macro_buff; size < fpos - (start_fpos) - 1; size++)
159
*trace++ = getc(cfile);
162
restore_scanner_state();
167
/** Here are the functions and declarations for the parameter stack **/
168
ParameterList parameters = NULL;
172
init_parameter_elem(number)
175
init_parameter_elem(int number)
181
/** allocate the space neeeded **/
182
new = (ParameterList) halloc(sizeof(struct parameter_list_type),
184
/** now allocate the memeory for the pointers to the parameters **/
186
new->list = (char **) halloc(number * sizeof(char *), "Parameter List");
188
/** initialize my pointers **/
189
for (count = 0; count < number; count++)
190
(new->list)[count] = NULL;
192
new->number = number;
201
push_parameters(ParameterList new)
206
fprintf(stderr, "Tried pushing a null list onto the parameter stack\n");
210
new->next = parameters;
221
/** Simply pops the top of the parameter list, being good and freeing
231
parameters = old->next;
233
/** Free the parameter text and pointers **/
234
if (old->number >0) {
235
for (count = 0; count < old->number; count++)
236
if ( (old->list)[count] ) free((char *) (old->list)[count]);
240
free(old); /** free the parameter **/
254
* This routine loads a macro if needed, and then parses it from the
260
curr_node->type = Macro;
261
curr_node->space = token.id[-1];
262
curr_node->next = alloc_node();
263
curr_node = curr_node->next;
264
macro = (MacroStore *) hash_find(gWindow->fMacroHashTable, token.id);
267
macro->macro_string = load_macro(macro);
268
get_parameter_strings(macro->number_parameters, macro->name);
269
parse_from_string(macro->macro_string);
272
curr_node->type = Endmacro;
273
curr_node->next = alloc_node();
274
curr_node = curr_node->next;
278
curr_node->type = Endmacro;
279
if (pop_parameters())
283
"parse_macro: Tried to pop an empty paramter stack\n");
288
fprintf(stderr, "parse_macro: Unknown keyword %s\n", token.id);
293
#define numeric(c) ((c >= '0' && c <= '9')?1:0)
297
get_parameter_strings(number, macro_name)
301
get_parameter_strings(int number,char * macro_name)
304
static char buffer[4096];
310
ParameterList new = init_parameter_elem(number);
315
if (!number) { /* nothing to be done */
316
push_parameters(new);
319
for (count = 0; count < number; count++) {
321
if (token.type != Lbrace) {
322
/** The macro is not in a group, uh oh **/
323
fprintf(stderr, "Wrong number of arguments to the macro %s\n",
327
for (lbrace_counter = 1, buffer_pntr = buffer;
329
switch (c = get_char()) {
331
fprintf(stderr, "GetParameterStrings: Unexpected EOF\n");
343
/* uh oh, I have a paramter reference inside a paramter */
345
if (parameters == NULL) {
350
((buffer_pntr > buffer + 1) &&
351
*(buffer_pntr - 1) == '\\' &&
352
*(buffer_pntr - 2) != '\\') ||
353
((buffer_pntr > buffer) &&
354
*(buffer_pntr - 1) == '\\')) {
360
for (pc = 0; numeric(c); pc++) {
365
pnum_chars[pc] = '\0';
366
pnum = atoi(pnum_chars);
368
/* Now copy the paramter */
369
while ((parameters->list)[pnum - 1][pc] != '\0')
370
*buffer_pntr++ = (parameters->list)[pnum - 1][pc++];
379
/*** Now add it to the current parameter list **/
380
size = strlen(buffer) + 1;
381
new->list[count] = (char *) halloc(size, "Parameter Strings");
382
strcpy(new->list[count], buffer);
384
push_parameters(new);
391
parse_parameters(void)
396
if (!number(token.id)) {
398
"Parse_parameter: Error Expected a number, got %s instead\n", token.id);
402
if ((value = atoi(token.id)) > parameters->number) {
403
/** had a bad parameter number **/
405
"Parse_parameter: Had a bad parameter number %d\n", value);
409
parse_from_string((parameters->list)[value - 1]);
410
curr_node->type = Endparameter;
417
Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd.
420
Redistribution and use in source and binary forms, with or without
421
modification, are permitted provided that the following conditions are
424
- Redistributions of source code must retain the above copyright
425
notice, this list of conditions and the following disclaimer.
427
- Redistributions in binary form must reproduce the above copyright
428
notice, this list of conditions and the following disclaimer in
429
the documentation and/or other materials provided with the
432
- Neither the name of The Numerical ALgorithms Group Ltd. nor the
433
names of its contributors may be used to endorse or promote products
434
derived from this software without specific prior written permission.
436
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
437
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
438
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
439
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
440
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
441
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
442
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
443
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
444
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
445
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
446
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
455
\begin{thebibliography}{99}
457
\end{thebibliography}