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