1
|
1 /* lex.l -*- C -*- |
|
2 |
|
3 Copyright (C) 1992, 1993 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with GNU CC; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 %x COMMENT |
|
24 %x NEW_MATRIX |
|
25 %x HELP_FCN |
|
26 %s TEXT_FCN |
|
27 %s DQSTRING |
|
28 %s STRING |
|
29 %s MATRIX |
|
30 |
|
31 %{ |
|
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" |
|
56 |
|
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 |
|
76 #include "SLStack.h" |
|
77 |
|
78 #include "variables.h" |
|
79 #include "symtab.h" |
|
80 #include "error.h" |
|
81 #include "utils.h" |
|
82 #include "tree.h" |
|
83 #include "y.tab.h" |
|
84 #include "parse.h" |
|
85 #include "lex.h" |
|
86 |
|
87 // Nonzero means we thing we are looking at the beginning of a |
|
88 // function definition. |
|
89 static int beginning_of_function = 0; |
|
90 |
|
91 // Nonzero means we think we are looking at a set command. |
|
92 static int doing_set = 0; |
|
93 |
|
94 // GAG. Stupid kludge so that [[1,2][3,4]] will work. |
|
95 static do_comma_insert = 0; |
|
96 |
|
97 // Brace level count. |
|
98 static int braceflag = 0; |
|
99 |
|
100 // Return transpose or start a string? |
|
101 static int quote_is_transpose = 0; |
|
102 |
|
103 // Nonzero means that we should convert spaces to a comma inside a |
|
104 // matrix definition. |
|
105 static int convert_spaces_to_comma = 1; |
|
106 |
|
107 // Another context hack, this time for the plot command's `using', |
|
108 // `title', and `with' keywords. |
|
109 static int cant_be_identifier = 0; |
|
110 |
|
111 // Is the closest nesting level a square brace or a paren? |
|
112 // |
|
113 // 1 -> brace, spaces are important (they can turn into commas) |
|
114 // 0 -> paren, spaces are not important |
|
115 // |
|
116 static SLStack <int> in_brace_or_paren; |
|
117 |
|
118 static void do_string_escapes (char *s); |
|
119 static void fixup_column_count (char *s); |
|
120 static int do_comma_insert_check (void); |
|
121 static int is_plot_keyword (char *s); |
|
122 static int is_keyword (char *s); |
|
123 static char *plot_style_token (char *s); |
|
124 static symbol_record *lookup_identifier (char *s); |
|
125 static void grab_help_text (void); |
|
126 static int match_any (char c, char *s); |
|
127 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); |
|
129 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 |
|
155 %} |
|
156 |
|
157 D [0-9] |
|
158 S [ \t] |
|
159 N [\n] |
|
160 SN [ \t\n] |
|
161 EL (\.\.\.) |
|
162 Im [iIjJ] |
|
163 QQ (\'\') |
|
164 ECHAR (\\.) |
|
165 QSTR ([^\n\'\\]*({QQ}|{ECHAR})*) |
|
166 DQSTR ([^\n\"\\]*{ECHAR}*) |
|
167 IDENT ([_a-zA-Z][_a-zA-Z0-9]*) |
|
168 EXPON ([DdEe][+-]?{D}+) |
|
169 %% |
|
170 |
|
171 \% | |
|
172 \# { |
|
173 if (beginning_of_function) |
|
174 { |
|
175 grab_help_text (); |
|
176 beginning_of_function = 0; |
|
177 } |
|
178 |
|
179 BEGIN COMMENT; |
|
180 current_input_column += yyleng; |
|
181 } |
|
182 |
|
183 <COMMENT>\n { |
|
184 BEGIN 0; |
|
185 current_input_column = 0; |
|
186 quote_is_transpose = 0; |
|
187 cant_be_identifier = 0; |
|
188 convert_spaces_to_comma = 1; |
|
189 return '\n'; |
|
190 } |
|
191 |
|
192 <COMMENT><<EOF>> { RETURN (END_OF_INPUT); } |
|
193 |
|
194 <COMMENT>.*$ { current_input_column += yyleng; } |
|
195 |
|
196 <NEW_MATRIX>[^ \t\n] { |
|
197 yyless (0); |
|
198 BEGIN MATRIX; |
|
199 } |
|
200 |
|
201 <NEW_MATRIX>{SN}* { |
|
202 fixup_column_count (yytext); |
|
203 BEGIN MATRIX; |
|
204 } |
|
205 |
|
206 <HELP_FCN>\n | |
|
207 <TEXT_FCN>\n { |
|
208 BEGIN 0; |
|
209 current_input_column = 0; |
|
210 quote_is_transpose = 0; |
|
211 cant_be_identifier = 0; |
|
212 convert_spaces_to_comma = 1; |
|
213 return '\n'; |
|
214 } |
|
215 |
|
216 <TEXT_FCN>[\;\,] { |
|
217 if (doing_set) |
|
218 { |
|
219 yylval.string = strsave (yytext); |
|
220 RETURN (TEXT); |
|
221 } |
|
222 else |
|
223 { |
|
224 BEGIN 0; |
|
225 RETURN (','); |
|
226 } |
|
227 } |
|
228 |
|
229 <HELP_FCN>[^ \t\n]*{S}* | |
|
230 <TEXT_FCN>[^ \t\n\;\,]*{S}* { |
|
231 |
|
232 static char *tok = (char *) NULL; |
|
233 delete [] tok; |
|
234 tok = strip_trailing_whitespace (yytext); |
|
235 |
|
236 yylval.string = strsave (tok); |
|
237 RETURN (TEXT); |
|
238 } |
|
239 |
|
240 <TEXT_FCN>\'{QSTR}*[\n\'] { |
|
241 if (yytext[yyleng-1] == '\n') |
|
242 { |
|
243 error ("unterminated string constant"); |
|
244 current_input_column = 0; |
|
245 jump_to_top_level (); |
|
246 } |
|
247 else |
|
248 { |
|
249 int off1 = doing_set ? 0 : 1; |
|
250 int off2 = doing_set ? 0 : 2; |
|
251 yylval.string = strsave (&yytext[off1]); |
|
252 yylval.string[yyleng-off2] = '\0'; |
|
253 current_input_column += yyleng; |
|
254 } |
|
255 do_string_escapes (yylval.string); |
|
256 return TEXT; |
|
257 } |
|
258 |
|
259 <TEXT_FCN>\"{DQSTR}*[\n\"] { |
|
260 if (yytext[yyleng-1] == '\n') |
|
261 { |
|
262 error ("unterminated string constant"); |
|
263 current_input_column = 0; |
|
264 jump_to_top_level (); |
|
265 } |
|
266 else |
|
267 { |
|
268 int off1 = doing_set ? 0 : 1; |
|
269 int off2 = doing_set ? 0 : 2; |
|
270 yylval.string = strsave (&yytext[off1]); |
|
271 yylval.string[yyleng-off2] = '\0'; |
|
272 current_input_column += yyleng; |
|
273 } |
|
274 do_string_escapes (yylval.string); |
|
275 return TEXT; |
|
276 } |
|
277 |
|
278 <TEXT_FCN>{S}* { current_input_column += yyleng; } |
|
279 |
|
280 <STRING>{QSTR}*[\n\'] { |
|
281 if (braceflag) |
|
282 BEGIN MATRIX; |
|
283 else |
|
284 BEGIN 0; |
|
285 |
|
286 if (yytext[yyleng-1] == '\n') |
|
287 { |
|
288 error ("unterminated string constant"); |
|
289 current_input_column = 0; |
|
290 jump_to_top_level (); |
|
291 } |
|
292 else |
|
293 { |
|
294 yylval.string = strsave (yytext); |
|
295 yylval.string[yyleng-1] = '\0'; |
|
296 current_input_column += yyleng; |
|
297 } |
|
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; |
|
303 } |
|
304 |
|
305 |
|
306 <DQSTRING>{DQSTR}*[\n\"] { |
|
307 if (braceflag) |
|
308 BEGIN MATRIX; |
|
309 else |
|
310 BEGIN 0; |
|
311 |
|
312 if (yytext[yyleng-1] == '\n') |
|
313 { |
|
314 error ("unterminated string constant"); |
|
315 current_input_column = 0; |
|
316 jump_to_top_level (); |
|
317 } |
|
318 else |
|
319 { |
|
320 yylval.string = strsave (yytext); |
|
321 yylval.string[yyleng-1] = '\0'; |
|
322 current_input_column += yyleng; |
|
323 } |
|
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; |
|
329 } |
|
330 |
|
331 <MATRIX>{SN}*\]{S}*/== { |
|
332 |
|
333 // For this and the next two rules, we're looking at ']', and we |
|
334 // need to know if the next token is '='. |
|
335 // |
|
336 // All this so we can handle the bogus syntax |
|
337 // |
|
338 // [x,y] % an expression by itself |
|
339 // [x,y] = expression % assignment to a list of identifiers |
|
340 // [x,y] == expression % test for equality |
|
341 // |
|
342 // It would have been so much easier if the delimiters were simply |
|
343 // different for the expression on the left hand side of the equals |
|
344 // operator. |
|
345 |
|
346 in_brace_or_paren.pop (); |
|
347 braceflag--; |
|
348 if (braceflag == 0) |
|
349 { |
|
350 if (!defining_func) |
|
351 promptflag++; |
|
352 BEGIN 0; |
|
353 } |
|
354 fixup_column_count (yytext); |
|
355 quote_is_transpose = 0; |
|
356 cant_be_identifier = 0; |
|
357 convert_spaces_to_comma = 1; |
|
358 return ']'; |
|
359 } |
|
360 |
|
361 <MATRIX>{SN}*\]{S}*/= { |
|
362 in_brace_or_paren.pop (); |
|
363 braceflag--; |
|
364 if (braceflag == 0) |
|
365 { |
|
366 BEGIN 0; |
|
367 if (!defining_func) |
|
368 promptflag++; |
|
369 } |
|
370 fixup_column_count (yytext); |
|
371 quote_is_transpose = 0; |
|
372 cant_be_identifier = 0; |
|
373 convert_spaces_to_comma = 1; |
|
374 if (maybe_screwed_again) |
|
375 return SCREW_TWO; |
|
376 else |
|
377 return ']'; |
|
378 } |
|
379 |
|
380 <MATRIX>{SN}*\]{S}* { |
|
381 fixup_column_count (yytext); |
|
382 |
|
383 in_brace_or_paren.pop (); |
|
384 braceflag--; |
|
385 if (braceflag == 0) |
|
386 { |
|
387 if (!defining_func) |
|
388 promptflag++; |
|
389 BEGIN 0; |
|
390 } |
|
391 else |
|
392 { |
|
393 int c0 = yytext[yyleng-1]; |
|
394 int spc_prev = (c0 == ' ' || c0 == '\t'); |
|
395 int bin_op = next_token_is_bin_op (spc_prev, |
|
396 yytext); |
|
397 int postfix_un_op |
|
398 = next_token_is_postfix_unary_op (spc_prev, |
|
399 yytext); |
|
400 |
|
401 int c1 = yyinput (); |
|
402 unput (c1); |
|
403 int other_op = match_any (c1, ",;\n]"); |
|
404 |
|
405 if (! (postfix_un_op || bin_op || other_op) |
|
406 && in_brace_or_paren.top () |
|
407 && convert_spaces_to_comma) |
|
408 { |
|
409 unput (','); |
|
410 return ']'; |
|
411 } |
|
412 } |
|
413 |
|
414 quote_is_transpose = 1; |
|
415 cant_be_identifier = 0; |
|
416 convert_spaces_to_comma = 1; |
|
417 return ']'; |
|
418 } |
|
419 |
|
420 <MATRIX>{S}*\,{S}* { RETURN (','); } |
|
421 |
|
422 <MATRIX>{S}+ { |
|
423 int bin_op = next_token_is_bin_op (1, yytext); |
|
424 int postfix_un_op |
|
425 = next_token_is_postfix_unary_op (1, yytext); |
|
426 |
|
427 if (! (postfix_un_op || bin_op) |
|
428 && in_brace_or_paren.top () |
|
429 && convert_spaces_to_comma) |
|
430 RETURN (','); |
|
431 } |
|
432 |
|
433 <MATRIX>{SN}*\;{SN}* | |
|
434 <MATRIX>{N}{SN}* { |
|
435 fixup_column_count (yytext); |
|
436 quote_is_transpose = 0; |
|
437 cant_be_identifier = 0; |
|
438 convert_spaces_to_comma = 1; |
|
439 return ';'; |
|
440 } |
|
441 |
|
442 \] { |
|
443 if (! in_brace_or_paren.empty ()) |
|
444 in_brace_or_paren.pop (); |
|
445 |
113
|
446 if (plotting && ! past_plot_range) |
1
|
447 { |
|
448 in_plot_range = 0; |
|
449 RETURN (CLOSE_BRACE); |
|
450 } |
|
451 else |
|
452 RETURN (']'); |
|
453 } |
|
454 |
|
455 {D}+{EXPON}?{Im} | |
|
456 {D}+\.{D}*{EXPON}?{Im} | |
|
457 \.{D}+{EXPON}?{Im} { |
|
458 int nread = sscanf (yytext, "%lf", &(yylval.number)); |
|
459 assert (nread == 1); |
|
460 quote_is_transpose = 1; |
|
461 cant_be_identifier = 1; |
|
462 convert_spaces_to_comma = 1; |
113
|
463 if (plotting && ! in_plot_range) |
|
464 past_plot_range = 1; |
1
|
465 current_input_column += yyleng; |
|
466 DO_COMMA_INSERT_CHECK; |
|
467 return IMAG_NUM; |
|
468 } |
|
469 |
|
470 {D}+{EXPON}? | |
|
471 {D}+\.{D}*{EXPON}? | |
|
472 \.{D}+{EXPON}? | |
|
473 { |
|
474 int nread = sscanf (yytext, "%lf", &(yylval.number)); |
|
475 assert (nread == 1); |
|
476 quote_is_transpose = 1; |
|
477 cant_be_identifier = 1; |
|
478 convert_spaces_to_comma = 1; |
113
|
479 if (plotting && ! in_plot_range) |
|
480 past_plot_range = 1; |
1
|
481 current_input_column += yyleng; |
|
482 DO_COMMA_INSERT_CHECK; |
|
483 return NUM; |
|
484 } |
|
485 |
|
486 \[{S}* { |
|
487 in_brace_or_paren.push (1); |
113
|
488 if (plotting && ! past_plot_range) |
1
|
489 { |
|
490 in_plot_range = 1; |
|
491 RETURN (OPEN_BRACE); |
|
492 } |
|
493 |
|
494 if (do_comma_insert) |
|
495 { |
|
496 yyless (0); |
|
497 do_comma_insert = 0; |
|
498 quote_is_transpose = 0; |
|
499 cant_be_identifier = 0; |
|
500 convert_spaces_to_comma = 1; |
|
501 return (','); |
|
502 } |
|
503 else |
|
504 { |
|
505 mlnm.push (1); |
|
506 braceflag++; |
|
507 promptflag--; |
|
508 BEGIN NEW_MATRIX; |
|
509 RETURN ('['); |
|
510 } |
|
511 } |
|
512 |
|
513 {S}* { current_input_column += yyleng; } |
|
514 |
|
515 {EL}{S}*\n { |
|
516 |
|
517 // Line continuation. |
|
518 |
|
519 promptflag--; |
|
520 current_input_column = 0; |
|
521 } |
|
522 |
|
523 <<EOF>> RETURN (END_OF_INPUT); |
|
524 |
|
525 {IDENT}{S}* { |
|
526 |
|
527 // Truncate the token at the first space or tab but don't write |
|
528 // directly on yytext. |
|
529 |
|
530 static char *tok = (char *) NULL; |
|
531 delete [] tok; |
|
532 tok = strip_trailing_whitespace (yytext); |
|
533 |
|
534 int kw_token = is_keyword (tok); |
|
535 if (kw_token) |
|
536 RETURN (kw_token); |
|
537 |
|
538 if (plotting && cant_be_identifier) |
|
539 { |
|
540 int plot_option_kw = is_plot_keyword (tok); |
|
541 if (plot_option_kw) |
|
542 { |
|
543 quote_is_transpose = 0; |
|
544 cant_be_identifier = 0; |
|
545 convert_spaces_to_comma = 1; |
|
546 current_input_column += yyleng; |
|
547 return plot_option_kw; |
|
548 } |
|
549 } |
|
550 |
113
|
551 if (plotting && ! in_plot_range) |
|
552 past_plot_range = 1; |
|
553 |
1
|
554 if (plotting && in_plot_style) |
|
555 { |
|
556 char *sty = plot_style_token (&tok[1]); |
|
557 if (sty != (char *) NULL) |
|
558 { |
|
559 yylval.string = strsave (sty); |
|
560 if (in_plot_style) |
|
561 { |
|
562 in_plot_style = 0; |
|
563 RETURN (STYLE); |
|
564 } |
|
565 } |
|
566 } |
|
567 |
|
568 cant_be_identifier = 1; |
|
569 |
|
570 // If we are looking at a text style function, set up to gobble its |
|
571 // arguments. These are also reserved words, but only because it |
|
572 // would be very difficult to do anything intelligent with them if |
|
573 // they were not reserved. |
|
574 |
|
575 if (is_text_function_name (tok)) |
|
576 { |
|
577 BEGIN TEXT_FCN; |
|
578 |
|
579 if (strcmp (tok, "clear") == 0) |
|
580 return CLEAR; |
|
581 else if (strcmp (tok, "help") == 0) |
|
582 BEGIN HELP_FCN; |
|
583 else if (strcmp (tok, "set") == 0) |
|
584 doing_set = 1; |
|
585 } |
|
586 |
|
587 yylval.sym_rec = lookup_identifier (tok); |
|
588 |
|
589 quote_is_transpose = 1; |
|
590 current_input_column += yyleng; |
|
591 DO_COMMA_INSERT_CHECK; |
|
592 |
|
593 if (! in_brace_or_paren.empty () |
|
594 && in_brace_or_paren.top ()) |
|
595 { |
|
596 int c0 = yytext[yyleng-1]; |
|
597 int spc_prev = (c0 == ' ' || c0 == '\t'); |
|
598 int bin_op = next_token_is_bin_op (spc_prev, yytext); |
|
599 |
|
600 int postfix_un_op |
|
601 = next_token_is_postfix_unary_op (spc_prev, yytext); |
|
602 |
|
603 int c1 = yyinput (); |
|
604 unput (c1); |
|
605 int other_op = match_any (c1, ",;\n]("); |
|
606 |
|
607 if (! (postfix_un_op || bin_op || other_op)) |
|
608 unput (','); |
|
609 } |
|
610 |
|
611 convert_spaces_to_comma = 1; |
|
612 return NAME; |
|
613 } |
|
614 |
|
615 {IDENT}/{S}*= { |
|
616 |
|
617 // We've found an identifier followed by some space and an equals |
|
618 // sign. If we are working on a function definition and the previous |
|
619 // token was `function', we have something like this |
|
620 // |
|
621 // function x = y <list> end |
|
622 // |
|
623 // which is a function named y returning a variable named x. The |
|
624 // symbol y belongs in the global symbol table (nested function |
|
625 // definitions are illegal) and the symbol x belongs in the |
|
626 // symbol table local to the function. |
|
627 // |
|
628 // If we're not defining a function, this should function exactly like |
|
629 // the case above. I suppose it would be nice to avoid duplicating |
|
630 // all the code, eh? |
|
631 |
|
632 int kw_token = is_keyword (yytext); |
|
633 if (kw_token) |
|
634 RETURN (kw_token); |
|
635 |
|
636 if (plotting && cant_be_identifier) |
|
637 { |
|
638 int plot_option_kw = is_plot_keyword (yytext); |
|
639 if (plot_option_kw) |
|
640 { |
|
641 quote_is_transpose = 0; |
|
642 convert_spaces_to_comma = 1; |
|
643 current_input_column += yyleng; |
|
644 return plot_option_kw; |
|
645 } |
|
646 } |
|
647 |
|
648 cant_be_identifier = 1; |
|
649 |
|
650 // If we are looking at a text style function, set up to gobble its |
|
651 // arguments. These are also reserved words, but only because it |
|
652 // would be very difficult to do anything intelligent with them if |
|
653 // they were not reserved. |
|
654 |
|
655 if (is_text_function_name (yytext)) |
|
656 { |
|
657 BEGIN TEXT_FCN; |
|
658 |
|
659 if (strcmp (yytext, "clear") == 0) |
|
660 return CLEAR; |
|
661 else if (strcmp (yytext, "help") == 0) |
|
662 BEGIN HELP_FCN; |
|
663 else if (strcmp (yytext, "set") == 0) |
|
664 doing_set = 1; |
|
665 } |
|
666 |
|
667 if (defining_func && maybe_screwed) |
|
668 curr_sym_tab = tmp_local_sym_tab; |
|
669 |
|
670 yylval.sym_rec = lookup_identifier (yytext); |
|
671 |
|
672 convert_spaces_to_comma = 1; |
|
673 current_input_column += yyleng; |
|
674 if (defining_func && maybe_screwed) |
|
675 { |
|
676 return SCREW; |
|
677 } |
|
678 else |
|
679 { |
|
680 quote_is_transpose = 1; |
|
681 DO_COMMA_INSERT_CHECK; |
|
682 return NAME; |
|
683 } |
|
684 } |
|
685 |
|
686 "\n" { |
|
687 quote_is_transpose = 0; |
|
688 cant_be_identifier = 0; |
|
689 current_input_column = 0; |
|
690 convert_spaces_to_comma = 1; |
|
691 return '\n'; |
|
692 } |
|
693 |
|
694 "'" { |
|
695 current_input_column++; |
|
696 convert_spaces_to_comma = 1; |
|
697 |
|
698 if (quote_is_transpose) |
|
699 { |
|
700 DO_COMMA_INSERT_CHECK; |
|
701 return QUOTE; |
|
702 } |
|
703 else |
|
704 BEGIN STRING; |
|
705 } |
|
706 |
|
707 ":" { |
|
708 if (plotting && (in_plot_range || in_plot_using)) |
|
709 RETURN (COLON); |
|
710 else |
|
711 BIN_OP_RETURN (':'); |
|
712 } |
|
713 |
|
714 \" { BEGIN DQSTRING; } |
|
715 ".**" { BIN_OP_RETURN (EPOW); } |
|
716 ".*" { BIN_OP_RETURN (EMUL); } |
|
717 "./" { BIN_OP_RETURN (EDIV); } |
|
718 ".\\" { BIN_OP_RETURN (ELEFTDIV); } |
|
719 ".^" { BIN_OP_RETURN (EPOW); } |
|
720 ".'" { DO_COMMA_INSERT_CHECK; RETURN (TRANSPOSE); } |
|
721 "++" { DO_COMMA_INSERT_CHECK; RETURN (PLUS_PLUS); } |
|
722 "--" { DO_COMMA_INSERT_CHECK; RETURN (MINUS_MINUS); } |
|
723 "<=" { BIN_OP_RETURN (EXPR_LE); } |
|
724 "==" { BIN_OP_RETURN (EXPR_EQ); } |
|
725 "~=" { BIN_OP_RETURN (EXPR_NE); } |
|
726 "!=" { BIN_OP_RETURN (EXPR_NE); } |
|
727 "<>" { BIN_OP_RETURN (EXPR_NE); } |
|
728 ">=" { BIN_OP_RETURN (EXPR_GE); } |
|
729 "||" { BIN_OP_RETURN (EXPR_OR); } |
|
730 "&&" { BIN_OP_RETURN (EXPR_AND); } |
|
731 "|" { BIN_OP_RETURN (EXPR_OR); } |
|
732 "&" { BIN_OP_RETURN (EXPR_AND); } |
113
|
733 "!" { |
|
734 if (plotting && ! in_plot_range) |
|
735 past_plot_range = 1; |
|
736 RETURN (EXPR_NOT); |
|
737 } |
|
738 "~" { |
|
739 if (plotting && ! in_plot_range) |
|
740 past_plot_range = 1; |
|
741 BIN_OP_RETURN (EXPR_NOT); |
|
742 } |
1
|
743 "<" { BIN_OP_RETURN (EXPR_LT); } |
|
744 ">" { BIN_OP_RETURN (EXPR_GT); } |
113
|
745 "+" { |
|
746 if (plotting && ! in_plot_range) |
|
747 past_plot_range = 1; |
|
748 BIN_OP_RETURN ('+'); |
|
749 } |
|
750 "-" { |
|
751 if (plotting && ! in_plot_range) |
|
752 past_plot_range = 1; |
|
753 BIN_OP_RETURN ('-'); |
|
754 } |
1
|
755 "**" { BIN_OP_RETURN (POW); } |
|
756 "*" { BIN_OP_RETURN ('*'); } |
|
757 "/" { BIN_OP_RETURN ('/'); } |
|
758 "\\" { BIN_OP_RETURN (LEFTDIV); } |
|
759 ";" { RETURN (';'); } |
|
760 "," { RETURN (','); } |
|
761 "^" { BIN_OP_RETURN (POW); } |
|
762 "=" { RETURN ('='); } |
|
763 "(" { |
113
|
764 if (plotting && ! in_plot_range) |
|
765 past_plot_range = 1; |
1
|
766 in_brace_or_paren.push (0); |
|
767 RETURN ('('); |
|
768 } |
|
769 ")" { |
|
770 if (! in_brace_or_paren.empty ()) |
|
771 in_brace_or_paren.pop (); |
|
772 DO_COMMA_INSERT_CHECK; |
|
773 current_input_column++; |
|
774 quote_is_transpose = 1; |
|
775 return ')'; |
|
776 } |
|
777 |
|
778 . { |
|
779 |
|
780 // We return everything else as single character tokens, which should |
|
781 // eventually result in a parse error. |
|
782 |
|
783 RETURN (yytext[0]); |
|
784 } |
|
785 |
|
786 %% |
|
787 |
|
788 /* |
|
789 * GAG. |
|
790 * |
|
791 * If we're reading a matrix and the next character is '[', make sure |
|
792 * that we insert a comma ahead of it. |
|
793 */ |
|
794 int |
|
795 do_comma_insert_check (void) |
|
796 { |
|
797 int tmp_len = yyleng; |
|
798 int c = yyinput (); |
|
799 do_comma_insert = (braceflag && c == '['); |
|
800 return tmp_len; |
|
801 } |
|
802 |
|
803 /* |
|
804 * Fix things up for errors or interrupts. |
|
805 */ |
|
806 void |
|
807 reset_parser (void) |
|
808 { |
|
809 BEGIN 0; |
|
810 promptflag = 1; |
|
811 doing_set = 0; |
|
812 braceflag = 0; |
|
813 maybe_screwed = 0; |
|
814 maybe_screwed_again = 0; |
|
815 looping = 0; |
|
816 iffing = 0; |
|
817 ml.clear (); |
|
818 mlnm.clear (); |
|
819 defining_func = 0; |
|
820 curr_sym_tab = top_level_sym_tab; |
|
821 get_input_from_eval_string = 0; |
|
822 quote_is_transpose = 0; |
|
823 current_input_column = 0; |
|
824 do_comma_insert = 0; |
|
825 plotting = 0; |
113
|
826 past_plot_range = 0; |
1
|
827 in_plot_range = 0; |
|
828 in_plot_using = 0; |
|
829 in_plot_style = 0; |
|
830 cant_be_identifier = 0; |
|
831 convert_spaces_to_comma = 1; |
|
832 beginning_of_function = 0; |
|
833 in_brace_or_paren.clear (); |
|
834 yyrestart (stdin); |
|
835 } |
|
836 |
|
837 static void |
|
838 do_string_escapes (char *s) |
|
839 { |
|
840 char *p1 = s; |
|
841 char *p2 = s; |
|
842 while (*p2 != '\0') |
|
843 { |
|
844 if (*p2 == '\\' && *(p2+1) != '\0') |
|
845 { |
|
846 switch (*++p2) |
|
847 { |
|
848 case 'a': |
|
849 *p1 = '\a'; |
|
850 break; |
|
851 case 'b': // backspace |
|
852 *p1 = '\b'; |
|
853 break; |
|
854 case 'f': // formfeed |
|
855 *p1 = '\f'; |
|
856 break; |
|
857 case 'n': // newline |
|
858 *p1 = '\n'; |
|
859 break; |
|
860 case 'r': // carriage return |
|
861 *p1 = '\r'; |
|
862 break; |
|
863 case 't': // horizontal tab |
|
864 *p1 = '\t'; |
|
865 break; |
|
866 case 'v': // vertical tab |
|
867 *p1 = '\v'; |
|
868 break; |
|
869 case '\\': // backslash |
|
870 *p1 = '\\'; |
|
871 break; |
|
872 case '\'': // quote |
|
873 *p1 = '\''; |
|
874 break; |
|
875 case '"': // double quote |
|
876 *p1 = '"'; |
|
877 break; |
|
878 default: |
|
879 warning ("unrecognized escape sequence `\\%c' -- converting to `%c'", |
|
880 *p2, *p2); |
|
881 *p1 = *p2; |
|
882 break; |
|
883 } |
|
884 } |
|
885 else if (*p2 == '\'' && *(p2+1) == '\'') |
|
886 { |
|
887 *p1 = '\''; |
|
888 p2++; |
|
889 } |
|
890 else |
|
891 { |
|
892 *p1 = *p2; |
|
893 } |
|
894 |
|
895 p1++; |
|
896 p2++; |
|
897 } |
|
898 |
|
899 *p1 = '\0'; |
|
900 } |
|
901 |
|
902 static void |
|
903 fixup_column_count (char *s) |
|
904 { |
|
905 char c; |
|
906 while ((c = *s++) != '\0') |
|
907 { |
|
908 if (c == '\n') |
|
909 current_input_column = 0; |
|
910 else |
|
911 current_input_column++; |
|
912 } |
|
913 } |
|
914 |
|
915 #ifdef yywrap |
|
916 #undef yywrap |
|
917 #endif |
|
918 int |
|
919 yywrap (void) |
|
920 { |
|
921 return 0; |
|
922 } |
|
923 |
|
924 /* |
|
925 * Tell us all what the current buffer is. |
|
926 */ |
|
927 YY_BUFFER_STATE |
|
928 current_buffer (void) |
|
929 { |
|
930 return YY_CURRENT_BUFFER; |
|
931 } |
|
932 |
|
933 /* |
|
934 * Create a new buffer. |
|
935 */ |
|
936 YY_BUFFER_STATE |
|
937 create_buffer (FILE *f) |
|
938 { |
|
939 return yy_create_buffer (f, YY_BUF_SIZE); |
|
940 } |
|
941 |
|
942 /* |
|
943 * Start reading a new buffer. |
|
944 */ |
|
945 void |
|
946 switch_to_buffer (YY_BUFFER_STATE buf) |
|
947 { |
|
948 yy_switch_to_buffer (buf); |
|
949 } |
|
950 |
|
951 /* |
|
952 * Delete a buffer. |
|
953 */ |
|
954 void |
|
955 delete_buffer (YY_BUFFER_STATE buf) |
|
956 { |
|
957 yy_delete_buffer (buf); |
|
958 } |
|
959 |
|
960 /* |
|
961 * Restore a buffer (for unwind-prot). |
|
962 */ |
|
963 void |
|
964 restore_input_buffer (void *buf) |
|
965 { |
|
966 switch_to_buffer ((YY_BUFFER_STATE) buf); |
|
967 } |
|
968 |
|
969 /* |
|
970 * Delete a buffer (for unwind-prot). |
|
971 */ |
|
972 void |
|
973 delete_input_buffer (void *buf) |
|
974 { |
|
975 delete_buffer ((YY_BUFFER_STATE) buf); |
|
976 } |
|
977 |
|
978 static char *plot_styles[] = |
|
979 { |
|
980 "dots", |
|
981 "dots", |
|
982 "errorbars", |
|
983 "impulses", |
|
984 "lines", |
|
985 "linespoints", |
|
986 "points", |
|
987 (char *) NULL, |
|
988 }; |
|
989 |
|
990 static char * |
|
991 plot_style_token (char *s) |
|
992 { |
|
993 char **tmp = plot_styles; |
|
994 while (*tmp != (char *) NULL) |
|
995 { |
|
996 if (almost_match (*tmp, s)) |
|
997 return *tmp; |
|
998 |
|
999 tmp++; |
|
1000 } |
|
1001 |
|
1002 return (char *) NULL; |
|
1003 } |
|
1004 |
|
1005 static int |
|
1006 is_plot_keyword (char *s) |
|
1007 { |
|
1008 if (almost_match ("title", s)) |
|
1009 return TITLE; |
|
1010 else if (almost_match ("using", s)) |
113
|
1011 { in_plot_using = 1; past_plot_range = 1; return USING; } |
1
|
1012 else if (almost_match ("with", s)) |
113
|
1013 { in_plot_style = 1; past_plot_range = 1; return WITH; } |
1
|
1014 else |
|
1015 return 0; |
|
1016 } |
|
1017 |
|
1018 /* |
|
1019 * Handle keywords. Could probably be more efficient... |
|
1020 */ |
|
1021 static int |
|
1022 is_keyword (char *s) |
|
1023 { |
|
1024 if (plotting && in_plot_style) |
|
1025 { |
|
1026 char *sty = plot_style_token (s); |
|
1027 if (sty != (char *) NULL) |
|
1028 { |
|
1029 in_plot_style = 0; |
|
1030 yylval.string = strsave (sty); |
|
1031 return STYLE; |
|
1032 } |
|
1033 } |
|
1034 |
|
1035 int end_found = 0; |
|
1036 if (strcmp ("break", s) == 0) |
|
1037 return BREAK; |
|
1038 else if (strcmp ("continue", s) == 0) |
|
1039 return CONTINUE; |
|
1040 else if (strcmp ("else", s) == 0) |
|
1041 { return ELSE; } |
|
1042 else if (strcmp ("elseif", s) == 0) |
|
1043 { return ELSEIF; } |
|
1044 else if (strcmp ("end", s) == 0) |
|
1045 { end_found = 1; yylval.ettype = simple_end; } |
|
1046 else if (strcmp ("endfor", s) == 0) |
|
1047 { end_found = 1; yylval.ettype = for_end; } |
|
1048 else if (strcmp ("endfunction", s) == 0) |
|
1049 { end_found = 1; yylval.ettype = function_end; } |
|
1050 else if (strcmp ("endif", s) == 0) |
|
1051 { end_found = 1; yylval.ettype = if_end; } |
|
1052 else if (strcmp ("endwhile", s) == 0) |
|
1053 { end_found = 1; yylval.ettype = while_end; } |
|
1054 else if (strcmp ("for", s) == 0) |
|
1055 { promptflag--; looping++; return FOR; } |
|
1056 else if (strcmp ("function", s) == 0) |
|
1057 { |
|
1058 if (defining_func) |
|
1059 { |
|
1060 error ("sorry, nested functions are a no-no..."); |
|
1061 jump_to_top_level (); |
|
1062 } |
|
1063 else |
|
1064 { |
|
1065 tmp_local_sym_tab = new symbol_table (); |
|
1066 curr_sym_tab = tmp_local_sym_tab; |
|
1067 defining_func = 1; |
|
1068 promptflag--; |
|
1069 beginning_of_function = 1; |
|
1070 help_buf[0] = '\0'; |
|
1071 return FCN; |
|
1072 } |
|
1073 } |
|
1074 else if (strcmp ("global", s) == 0) |
|
1075 return GLOBAL; |
|
1076 else if (strcmp ("gplot", s) == 0) |
|
1077 { plotting = 1; yylval.pttype = two_dee; return PLOT; } |
|
1078 else if (strcmp ("gsplot", s) == 0) |
|
1079 { plotting = 1; yylval.pttype = three_dee; return PLOT; } |
|
1080 else if (strcmp ("if", s) == 0) |
|
1081 { iffing++; promptflag--; return IF; } |
|
1082 else if (strcmp ("return", s) == 0) |
|
1083 return FUNC_RET; |
|
1084 else if (strcmp ("while", s) == 0) |
|
1085 { promptflag--; looping++; return WHILE; } |
|
1086 |
|
1087 if (end_found) |
|
1088 { |
|
1089 if (!defining_func && !looping) |
|
1090 promptflag++; |
|
1091 return END; |
|
1092 } |
|
1093 |
|
1094 return 0; |
|
1095 } |
|
1096 |
|
1097 static symbol_record * |
|
1098 lookup_identifier (char *name) |
|
1099 { |
120
|
1100 symbol_record *lsr = curr_sym_tab->lookup (name, 0, 0); |
|
1101 if (lsr != (symbol_record *) NULL) |
|
1102 return lsr; |
|
1103 |
1
|
1104 symbol_record *gsr = global_sym_tab->lookup (name, 0, 0); |
|
1105 if (curr_sym_tab == top_level_sym_tab && gsr != (symbol_record *) NULL) |
|
1106 return gsr; |
|
1107 |
|
1108 return curr_sym_tab->lookup (name, 1, 0); |
|
1109 } |
|
1110 |
|
1111 static void |
|
1112 grab_help_text (void) |
|
1113 { |
|
1114 int max_len = HELP_BUF_LENGTH - 1; |
|
1115 |
|
1116 int in_comment = 1; |
|
1117 int len = 0; |
|
1118 int c; |
|
1119 |
|
1120 while ((c = yyinput ()) != EOF) |
|
1121 { |
|
1122 if (in_comment) |
|
1123 { |
|
1124 help_buf[len++] = c; |
|
1125 if (c == '\n') |
|
1126 in_comment = 0; |
|
1127 } |
|
1128 else |
|
1129 { |
|
1130 switch (c) |
|
1131 { |
|
1132 case '%': |
|
1133 case '#': |
|
1134 in_comment = 1; |
|
1135 case ' ': |
|
1136 case '\t': |
|
1137 break; |
|
1138 default: |
|
1139 goto done; |
|
1140 } |
|
1141 } |
|
1142 |
|
1143 if (len > max_len) |
|
1144 { |
|
1145 message ("grab_help_text", |
|
1146 "buffer overflow after caching %d characters", |
|
1147 max_len); |
|
1148 |
|
1149 goto done; |
|
1150 } |
|
1151 } |
|
1152 |
|
1153 done: |
|
1154 |
|
1155 // Make sure there's an end of line so yylex sees an end to the |
|
1156 // comment immediately. |
|
1157 |
|
1158 yyunput (c, yytext); |
|
1159 if (c != '\n') |
|
1160 yyunput ('\n', yytext); |
|
1161 |
|
1162 help_buf[len] = '\0'; |
|
1163 } |
|
1164 |
|
1165 static int |
|
1166 match_any (char c, char *s) |
|
1167 { |
|
1168 char tmp; |
|
1169 while ((tmp = *s++) != '\0') |
|
1170 { |
|
1171 if (c == tmp) |
|
1172 return 1; |
|
1173 } |
|
1174 return 0; |
|
1175 } |
|
1176 |
|
1177 static int |
|
1178 looks_like_bin_op (int spc_prev, int spc_next) |
|
1179 { |
|
1180 return ((spc_prev && spc_next) || ! (spc_prev || spc_next)); |
|
1181 } |
|
1182 |
|
1183 static int |
|
1184 next_char_is_space (void) |
|
1185 { |
|
1186 int c = yyinput (); |
|
1187 yyunput (c, yytext); |
|
1188 return (c == ' ' || c == '\t'); |
|
1189 } |
|
1190 |
|
1191 static int |
|
1192 next_token_is_postfix_unary_op (int spc_prev, char *yytext) |
|
1193 { |
|
1194 int un_op = 0; |
|
1195 |
|
1196 int c0 = yyinput (); |
|
1197 int c1 = yyinput (); |
|
1198 |
|
1199 yyunput (c1, yytext); |
|
1200 yyunput (c0, yytext); |
|
1201 |
|
1202 int transpose = (c0 == '.' && c1 == '\''); |
|
1203 int hermitian = (c0 == '\''); |
|
1204 |
|
1205 un_op = (transpose || (hermitian && ! spc_prev)); |
|
1206 |
|
1207 return un_op; |
|
1208 } |
|
1209 |
|
1210 static int |
|
1211 next_token_is_bin_op (int spc_prev, char *yytext) |
|
1212 { |
|
1213 int bin_op = 0; |
|
1214 int spc_next = 0; |
|
1215 |
|
1216 int c0 = yyinput (); |
|
1217 int c1 = yyinput (); |
|
1218 |
|
1219 switch (c0) |
|
1220 { |
|
1221 case '+': case '-': case '/': |
|
1222 case ':': case '\\': case '^': |
|
1223 spc_next = (c1 == ' ' || c1 == '\t'); |
|
1224 break; |
|
1225 |
|
1226 case '&': |
|
1227 if (c1 == '&') |
|
1228 spc_next = next_char_is_space (); |
|
1229 else |
|
1230 spc_next = (c1 == ' ' || c1 == '\t'); |
|
1231 break; |
|
1232 |
|
1233 case '*': |
|
1234 if (c1 == '*') |
|
1235 spc_next = next_char_is_space (); |
|
1236 else |
|
1237 spc_next = (c1 == ' ' || c1 == '\t'); |
|
1238 break; |
|
1239 |
|
1240 case '|': |
|
1241 if (c1 == '|') |
|
1242 spc_next = next_char_is_space (); |
|
1243 else |
|
1244 spc_next = (c1 == ' ' || c1 == '\t'); |
|
1245 break; |
|
1246 |
|
1247 case '<': |
|
1248 if (c1 == '=' || c1 == '>') |
|
1249 spc_next = next_char_is_space (); |
|
1250 else |
|
1251 spc_next = (c1 == ' ' || c1 == '\t'); |
|
1252 break; |
|
1253 |
|
1254 case '>': |
|
1255 if (c1 == '=') |
|
1256 spc_next = next_char_is_space (); |
|
1257 else |
|
1258 spc_next = (c1 == ' ' || c1 == '\t'); |
|
1259 break; |
|
1260 |
|
1261 case '~': case '!': case '=': |
|
1262 if (c1 == '=') |
|
1263 spc_next = next_char_is_space (); |
|
1264 else |
|
1265 goto done; |
|
1266 break; |
|
1267 |
|
1268 case '.': |
|
1269 if (c1 == '*') |
|
1270 { |
|
1271 int c2 = yyinput (); |
|
1272 if (c2 == '*') |
|
1273 spc_next = next_char_is_space (); |
|
1274 else |
|
1275 spc_next = (c2 == ' ' || c2 == '\t'); |
|
1276 yyunput (c2, yytext); |
|
1277 } |
|
1278 else if (c1 == '/' || c1 == '\\' || c1 == '^') |
|
1279 spc_next = next_char_is_space (); |
|
1280 else |
|
1281 goto done; |
|
1282 break; |
|
1283 |
|
1284 default: |
|
1285 goto done; |
|
1286 } |
|
1287 |
|
1288 bin_op = looks_like_bin_op (spc_prev, spc_next); |
|
1289 |
|
1290 done: |
|
1291 yyunput (c1, yytext); |
|
1292 yyunput (c0, yytext); |
|
1293 |
|
1294 return bin_op; |
|
1295 } |
|
1296 |
|
1297 char * |
|
1298 strip_trailing_whitespace (char *s) |
|
1299 { |
|
1300 char *retval = strsave (s); |
|
1301 |
|
1302 char *t = strchr (retval, ' '); |
|
1303 if (t != (char *) NULL) |
|
1304 *t = '\0'; |
|
1305 |
|
1306 t = strchr (retval, '\t'); |
|
1307 if (t != (char *) NULL) |
|
1308 *t = '\0'; |
|
1309 |
|
1310 return retval; |
|
1311 } |
|
1312 |
|
1313 void |
|
1314 check_for_garbage_after_fcn_def (void) |
|
1315 { |
|
1316 // By making a newline be the next character to be read, we will force |
|
1317 // the parser to return after reading the function. Calling yyunput |
|
1318 // with EOF seems not to work... |
|
1319 |
|
1320 int in_comment = 0; |
|
1321 int lineno = input_line_number; |
|
1322 int c; |
|
1323 while ((c = yyinput ()) != EOF) |
|
1324 { |
|
1325 switch (c) |
|
1326 { |
|
1327 case ' ': |
|
1328 case '\t': |
|
1329 case ';': |
|
1330 case ',': |
|
1331 break; |
|
1332 case '\n': |
|
1333 if (in_comment) |
|
1334 in_comment = 0; |
|
1335 break; |
|
1336 case '%': |
|
1337 case '#': |
|
1338 in_comment = 1; |
|
1339 break; |
|
1340 default: |
|
1341 if (in_comment) |
|
1342 break; |
|
1343 else |
|
1344 { |
|
1345 warning ("ignoring trailing garbage after end of function\n\ |
|
1346 near line %d of file `%s.m'", lineno, curr_m_file_name); |
|
1347 |
|
1348 yyunput ('\n', yytext); |
|
1349 return; |
|
1350 } |
|
1351 } |
|
1352 } |
|
1353 yyunput ('\n', yytext); |
|
1354 } |
|
1355 |
|
1356 /* Maybe someday... |
|
1357 |
|
1358 "+=" return ADD_EQ; |
|
1359 "-=" return SUB_EQ; |
|
1360 "*=" return MUL_EQ; |
|
1361 "/=" return DIV_EQ; |
|
1362 "\\=" return LEFTDIV_EQ; |
|
1363 ".+=" return ADD_EQ; |
|
1364 ".-=" return SUB_EQ; |
|
1365 ".*=" return EMUL_EQ; |
|
1366 "./=" return EDIV_EQ; |
|
1367 ".\\=" return ELEFTDIV_EQ; |
|
1368 |
|
1369 */ |