Mercurial > octave-dspies
comparison src/lex.l @ 143:7849db4b6dbc
[project @ 1993-10-04 02:36:45 by jwe]
author | jwe |
---|---|
date | Mon, 04 Oct 1993 02:36:58 +0000 |
parents | 5a7e0475450a |
children | edfa5a96c5f1 |
comparison
equal
deleted
inserted
replaced
142:6906d6591452 | 143:7849db4b6dbc |
---|---|
28 %s STRING | 28 %s STRING |
29 %s MATRIX | 29 %s MATRIX |
30 | 30 |
31 %{ | 31 %{ |
32 | 32 |
33 // Arrange to get input via readline. | |
34 | |
35 #ifdef YY_INPUT | |
36 #undef YY_INPUT | |
37 #define YY_INPUT(buf,result,max_size) \ | |
38 if ((result = octave_read (buf, max_size)) < 0) \ | |
39 YY_FATAL_ERROR ("octave_read () in flex scanner failed"); | |
40 #endif | |
41 | |
42 // Try to avoid crashing out completely on fatal scanner errors. | |
43 | |
44 #ifdef YY_FATAL_ERROR | |
45 #undef YY_FATAL_ERROR | |
46 #define YY_FATAL_ERROR(msg) \ | |
47 do \ | |
48 { \ | |
49 error (msg); \ | |
50 jump_to_top_level (); \ | |
51 } \ | |
52 while (0) | |
53 #endif | |
54 | |
55 #include "input.h" | 33 #include "input.h" |
56 | 34 #include "token.h" |
57 // The type of an END token. This declaration is repeated in parse.y. | |
58 // It must appear before y.tab.h is included. | |
59 enum end_tok_type | |
60 { | |
61 simple_end, | |
62 for_end, | |
63 function_end, | |
64 if_end, | |
65 while_end, | |
66 }; | |
67 | |
68 // The type of a PLOT token. This declaration is repeated in parse.y. | |
69 // It must appear before y.tab.h is included. | |
70 enum plot_tok_type | |
71 { | |
72 two_dee = 2, | |
73 three_dee = 3, | |
74 }; | |
75 | 35 |
76 #include "SLStack.h" | 36 #include "SLStack.h" |
77 | 37 |
38 // Stack to hold tokens so that we can delete them when the parser is | |
39 // reset and avoid growing forever just because we are stashing some | |
40 // information. This has to appear before lex.h is included, because | |
41 // one of the macros defined there uses token_stack. | |
42 static SLStack <token*> token_stack; | |
43 | |
78 #include "variables.h" | 44 #include "variables.h" |
45 #include "octave.h" | |
79 #include "symtab.h" | 46 #include "symtab.h" |
80 #include "error.h" | 47 #include "error.h" |
81 #include "utils.h" | 48 #include "utils.h" |
82 #include "tree.h" | 49 #include "tree.h" |
83 #include "y.tab.h" | 50 #include "y.tab.h" |
125 static void grab_help_text (void); | 92 static void grab_help_text (void); |
126 static int match_any (char c, char *s); | 93 static int match_any (char c, char *s); |
127 static int next_token_is_bin_op (int spc_prev, char *yytext); | 94 static int next_token_is_bin_op (int spc_prev, char *yytext); |
128 static int next_token_is_postfix_unary_op (int spc_prev, char *yytext); | 95 static int next_token_is_postfix_unary_op (int spc_prev, char *yytext); |
129 static char *strip_trailing_whitespace (char *s); | 96 static char *strip_trailing_whitespace (char *s); |
130 | |
131 #define DO_COMMA_INSERT_CHECK yyless (do_comma_insert_check ()) | |
132 | |
133 #define RETURN(token) \ | |
134 do \ | |
135 { \ | |
136 current_input_column += yyleng; \ | |
137 quote_is_transpose = 0; \ | |
138 cant_be_identifier = 0; \ | |
139 convert_spaces_to_comma = 1; \ | |
140 return (token); \ | |
141 } \ | |
142 while (0) | |
143 | |
144 #define BIN_OP_RETURN(token) \ | |
145 do \ | |
146 { \ | |
147 current_input_column += yyleng; \ | |
148 quote_is_transpose = 0; \ | |
149 cant_be_identifier = 0; \ | |
150 convert_spaces_to_comma = 0; \ | |
151 return (token); \ | |
152 } \ | |
153 while (0) | |
154 | 97 |
155 %} | 98 %} |
156 | 99 |
157 D [0-9] | 100 D [0-9] |
158 S [ \t] | 101 S [ \t] |
180 current_input_column += yyleng; | 123 current_input_column += yyleng; |
181 } | 124 } |
182 | 125 |
183 <COMMENT>\n { | 126 <COMMENT>\n { |
184 BEGIN 0; | 127 BEGIN 0; |
185 current_input_column = 0; | 128 current_input_column = 1; |
186 quote_is_transpose = 0; | 129 quote_is_transpose = 0; |
187 cant_be_identifier = 0; | 130 cant_be_identifier = 0; |
188 convert_spaces_to_comma = 1; | 131 convert_spaces_to_comma = 1; |
189 return '\n'; | 132 return '\n'; |
190 } | 133 } |
191 | 134 |
192 <COMMENT><<EOF>> { RETURN (END_OF_INPUT); } | 135 <COMMENT><<EOF>> { TOK_RETURN (END_OF_INPUT); } |
193 | 136 |
194 <COMMENT>.*$ { current_input_column += yyleng; } | 137 <COMMENT>.*$ { current_input_column += yyleng; } |
195 | 138 |
196 <NEW_MATRIX>[^ \t\n] { | 139 <NEW_MATRIX>[^ \t\n] { |
197 yyless (0); | 140 yyless (0); |
204 } | 147 } |
205 | 148 |
206 <HELP_FCN>\n | | 149 <HELP_FCN>\n | |
207 <TEXT_FCN>\n { | 150 <TEXT_FCN>\n { |
208 BEGIN 0; | 151 BEGIN 0; |
209 current_input_column = 0; | 152 current_input_column = 1; |
210 quote_is_transpose = 0; | 153 quote_is_transpose = 0; |
211 cant_be_identifier = 0; | 154 cant_be_identifier = 0; |
212 convert_spaces_to_comma = 1; | 155 convert_spaces_to_comma = 1; |
213 return '\n'; | 156 return '\n'; |
214 } | 157 } |
215 | 158 |
216 <TEXT_FCN>[\;\,] { | 159 <TEXT_FCN>[\;\,] { |
217 if (doing_set) | 160 if (doing_set) |
218 { | 161 { |
219 yylval.string = strsave (yytext); | 162 yylval.tok_val = new token (yytext); |
220 RETURN (TEXT); | 163 token_stack.push (yylval.tok_val); |
164 TOK_RETURN (TEXT); | |
221 } | 165 } |
222 else | 166 else |
223 { | 167 { |
224 BEGIN 0; | 168 BEGIN 0; |
225 RETURN (','); | 169 TOK_RETURN (','); |
226 } | 170 } |
227 } | 171 } |
228 | 172 |
229 <HELP_FCN>[^ \t\n]*{S}* | | 173 <HELP_FCN>[^ \t\n]*{S}* | |
230 <TEXT_FCN>[^ \t\n\;\,]*{S}* { | 174 <TEXT_FCN>[^ \t\n\;\,]*{S}* { |
231 | 175 static char *tok = (char *) NULL; |
232 static char *tok = (char *) NULL; | 176 delete [] tok; |
233 delete [] tok; | 177 tok = strip_trailing_whitespace (yytext); |
234 tok = strip_trailing_whitespace (yytext); | 178 yylval.tok_val = new token (tok); |
235 | 179 token_stack.push (yylval.tok_val); |
236 yylval.string = strsave (tok); | 180 TOK_RETURN (TEXT); |
237 RETURN (TEXT); | 181 } |
238 } | |
239 | 182 |
240 <TEXT_FCN>\'{QSTR}*[\n\'] { | 183 <TEXT_FCN>\'{QSTR}*[\n\'] { |
241 if (yytext[yyleng-1] == '\n') | 184 if (yytext[yyleng-1] == '\n') |
242 { | 185 { |
243 error ("unterminated string constant"); | 186 error ("unterminated string constant"); |
244 current_input_column = 0; | 187 current_input_column = 1; |
245 jump_to_top_level (); | 188 jump_to_top_level (); |
246 } | 189 } |
247 else | 190 else |
248 { | 191 { |
192 static char *tok = (char *) NULL; | |
193 delete [] tok; | |
249 int off1 = doing_set ? 0 : 1; | 194 int off1 = doing_set ? 0 : 1; |
250 int off2 = doing_set ? 0 : 2; | 195 int off2 = doing_set ? 0 : 2; |
251 yylval.string = strsave (&yytext[off1]); | 196 tok = strsave (&yytext[off1]); |
252 yylval.string[yyleng-off2] = '\0'; | 197 tok[yyleng-off2] = '\0'; |
198 do_string_escapes (tok); | |
199 yylval.tok_val = new token (tok); | |
200 token_stack.push (yylval.tok_val); | |
253 current_input_column += yyleng; | 201 current_input_column += yyleng; |
254 } | 202 } |
255 do_string_escapes (yylval.string); | |
256 return TEXT; | 203 return TEXT; |
257 } | 204 } |
258 | 205 |
259 <TEXT_FCN>\"{DQSTR}*[\n\"] { | 206 <TEXT_FCN>\"{DQSTR}*[\n\"] { |
260 if (yytext[yyleng-1] == '\n') | 207 if (yytext[yyleng-1] == '\n') |
261 { | 208 { |
262 error ("unterminated string constant"); | 209 error ("unterminated string constant"); |
263 current_input_column = 0; | 210 current_input_column = 1; |
264 jump_to_top_level (); | 211 jump_to_top_level (); |
265 } | 212 } |
266 else | 213 else |
267 { | 214 { |
215 static char *tok = (char *) NULL; | |
216 delete [] tok; | |
268 int off1 = doing_set ? 0 : 1; | 217 int off1 = doing_set ? 0 : 1; |
269 int off2 = doing_set ? 0 : 2; | 218 int off2 = doing_set ? 0 : 2; |
270 yylval.string = strsave (&yytext[off1]); | 219 tok = strsave (&yytext[off1]); |
271 yylval.string[yyleng-off2] = '\0'; | 220 tok[yyleng-off2] = '\0'; |
221 do_string_escapes (tok); | |
222 yylval.tok_val = new token (tok); | |
223 token_stack.push (yylval.tok_val); | |
272 current_input_column += yyleng; | 224 current_input_column += yyleng; |
273 } | 225 } |
274 do_string_escapes (yylval.string); | |
275 return TEXT; | 226 return TEXT; |
276 } | 227 } |
277 | 228 |
278 <TEXT_FCN>{S}* { current_input_column += yyleng; } | 229 <TEXT_FCN>{S}* { current_input_column += yyleng; } |
279 | 230 |
284 BEGIN 0; | 235 BEGIN 0; |
285 | 236 |
286 if (yytext[yyleng-1] == '\n') | 237 if (yytext[yyleng-1] == '\n') |
287 { | 238 { |
288 error ("unterminated string constant"); | 239 error ("unterminated string constant"); |
289 current_input_column = 0; | 240 current_input_column = 1; |
290 jump_to_top_level (); | 241 jump_to_top_level (); |
291 } | 242 } |
292 else | 243 else |
293 { | 244 { |
294 yylval.string = strsave (yytext); | 245 static char *tok = (char *) NULL; |
295 yylval.string[yyleng-1] = '\0'; | 246 delete [] tok; |
247 tok = strsave (yytext); | |
248 tok[yyleng-1] = '\0'; | |
249 do_string_escapes (tok); | |
250 yylval.tok_val = new token (tok); | |
251 token_stack.push (yylval.tok_val); | |
252 quote_is_transpose = 1; | |
253 cant_be_identifier = 1; | |
254 convert_spaces_to_comma = 1; | |
296 current_input_column += yyleng; | 255 current_input_column += yyleng; |
297 } | 256 } |
298 do_string_escapes (yylval.string); | |
299 quote_is_transpose = 1; | |
300 cant_be_identifier = 1; | |
301 convert_spaces_to_comma = 1; | |
302 return TEXT; | 257 return TEXT; |
303 } | 258 } |
304 | 259 |
305 | 260 |
306 <DQSTRING>{DQSTR}*[\n\"] { | 261 <DQSTRING>{DQSTR}*[\n\"] { |
310 BEGIN 0; | 265 BEGIN 0; |
311 | 266 |
312 if (yytext[yyleng-1] == '\n') | 267 if (yytext[yyleng-1] == '\n') |
313 { | 268 { |
314 error ("unterminated string constant"); | 269 error ("unterminated string constant"); |
315 current_input_column = 0; | 270 current_input_column = 1; |
316 jump_to_top_level (); | 271 jump_to_top_level (); |
317 } | 272 } |
318 else | 273 else |
319 { | 274 { |
320 yylval.string = strsave (yytext); | 275 static char *tok = (char *) NULL; |
321 yylval.string[yyleng-1] = '\0'; | 276 delete [] tok; |
277 tok = strsave (yytext); | |
278 tok[yyleng-1] = '\0'; | |
279 do_string_escapes (tok); | |
280 yylval.tok_val = new token (tok); | |
281 token_stack.push (yylval.tok_val); | |
282 quote_is_transpose = 1; | |
283 cant_be_identifier = 1; | |
284 convert_spaces_to_comma = 1; | |
322 current_input_column += yyleng; | 285 current_input_column += yyleng; |
323 } | 286 } |
324 do_string_escapes (yylval.string); | |
325 quote_is_transpose = 1; | |
326 cant_be_identifier = 1; | |
327 convert_spaces_to_comma = 1; | |
328 return TEXT; | 287 return TEXT; |
329 } | 288 } |
330 | 289 |
331 <MATRIX>{SN}*\]{S}*/== { | 290 <MATRIX>{SN}*\]{S}*/== { |
332 | 291 |
345 | 304 |
346 in_brace_or_paren.pop (); | 305 in_brace_or_paren.pop (); |
347 braceflag--; | 306 braceflag--; |
348 if (braceflag == 0) | 307 if (braceflag == 0) |
349 { | 308 { |
350 if (!defining_func) | 309 if (! defining_func) |
351 promptflag++; | 310 promptflag++; |
352 BEGIN 0; | 311 BEGIN 0; |
353 } | 312 } |
354 fixup_column_count (yytext); | 313 fixup_column_count (yytext); |
355 quote_is_transpose = 0; | 314 quote_is_transpose = 0; |
362 in_brace_or_paren.pop (); | 321 in_brace_or_paren.pop (); |
363 braceflag--; | 322 braceflag--; |
364 if (braceflag == 0) | 323 if (braceflag == 0) |
365 { | 324 { |
366 BEGIN 0; | 325 BEGIN 0; |
367 if (!defining_func) | 326 if (! defining_func) |
368 promptflag++; | 327 promptflag++; |
369 } | 328 } |
370 fixup_column_count (yytext); | 329 fixup_column_count (yytext); |
371 quote_is_transpose = 0; | 330 quote_is_transpose = 0; |
372 cant_be_identifier = 0; | 331 cant_be_identifier = 0; |
382 | 341 |
383 in_brace_or_paren.pop (); | 342 in_brace_or_paren.pop (); |
384 braceflag--; | 343 braceflag--; |
385 if (braceflag == 0) | 344 if (braceflag == 0) |
386 { | 345 { |
387 if (!defining_func) | 346 if (! defining_func) |
388 promptflag++; | 347 promptflag++; |
389 BEGIN 0; | 348 BEGIN 0; |
390 } | 349 } |
391 else | 350 else |
392 { | 351 { |
415 cant_be_identifier = 0; | 374 cant_be_identifier = 0; |
416 convert_spaces_to_comma = 1; | 375 convert_spaces_to_comma = 1; |
417 return ']'; | 376 return ']'; |
418 } | 377 } |
419 | 378 |
420 <MATRIX>{S}*\,{S}* { RETURN (','); } | 379 <MATRIX>{S}*\,{S}* { TOK_RETURN (','); } |
421 | 380 |
422 <MATRIX>{S}+ { | 381 <MATRIX>{S}+ { |
423 int bin_op = next_token_is_bin_op (1, yytext); | 382 int bin_op = next_token_is_bin_op (1, yytext); |
424 int postfix_un_op | 383 int postfix_un_op |
425 = next_token_is_postfix_unary_op (1, yytext); | 384 = next_token_is_postfix_unary_op (1, yytext); |
426 | 385 |
427 if (! (postfix_un_op || bin_op) | 386 if (! (postfix_un_op || bin_op) |
428 && in_brace_or_paren.top () | 387 && in_brace_or_paren.top () |
429 && convert_spaces_to_comma) | 388 && convert_spaces_to_comma) |
430 RETURN (','); | 389 TOK_RETURN (','); |
431 } | 390 } |
432 | 391 |
433 <MATRIX>{SN}*\;{SN}* | | 392 <MATRIX>{SN}*\;{SN}* | |
434 <MATRIX>{N}{SN}* { | 393 <MATRIX>{N}{SN}* { |
435 fixup_column_count (yytext); | 394 fixup_column_count (yytext); |
444 in_brace_or_paren.pop (); | 403 in_brace_or_paren.pop (); |
445 | 404 |
446 if (plotting && ! past_plot_range) | 405 if (plotting && ! past_plot_range) |
447 { | 406 { |
448 in_plot_range = 0; | 407 in_plot_range = 0; |
449 RETURN (CLOSE_BRACE); | 408 TOK_RETURN (CLOSE_BRACE); |
450 } | 409 } |
451 else | 410 else |
452 RETURN (']'); | 411 TOK_RETURN (']'); |
453 } | 412 } |
454 | 413 |
455 {D}+{EXPON}?{Im} | | 414 {D}+{EXPON}?{Im} | |
456 {D}+\.{D}*{EXPON}?{Im} | | 415 {D}+\.{D}*{EXPON}?{Im} | |
457 \.{D}+{EXPON}?{Im} { | 416 \.{D}+{EXPON}?{Im} { |
458 int nread = sscanf (yytext, "%lf", &(yylval.number)); | 417 double value; |
418 int nread = sscanf (yytext, "%lf", &value); | |
459 assert (nread == 1); | 419 assert (nread == 1); |
460 quote_is_transpose = 1; | 420 quote_is_transpose = 1; |
461 cant_be_identifier = 1; | 421 cant_be_identifier = 1; |
462 convert_spaces_to_comma = 1; | 422 convert_spaces_to_comma = 1; |
463 if (plotting && ! in_plot_range) | 423 if (plotting && ! in_plot_range) |
464 past_plot_range = 1; | 424 past_plot_range = 1; |
425 yylval.tok_val = new token (value, | |
426 input_line_number, | |
427 current_input_column); | |
428 token_stack.push (yylval.tok_val); | |
465 current_input_column += yyleng; | 429 current_input_column += yyleng; |
466 DO_COMMA_INSERT_CHECK; | 430 DO_COMMA_INSERT_CHECK; |
467 return IMAG_NUM; | 431 return IMAG_NUM; |
468 } | 432 } |
469 | 433 |
470 {D}+{EXPON}? | | 434 {D}+{EXPON}? | |
471 {D}+\.{D}*{EXPON}? | | 435 {D}+\.{D}*{EXPON}? | |
472 \.{D}+{EXPON}? | | 436 \.{D}+{EXPON}? | |
473 { | 437 { |
474 int nread = sscanf (yytext, "%lf", &(yylval.number)); | 438 double value; |
439 int nread = sscanf (yytext, "%lf", &value); | |
475 assert (nread == 1); | 440 assert (nread == 1); |
476 quote_is_transpose = 1; | 441 quote_is_transpose = 1; |
477 cant_be_identifier = 1; | 442 cant_be_identifier = 1; |
478 convert_spaces_to_comma = 1; | 443 convert_spaces_to_comma = 1; |
479 if (plotting && ! in_plot_range) | 444 if (plotting && ! in_plot_range) |
480 past_plot_range = 1; | 445 past_plot_range = 1; |
446 yylval.tok_val = new token (value, | |
447 input_line_number, | |
448 current_input_column); | |
449 token_stack.push (yylval.tok_val); | |
481 current_input_column += yyleng; | 450 current_input_column += yyleng; |
482 DO_COMMA_INSERT_CHECK; | 451 DO_COMMA_INSERT_CHECK; |
483 return NUM; | 452 return NUM; |
484 } | 453 } |
485 | 454 |
486 \[{S}* { | 455 \[{S}* { |
487 in_brace_or_paren.push (1); | 456 in_brace_or_paren.push (1); |
488 if (plotting && ! past_plot_range) | 457 if (plotting && ! past_plot_range) |
489 { | 458 { |
490 in_plot_range = 1; | 459 in_plot_range = 1; |
491 RETURN (OPEN_BRACE); | 460 TOK_RETURN (OPEN_BRACE); |
492 } | 461 } |
493 | 462 |
494 if (do_comma_insert) | 463 if (do_comma_insert) |
495 { | 464 { |
496 yyless (0); | 465 yyless (0); |
504 { | 473 { |
505 mlnm.push (1); | 474 mlnm.push (1); |
506 braceflag++; | 475 braceflag++; |
507 promptflag--; | 476 promptflag--; |
508 BEGIN NEW_MATRIX; | 477 BEGIN NEW_MATRIX; |
509 RETURN ('['); | 478 TOK_RETURN ('['); |
510 } | 479 } |
511 } | 480 } |
512 | 481 |
513 {S}* { current_input_column += yyleng; } | 482 {S}* { current_input_column += yyleng; } |
514 | 483 |
515 {EL}{S}*\n { | 484 {EL}{S}*\n { |
516 | 485 |
517 // Line continuation. | 486 // Line continuation. |
518 | 487 |
519 promptflag--; | 488 promptflag--; |
520 current_input_column = 0; | 489 current_input_column = 1; |
521 } | 490 } |
522 | 491 |
523 <<EOF>> RETURN (END_OF_INPUT); | 492 <<EOF>> TOK_RETURN (END_OF_INPUT); |
524 | 493 |
525 {IDENT}{S}* { | 494 {IDENT}{S}* { |
526 | 495 |
527 // Truncate the token at the first space or tab but don't write | 496 // Truncate the token at the first space or tab but don't write |
528 // directly on yytext. | 497 // directly on yytext. |
531 delete [] tok; | 500 delete [] tok; |
532 tok = strip_trailing_whitespace (yytext); | 501 tok = strip_trailing_whitespace (yytext); |
533 | 502 |
534 int kw_token = is_keyword (tok); | 503 int kw_token = is_keyword (tok); |
535 if (kw_token) | 504 if (kw_token) |
536 RETURN (kw_token); | 505 TOK_RETURN (kw_token); |
537 | 506 |
538 if (plotting && cant_be_identifier) | 507 if (plotting && cant_be_identifier) |
539 { | 508 { |
540 int plot_option_kw = is_plot_keyword (tok); | 509 int plot_option_kw = is_plot_keyword (tok); |
541 if (plot_option_kw) | 510 if (plot_option_kw) |
554 if (plotting && in_plot_style) | 523 if (plotting && in_plot_style) |
555 { | 524 { |
556 char *sty = plot_style_token (&tok[1]); | 525 char *sty = plot_style_token (&tok[1]); |
557 if (sty != (char *) NULL) | 526 if (sty != (char *) NULL) |
558 { | 527 { |
559 yylval.string = strsave (sty); | 528 yylval.tok_val = new token (sty); |
529 token_stack.push (yylval.tok_val); | |
560 if (in_plot_style) | 530 if (in_plot_style) |
561 { | 531 { |
562 in_plot_style = 0; | 532 in_plot_style = 0; |
563 RETURN (STYLE); | 533 TOK_RETURN (STYLE); |
564 } | 534 } |
565 } | 535 } |
566 } | 536 } |
567 | 537 |
568 cant_be_identifier = 1; | 538 cant_be_identifier = 1; |
575 if (is_text_function_name (tok)) | 545 if (is_text_function_name (tok)) |
576 { | 546 { |
577 BEGIN TEXT_FCN; | 547 BEGIN TEXT_FCN; |
578 | 548 |
579 if (strcmp (tok, "clear") == 0) | 549 if (strcmp (tok, "clear") == 0) |
580 return CLEAR; | 550 { |
551 symbol_record *sr = | |
552 global_sym_tab->lookup ("clear", 1, 0); | |
553 assert (sr != (symbol_record *) NULL); | |
554 yylval.tok_val = new token (sr, input_line_number, | |
555 current_input_column); | |
556 token_stack.push (yylval.tok_val); | |
557 return CLEAR; | |
558 } | |
581 else if (strcmp (tok, "help") == 0) | 559 else if (strcmp (tok, "help") == 0) |
582 BEGIN HELP_FCN; | 560 BEGIN HELP_FCN; |
583 else if (strcmp (tok, "set") == 0) | 561 else if (strcmp (tok, "set") == 0) |
584 doing_set = 1; | 562 doing_set = 1; |
585 } | 563 } |
586 | 564 |
587 yylval.sym_rec = lookup_identifier (tok); | 565 yylval.tok_val = new token (lookup_identifier (tok), |
566 input_line_number, | |
567 current_input_column); | |
568 token_stack.push (yylval.tok_val); | |
588 | 569 |
589 quote_is_transpose = 1; | 570 quote_is_transpose = 1; |
590 current_input_column += yyleng; | 571 current_input_column += yyleng; |
591 DO_COMMA_INSERT_CHECK; | 572 DO_COMMA_INSERT_CHECK; |
592 | 573 |
629 // the case above. I suppose it would be nice to avoid duplicating | 610 // the case above. I suppose it would be nice to avoid duplicating |
630 // all the code, eh? | 611 // all the code, eh? |
631 | 612 |
632 int kw_token = is_keyword (yytext); | 613 int kw_token = is_keyword (yytext); |
633 if (kw_token) | 614 if (kw_token) |
634 RETURN (kw_token); | 615 TOK_RETURN (kw_token); |
635 | 616 |
636 if (plotting && cant_be_identifier) | 617 if (plotting && cant_be_identifier) |
637 { | 618 { |
638 int plot_option_kw = is_plot_keyword (yytext); | 619 int plot_option_kw = is_plot_keyword (yytext); |
639 if (plot_option_kw) | 620 if (plot_option_kw) |
655 if (is_text_function_name (yytext)) | 636 if (is_text_function_name (yytext)) |
656 { | 637 { |
657 BEGIN TEXT_FCN; | 638 BEGIN TEXT_FCN; |
658 | 639 |
659 if (strcmp (yytext, "clear") == 0) | 640 if (strcmp (yytext, "clear") == 0) |
660 return CLEAR; | 641 { |
642 symbol_record *sr = | |
643 global_sym_tab->lookup ("clear", 1, 0); | |
644 assert (sr != (symbol_record *) NULL); | |
645 yylval.tok_val = new token (sr, input_line_number, | |
646 current_input_column); | |
647 token_stack.push (yylval.tok_val); | |
648 return CLEAR; | |
649 } | |
661 else if (strcmp (yytext, "help") == 0) | 650 else if (strcmp (yytext, "help") == 0) |
662 BEGIN HELP_FCN; | 651 BEGIN HELP_FCN; |
663 else if (strcmp (yytext, "set") == 0) | 652 else if (strcmp (yytext, "set") == 0) |
664 doing_set = 1; | 653 doing_set = 1; |
665 } | 654 } |
666 | 655 |
667 if (defining_func && maybe_screwed) | 656 if (defining_func && maybe_screwed) |
668 curr_sym_tab = tmp_local_sym_tab; | 657 curr_sym_tab = tmp_local_sym_tab; |
669 | 658 |
670 yylval.sym_rec = lookup_identifier (yytext); | 659 yylval.tok_val = new token (lookup_identifier (yytext), |
660 input_line_number, | |
661 current_input_column); | |
662 token_stack.push (yylval.tok_val); | |
671 | 663 |
672 convert_spaces_to_comma = 1; | 664 convert_spaces_to_comma = 1; |
673 current_input_column += yyleng; | 665 current_input_column += yyleng; |
674 if (defining_func && maybe_screwed) | 666 if (defining_func && maybe_screwed) |
675 { | 667 { |
684 } | 676 } |
685 | 677 |
686 "\n" { | 678 "\n" { |
687 quote_is_transpose = 0; | 679 quote_is_transpose = 0; |
688 cant_be_identifier = 0; | 680 cant_be_identifier = 0; |
689 current_input_column = 0; | 681 current_input_column = 1; |
690 convert_spaces_to_comma = 1; | 682 convert_spaces_to_comma = 1; |
691 return '\n'; | 683 return '\n'; |
692 } | 684 } |
693 | 685 |
694 "'" { | 686 "'" { |
704 BEGIN STRING; | 696 BEGIN STRING; |
705 } | 697 } |
706 | 698 |
707 ":" { | 699 ":" { |
708 if (plotting && (in_plot_range || in_plot_using)) | 700 if (plotting && (in_plot_range || in_plot_using)) |
709 RETURN (COLON); | 701 BIN_OP_RETURN (COLON, 1); |
710 else | 702 else |
711 BIN_OP_RETURN (':'); | 703 BIN_OP_RETURN (':', 0); |
712 } | 704 } |
713 | 705 |
714 \" { BEGIN DQSTRING; } | 706 \" { BEGIN DQSTRING; } |
715 ".**" { BIN_OP_RETURN (EPOW); } | 707 ".**" { BIN_OP_RETURN (EPOW, 0); } |
716 ".*" { BIN_OP_RETURN (EMUL); } | 708 ".*" { BIN_OP_RETURN (EMUL, 0); } |
717 "./" { BIN_OP_RETURN (EDIV); } | 709 "./" { BIN_OP_RETURN (EDIV, 0); } |
718 ".\\" { BIN_OP_RETURN (ELEFTDIV); } | 710 ".\\" { BIN_OP_RETURN (ELEFTDIV, 0); } |
719 ".^" { BIN_OP_RETURN (EPOW); } | 711 ".^" { BIN_OP_RETURN (EPOW, 0); } |
720 ".'" { DO_COMMA_INSERT_CHECK; RETURN (TRANSPOSE); } | 712 ".'" { DO_COMMA_INSERT_CHECK; BIN_OP_RETURN (TRANSPOSE, 1); } |
721 "++" { DO_COMMA_INSERT_CHECK; RETURN (PLUS_PLUS); } | 713 "++" { DO_COMMA_INSERT_CHECK; BIN_OP_RETURN (PLUS_PLUS, 1); } |
722 "--" { DO_COMMA_INSERT_CHECK; RETURN (MINUS_MINUS); } | 714 "--" { DO_COMMA_INSERT_CHECK; BIN_OP_RETURN (MINUS_MINUS, 1); } |
723 "<=" { BIN_OP_RETURN (EXPR_LE); } | 715 "<=" { BIN_OP_RETURN (EXPR_LE, 0); } |
724 "==" { BIN_OP_RETURN (EXPR_EQ); } | 716 "==" { BIN_OP_RETURN (EXPR_EQ, 0); } |
725 "~=" { BIN_OP_RETURN (EXPR_NE); } | 717 "~=" { BIN_OP_RETURN (EXPR_NE, 0); } |
726 "!=" { BIN_OP_RETURN (EXPR_NE); } | 718 "!=" { BIN_OP_RETURN (EXPR_NE, 0); } |
727 "<>" { BIN_OP_RETURN (EXPR_NE); } | 719 "<>" { BIN_OP_RETURN (EXPR_NE, 0); } |
728 ">=" { BIN_OP_RETURN (EXPR_GE); } | 720 ">=" { BIN_OP_RETURN (EXPR_GE, 0); } |
729 "||" { BIN_OP_RETURN (EXPR_OR); } | 721 "||" { BIN_OP_RETURN (EXPR_OR, 0); } |
730 "&&" { BIN_OP_RETURN (EXPR_AND); } | 722 "&&" { BIN_OP_RETURN (EXPR_AND, 0); } |
731 "|" { BIN_OP_RETURN (EXPR_OR); } | 723 "|" { BIN_OP_RETURN (EXPR_OR, 0); } |
732 "&" { BIN_OP_RETURN (EXPR_AND); } | 724 "&" { BIN_OP_RETURN (EXPR_AND, 0); } |
733 "!" { | 725 "!" { |
734 if (plotting && ! in_plot_range) | 726 if (plotting && ! in_plot_range) |
735 past_plot_range = 1; | 727 past_plot_range = 1; |
736 RETURN (EXPR_NOT); | 728 BIN_OP_RETURN (EXPR_NOT, 1); |
737 } | 729 } |
738 "~" { | 730 "~" { |
739 if (plotting && ! in_plot_range) | 731 if (plotting && ! in_plot_range) |
740 past_plot_range = 1; | 732 past_plot_range = 1; |
741 BIN_OP_RETURN (EXPR_NOT); | 733 BIN_OP_RETURN (EXPR_NOT, 0); |
742 } | 734 } |
743 "<" { BIN_OP_RETURN (EXPR_LT); } | 735 "<" { BIN_OP_RETURN (EXPR_LT, 0); } |
744 ">" { BIN_OP_RETURN (EXPR_GT); } | 736 ">" { BIN_OP_RETURN (EXPR_GT, 0); } |
745 "+" { | 737 "+" { |
746 if (plotting && ! in_plot_range) | 738 if (plotting && ! in_plot_range) |
747 past_plot_range = 1; | 739 past_plot_range = 1; |
748 BIN_OP_RETURN ('+'); | 740 BIN_OP_RETURN ('+', 0); |
749 } | 741 } |
750 "-" { | 742 "-" { |
751 if (plotting && ! in_plot_range) | 743 if (plotting && ! in_plot_range) |
752 past_plot_range = 1; | 744 past_plot_range = 1; |
753 BIN_OP_RETURN ('-'); | 745 BIN_OP_RETURN ('-', 0); |
754 } | 746 } |
755 "**" { BIN_OP_RETURN (POW); } | 747 "**" { BIN_OP_RETURN (POW, 0); } |
756 "*" { BIN_OP_RETURN ('*'); } | 748 "*" { BIN_OP_RETURN ('*', 0); } |
757 "/" { BIN_OP_RETURN ('/'); } | 749 "/" { BIN_OP_RETURN ('/', 0); } |
758 "\\" { BIN_OP_RETURN (LEFTDIV); } | 750 "\\" { BIN_OP_RETURN (LEFTDIV, 0); } |
759 ";" { RETURN (';'); } | 751 ";" { BIN_OP_RETURN (';', 1); } |
760 "," { RETURN (','); } | 752 "," { BIN_OP_RETURN (',', 1); } |
761 "^" { BIN_OP_RETURN (POW); } | 753 "^" { BIN_OP_RETURN (POW, 0); } |
762 "=" { RETURN ('='); } | 754 "=" { BIN_OP_RETURN ('=', 1); } |
763 "(" { | 755 "(" { |
764 if (plotting && ! in_plot_range) | 756 if (plotting && ! in_plot_range) |
765 past_plot_range = 1; | 757 past_plot_range = 1; |
766 in_brace_or_paren.push (0); | 758 in_brace_or_paren.push (0); |
767 RETURN ('('); | 759 TOK_RETURN ('('); |
768 } | 760 } |
769 ")" { | 761 ")" { |
770 if (! in_brace_or_paren.empty ()) | 762 if (! in_brace_or_paren.empty ()) |
771 in_brace_or_paren.pop (); | 763 in_brace_or_paren.pop (); |
772 DO_COMMA_INSERT_CHECK; | 764 DO_COMMA_INSERT_CHECK; |
778 . { | 770 . { |
779 | 771 |
780 // We return everything else as single character tokens, which should | 772 // We return everything else as single character tokens, which should |
781 // eventually result in a parse error. | 773 // eventually result in a parse error. |
782 | 774 |
783 RETURN (yytext[0]); | 775 TOK_RETURN (yytext[0]); |
784 } | 776 } |
785 | 777 |
786 %% | 778 %% |
787 | 779 |
788 /* | 780 /* |
799 do_comma_insert = (braceflag && c == '['); | 791 do_comma_insert = (braceflag && c == '['); |
800 return tmp_len; | 792 return tmp_len; |
801 } | 793 } |
802 | 794 |
803 /* | 795 /* |
804 * Fix things up for errors or interrupts. | 796 * Fix things up for errors or interrupts. This could use a few |
797 * comments now, eh? | |
805 */ | 798 */ |
806 void | 799 void |
807 reset_parser (void) | 800 reset_parser (void) |
808 { | 801 { |
809 BEGIN 0; | 802 BEGIN 0; |
818 mlnm.clear (); | 811 mlnm.clear (); |
819 defining_func = 0; | 812 defining_func = 0; |
820 curr_sym_tab = top_level_sym_tab; | 813 curr_sym_tab = top_level_sym_tab; |
821 get_input_from_eval_string = 0; | 814 get_input_from_eval_string = 0; |
822 quote_is_transpose = 0; | 815 quote_is_transpose = 0; |
823 current_input_column = 0; | 816 current_input_column = 1; |
817 // Might have been reset by defining a function. | |
818 input_line_number = current_command_number - 1; | |
824 do_comma_insert = 0; | 819 do_comma_insert = 0; |
825 plotting = 0; | 820 plotting = 0; |
826 past_plot_range = 0; | 821 past_plot_range = 0; |
827 in_plot_range = 0; | 822 in_plot_range = 0; |
828 in_plot_using = 0; | 823 in_plot_using = 0; |
829 in_plot_style = 0; | 824 in_plot_style = 0; |
830 cant_be_identifier = 0; | 825 cant_be_identifier = 0; |
831 convert_spaces_to_comma = 1; | 826 convert_spaces_to_comma = 1; |
832 beginning_of_function = 0; | 827 beginning_of_function = 0; |
833 in_brace_or_paren.clear (); | 828 in_brace_or_paren.clear (); |
829 while (! token_stack.empty ()) | |
830 delete token_stack.pop (); | |
834 yyrestart (stdin); | 831 yyrestart (stdin); |
835 } | 832 } |
836 | 833 |
837 static void | 834 static void |
838 do_string_escapes (char *s) | 835 do_string_escapes (char *s) |
904 { | 901 { |
905 char c; | 902 char c; |
906 while ((c = *s++) != '\0') | 903 while ((c = *s++) != '\0') |
907 { | 904 { |
908 if (c == '\n') | 905 if (c == '\n') |
909 current_input_column = 0; | 906 current_input_column = 1; |
910 else | 907 else |
911 current_input_column++; | 908 current_input_column++; |
912 } | 909 } |
913 } | 910 } |
914 | 911 |
1025 { | 1022 { |
1026 char *sty = plot_style_token (s); | 1023 char *sty = plot_style_token (s); |
1027 if (sty != (char *) NULL) | 1024 if (sty != (char *) NULL) |
1028 { | 1025 { |
1029 in_plot_style = 0; | 1026 in_plot_style = 0; |
1030 yylval.string = strsave (sty); | 1027 yylval.tok_val = new token (sty); |
1028 token_stack.push (yylval.tok_val); | |
1031 return STYLE; | 1029 return STYLE; |
1032 } | 1030 } |
1033 } | 1031 } |
1034 | 1032 |
1033 int l = input_line_number; | |
1034 int c = current_input_column; | |
1035 | |
1035 int end_found = 0; | 1036 int end_found = 0; |
1036 if (strcmp ("break", s) == 0) | 1037 if (strcmp ("break", s) == 0) |
1037 return BREAK; | 1038 { |
1039 return BREAK; | |
1040 } | |
1038 else if (strcmp ("continue", s) == 0) | 1041 else if (strcmp ("continue", s) == 0) |
1039 return CONTINUE; | 1042 { |
1043 return CONTINUE; | |
1044 } | |
1040 else if (strcmp ("else", s) == 0) | 1045 else if (strcmp ("else", s) == 0) |
1041 { return ELSE; } | 1046 { |
1047 return ELSE; | |
1048 } | |
1042 else if (strcmp ("elseif", s) == 0) | 1049 else if (strcmp ("elseif", s) == 0) |
1043 { return ELSEIF; } | 1050 { |
1051 return ELSEIF; | |
1052 } | |
1044 else if (strcmp ("end", s) == 0) | 1053 else if (strcmp ("end", s) == 0) |
1045 { end_found = 1; yylval.ettype = simple_end; } | 1054 { |
1055 end_found = 1; | |
1056 yylval.tok_val = new token (token::simple_end, l, c); | |
1057 token_stack.push (yylval.tok_val); | |
1058 } | |
1046 else if (strcmp ("endfor", s) == 0) | 1059 else if (strcmp ("endfor", s) == 0) |
1047 { end_found = 1; yylval.ettype = for_end; } | 1060 { |
1061 end_found = 1; | |
1062 yylval.tok_val = new token (token::for_end, l, c); | |
1063 token_stack.push (yylval.tok_val); | |
1064 } | |
1048 else if (strcmp ("endfunction", s) == 0) | 1065 else if (strcmp ("endfunction", s) == 0) |
1049 { end_found = 1; yylval.ettype = function_end; } | 1066 { |
1067 end_found = 1; | |
1068 yylval.tok_val = new token (token::function_end, l, c); | |
1069 token_stack.push (yylval.tok_val); | |
1070 } | |
1050 else if (strcmp ("endif", s) == 0) | 1071 else if (strcmp ("endif", s) == 0) |
1051 { end_found = 1; yylval.ettype = if_end; } | 1072 { |
1073 end_found = 1; | |
1074 yylval.tok_val = new token (token::if_end, l, c); | |
1075 token_stack.push (yylval.tok_val); | |
1076 } | |
1052 else if (strcmp ("endwhile", s) == 0) | 1077 else if (strcmp ("endwhile", s) == 0) |
1053 { end_found = 1; yylval.ettype = while_end; } | 1078 { |
1079 end_found = 1; | |
1080 yylval.tok_val = new token (token::while_end, l, c); | |
1081 token_stack.push (yylval.tok_val); | |
1082 } | |
1054 else if (strcmp ("for", s) == 0) | 1083 else if (strcmp ("for", s) == 0) |
1055 { promptflag--; looping++; return FOR; } | 1084 { |
1085 promptflag--; | |
1086 looping++; | |
1087 return FOR; | |
1088 } | |
1056 else if (strcmp ("function", s) == 0) | 1089 else if (strcmp ("function", s) == 0) |
1057 { | 1090 { |
1058 if (defining_func) | 1091 if (defining_func) |
1059 { | 1092 { |
1060 error ("sorry, nested functions are a no-no..."); | 1093 error ("sorry, nested functions are a no-no..."); |
1066 curr_sym_tab = tmp_local_sym_tab; | 1099 curr_sym_tab = tmp_local_sym_tab; |
1067 defining_func = 1; | 1100 defining_func = 1; |
1068 promptflag--; | 1101 promptflag--; |
1069 beginning_of_function = 1; | 1102 beginning_of_function = 1; |
1070 help_buf[0] = '\0'; | 1103 help_buf[0] = '\0'; |
1104 input_line_number = 1; | |
1071 return FCN; | 1105 return FCN; |
1072 } | 1106 } |
1073 } | 1107 } |
1074 else if (strcmp ("global", s) == 0) | 1108 else if (strcmp ("global", s) == 0) |
1075 return GLOBAL; | 1109 { |
1110 return GLOBAL; | |
1111 } | |
1076 else if (strcmp ("gplot", s) == 0) | 1112 else if (strcmp ("gplot", s) == 0) |
1077 { plotting = 1; yylval.pttype = two_dee; return PLOT; } | 1113 { |
1114 plotting = 1; | |
1115 yylval.tok_val = new token (token::two_dee, l, c); | |
1116 return PLOT; | |
1117 } | |
1078 else if (strcmp ("gsplot", s) == 0) | 1118 else if (strcmp ("gsplot", s) == 0) |
1079 { plotting = 1; yylval.pttype = three_dee; return PLOT; } | 1119 { |
1120 plotting = 1; | |
1121 yylval.tok_val = new token (token::three_dee, l, c); | |
1122 token_stack.push (yylval.tok_val); | |
1123 return PLOT; | |
1124 } | |
1080 else if (strcmp ("if", s) == 0) | 1125 else if (strcmp ("if", s) == 0) |
1081 { iffing++; promptflag--; return IF; } | 1126 { |
1127 iffing++; | |
1128 promptflag--; | |
1129 return IF; | |
1130 } | |
1082 else if (strcmp ("return", s) == 0) | 1131 else if (strcmp ("return", s) == 0) |
1083 return FUNC_RET; | 1132 { |
1133 return FUNC_RET; | |
1134 } | |
1084 else if (strcmp ("while", s) == 0) | 1135 else if (strcmp ("while", s) == 0) |
1085 { promptflag--; looping++; return WHILE; } | 1136 { |
1137 promptflag--; | |
1138 looping++; | |
1139 return WHILE; | |
1140 } | |
1086 | 1141 |
1087 if (end_found) | 1142 if (end_found) |
1088 { | 1143 { |
1089 if (!defining_func && !looping) | 1144 if (! defining_func && ! looping) |
1090 promptflag++; | 1145 promptflag++; |
1091 return END; | 1146 return END; |
1092 } | 1147 } |
1093 | 1148 |
1094 return 0; | 1149 return 0; |