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