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