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