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