1
|
1 /* lex.l -*- C -*- |
|
2 |
296
|
3 Copyright (C) 1992, 1993, 1994 John W. Eaton |
1
|
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 /* |
287
|
630 * Fix things up for errors or interrupts. The parser is never called |
|
631 * recursively, so it is always safe to reinitialize its state before |
|
632 * doing any parsing. |
1
|
633 */ |
|
634 void |
|
635 reset_parser (void) |
|
636 { |
287
|
637 // Start off on the right foot. |
1
|
638 BEGIN 0; |
166
|
639 error_state = 0; |
287
|
640 |
|
641 // We do want a prompt by default. |
1
|
642 promptflag = 1; |
287
|
643 |
|
644 // Not initially screwed by `function [...] = f (...)' syntax. |
1
|
645 maybe_screwed = 0; |
|
646 maybe_screwed_again = 0; |
287
|
647 |
|
648 // Not initially inside a loop or if statement. |
1
|
649 looping = 0; |
|
650 iffing = 0; |
287
|
651 |
|
652 // Quote marks strings intially. |
|
653 quote_is_transpose = 0; |
|
654 |
|
655 // Next token can be identifier. |
|
656 cant_be_identifier = 0; |
|
657 |
|
658 // No need to do comma insert or convert spaces to comma at beginning |
|
659 // of input. |
|
660 do_comma_insert = 0; |
|
661 convert_spaces_to_comma = 1; |
|
662 |
|
663 // Not initially defining a function. |
|
664 beginning_of_function = 0; |
|
665 defining_func = 0; |
|
666 |
|
667 // Not initially doing any plotting or setting of plot attributes. |
|
668 plotting = 0; |
|
669 in_plot_range = 0; |
|
670 past_plot_range = 0; |
|
671 in_plot_using = 0; |
|
672 in_plot_style = 0; |
|
673 doing_set = 0; |
|
674 |
|
675 // Error may have occurred inside some parentheses or braces. |
|
676 in_brace_or_paren.clear (); |
|
677 |
|
678 // Not initially defining a matrix list. |
|
679 braceflag = 0; |
1
|
680 ml.clear (); |
|
681 mlnm.clear (); |
287
|
682 |
|
683 // Clear out the stack of token info used to track line and column |
|
684 // numbers. |
143
|
685 while (! token_stack.empty ()) |
|
686 delete token_stack.pop (); |
287
|
687 |
|
688 // Can be reset by defining a function. |
|
689 current_input_column = 1; |
|
690 input_line_number = current_command_number - 1; |
|
691 |
|
692 // Only ask for input from stdin if we are expecting interactive |
|
693 // input. |
338
|
694 if (interactive && ! (reading_fcn_file || get_input_from_eval_string)) |
287
|
695 yyrestart (stdin); |
1
|
696 } |
|
697 |
146
|
698 /* |
|
699 * Replace backslash escapes in a string with the real values. |
|
700 */ |
1
|
701 static void |
|
702 do_string_escapes (char *s) |
|
703 { |
|
704 char *p1 = s; |
|
705 char *p2 = s; |
|
706 while (*p2 != '\0') |
|
707 { |
|
708 if (*p2 == '\\' && *(p2+1) != '\0') |
|
709 { |
|
710 switch (*++p2) |
|
711 { |
|
712 case 'a': |
|
713 *p1 = '\a'; |
|
714 break; |
|
715 case 'b': // backspace |
|
716 *p1 = '\b'; |
|
717 break; |
|
718 case 'f': // formfeed |
|
719 *p1 = '\f'; |
|
720 break; |
|
721 case 'n': // newline |
|
722 *p1 = '\n'; |
|
723 break; |
|
724 case 'r': // carriage return |
|
725 *p1 = '\r'; |
|
726 break; |
|
727 case 't': // horizontal tab |
|
728 *p1 = '\t'; |
|
729 break; |
|
730 case 'v': // vertical tab |
|
731 *p1 = '\v'; |
|
732 break; |
|
733 case '\\': // backslash |
|
734 *p1 = '\\'; |
|
735 break; |
|
736 case '\'': // quote |
|
737 *p1 = '\''; |
|
738 break; |
|
739 case '"': // double quote |
|
740 *p1 = '"'; |
|
741 break; |
|
742 default: |
|
743 warning ("unrecognized escape sequence `\\%c' -- converting to `%c'", |
|
744 *p2, *p2); |
|
745 *p1 = *p2; |
|
746 break; |
|
747 } |
|
748 } |
|
749 else if (*p2 == '\'' && *(p2+1) == '\'') |
|
750 { |
|
751 *p1 = '\''; |
|
752 p2++; |
|
753 } |
|
754 else |
|
755 { |
|
756 *p1 = *p2; |
|
757 } |
|
758 |
|
759 p1++; |
|
760 p2++; |
|
761 } |
|
762 |
|
763 *p1 = '\0'; |
|
764 } |
|
765 |
146
|
766 /* |
|
767 * If we read some newlines, we need figure out what column we're |
|
768 * really looking at. |
|
769 */ |
1
|
770 static void |
|
771 fixup_column_count (char *s) |
|
772 { |
|
773 char c; |
|
774 while ((c = *s++) != '\0') |
|
775 { |
|
776 if (c == '\n') |
143
|
777 current_input_column = 1; |
1
|
778 else |
|
779 current_input_column++; |
|
780 } |
|
781 } |
|
782 |
246
|
783 /* |
|
784 * Include these so that we don't have to link to libfl.a. |
|
785 */ |
|
786 |
1
|
787 #ifdef yywrap |
|
788 #undef yywrap |
|
789 #endif |
246
|
790 static int |
1
|
791 yywrap (void) |
|
792 { |
287
|
793 return 1; |
1
|
794 } |
|
795 |
287
|
796 /* |
|
797 * These are not needed with flex-2.4.6, but may be needed with |
|
798 * earlier 2.4.x versions. |
|
799 */ |
|
800 #if 0 |
246
|
801 static void * |
|
802 yy_flex_alloc (int size) |
|
803 { |
|
804 return (void *) malloc ((unsigned) size); |
|
805 } |
|
806 |
|
807 static void * |
|
808 yy_flex_realloc (void *ptr, int size) |
|
809 { |
|
810 return (void *) realloc (ptr, (unsigned) size); |
|
811 } |
|
812 |
|
813 static void |
|
814 yy_flex_free (void *ptr) |
|
815 { |
|
816 free (ptr); |
|
817 } |
287
|
818 #endif |
246
|
819 |
1
|
820 /* |
|
821 * Tell us all what the current buffer is. |
|
822 */ |
|
823 YY_BUFFER_STATE |
|
824 current_buffer (void) |
|
825 { |
|
826 return YY_CURRENT_BUFFER; |
|
827 } |
|
828 |
|
829 /* |
|
830 * Create a new buffer. |
|
831 */ |
|
832 YY_BUFFER_STATE |
|
833 create_buffer (FILE *f) |
|
834 { |
|
835 return yy_create_buffer (f, YY_BUF_SIZE); |
|
836 } |
|
837 |
|
838 /* |
|
839 * Start reading a new buffer. |
|
840 */ |
|
841 void |
|
842 switch_to_buffer (YY_BUFFER_STATE buf) |
|
843 { |
|
844 yy_switch_to_buffer (buf); |
|
845 } |
|
846 |
|
847 /* |
|
848 * Delete a buffer. |
|
849 */ |
|
850 void |
|
851 delete_buffer (YY_BUFFER_STATE buf) |
|
852 { |
|
853 yy_delete_buffer (buf); |
|
854 } |
|
855 |
|
856 /* |
|
857 * Restore a buffer (for unwind-prot). |
|
858 */ |
|
859 void |
|
860 restore_input_buffer (void *buf) |
|
861 { |
|
862 switch_to_buffer ((YY_BUFFER_STATE) buf); |
|
863 } |
|
864 |
|
865 /* |
|
866 * Delete a buffer (for unwind-prot). |
|
867 */ |
|
868 void |
|
869 delete_input_buffer (void *buf) |
|
870 { |
|
871 delete_buffer ((YY_BUFFER_STATE) buf); |
|
872 } |
|
873 |
146
|
874 /* |
|
875 * Check to see if a character string matches any of the possible line |
|
876 * styles for plots. |
|
877 */ |
1
|
878 static char * |
|
879 plot_style_token (char *s) |
|
880 { |
146
|
881 static char *plot_styles[] = |
|
882 { |
|
883 "dots", |
|
884 "errorbars", |
|
885 "impulses", |
|
886 "lines", |
|
887 "linespoints", |
|
888 "points", |
|
889 (char *) NULL, |
|
890 }; |
|
891 |
1
|
892 char **tmp = plot_styles; |
|
893 while (*tmp != (char *) NULL) |
|
894 { |
|
895 if (almost_match (*tmp, s)) |
|
896 return *tmp; |
|
897 |
|
898 tmp++; |
|
899 } |
|
900 |
|
901 return (char *) NULL; |
|
902 } |
|
903 |
146
|
904 /* |
|
905 * Check to see if a character string matches any one of the plot |
|
906 * option keywords. |
|
907 */ |
1
|
908 static int |
|
909 is_plot_keyword (char *s) |
|
910 { |
|
911 if (almost_match ("title", s)) |
146
|
912 { |
|
913 return TITLE; |
|
914 } |
1
|
915 else if (almost_match ("using", s)) |
146
|
916 { |
|
917 in_plot_using = 1; |
|
918 past_plot_range = 1; |
|
919 return USING; |
|
920 } |
1
|
921 else if (almost_match ("with", s)) |
146
|
922 { |
|
923 in_plot_style = 1; |
|
924 past_plot_range = 1; |
|
925 return WITH; |
|
926 } |
1
|
927 else |
146
|
928 { |
|
929 return 0; |
|
930 } |
1
|
931 } |
|
932 |
|
933 /* |
|
934 * Handle keywords. Could probably be more efficient... |
|
935 */ |
|
936 static int |
|
937 is_keyword (char *s) |
|
938 { |
|
939 if (plotting && in_plot_style) |
|
940 { |
|
941 char *sty = plot_style_token (s); |
|
942 if (sty != (char *) NULL) |
|
943 { |
|
944 in_plot_style = 0; |
143
|
945 yylval.tok_val = new token (sty); |
|
946 token_stack.push (yylval.tok_val); |
1
|
947 return STYLE; |
|
948 } |
|
949 } |
|
950 |
143
|
951 int l = input_line_number; |
|
952 int c = current_input_column; |
|
953 |
1
|
954 int end_found = 0; |
|
955 if (strcmp ("break", s) == 0) |
143
|
956 { |
191
|
957 yylval.tok_val = new token (l, c); |
|
958 token_stack.push (yylval.tok_val); |
143
|
959 return BREAK; |
|
960 } |
1
|
961 else if (strcmp ("continue", s) == 0) |
143
|
962 { |
191
|
963 yylval.tok_val = new token (l, c); |
|
964 token_stack.push (yylval.tok_val); |
143
|
965 return CONTINUE; |
|
966 } |
1
|
967 else if (strcmp ("else", s) == 0) |
143
|
968 { |
191
|
969 yylval.tok_val = new token (l, c); |
|
970 token_stack.push (yylval.tok_val); |
143
|
971 return ELSE; |
|
972 } |
1
|
973 else if (strcmp ("elseif", s) == 0) |
143
|
974 { |
191
|
975 yylval.tok_val = new token (l, c); |
|
976 token_stack.push (yylval.tok_val); |
143
|
977 return ELSEIF; |
|
978 } |
1
|
979 else if (strcmp ("end", s) == 0) |
143
|
980 { |
|
981 end_found = 1; |
|
982 yylval.tok_val = new token (token::simple_end, l, c); |
|
983 token_stack.push (yylval.tok_val); |
|
984 } |
1
|
985 else if (strcmp ("endfor", s) == 0) |
143
|
986 { |
|
987 end_found = 1; |
|
988 yylval.tok_val = new token (token::for_end, l, c); |
|
989 token_stack.push (yylval.tok_val); |
|
990 } |
1
|
991 else if (strcmp ("endfunction", s) == 0) |
143
|
992 { |
|
993 end_found = 1; |
|
994 yylval.tok_val = new token (token::function_end, l, c); |
|
995 token_stack.push (yylval.tok_val); |
|
996 } |
1
|
997 else if (strcmp ("endif", s) == 0) |
143
|
998 { |
|
999 end_found = 1; |
|
1000 yylval.tok_val = new token (token::if_end, l, c); |
|
1001 token_stack.push (yylval.tok_val); |
|
1002 } |
1
|
1003 else if (strcmp ("endwhile", s) == 0) |
143
|
1004 { |
|
1005 end_found = 1; |
|
1006 yylval.tok_val = new token (token::while_end, l, c); |
|
1007 token_stack.push (yylval.tok_val); |
|
1008 } |
1
|
1009 else if (strcmp ("for", s) == 0) |
143
|
1010 { |
|
1011 promptflag--; |
|
1012 looping++; |
191
|
1013 yylval.tok_val = new token (l, c); |
|
1014 token_stack.push (yylval.tok_val); |
143
|
1015 return FOR; |
|
1016 } |
1
|
1017 else if (strcmp ("function", s) == 0) |
|
1018 { |
|
1019 if (defining_func) |
|
1020 { |
191
|
1021 error ("function keyword invalid within a function body"); |
|
1022 |
338
|
1023 if ((reading_fcn_file || reading_script_file) |
|
1024 && curr_fcn_file_name != (char *) NULL) |
191
|
1025 error ("defining new function near line %d of file `%s'", |
|
1026 input_line_number, |
338
|
1027 curr_fcn_file_name); |
191
|
1028 else |
|
1029 error ("defining new function near line %d", input_line_number); |
|
1030 |
|
1031 jump_to_top_level (); // XXX FIXME XXX |
1
|
1032 } |
|
1033 else |
|
1034 { |
|
1035 tmp_local_sym_tab = new symbol_table (); |
|
1036 curr_sym_tab = tmp_local_sym_tab; |
|
1037 defining_func = 1; |
|
1038 promptflag--; |
|
1039 beginning_of_function = 1; |
|
1040 help_buf[0] = '\0'; |
143
|
1041 input_line_number = 1; |
1
|
1042 return FCN; |
|
1043 } |
|
1044 } |
|
1045 else if (strcmp ("global", s) == 0) |
143
|
1046 { |
|
1047 return GLOBAL; |
|
1048 } |
1
|
1049 else if (strcmp ("gplot", s) == 0) |
143
|
1050 { |
|
1051 plotting = 1; |
|
1052 yylval.tok_val = new token (token::two_dee, l, c); |
|
1053 return PLOT; |
|
1054 } |
1
|
1055 else if (strcmp ("gsplot", s) == 0) |
143
|
1056 { |
|
1057 plotting = 1; |
|
1058 yylval.tok_val = new token (token::three_dee, l, c); |
|
1059 token_stack.push (yylval.tok_val); |
|
1060 return PLOT; |
|
1061 } |
1
|
1062 else if (strcmp ("if", s) == 0) |
143
|
1063 { |
|
1064 iffing++; |
|
1065 promptflag--; |
191
|
1066 yylval.tok_val = new token (l, c); |
|
1067 token_stack.push (yylval.tok_val); |
143
|
1068 return IF; |
|
1069 } |
1
|
1070 else if (strcmp ("return", s) == 0) |
143
|
1071 { |
191
|
1072 yylval.tok_val = new token (l, c); |
|
1073 token_stack.push (yylval.tok_val); |
143
|
1074 return FUNC_RET; |
|
1075 } |
1
|
1076 else if (strcmp ("while", s) == 0) |
143
|
1077 { |
|
1078 promptflag--; |
|
1079 looping++; |
191
|
1080 yylval.tok_val = new token (l, c); |
|
1081 token_stack.push (yylval.tok_val); |
143
|
1082 return WHILE; |
|
1083 } |
1
|
1084 |
|
1085 if (end_found) |
|
1086 { |
143
|
1087 if (! defining_func && ! looping) |
1
|
1088 promptflag++; |
|
1089 return END; |
|
1090 } |
|
1091 |
|
1092 return 0; |
|
1093 } |
|
1094 |
146
|
1095 /* |
195
|
1096 * Try to find an identifier. All binding to global or builtin |
|
1097 * variables occurs when expressions are evaluated. |
146
|
1098 */ |
1
|
1099 static symbol_record * |
|
1100 lookup_identifier (char *name) |
|
1101 { |
|
1102 return curr_sym_tab->lookup (name, 1, 0); |
|
1103 } |
|
1104 |
146
|
1105 /* |
338
|
1106 * Grab the help text from an function file. |
146
|
1107 */ |
1
|
1108 static void |
|
1109 grab_help_text (void) |
|
1110 { |
|
1111 int max_len = HELP_BUF_LENGTH - 1; |
|
1112 |
|
1113 int in_comment = 1; |
|
1114 int len = 0; |
|
1115 int c; |
|
1116 |
|
1117 while ((c = yyinput ()) != EOF) |
|
1118 { |
|
1119 if (in_comment) |
|
1120 { |
|
1121 help_buf[len++] = c; |
|
1122 if (c == '\n') |
|
1123 in_comment = 0; |
|
1124 } |
|
1125 else |
|
1126 { |
|
1127 switch (c) |
|
1128 { |
|
1129 case '%': |
|
1130 case '#': |
|
1131 in_comment = 1; |
|
1132 case ' ': |
|
1133 case '\t': |
|
1134 break; |
|
1135 default: |
|
1136 goto done; |
|
1137 } |
|
1138 } |
|
1139 |
|
1140 if (len > max_len) |
|
1141 { |
216
|
1142 warning ("grab_help_text: buffer overflow after caching %d chars", |
1
|
1143 max_len); |
|
1144 |
|
1145 goto done; |
|
1146 } |
|
1147 } |
|
1148 |
|
1149 done: |
|
1150 |
|
1151 // Make sure there's an end of line so yylex sees an end to the |
|
1152 // comment immediately. |
|
1153 |
|
1154 yyunput (c, yytext); |
|
1155 if (c != '\n') |
|
1156 yyunput ('\n', yytext); |
|
1157 |
|
1158 help_buf[len] = '\0'; |
|
1159 } |
|
1160 |
146
|
1161 /* |
|
1162 * Return 1 if the given character matches any character in the given |
|
1163 * string. |
|
1164 */ |
1
|
1165 static int |
|
1166 match_any (char c, char *s) |
|
1167 { |
|
1168 char tmp; |
|
1169 while ((tmp = *s++) != '\0') |
|
1170 { |
|
1171 if (c == tmp) |
|
1172 return 1; |
|
1173 } |
|
1174 return 0; |
|
1175 } |
|
1176 |
146
|
1177 /* |
|
1178 * Given information about the spacing surrounding an operator, |
|
1179 * return 1 if it looks like it should be treated as a binary |
|
1180 * operator. For example, |
|
1181 * |
|
1182 * [ 1 + 2 ] or [ 1+2 ] ==> binary |
|
1183 * |
|
1184 * The case of [ 1+ 2 ] should also be treated as a binary operator, |
|
1185 * but it is handled by the caller. |
|
1186 */ |
1
|
1187 static int |
|
1188 looks_like_bin_op (int spc_prev, int spc_next) |
|
1189 { |
|
1190 return ((spc_prev && spc_next) || ! (spc_prev || spc_next)); |
|
1191 } |
|
1192 |
146
|
1193 /* |
|
1194 * Duh. |
|
1195 */ |
1
|
1196 static int |
|
1197 next_char_is_space (void) |
|
1198 { |
|
1199 int c = yyinput (); |
|
1200 yyunput (c, yytext); |
|
1201 return (c == ' ' || c == '\t'); |
|
1202 } |
|
1203 |
146
|
1204 /* |
|
1205 * Try to determine if the next token should be treated as a postfix |
|
1206 * unary operator. This is ugly, but it seems to do the right thing. |
|
1207 */ |
1
|
1208 static int |
|
1209 next_token_is_postfix_unary_op (int spc_prev, char *yytext) |
|
1210 { |
|
1211 int un_op = 0; |
|
1212 |
|
1213 int c0 = yyinput (); |
|
1214 int c1 = yyinput (); |
|
1215 |
|
1216 yyunput (c1, yytext); |
|
1217 yyunput (c0, yytext); |
|
1218 |
|
1219 int transpose = (c0 == '.' && c1 == '\''); |
|
1220 int hermitian = (c0 == '\''); |
|
1221 |
|
1222 un_op = (transpose || (hermitian && ! spc_prev)); |
|
1223 |
|
1224 return un_op; |
|
1225 } |
|
1226 |
146
|
1227 /* |
|
1228 * Try to determine if the next token should be treated as a binary |
|
1229 * operator. This is even uglier, but it also seems to do the right |
|
1230 * thing. |
|
1231 */ |
1
|
1232 static int |
|
1233 next_token_is_bin_op (int spc_prev, char *yytext) |
|
1234 { |
|
1235 int bin_op = 0; |
|
1236 int spc_next = 0; |
|
1237 |
|
1238 int c0 = yyinput (); |
|
1239 int c1 = yyinput (); |
|
1240 |
|
1241 switch (c0) |
|
1242 { |
|
1243 case '+': case '-': case '/': |
|
1244 case ':': case '\\': case '^': |
|
1245 spc_next = (c1 == ' ' || c1 == '\t'); |
|
1246 break; |
|
1247 |
|
1248 case '&': |
|
1249 if (c1 == '&') |
|
1250 spc_next = next_char_is_space (); |
|
1251 else |
|
1252 spc_next = (c1 == ' ' || c1 == '\t'); |
|
1253 break; |
|
1254 |
|
1255 case '*': |
|
1256 if (c1 == '*') |
|
1257 spc_next = next_char_is_space (); |
|
1258 else |
|
1259 spc_next = (c1 == ' ' || c1 == '\t'); |
|
1260 break; |
|
1261 |
|
1262 case '|': |
|
1263 if (c1 == '|') |
|
1264 spc_next = next_char_is_space (); |
|
1265 else |
|
1266 spc_next = (c1 == ' ' || c1 == '\t'); |
|
1267 break; |
|
1268 |
|
1269 case '<': |
|
1270 if (c1 == '=' || c1 == '>') |
|
1271 spc_next = next_char_is_space (); |
|
1272 else |
|
1273 spc_next = (c1 == ' ' || c1 == '\t'); |
|
1274 break; |
|
1275 |
|
1276 case '>': |
|
1277 if (c1 == '=') |
|
1278 spc_next = next_char_is_space (); |
|
1279 else |
|
1280 spc_next = (c1 == ' ' || c1 == '\t'); |
|
1281 break; |
|
1282 |
|
1283 case '~': case '!': case '=': |
|
1284 if (c1 == '=') |
|
1285 spc_next = next_char_is_space (); |
|
1286 else |
|
1287 goto done; |
|
1288 break; |
|
1289 |
|
1290 case '.': |
|
1291 if (c1 == '*') |
|
1292 { |
|
1293 int c2 = yyinput (); |
|
1294 if (c2 == '*') |
|
1295 spc_next = next_char_is_space (); |
|
1296 else |
|
1297 spc_next = (c2 == ' ' || c2 == '\t'); |
|
1298 yyunput (c2, yytext); |
|
1299 } |
|
1300 else if (c1 == '/' || c1 == '\\' || c1 == '^') |
|
1301 spc_next = next_char_is_space (); |
|
1302 else |
|
1303 goto done; |
|
1304 break; |
|
1305 |
|
1306 default: |
|
1307 goto done; |
|
1308 } |
|
1309 |
|
1310 bin_op = looks_like_bin_op (spc_prev, spc_next); |
|
1311 |
|
1312 done: |
|
1313 yyunput (c1, yytext); |
|
1314 yyunput (c0, yytext); |
|
1315 |
|
1316 return bin_op; |
|
1317 } |
|
1318 |
146
|
1319 /* |
|
1320 * Used to delete trailing white space from tokens. |
|
1321 */ |
|
1322 static char * |
1
|
1323 strip_trailing_whitespace (char *s) |
|
1324 { |
|
1325 char *retval = strsave (s); |
|
1326 |
|
1327 char *t = strchr (retval, ' '); |
|
1328 if (t != (char *) NULL) |
|
1329 *t = '\0'; |
|
1330 |
|
1331 t = strchr (retval, '\t'); |
|
1332 if (t != (char *) NULL) |
|
1333 *t = '\0'; |
|
1334 |
|
1335 return retval; |
|
1336 } |
|
1337 |
146
|
1338 /* |
|
1339 * Figure out exactly what kind of token to return when we have seen |
|
1340 * an identifier. Handles keywords. |
|
1341 */ |
|
1342 static int |
|
1343 handle_identifier (char *tok, int next_tok_is_eq) |
|
1344 { |
|
1345 // If we have a regular keyword, or a plot STYLE, return it. STYLE is |
|
1346 // special only because it can't be followed by an identifier. |
|
1347 |
|
1348 int kw_token = is_keyword (tok); |
|
1349 if (kw_token) |
|
1350 { |
|
1351 if (kw_token == STYLE) |
|
1352 { |
|
1353 current_input_column += yyleng; |
|
1354 quote_is_transpose = 0; |
|
1355 cant_be_identifier = 1; |
|
1356 convert_spaces_to_comma = 1; |
|
1357 return kw_token; |
|
1358 } |
|
1359 else |
|
1360 TOK_RETURN (kw_token); |
|
1361 } |
|
1362 |
|
1363 // See if we have a plot keyword (title, using, or with). |
|
1364 |
148
|
1365 int plot_option_kw = is_plot_keyword (tok); |
|
1366 if (plotting && cant_be_identifier && plot_option_kw) |
146
|
1367 TOK_RETURN (plot_option_kw); |
|
1368 |
|
1369 // Yes, we really do need both of these plot_range variables. One |
|
1370 // is used to mark when we are past all possiblity of a plot range, |
|
1371 // the other is used to mark when we are actually between the square |
|
1372 // brackets that surround the range. |
|
1373 |
|
1374 if (plotting && ! in_plot_range) |
|
1375 past_plot_range = 1; |
|
1376 |
|
1377 // It is always an error for an identifier to be followed directly by |
|
1378 // another identifier. |
|
1379 |
|
1380 cant_be_identifier = 1; |
|
1381 |
|
1382 // If we are looking at a text style function, set up to gobble its |
|
1383 // arguments. These are also reserved words, but only because it |
|
1384 // would be very difficult to do anything intelligent with them if |
|
1385 // they were not reserved. |
|
1386 |
|
1387 if (is_text_function_name (tok)) |
|
1388 { |
|
1389 BEGIN TEXT_FCN; |
|
1390 |
169
|
1391 if (strcmp (tok, "help") == 0) |
146
|
1392 BEGIN HELP_FCN; |
|
1393 else if (strcmp (tok, "set") == 0) |
|
1394 doing_set = 1; |
|
1395 } |
|
1396 |
|
1397 // Make sure we put the return values of a function in the symbol |
|
1398 // table that is local to the function. |
|
1399 |
|
1400 if (next_tok_is_eq && defining_func && maybe_screwed) |
|
1401 curr_sym_tab = tmp_local_sym_tab; |
|
1402 |
|
1403 // Find the token in the symbol table. |
|
1404 |
|
1405 yylval.tok_val = new token (lookup_identifier (tok), |
|
1406 input_line_number, |
|
1407 current_input_column); |
|
1408 |
|
1409 token_stack.push (yylval.tok_val); |
|
1410 |
|
1411 // After seeing an identifer, it is ok to convert spaces to a comma |
|
1412 // (if needed). |
|
1413 |
|
1414 convert_spaces_to_comma = 1; |
|
1415 current_input_column += yyleng; |
|
1416 |
|
1417 // If we are defining a function and we have not seen the parameter |
|
1418 // list yet and the next token is `=', return a token that represents |
|
1419 // the only return value for the function. For example, |
|
1420 // |
|
1421 // function SCREW = f (args); |
|
1422 // |
|
1423 // The variable maybe_screwed is reset in parse.y. |
|
1424 |
|
1425 if (next_tok_is_eq) |
|
1426 { |
|
1427 if (defining_func && maybe_screwed) |
|
1428 return SCREW; |
|
1429 else |
|
1430 return NAME; |
|
1431 } |
|
1432 |
|
1433 // At this point, we are only dealing with identifiers that are not |
|
1434 // followed by `=' (if the next token is `=', there is no need to |
|
1435 // check to see if we should insert a comma (invalid syntax), or allow |
|
1436 // a following `'' to be treated as a transpose (the next token is |
|
1437 // `=', so it can't be `''. |
|
1438 |
|
1439 quote_is_transpose = 1; |
|
1440 do_comma_insert_check (); |
|
1441 |
|
1442 // Check to see if we should insert a comma. |
|
1443 |
|
1444 if (! in_brace_or_paren.empty () && in_brace_or_paren.top ()) |
|
1445 { |
|
1446 int c0 = yytext[yyleng-1]; |
|
1447 int spc_prev = (c0 == ' ' || c0 == '\t'); |
|
1448 int bin_op = next_token_is_bin_op (spc_prev, yytext); |
|
1449 |
|
1450 int postfix_un_op = next_token_is_postfix_unary_op (spc_prev, |
|
1451 yytext); |
|
1452 |
|
1453 int c1 = yyinput (); |
|
1454 unput (c1); |
|
1455 int other_op = match_any (c1, ",;\n]("); |
|
1456 |
|
1457 if (! (postfix_un_op || bin_op || other_op)) |
|
1458 unput (','); |
|
1459 } |
|
1460 |
|
1461 return NAME; |
|
1462 } |
|
1463 |
|
1464 /* |
338
|
1465 * Print a warning if a function file that defines a function has |
|
1466 * anything other than comments and whitespace following the END token |
|
1467 * that matches the FUNCTION statement. |
146
|
1468 */ |
1
|
1469 void |
|
1470 check_for_garbage_after_fcn_def (void) |
|
1471 { |
|
1472 // By making a newline be the next character to be read, we will force |
|
1473 // the parser to return after reading the function. Calling yyunput |
|
1474 // with EOF seems not to work... |
|
1475 |
|
1476 int in_comment = 0; |
|
1477 int lineno = input_line_number; |
|
1478 int c; |
|
1479 while ((c = yyinput ()) != EOF) |
|
1480 { |
|
1481 switch (c) |
|
1482 { |
|
1483 case ' ': |
|
1484 case '\t': |
|
1485 case ';': |
|
1486 case ',': |
|
1487 break; |
|
1488 case '\n': |
|
1489 if (in_comment) |
|
1490 in_comment = 0; |
|
1491 break; |
|
1492 case '%': |
|
1493 case '#': |
|
1494 in_comment = 1; |
|
1495 break; |
|
1496 default: |
|
1497 if (in_comment) |
|
1498 break; |
|
1499 else |
|
1500 { |
|
1501 warning ("ignoring trailing garbage after end of function\n\ |
340
|
1502 near line %d of file `%s'", lineno, curr_fcn_file_name); |
1
|
1503 |
|
1504 yyunput ('\n', yytext); |
|
1505 return; |
|
1506 } |
|
1507 } |
|
1508 } |
|
1509 yyunput ('\n', yytext); |
|
1510 } |
|
1511 |
|
1512 /* Maybe someday... |
|
1513 |
|
1514 "+=" return ADD_EQ; |
|
1515 "-=" return SUB_EQ; |
|
1516 "*=" return MUL_EQ; |
|
1517 "/=" return DIV_EQ; |
|
1518 "\\=" return LEFTDIV_EQ; |
|
1519 ".+=" return ADD_EQ; |
|
1520 ".-=" return SUB_EQ; |
|
1521 ".*=" return EMUL_EQ; |
|
1522 "./=" return EDIV_EQ; |
|
1523 ".\\=" return ELEFTDIV_EQ; |
|
1524 |
|
1525 */ |