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 |
|
446 if (plotting) |
|
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; |
|
463 current_input_column += yyleng; |
|
464 DO_COMMA_INSERT_CHECK; |
|
465 return IMAG_NUM; |
|
466 } |
|
467 |
|
468 {D}+{EXPON}? | |
|
469 {D}+\.{D}*{EXPON}? | |
|
470 \.{D}+{EXPON}? | |
|
471 { |
|
472 int nread = sscanf (yytext, "%lf", &(yylval.number)); |
|
473 assert (nread == 1); |
|
474 quote_is_transpose = 1; |
|
475 cant_be_identifier = 1; |
|
476 convert_spaces_to_comma = 1; |
|
477 current_input_column += yyleng; |
|
478 DO_COMMA_INSERT_CHECK; |
|
479 return NUM; |
|
480 } |
|
481 |
|
482 \[{S}* { |
|
483 in_brace_or_paren.push (1); |
|
484 if (plotting) |
|
485 { |
|
486 in_plot_range = 1; |
|
487 RETURN (OPEN_BRACE); |
|
488 } |
|
489 |
|
490 if (do_comma_insert) |
|
491 { |
|
492 yyless (0); |
|
493 do_comma_insert = 0; |
|
494 quote_is_transpose = 0; |
|
495 cant_be_identifier = 0; |
|
496 convert_spaces_to_comma = 1; |
|
497 return (','); |
|
498 } |
|
499 else |
|
500 { |
|
501 mlnm.push (1); |
|
502 braceflag++; |
|
503 promptflag--; |
|
504 BEGIN NEW_MATRIX; |
|
505 RETURN ('['); |
|
506 } |
|
507 } |
|
508 |
|
509 {S}* { current_input_column += yyleng; } |
|
510 |
|
511 {EL}{S}*\n { |
|
512 |
|
513 // Line continuation. |
|
514 |
|
515 promptflag--; |
|
516 current_input_column = 0; |
|
517 } |
|
518 |
|
519 <<EOF>> RETURN (END_OF_INPUT); |
|
520 |
|
521 {IDENT}{S}* { |
|
522 |
|
523 // Truncate the token at the first space or tab but don't write |
|
524 // directly on yytext. |
|
525 |
|
526 static char *tok = (char *) NULL; |
|
527 delete [] tok; |
|
528 tok = strip_trailing_whitespace (yytext); |
|
529 |
|
530 int kw_token = is_keyword (tok); |
|
531 if (kw_token) |
|
532 RETURN (kw_token); |
|
533 |
|
534 if (plotting && cant_be_identifier) |
|
535 { |
|
536 int plot_option_kw = is_plot_keyword (tok); |
|
537 if (plot_option_kw) |
|
538 { |
|
539 quote_is_transpose = 0; |
|
540 cant_be_identifier = 0; |
|
541 convert_spaces_to_comma = 1; |
|
542 current_input_column += yyleng; |
|
543 return plot_option_kw; |
|
544 } |
|
545 } |
|
546 |
|
547 if (plotting && in_plot_style) |
|
548 { |
|
549 char *sty = plot_style_token (&tok[1]); |
|
550 if (sty != (char *) NULL) |
|
551 { |
|
552 yylval.string = strsave (sty); |
|
553 if (in_plot_style) |
|
554 { |
|
555 in_plot_style = 0; |
|
556 RETURN (STYLE); |
|
557 } |
|
558 } |
|
559 } |
|
560 |
|
561 cant_be_identifier = 1; |
|
562 |
|
563 // If we are looking at a text style function, set up to gobble its |
|
564 // arguments. These are also reserved words, but only because it |
|
565 // would be very difficult to do anything intelligent with them if |
|
566 // they were not reserved. |
|
567 |
|
568 if (is_text_function_name (tok)) |
|
569 { |
|
570 BEGIN TEXT_FCN; |
|
571 |
|
572 if (strcmp (tok, "clear") == 0) |
|
573 return CLEAR; |
|
574 else if (strcmp (tok, "help") == 0) |
|
575 BEGIN HELP_FCN; |
|
576 else if (strcmp (tok, "set") == 0) |
|
577 doing_set = 1; |
|
578 } |
|
579 |
|
580 yylval.sym_rec = lookup_identifier (tok); |
|
581 |
|
582 quote_is_transpose = 1; |
|
583 current_input_column += yyleng; |
|
584 DO_COMMA_INSERT_CHECK; |
|
585 |
|
586 if (! in_brace_or_paren.empty () |
|
587 && in_brace_or_paren.top ()) |
|
588 { |
|
589 int c0 = yytext[yyleng-1]; |
|
590 int spc_prev = (c0 == ' ' || c0 == '\t'); |
|
591 int bin_op = next_token_is_bin_op (spc_prev, yytext); |
|
592 |
|
593 int postfix_un_op |
|
594 = next_token_is_postfix_unary_op (spc_prev, yytext); |
|
595 |
|
596 int c1 = yyinput (); |
|
597 unput (c1); |
|
598 int other_op = match_any (c1, ",;\n]("); |
|
599 |
|
600 if (! (postfix_un_op || bin_op || other_op)) |
|
601 unput (','); |
|
602 } |
|
603 |
|
604 convert_spaces_to_comma = 1; |
|
605 return NAME; |
|
606 } |
|
607 |
|
608 {IDENT}/{S}*= { |
|
609 |
|
610 // We've found an identifier followed by some space and an equals |
|
611 // sign. If we are working on a function definition and the previous |
|
612 // token was `function', we have something like this |
|
613 // |
|
614 // function x = y <list> end |
|
615 // |
|
616 // which is a function named y returning a variable named x. The |
|
617 // symbol y belongs in the global symbol table (nested function |
|
618 // definitions are illegal) and the symbol x belongs in the |
|
619 // symbol table local to the function. |
|
620 // |
|
621 // If we're not defining a function, this should function exactly like |
|
622 // the case above. I suppose it would be nice to avoid duplicating |
|
623 // all the code, eh? |
|
624 |
|
625 int kw_token = is_keyword (yytext); |
|
626 if (kw_token) |
|
627 RETURN (kw_token); |
|
628 |
|
629 if (plotting && cant_be_identifier) |
|
630 { |
|
631 int plot_option_kw = is_plot_keyword (yytext); |
|
632 if (plot_option_kw) |
|
633 { |
|
634 quote_is_transpose = 0; |
|
635 convert_spaces_to_comma = 1; |
|
636 current_input_column += yyleng; |
|
637 return plot_option_kw; |
|
638 } |
|
639 } |
|
640 |
|
641 cant_be_identifier = 1; |
|
642 |
|
643 // If we are looking at a text style function, set up to gobble its |
|
644 // arguments. These are also reserved words, but only because it |
|
645 // would be very difficult to do anything intelligent with them if |
|
646 // they were not reserved. |
|
647 |
|
648 if (is_text_function_name (yytext)) |
|
649 { |
|
650 BEGIN TEXT_FCN; |
|
651 |
|
652 if (strcmp (yytext, "clear") == 0) |
|
653 return CLEAR; |
|
654 else if (strcmp (yytext, "help") == 0) |
|
655 BEGIN HELP_FCN; |
|
656 else if (strcmp (yytext, "set") == 0) |
|
657 doing_set = 1; |
|
658 } |
|
659 |
|
660 if (defining_func && maybe_screwed) |
|
661 curr_sym_tab = tmp_local_sym_tab; |
|
662 |
|
663 yylval.sym_rec = lookup_identifier (yytext); |
|
664 |
|
665 convert_spaces_to_comma = 1; |
|
666 current_input_column += yyleng; |
|
667 if (defining_func && maybe_screwed) |
|
668 { |
|
669 return SCREW; |
|
670 } |
|
671 else |
|
672 { |
|
673 quote_is_transpose = 1; |
|
674 DO_COMMA_INSERT_CHECK; |
|
675 return NAME; |
|
676 } |
|
677 } |
|
678 |
|
679 "\n" { |
|
680 quote_is_transpose = 0; |
|
681 cant_be_identifier = 0; |
|
682 current_input_column = 0; |
|
683 convert_spaces_to_comma = 1; |
|
684 return '\n'; |
|
685 } |
|
686 |
|
687 "'" { |
|
688 current_input_column++; |
|
689 convert_spaces_to_comma = 1; |
|
690 |
|
691 if (quote_is_transpose) |
|
692 { |
|
693 DO_COMMA_INSERT_CHECK; |
|
694 return QUOTE; |
|
695 } |
|
696 else |
|
697 BEGIN STRING; |
|
698 } |
|
699 |
|
700 ":" { |
|
701 if (plotting && (in_plot_range || in_plot_using)) |
|
702 RETURN (COLON); |
|
703 else |
|
704 BIN_OP_RETURN (':'); |
|
705 } |
|
706 |
|
707 \" { BEGIN DQSTRING; } |
|
708 ".**" { BIN_OP_RETURN (EPOW); } |
|
709 ".*" { BIN_OP_RETURN (EMUL); } |
|
710 "./" { BIN_OP_RETURN (EDIV); } |
|
711 ".\\" { BIN_OP_RETURN (ELEFTDIV); } |
|
712 ".^" { BIN_OP_RETURN (EPOW); } |
|
713 ".'" { DO_COMMA_INSERT_CHECK; RETURN (TRANSPOSE); } |
|
714 "++" { DO_COMMA_INSERT_CHECK; RETURN (PLUS_PLUS); } |
|
715 "--" { DO_COMMA_INSERT_CHECK; RETURN (MINUS_MINUS); } |
|
716 "<=" { BIN_OP_RETURN (EXPR_LE); } |
|
717 "==" { BIN_OP_RETURN (EXPR_EQ); } |
|
718 "~=" { BIN_OP_RETURN (EXPR_NE); } |
|
719 "!=" { BIN_OP_RETURN (EXPR_NE); } |
|
720 "<>" { BIN_OP_RETURN (EXPR_NE); } |
|
721 ">=" { BIN_OP_RETURN (EXPR_GE); } |
|
722 "||" { BIN_OP_RETURN (EXPR_OR); } |
|
723 "&&" { BIN_OP_RETURN (EXPR_AND); } |
|
724 "|" { BIN_OP_RETURN (EXPR_OR); } |
|
725 "&" { BIN_OP_RETURN (EXPR_AND); } |
|
726 "!" { RETURN (EXPR_NOT); } |
|
727 "~" { BIN_OP_RETURN (EXPR_NOT); } |
|
728 "<" { BIN_OP_RETURN (EXPR_LT); } |
|
729 ">" { BIN_OP_RETURN (EXPR_GT); } |
|
730 "+" { BIN_OP_RETURN ('+'); } |
|
731 "-" { BIN_OP_RETURN ('-'); } |
|
732 "**" { BIN_OP_RETURN (POW); } |
|
733 "*" { BIN_OP_RETURN ('*'); } |
|
734 "/" { BIN_OP_RETURN ('/'); } |
|
735 "\\" { BIN_OP_RETURN (LEFTDIV); } |
|
736 ";" { RETURN (';'); } |
|
737 "," { RETURN (','); } |
|
738 "^" { BIN_OP_RETURN (POW); } |
|
739 "=" { RETURN ('='); } |
|
740 "(" { |
|
741 in_brace_or_paren.push (0); |
|
742 RETURN ('('); |
|
743 } |
|
744 ")" { |
|
745 if (! in_brace_or_paren.empty ()) |
|
746 in_brace_or_paren.pop (); |
|
747 DO_COMMA_INSERT_CHECK; |
|
748 current_input_column++; |
|
749 quote_is_transpose = 1; |
|
750 return ')'; |
|
751 } |
|
752 |
|
753 . { |
|
754 |
|
755 // We return everything else as single character tokens, which should |
|
756 // eventually result in a parse error. |
|
757 |
|
758 RETURN (yytext[0]); |
|
759 } |
|
760 |
|
761 %% |
|
762 |
|
763 /* |
|
764 * GAG. |
|
765 * |
|
766 * If we're reading a matrix and the next character is '[', make sure |
|
767 * that we insert a comma ahead of it. |
|
768 */ |
|
769 int |
|
770 do_comma_insert_check (void) |
|
771 { |
|
772 int tmp_len = yyleng; |
|
773 int c = yyinput (); |
|
774 do_comma_insert = (braceflag && c == '['); |
|
775 return tmp_len; |
|
776 } |
|
777 |
|
778 /* |
|
779 * Fix things up for errors or interrupts. |
|
780 */ |
|
781 void |
|
782 reset_parser (void) |
|
783 { |
|
784 BEGIN 0; |
|
785 promptflag = 1; |
|
786 doing_set = 0; |
|
787 braceflag = 0; |
|
788 maybe_screwed = 0; |
|
789 maybe_screwed_again = 0; |
|
790 looping = 0; |
|
791 iffing = 0; |
|
792 ml.clear (); |
|
793 mlnm.clear (); |
|
794 defining_func = 0; |
|
795 curr_sym_tab = top_level_sym_tab; |
|
796 get_input_from_eval_string = 0; |
|
797 quote_is_transpose = 0; |
|
798 current_input_column = 0; |
|
799 do_comma_insert = 0; |
|
800 plotting = 0; |
|
801 in_plot_range = 0; |
|
802 in_plot_using = 0; |
|
803 in_plot_style = 0; |
|
804 cant_be_identifier = 0; |
|
805 convert_spaces_to_comma = 1; |
|
806 beginning_of_function = 0; |
|
807 in_brace_or_paren.clear (); |
|
808 yyrestart (stdin); |
|
809 } |
|
810 |
|
811 static void |
|
812 do_string_escapes (char *s) |
|
813 { |
|
814 char *p1 = s; |
|
815 char *p2 = s; |
|
816 while (*p2 != '\0') |
|
817 { |
|
818 if (*p2 == '\\' && *(p2+1) != '\0') |
|
819 { |
|
820 switch (*++p2) |
|
821 { |
|
822 case 'a': |
|
823 *p1 = '\a'; |
|
824 break; |
|
825 case 'b': // backspace |
|
826 *p1 = '\b'; |
|
827 break; |
|
828 case 'f': // formfeed |
|
829 *p1 = '\f'; |
|
830 break; |
|
831 case 'n': // newline |
|
832 *p1 = '\n'; |
|
833 break; |
|
834 case 'r': // carriage return |
|
835 *p1 = '\r'; |
|
836 break; |
|
837 case 't': // horizontal tab |
|
838 *p1 = '\t'; |
|
839 break; |
|
840 case 'v': // vertical tab |
|
841 *p1 = '\v'; |
|
842 break; |
|
843 case '\\': // backslash |
|
844 *p1 = '\\'; |
|
845 break; |
|
846 case '\'': // quote |
|
847 *p1 = '\''; |
|
848 break; |
|
849 case '"': // double quote |
|
850 *p1 = '"'; |
|
851 break; |
|
852 default: |
|
853 warning ("unrecognized escape sequence `\\%c' -- converting to `%c'", |
|
854 *p2, *p2); |
|
855 *p1 = *p2; |
|
856 break; |
|
857 } |
|
858 } |
|
859 else if (*p2 == '\'' && *(p2+1) == '\'') |
|
860 { |
|
861 *p1 = '\''; |
|
862 p2++; |
|
863 } |
|
864 else |
|
865 { |
|
866 *p1 = *p2; |
|
867 } |
|
868 |
|
869 p1++; |
|
870 p2++; |
|
871 } |
|
872 |
|
873 *p1 = '\0'; |
|
874 } |
|
875 |
|
876 static void |
|
877 fixup_column_count (char *s) |
|
878 { |
|
879 char c; |
|
880 while ((c = *s++) != '\0') |
|
881 { |
|
882 if (c == '\n') |
|
883 current_input_column = 0; |
|
884 else |
|
885 current_input_column++; |
|
886 } |
|
887 } |
|
888 |
|
889 #ifdef yywrap |
|
890 #undef yywrap |
|
891 #endif |
|
892 int |
|
893 yywrap (void) |
|
894 { |
|
895 return 0; |
|
896 } |
|
897 |
|
898 /* |
|
899 * Tell us all what the current buffer is. |
|
900 */ |
|
901 YY_BUFFER_STATE |
|
902 current_buffer (void) |
|
903 { |
|
904 return YY_CURRENT_BUFFER; |
|
905 } |
|
906 |
|
907 /* |
|
908 * Create a new buffer. |
|
909 */ |
|
910 YY_BUFFER_STATE |
|
911 create_buffer (FILE *f) |
|
912 { |
|
913 return yy_create_buffer (f, YY_BUF_SIZE); |
|
914 } |
|
915 |
|
916 /* |
|
917 * Start reading a new buffer. |
|
918 */ |
|
919 void |
|
920 switch_to_buffer (YY_BUFFER_STATE buf) |
|
921 { |
|
922 yy_switch_to_buffer (buf); |
|
923 } |
|
924 |
|
925 /* |
|
926 * Delete a buffer. |
|
927 */ |
|
928 void |
|
929 delete_buffer (YY_BUFFER_STATE buf) |
|
930 { |
|
931 yy_delete_buffer (buf); |
|
932 } |
|
933 |
|
934 /* |
|
935 * Restore a buffer (for unwind-prot). |
|
936 */ |
|
937 void |
|
938 restore_input_buffer (void *buf) |
|
939 { |
|
940 switch_to_buffer ((YY_BUFFER_STATE) buf); |
|
941 } |
|
942 |
|
943 /* |
|
944 * Delete a buffer (for unwind-prot). |
|
945 */ |
|
946 void |
|
947 delete_input_buffer (void *buf) |
|
948 { |
|
949 delete_buffer ((YY_BUFFER_STATE) buf); |
|
950 } |
|
951 |
|
952 static char *plot_styles[] = |
|
953 { |
|
954 "dots", |
|
955 "dots", |
|
956 "errorbars", |
|
957 "impulses", |
|
958 "lines", |
|
959 "linespoints", |
|
960 "points", |
|
961 (char *) NULL, |
|
962 }; |
|
963 |
|
964 static char * |
|
965 plot_style_token (char *s) |
|
966 { |
|
967 char **tmp = plot_styles; |
|
968 while (*tmp != (char *) NULL) |
|
969 { |
|
970 if (almost_match (*tmp, s)) |
|
971 return *tmp; |
|
972 |
|
973 tmp++; |
|
974 } |
|
975 |
|
976 return (char *) NULL; |
|
977 } |
|
978 |
|
979 static int |
|
980 is_plot_keyword (char *s) |
|
981 { |
|
982 if (almost_match ("title", s)) |
|
983 return TITLE; |
|
984 else if (almost_match ("using", s)) |
|
985 { in_plot_using = 1; return USING; } |
|
986 else if (almost_match ("with", s)) |
|
987 { in_plot_style = 1; return WITH; } |
|
988 else |
|
989 return 0; |
|
990 } |
|
991 |
|
992 /* |
|
993 * Handle keywords. Could probably be more efficient... |
|
994 */ |
|
995 static int |
|
996 is_keyword (char *s) |
|
997 { |
|
998 if (plotting && in_plot_style) |
|
999 { |
|
1000 char *sty = plot_style_token (s); |
|
1001 if (sty != (char *) NULL) |
|
1002 { |
|
1003 in_plot_style = 0; |
|
1004 yylval.string = strsave (sty); |
|
1005 return STYLE; |
|
1006 } |
|
1007 } |
|
1008 |
|
1009 int end_found = 0; |
|
1010 if (strcmp ("break", s) == 0) |
|
1011 return BREAK; |
|
1012 else if (strcmp ("continue", s) == 0) |
|
1013 return CONTINUE; |
|
1014 else if (strcmp ("else", s) == 0) |
|
1015 { return ELSE; } |
|
1016 else if (strcmp ("elseif", s) == 0) |
|
1017 { return ELSEIF; } |
|
1018 else if (strcmp ("end", s) == 0) |
|
1019 { end_found = 1; yylval.ettype = simple_end; } |
|
1020 else if (strcmp ("endfor", s) == 0) |
|
1021 { end_found = 1; yylval.ettype = for_end; } |
|
1022 else if (strcmp ("endfunction", s) == 0) |
|
1023 { end_found = 1; yylval.ettype = function_end; } |
|
1024 else if (strcmp ("endif", s) == 0) |
|
1025 { end_found = 1; yylval.ettype = if_end; } |
|
1026 else if (strcmp ("endwhile", s) == 0) |
|
1027 { end_found = 1; yylval.ettype = while_end; } |
|
1028 else if (strcmp ("for", s) == 0) |
|
1029 { promptflag--; looping++; return FOR; } |
|
1030 else if (strcmp ("function", s) == 0) |
|
1031 { |
|
1032 if (defining_func) |
|
1033 { |
|
1034 error ("sorry, nested functions are a no-no..."); |
|
1035 jump_to_top_level (); |
|
1036 } |
|
1037 else |
|
1038 { |
|
1039 tmp_local_sym_tab = new symbol_table (); |
|
1040 curr_sym_tab = tmp_local_sym_tab; |
|
1041 defining_func = 1; |
|
1042 promptflag--; |
|
1043 beginning_of_function = 1; |
|
1044 help_buf[0] = '\0'; |
|
1045 return FCN; |
|
1046 } |
|
1047 } |
|
1048 else if (strcmp ("global", s) == 0) |
|
1049 return GLOBAL; |
|
1050 else if (strcmp ("gplot", s) == 0) |
|
1051 { plotting = 1; yylval.pttype = two_dee; return PLOT; } |
|
1052 else if (strcmp ("gsplot", s) == 0) |
|
1053 { plotting = 1; yylval.pttype = three_dee; return PLOT; } |
|
1054 else if (strcmp ("if", s) == 0) |
|
1055 { iffing++; promptflag--; return IF; } |
|
1056 else if (strcmp ("return", s) == 0) |
|
1057 return FUNC_RET; |
|
1058 else if (strcmp ("while", s) == 0) |
|
1059 { promptflag--; looping++; return WHILE; } |
|
1060 |
|
1061 if (end_found) |
|
1062 { |
|
1063 if (!defining_func && !looping) |
|
1064 promptflag++; |
|
1065 return END; |
|
1066 } |
|
1067 |
|
1068 return 0; |
|
1069 } |
|
1070 |
|
1071 static symbol_record * |
|
1072 lookup_identifier (char *name) |
|
1073 { |
|
1074 symbol_record *gsr = global_sym_tab->lookup (name, 0, 0); |
|
1075 |
|
1076 if (curr_sym_tab == top_level_sym_tab && gsr != (symbol_record *) NULL) |
|
1077 return gsr; |
|
1078 |
|
1079 return curr_sym_tab->lookup (name, 1, 0); |
|
1080 } |
|
1081 |
|
1082 static void |
|
1083 grab_help_text (void) |
|
1084 { |
|
1085 int max_len = HELP_BUF_LENGTH - 1; |
|
1086 |
|
1087 int in_comment = 1; |
|
1088 int len = 0; |
|
1089 int c; |
|
1090 |
|
1091 while ((c = yyinput ()) != EOF) |
|
1092 { |
|
1093 if (in_comment) |
|
1094 { |
|
1095 help_buf[len++] = c; |
|
1096 if (c == '\n') |
|
1097 in_comment = 0; |
|
1098 } |
|
1099 else |
|
1100 { |
|
1101 switch (c) |
|
1102 { |
|
1103 case '%': |
|
1104 case '#': |
|
1105 in_comment = 1; |
|
1106 case ' ': |
|
1107 case '\t': |
|
1108 break; |
|
1109 default: |
|
1110 goto done; |
|
1111 } |
|
1112 } |
|
1113 |
|
1114 if (len > max_len) |
|
1115 { |
|
1116 message ("grab_help_text", |
|
1117 "buffer overflow after caching %d characters", |
|
1118 max_len); |
|
1119 |
|
1120 goto done; |
|
1121 } |
|
1122 } |
|
1123 |
|
1124 done: |
|
1125 |
|
1126 // Make sure there's an end of line so yylex sees an end to the |
|
1127 // comment immediately. |
|
1128 |
|
1129 yyunput (c, yytext); |
|
1130 if (c != '\n') |
|
1131 yyunput ('\n', yytext); |
|
1132 |
|
1133 help_buf[len] = '\0'; |
|
1134 } |
|
1135 |
|
1136 static int |
|
1137 match_any (char c, char *s) |
|
1138 { |
|
1139 char tmp; |
|
1140 while ((tmp = *s++) != '\0') |
|
1141 { |
|
1142 if (c == tmp) |
|
1143 return 1; |
|
1144 } |
|
1145 return 0; |
|
1146 } |
|
1147 |
|
1148 static int |
|
1149 looks_like_bin_op (int spc_prev, int spc_next) |
|
1150 { |
|
1151 return ((spc_prev && spc_next) || ! (spc_prev || spc_next)); |
|
1152 } |
|
1153 |
|
1154 static int |
|
1155 next_char_is_space (void) |
|
1156 { |
|
1157 int c = yyinput (); |
|
1158 yyunput (c, yytext); |
|
1159 return (c == ' ' || c == '\t'); |
|
1160 } |
|
1161 |
|
1162 static int |
|
1163 next_token_is_postfix_unary_op (int spc_prev, char *yytext) |
|
1164 { |
|
1165 int un_op = 0; |
|
1166 |
|
1167 int c0 = yyinput (); |
|
1168 int c1 = yyinput (); |
|
1169 |
|
1170 yyunput (c1, yytext); |
|
1171 yyunput (c0, yytext); |
|
1172 |
|
1173 int transpose = (c0 == '.' && c1 == '\''); |
|
1174 int hermitian = (c0 == '\''); |
|
1175 |
|
1176 un_op = (transpose || (hermitian && ! spc_prev)); |
|
1177 |
|
1178 return un_op; |
|
1179 } |
|
1180 |
|
1181 static int |
|
1182 next_token_is_bin_op (int spc_prev, char *yytext) |
|
1183 { |
|
1184 int bin_op = 0; |
|
1185 int spc_next = 0; |
|
1186 |
|
1187 int c0 = yyinput (); |
|
1188 int c1 = yyinput (); |
|
1189 |
|
1190 switch (c0) |
|
1191 { |
|
1192 case '+': case '-': case '/': |
|
1193 case ':': case '\\': case '^': |
|
1194 spc_next = (c1 == ' ' || c1 == '\t'); |
|
1195 break; |
|
1196 |
|
1197 case '&': |
|
1198 if (c1 == '&') |
|
1199 spc_next = next_char_is_space (); |
|
1200 else |
|
1201 spc_next = (c1 == ' ' || c1 == '\t'); |
|
1202 break; |
|
1203 |
|
1204 case '*': |
|
1205 if (c1 == '*') |
|
1206 spc_next = next_char_is_space (); |
|
1207 else |
|
1208 spc_next = (c1 == ' ' || c1 == '\t'); |
|
1209 break; |
|
1210 |
|
1211 case '|': |
|
1212 if (c1 == '|') |
|
1213 spc_next = next_char_is_space (); |
|
1214 else |
|
1215 spc_next = (c1 == ' ' || c1 == '\t'); |
|
1216 break; |
|
1217 |
|
1218 case '<': |
|
1219 if (c1 == '=' || c1 == '>') |
|
1220 spc_next = next_char_is_space (); |
|
1221 else |
|
1222 spc_next = (c1 == ' ' || c1 == '\t'); |
|
1223 break; |
|
1224 |
|
1225 case '>': |
|
1226 if (c1 == '=') |
|
1227 spc_next = next_char_is_space (); |
|
1228 else |
|
1229 spc_next = (c1 == ' ' || c1 == '\t'); |
|
1230 break; |
|
1231 |
|
1232 case '~': case '!': case '=': |
|
1233 if (c1 == '=') |
|
1234 spc_next = next_char_is_space (); |
|
1235 else |
|
1236 goto done; |
|
1237 break; |
|
1238 |
|
1239 case '.': |
|
1240 if (c1 == '*') |
|
1241 { |
|
1242 int c2 = yyinput (); |
|
1243 if (c2 == '*') |
|
1244 spc_next = next_char_is_space (); |
|
1245 else |
|
1246 spc_next = (c2 == ' ' || c2 == '\t'); |
|
1247 yyunput (c2, yytext); |
|
1248 } |
|
1249 else if (c1 == '/' || c1 == '\\' || c1 == '^') |
|
1250 spc_next = next_char_is_space (); |
|
1251 else |
|
1252 goto done; |
|
1253 break; |
|
1254 |
|
1255 default: |
|
1256 goto done; |
|
1257 } |
|
1258 |
|
1259 bin_op = looks_like_bin_op (spc_prev, spc_next); |
|
1260 |
|
1261 done: |
|
1262 yyunput (c1, yytext); |
|
1263 yyunput (c0, yytext); |
|
1264 |
|
1265 return bin_op; |
|
1266 } |
|
1267 |
|
1268 char * |
|
1269 strip_trailing_whitespace (char *s) |
|
1270 { |
|
1271 char *retval = strsave (s); |
|
1272 |
|
1273 char *t = strchr (retval, ' '); |
|
1274 if (t != (char *) NULL) |
|
1275 *t = '\0'; |
|
1276 |
|
1277 t = strchr (retval, '\t'); |
|
1278 if (t != (char *) NULL) |
|
1279 *t = '\0'; |
|
1280 |
|
1281 return retval; |
|
1282 } |
|
1283 |
|
1284 void |
|
1285 check_for_garbage_after_fcn_def (void) |
|
1286 { |
|
1287 // By making a newline be the next character to be read, we will force |
|
1288 // the parser to return after reading the function. Calling yyunput |
|
1289 // with EOF seems not to work... |
|
1290 |
|
1291 int in_comment = 0; |
|
1292 int lineno = input_line_number; |
|
1293 int c; |
|
1294 while ((c = yyinput ()) != EOF) |
|
1295 { |
|
1296 switch (c) |
|
1297 { |
|
1298 case ' ': |
|
1299 case '\t': |
|
1300 case ';': |
|
1301 case ',': |
|
1302 break; |
|
1303 case '\n': |
|
1304 if (in_comment) |
|
1305 in_comment = 0; |
|
1306 break; |
|
1307 case '%': |
|
1308 case '#': |
|
1309 in_comment = 1; |
|
1310 break; |
|
1311 default: |
|
1312 if (in_comment) |
|
1313 break; |
|
1314 else |
|
1315 { |
|
1316 warning ("ignoring trailing garbage after end of function\n\ |
|
1317 near line %d of file `%s.m'", lineno, curr_m_file_name); |
|
1318 |
|
1319 yyunput ('\n', yytext); |
|
1320 return; |
|
1321 } |
|
1322 } |
|
1323 } |
|
1324 yyunput ('\n', yytext); |
|
1325 } |
|
1326 |
|
1327 /* Maybe someday... |
|
1328 |
|
1329 "+=" return ADD_EQ; |
|
1330 "-=" return SUB_EQ; |
|
1331 "*=" return MUL_EQ; |
|
1332 "/=" return DIV_EQ; |
|
1333 "\\=" return LEFTDIV_EQ; |
|
1334 ".+=" return ADD_EQ; |
|
1335 ".-=" return SUB_EQ; |
|
1336 ".*=" return EMUL_EQ; |
|
1337 "./=" return EDIV_EQ; |
|
1338 ".\\=" return ELEFTDIV_EQ; |
|
1339 |
|
1340 */ |