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