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 %{ |
970
|
548 // Identifiers. Truncate the token at the first space or tab but |
|
549 // don't write directly on yytext. |
968
|
550 %} |
|
551 |
967
|
552 {IDENT}{S}* { |
|
553 static char *tok = 0; |
|
554 delete [] tok; |
|
555 tok = strip_trailing_whitespace (yytext); |
970
|
556 int c = yyinput (); |
|
557 unput (c); |
|
558 return handle_identifier (tok, (c == '=')); |
967
|
559 } |
1
|
560 |
968
|
561 %{ |
|
562 // A new line character. New line characters inside matrix constants |
|
563 // are handled by the <MATRIX> start state code above. |
|
564 %} |
|
565 |
967
|
566 {NL} { |
|
567 quote_is_transpose = 0; |
|
568 cant_be_identifier = 0; |
|
569 current_input_column = 1; |
|
570 convert_spaces_to_comma = 1; |
|
571 return '\n'; |
|
572 } |
1
|
573 |
968
|
574 %{ |
|
575 // Single quote can either be the beginning of a string or a transpose |
|
576 // operator. |
|
577 %} |
|
578 |
967
|
579 "'" { |
|
580 current_input_column++; |
|
581 convert_spaces_to_comma = 1; |
1
|
582 |
967
|
583 if (quote_is_transpose) |
|
584 { |
|
585 do_comma_insert_check (); |
|
586 return QUOTE; |
|
587 } |
|
588 else |
|
589 BEGIN STRING; |
|
590 } |
1
|
591 |
968
|
592 %{ |
|
593 // Colon operator is handled differently if we are in the range part |
|
594 // of a plot command. |
|
595 %} |
|
596 |
967
|
597 ":" { |
|
598 if (plotting && (in_plot_range || in_plot_using)) |
|
599 BIN_OP_RETURN (COLON, 1); |
|
600 else |
|
601 BIN_OP_RETURN (':', 0); |
|
602 } |
1
|
603 |
968
|
604 %{ |
|
605 // Gobble comments. |
|
606 %} |
|
607 |
967
|
608 {CCHAR} { |
|
609 if (in_brace_or_paren.empty () && beginning_of_function) |
|
610 { |
|
611 grab_help_text (); |
|
612 beginning_of_function = 0; |
|
613 } |
|
614 else |
|
615 { |
|
616 int c; |
|
617 while ((c = yyinput ()) != EOF && c != '\n') |
|
618 ; // Eat comment. |
|
619 } |
440
|
620 |
967
|
621 quote_is_transpose = 0; |
|
622 cant_be_identifier = 0; |
|
623 current_input_column = 1; |
|
624 convert_spaces_to_comma = 1; |
|
625 return '\n'; |
|
626 } |
440
|
627 |
968
|
628 %{ |
|
629 // Other operators. |
|
630 %} |
|
631 |
1
|
632 \" { BEGIN DQSTRING; } |
143
|
633 ".*" { BIN_OP_RETURN (EMUL, 0); } |
|
634 "./" { BIN_OP_RETURN (EDIV, 0); } |
|
635 ".\\" { BIN_OP_RETURN (ELEFTDIV, 0); } |
967
|
636 {EPOW} { BIN_OP_RETURN (EPOW, 0); } |
146
|
637 ".'" { do_comma_insert_check (); BIN_OP_RETURN (TRANSPOSE, 1); } |
|
638 "++" { do_comma_insert_check (); BIN_OP_RETURN (PLUS_PLUS, 1); } |
|
639 "--" { do_comma_insert_check (); BIN_OP_RETURN (MINUS_MINUS, 1); } |
143
|
640 "<=" { BIN_OP_RETURN (EXPR_LE, 0); } |
|
641 "==" { BIN_OP_RETURN (EXPR_EQ, 0); } |
967
|
642 {NOTEQ} { BIN_OP_RETURN (EXPR_NE, 0); } |
143
|
643 ">=" { BIN_OP_RETURN (EXPR_GE, 0); } |
|
644 "|" { BIN_OP_RETURN (EXPR_OR, 0); } |
|
645 "&" { BIN_OP_RETURN (EXPR_AND, 0); } |
|
646 "<" { BIN_OP_RETURN (EXPR_LT, 0); } |
|
647 ">" { BIN_OP_RETURN (EXPR_GT, 0); } |
|
648 "*" { BIN_OP_RETURN ('*', 0); } |
|
649 "/" { BIN_OP_RETURN ('/', 0); } |
|
650 "\\" { BIN_OP_RETURN (LEFTDIV, 0); } |
|
651 ";" { BIN_OP_RETURN (';', 1); } |
|
652 "," { BIN_OP_RETURN (',', 1); } |
967
|
653 {POW} { BIN_OP_RETURN (POW, 0); } |
143
|
654 "=" { BIN_OP_RETURN ('=', 1); } |
967
|
655 |
|
656 "||" { |
|
657 #ifdef SHORT_CIRCUIT_LOGICALS |
|
658 BIN_OP_RETURN (EXPR_OR_OR, 0); |
|
659 #else |
|
660 BIN_OP_RETURN (EXPR_OR, 0); |
|
661 #endif |
|
662 } |
|
663 |
|
664 "&&" { |
|
665 #ifdef SHORT_CIRCUIT_LOGICALS |
|
666 BIN_OP_RETURN (EXPR_AND_AND, 0); |
|
667 #else |
|
668 BIN_OP_RETURN (EXPR_AND, 0); |
|
669 #endif |
|
670 } |
|
671 |
|
672 {NOT} { |
|
673 if (plotting && ! in_plot_range) |
|
674 past_plot_range = 1; |
|
675 BIN_OP_RETURN (EXPR_NOT, 0); |
|
676 } |
1
|
677 |
967
|
678 {PLUS} { |
|
679 if (plotting && ! in_plot_range) |
|
680 past_plot_range = 1; |
|
681 BIN_OP_RETURN ('+', 0); |
|
682 } |
|
683 |
|
684 {MINUS} { |
|
685 if (plotting && ! in_plot_range) |
|
686 past_plot_range = 1; |
|
687 BIN_OP_RETURN ('-', 0); |
|
688 } |
|
689 |
|
690 "(" { |
|
691 if (plotting && ! in_plot_range) |
|
692 past_plot_range = 1; |
|
693 in_brace_or_paren.push (0); |
|
694 TOK_RETURN ('('); |
|
695 } |
|
696 |
|
697 ")" { |
|
698 if (! in_brace_or_paren.empty ()) |
|
699 in_brace_or_paren.pop (); |
|
700 do_comma_insert_check (); |
|
701 current_input_column++; |
|
702 cant_be_identifier = 1; |
|
703 quote_is_transpose = 1; |
|
704 convert_spaces_to_comma = (! in_brace_or_paren.empty () |
|
705 && in_brace_or_paren.top ()); |
|
706 return ')'; |
|
707 } |
|
708 |
968
|
709 %{ |
1
|
710 // We return everything else as single character tokens, which should |
|
711 // eventually result in a parse error. |
968
|
712 %} |
1
|
713 |
968
|
714 . { TOK_RETURN (yytext[0]); } |
1
|
715 |
|
716 %% |
|
717 |
767
|
718 // GAG. |
|
719 // |
|
720 // If we're reading a matrix and the next character is '[', make sure |
|
721 // that we insert a comma ahead of it. |
|
722 |
146
|
723 void |
1
|
724 do_comma_insert_check (void) |
|
725 { |
|
726 int c = yyinput (); |
146
|
727 yyunput (c, yytext); |
1
|
728 do_comma_insert = (braceflag && c == '['); |
|
729 } |
|
730 |
767
|
731 // Fix things up for errors or interrupts. The parser is never called |
|
732 // recursively, so it is always safe to reinitialize its state before |
|
733 // doing any parsing. |
|
734 |
1
|
735 void |
|
736 reset_parser (void) |
|
737 { |
287
|
738 // Start off on the right foot. |
1
|
739 BEGIN 0; |
166
|
740 error_state = 0; |
287
|
741 |
|
742 // We do want a prompt by default. |
1
|
743 promptflag = 1; |
287
|
744 |
|
745 // Not initially screwed by `function [...] = f (...)' syntax. |
1
|
746 maybe_screwed = 0; |
|
747 maybe_screwed_again = 0; |
287
|
748 |
|
749 // Not initially inside a loop or if statement. |
1
|
750 looping = 0; |
|
751 iffing = 0; |
287
|
752 |
|
753 // Quote marks strings intially. |
|
754 quote_is_transpose = 0; |
|
755 |
|
756 // Next token can be identifier. |
|
757 cant_be_identifier = 0; |
|
758 |
|
759 // No need to do comma insert or convert spaces to comma at beginning |
|
760 // of input. |
|
761 do_comma_insert = 0; |
|
762 convert_spaces_to_comma = 1; |
|
763 |
|
764 // Not initially defining a function. |
|
765 beginning_of_function = 0; |
|
766 defining_func = 0; |
|
767 |
|
768 // Not initially doing any plotting or setting of plot attributes. |
|
769 plotting = 0; |
|
770 in_plot_range = 0; |
|
771 past_plot_range = 0; |
|
772 in_plot_using = 0; |
|
773 in_plot_style = 0; |
|
774 doing_set = 0; |
|
775 |
747
|
776 // Not initially looking at indirect references. |
|
777 looking_at_indirect_ref = 0; |
|
778 |
287
|
779 // Error may have occurred inside some parentheses or braces. |
|
780 in_brace_or_paren.clear (); |
|
781 |
|
782 // Not initially defining a matrix list. |
|
783 braceflag = 0; |
1
|
784 ml.clear (); |
|
785 mlnm.clear (); |
287
|
786 |
|
787 // Clear out the stack of token info used to track line and column |
|
788 // numbers. |
143
|
789 while (! token_stack.empty ()) |
|
790 delete token_stack.pop (); |
287
|
791 |
|
792 // Can be reset by defining a function. |
|
793 current_input_column = 1; |
373
|
794 if (! reading_script_file) |
|
795 input_line_number = current_command_number - 1; |
287
|
796 |
|
797 // Only ask for input from stdin if we are expecting interactive |
|
798 // input. |
338
|
799 if (interactive && ! (reading_fcn_file || get_input_from_eval_string)) |
287
|
800 yyrestart (stdin); |
1
|
801 } |
|
802 |
767
|
803 // Replace backslash escapes in a string with the real values. |
|
804 |
1
|
805 static void |
|
806 do_string_escapes (char *s) |
|
807 { |
|
808 char *p1 = s; |
|
809 char *p2 = s; |
|
810 while (*p2 != '\0') |
|
811 { |
|
812 if (*p2 == '\\' && *(p2+1) != '\0') |
|
813 { |
|
814 switch (*++p2) |
|
815 { |
|
816 case 'a': |
|
817 *p1 = '\a'; |
|
818 break; |
777
|
819 |
1
|
820 case 'b': // backspace |
|
821 *p1 = '\b'; |
|
822 break; |
777
|
823 |
1
|
824 case 'f': // formfeed |
|
825 *p1 = '\f'; |
|
826 break; |
777
|
827 |
1
|
828 case 'n': // newline |
|
829 *p1 = '\n'; |
|
830 break; |
777
|
831 |
1
|
832 case 'r': // carriage return |
|
833 *p1 = '\r'; |
|
834 break; |
777
|
835 |
1
|
836 case 't': // horizontal tab |
|
837 *p1 = '\t'; |
|
838 break; |
777
|
839 |
1
|
840 case 'v': // vertical tab |
|
841 *p1 = '\v'; |
|
842 break; |
777
|
843 |
1
|
844 case '\\': // backslash |
|
845 *p1 = '\\'; |
|
846 break; |
777
|
847 |
1
|
848 case '\'': // quote |
|
849 *p1 = '\''; |
|
850 break; |
777
|
851 |
1
|
852 case '"': // double quote |
|
853 *p1 = '"'; |
|
854 break; |
777
|
855 |
1
|
856 default: |
777
|
857 warning ("unrecognized escape sequence `\\%c' --\ |
|
858 converting to `%c'", *p2, *p2); |
1
|
859 *p1 = *p2; |
|
860 break; |
|
861 } |
|
862 } |
|
863 else if (*p2 == '\'' && *(p2+1) == '\'') |
|
864 { |
|
865 *p1 = '\''; |
|
866 p2++; |
|
867 } |
|
868 else |
|
869 { |
|
870 *p1 = *p2; |
|
871 } |
|
872 |
|
873 p1++; |
|
874 p2++; |
|
875 } |
|
876 |
|
877 *p1 = '\0'; |
|
878 } |
|
879 |
767
|
880 // If we read some newlines, we need figure out what column we're |
|
881 // really looking at. |
|
882 |
1
|
883 static void |
|
884 fixup_column_count (char *s) |
|
885 { |
|
886 char c; |
|
887 while ((c = *s++) != '\0') |
|
888 { |
|
889 if (c == '\n') |
143
|
890 current_input_column = 1; |
1
|
891 else |
|
892 current_input_column++; |
|
893 } |
|
894 } |
|
895 |
767
|
896 // Include these so that we don't have to link to libfl.a. |
246
|
897 |
1
|
898 #ifdef yywrap |
|
899 #undef yywrap |
|
900 #endif |
246
|
901 static int |
1
|
902 yywrap (void) |
|
903 { |
287
|
904 return 1; |
1
|
905 } |
|
906 |
767
|
907 // These are not needed with flex-2.4.6, but may be needed with |
|
908 // earlier 2.4.x versions. |
|
909 |
287
|
910 #if 0 |
246
|
911 static void * |
|
912 yy_flex_alloc (int size) |
|
913 { |
|
914 return (void *) malloc ((unsigned) size); |
|
915 } |
|
916 |
|
917 static void * |
|
918 yy_flex_realloc (void *ptr, int size) |
|
919 { |
|
920 return (void *) realloc (ptr, (unsigned) size); |
|
921 } |
|
922 |
|
923 static void |
|
924 yy_flex_free (void *ptr) |
|
925 { |
|
926 free (ptr); |
|
927 } |
287
|
928 #endif |
246
|
929 |
767
|
930 // Tell us all what the current buffer is. |
|
931 |
1
|
932 YY_BUFFER_STATE |
|
933 current_buffer (void) |
|
934 { |
|
935 return YY_CURRENT_BUFFER; |
|
936 } |
|
937 |
767
|
938 // Create a new buffer. |
|
939 |
1
|
940 YY_BUFFER_STATE |
|
941 create_buffer (FILE *f) |
|
942 { |
|
943 return yy_create_buffer (f, YY_BUF_SIZE); |
|
944 } |
|
945 |
767
|
946 // Start reading a new buffer. |
|
947 |
1
|
948 void |
|
949 switch_to_buffer (YY_BUFFER_STATE buf) |
|
950 { |
|
951 yy_switch_to_buffer (buf); |
|
952 } |
|
953 |
767
|
954 // Delete a buffer. |
|
955 |
1
|
956 void |
|
957 delete_buffer (YY_BUFFER_STATE buf) |
|
958 { |
|
959 yy_delete_buffer (buf); |
|
960 } |
|
961 |
767
|
962 // Restore a buffer (for unwind-prot). |
|
963 |
1
|
964 void |
|
965 restore_input_buffer (void *buf) |
|
966 { |
|
967 switch_to_buffer ((YY_BUFFER_STATE) buf); |
|
968 } |
|
969 |
767
|
970 // Delete a buffer (for unwind-prot). |
|
971 |
1
|
972 void |
|
973 delete_input_buffer (void *buf) |
|
974 { |
|
975 delete_buffer ((YY_BUFFER_STATE) buf); |
|
976 } |
|
977 |
767
|
978 // Check to see if a character string matches any of the possible line |
|
979 // styles for plots. |
|
980 |
1
|
981 static char * |
|
982 plot_style_token (char *s) |
|
983 { |
146
|
984 static char *plot_styles[] = |
|
985 { |
925
|
986 "boxes", |
924
|
987 "boxerrorbars", |
146
|
988 "dots", |
|
989 "errorbars", |
|
990 "impulses", |
|
991 "lines", |
|
992 "linespoints", |
|
993 "points", |
926
|
994 "steps", |
522
|
995 0, |
146
|
996 }; |
|
997 |
1
|
998 char **tmp = plot_styles; |
522
|
999 while (*tmp) |
1
|
1000 { |
|
1001 if (almost_match (*tmp, s)) |
|
1002 return *tmp; |
|
1003 |
|
1004 tmp++; |
|
1005 } |
|
1006 |
522
|
1007 return 0; |
1
|
1008 } |
|
1009 |
767
|
1010 // Check to see if a character string matches any one of the plot |
941
|
1011 // option keywords. Don't match abbreviations for clear, since that's |
|
1012 // not a gnuplot keyword (users will probably only expect to be able |
|
1013 // to abbreviate actual gnuplot keywords). |
767
|
1014 |
1
|
1015 static int |
|
1016 is_plot_keyword (char *s) |
|
1017 { |
|
1018 if (almost_match ("title", s)) |
146
|
1019 { |
|
1020 return TITLE; |
|
1021 } |
1
|
1022 else if (almost_match ("using", s)) |
146
|
1023 { |
|
1024 in_plot_using = 1; |
|
1025 return USING; |
|
1026 } |
1
|
1027 else if (almost_match ("with", s)) |
146
|
1028 { |
|
1029 in_plot_style = 1; |
|
1030 return WITH; |
|
1031 } |
941
|
1032 else if (strcmp ("clear", s) == 0) |
883
|
1033 { |
|
1034 return CLEAR; |
|
1035 } |
1
|
1036 else |
146
|
1037 { |
|
1038 return 0; |
|
1039 } |
1
|
1040 } |
|
1041 |
767
|
1042 // Handle keywords. Could probably be more efficient... |
|
1043 |
1
|
1044 static int |
|
1045 is_keyword (char *s) |
|
1046 { |
|
1047 if (plotting && in_plot_style) |
|
1048 { |
|
1049 char *sty = plot_style_token (s); |
522
|
1050 if (sty) |
1
|
1051 { |
|
1052 in_plot_style = 0; |
143
|
1053 yylval.tok_val = new token (sty); |
|
1054 token_stack.push (yylval.tok_val); |
1
|
1055 return STYLE; |
|
1056 } |
|
1057 } |
|
1058 |
143
|
1059 int l = input_line_number; |
|
1060 int c = current_input_column; |
|
1061 |
922
|
1062 // XXX FIXME XXX -- this has really become too large a list to search |
|
1063 // like this... |
|
1064 |
1
|
1065 int end_found = 0; |
|
1066 if (strcmp ("break", s) == 0) |
143
|
1067 { |
191
|
1068 yylval.tok_val = new token (l, c); |
|
1069 token_stack.push (yylval.tok_val); |
143
|
1070 return BREAK; |
|
1071 } |
1
|
1072 else if (strcmp ("continue", s) == 0) |
143
|
1073 { |
191
|
1074 yylval.tok_val = new token (l, c); |
|
1075 token_stack.push (yylval.tok_val); |
143
|
1076 return CONTINUE; |
|
1077 } |
1
|
1078 else if (strcmp ("else", s) == 0) |
143
|
1079 { |
191
|
1080 yylval.tok_val = new token (l, c); |
|
1081 token_stack.push (yylval.tok_val); |
143
|
1082 return ELSE; |
|
1083 } |
1
|
1084 else if (strcmp ("elseif", s) == 0) |
143
|
1085 { |
191
|
1086 yylval.tok_val = new token (l, c); |
|
1087 token_stack.push (yylval.tok_val); |
143
|
1088 return ELSEIF; |
|
1089 } |
1
|
1090 else if (strcmp ("end", s) == 0) |
143
|
1091 { |
|
1092 end_found = 1; |
|
1093 yylval.tok_val = new token (token::simple_end, l, c); |
|
1094 token_stack.push (yylval.tok_val); |
|
1095 } |
1
|
1096 else if (strcmp ("endfor", s) == 0) |
143
|
1097 { |
|
1098 end_found = 1; |
|
1099 yylval.tok_val = new token (token::for_end, l, c); |
|
1100 token_stack.push (yylval.tok_val); |
|
1101 } |
1
|
1102 else if (strcmp ("endfunction", s) == 0) |
143
|
1103 { |
|
1104 end_found = 1; |
|
1105 yylval.tok_val = new token (token::function_end, l, c); |
|
1106 token_stack.push (yylval.tok_val); |
|
1107 } |
1
|
1108 else if (strcmp ("endif", s) == 0) |
143
|
1109 { |
|
1110 end_found = 1; |
|
1111 yylval.tok_val = new token (token::if_end, l, c); |
|
1112 token_stack.push (yylval.tok_val); |
|
1113 } |
1
|
1114 else if (strcmp ("endwhile", s) == 0) |
143
|
1115 { |
|
1116 end_found = 1; |
|
1117 yylval.tok_val = new token (token::while_end, l, c); |
|
1118 token_stack.push (yylval.tok_val); |
|
1119 } |
1
|
1120 else if (strcmp ("for", s) == 0) |
143
|
1121 { |
|
1122 promptflag--; |
|
1123 looping++; |
191
|
1124 yylval.tok_val = new token (l, c); |
|
1125 token_stack.push (yylval.tok_val); |
143
|
1126 return FOR; |
|
1127 } |
1
|
1128 else if (strcmp ("function", s) == 0) |
|
1129 { |
|
1130 if (defining_func) |
|
1131 { |
191
|
1132 error ("function keyword invalid within a function body"); |
|
1133 |
338
|
1134 if ((reading_fcn_file || reading_script_file) |
522
|
1135 && curr_fcn_file_name) |
341
|
1136 error ("defining new function near line %d of file `%s.m'", |
|
1137 input_line_number, curr_fcn_file_name); |
191
|
1138 else |
|
1139 error ("defining new function near line %d", input_line_number); |
|
1140 |
627
|
1141 return LEXICAL_ERROR; |
1
|
1142 } |
|
1143 else |
|
1144 { |
|
1145 tmp_local_sym_tab = new symbol_table (); |
|
1146 curr_sym_tab = tmp_local_sym_tab; |
|
1147 defining_func = 1; |
|
1148 promptflag--; |
|
1149 beginning_of_function = 1; |
|
1150 help_buf[0] = '\0'; |
143
|
1151 input_line_number = 1; |
1
|
1152 return FCN; |
|
1153 } |
|
1154 } |
|
1155 else if (strcmp ("global", s) == 0) |
143
|
1156 { |
578
|
1157 yylval.tok_val = new token (l, c); |
|
1158 token_stack.push (yylval.tok_val); |
143
|
1159 return GLOBAL; |
|
1160 } |
1
|
1161 else if (strcmp ("gplot", s) == 0) |
143
|
1162 { |
|
1163 plotting = 1; |
|
1164 yylval.tok_val = new token (token::two_dee, l, c); |
476
|
1165 token_stack.push (yylval.tok_val); |
143
|
1166 return PLOT; |
|
1167 } |
1
|
1168 else if (strcmp ("gsplot", s) == 0) |
143
|
1169 { |
|
1170 plotting = 1; |
|
1171 yylval.tok_val = new token (token::three_dee, l, c); |
|
1172 token_stack.push (yylval.tok_val); |
|
1173 return PLOT; |
|
1174 } |
476
|
1175 else if (strcmp ("replot", s) == 0) |
|
1176 { |
|
1177 plotting = 1; |
|
1178 yylval.tok_val = new token (token::replot, l, c); |
|
1179 token_stack.push (yylval.tok_val); |
|
1180 return PLOT; |
|
1181 } |
1
|
1182 else if (strcmp ("if", s) == 0) |
143
|
1183 { |
|
1184 iffing++; |
|
1185 promptflag--; |
191
|
1186 yylval.tok_val = new token (l, c); |
|
1187 token_stack.push (yylval.tok_val); |
143
|
1188 return IF; |
|
1189 } |
1
|
1190 else if (strcmp ("return", s) == 0) |
143
|
1191 { |
191
|
1192 yylval.tok_val = new token (l, c); |
|
1193 token_stack.push (yylval.tok_val); |
143
|
1194 return FUNC_RET; |
|
1195 } |
1
|
1196 else if (strcmp ("while", s) == 0) |
143
|
1197 { |
|
1198 promptflag--; |
|
1199 looping++; |
191
|
1200 yylval.tok_val = new token (l, c); |
|
1201 token_stack.push (yylval.tok_val); |
143
|
1202 return WHILE; |
|
1203 } |
916
|
1204 else if (strcmp ("unwind_protect", s) == 0) |
|
1205 { |
|
1206 promptflag--; |
|
1207 yylval.tok_val = new token (l, c); |
|
1208 token_stack.push (yylval.tok_val); |
924
|
1209 return UNWIND; |
916
|
1210 } |
|
1211 else if (strcmp ("unwind_protect_cleanup", s) == 0) |
|
1212 { |
|
1213 yylval.tok_val = new token (l, c); |
|
1214 token_stack.push (yylval.tok_val); |
924
|
1215 return CLEANUP; |
916
|
1216 } |
|
1217 else if (strcmp ("end_unwind_protect", s) == 0) |
|
1218 { |
|
1219 end_found = 1; |
|
1220 yylval.tok_val = new token (token::unwind_protect_end, l, c); |
|
1221 token_stack.push (yylval.tok_val); |
|
1222 } |
922
|
1223 else if (strcmp ("all_va_args", s) == 0) |
|
1224 { |
|
1225 yylval.tok_val = new token (l, c); |
|
1226 token_stack.push (yylval.tok_val); |
|
1227 return ALL_VA_ARGS; |
|
1228 } |
1
|
1229 |
|
1230 if (end_found) |
|
1231 { |
143
|
1232 if (! defining_func && ! looping) |
1
|
1233 promptflag++; |
|
1234 return END; |
|
1235 } |
|
1236 |
|
1237 return 0; |
|
1238 } |
|
1239 |
767
|
1240 // Try to find an identifier. All binding to global or builtin |
|
1241 // variables occurs when expressions are evaluated. |
|
1242 |
1
|
1243 static symbol_record * |
|
1244 lookup_identifier (char *name) |
|
1245 { |
|
1246 return curr_sym_tab->lookup (name, 1, 0); |
|
1247 } |
|
1248 |
767
|
1249 // Grab the help text from an function file. |
|
1250 |
1
|
1251 static void |
|
1252 grab_help_text (void) |
|
1253 { |
|
1254 int max_len = HELP_BUF_LENGTH - 1; |
|
1255 |
|
1256 int in_comment = 1; |
|
1257 int len = 0; |
576
|
1258 int c = 0; |
1
|
1259 |
|
1260 while ((c = yyinput ()) != EOF) |
|
1261 { |
|
1262 if (in_comment) |
|
1263 { |
|
1264 help_buf[len++] = c; |
|
1265 if (c == '\n') |
|
1266 in_comment = 0; |
|
1267 } |
|
1268 else |
|
1269 { |
|
1270 switch (c) |
|
1271 { |
|
1272 case '%': |
|
1273 case '#': |
|
1274 in_comment = 1; |
777
|
1275 break; |
|
1276 |
1
|
1277 case ' ': |
|
1278 case '\t': |
446
|
1279 break; |
777
|
1280 |
440
|
1281 default: |
446
|
1282 goto done; |
1
|
1283 } |
|
1284 } |
|
1285 |
|
1286 if (len > max_len) |
|
1287 { |
216
|
1288 warning ("grab_help_text: buffer overflow after caching %d chars", |
1
|
1289 max_len); |
440
|
1290 break; |
1
|
1291 } |
|
1292 } |
|
1293 |
446
|
1294 done: |
|
1295 |
576
|
1296 if (c) |
|
1297 yyunput (c, yytext); |
|
1298 |
1
|
1299 help_buf[len] = '\0'; |
|
1300 } |
|
1301 |
767
|
1302 // Return 1 if the given character matches any character in the given |
|
1303 // string. |
|
1304 |
1
|
1305 static int |
|
1306 match_any (char c, char *s) |
|
1307 { |
|
1308 char tmp; |
|
1309 while ((tmp = *s++) != '\0') |
|
1310 { |
|
1311 if (c == tmp) |
|
1312 return 1; |
|
1313 } |
|
1314 return 0; |
|
1315 } |
|
1316 |
767
|
1317 // Given information about the spacing surrounding an operator, |
|
1318 // return 1 if it looks like it should be treated as a binary |
|
1319 // operator. For example, |
|
1320 // |
|
1321 // [ 1 + 2 ] or [ 1+ 2] or [ 1+2 ] ==> binary |
|
1322 |
1
|
1323 static int |
|
1324 looks_like_bin_op (int spc_prev, int spc_next) |
|
1325 { |
608
|
1326 return ((spc_prev && spc_next) || ! spc_prev); |
1
|
1327 } |
|
1328 |
767
|
1329 // Duh. |
|
1330 |
1
|
1331 static int |
|
1332 next_char_is_space (void) |
|
1333 { |
|
1334 int c = yyinput (); |
|
1335 yyunput (c, yytext); |
|
1336 return (c == ' ' || c == '\t'); |
|
1337 } |
|
1338 |
767
|
1339 // Try to determine if the next token should be treated as a postfix |
|
1340 // unary operator. This is ugly, but it seems to do the right thing. |
|
1341 |
1
|
1342 static int |
|
1343 next_token_is_postfix_unary_op (int spc_prev, char *yytext) |
|
1344 { |
|
1345 int un_op = 0; |
|
1346 |
|
1347 int c0 = yyinput (); |
|
1348 int c1 = yyinput (); |
|
1349 |
|
1350 yyunput (c1, yytext); |
|
1351 yyunput (c0, yytext); |
|
1352 |
|
1353 int transpose = (c0 == '.' && c1 == '\''); |
|
1354 int hermitian = (c0 == '\''); |
|
1355 |
|
1356 un_op = (transpose || (hermitian && ! spc_prev)); |
|
1357 |
|
1358 return un_op; |
|
1359 } |
|
1360 |
767
|
1361 // Try to determine if the next token should be treated as a binary |
|
1362 // operator. This is even uglier, but it also seems to do the right |
|
1363 // thing. |
|
1364 |
1
|
1365 static int |
|
1366 next_token_is_bin_op (int spc_prev, char *yytext) |
|
1367 { |
|
1368 int bin_op = 0; |
|
1369 int spc_next = 0; |
|
1370 |
|
1371 int c0 = yyinput (); |
|
1372 int c1 = yyinput (); |
|
1373 |
|
1374 switch (c0) |
|
1375 { |
777
|
1376 case '+': |
|
1377 case '-': |
|
1378 case '/': |
|
1379 case ':': |
|
1380 case '\\': |
|
1381 case '^': |
1
|
1382 spc_next = (c1 == ' ' || c1 == '\t'); |
|
1383 break; |
|
1384 |
|
1385 case '&': |
|
1386 if (c1 == '&') |
|
1387 spc_next = next_char_is_space (); |
|
1388 else |
|
1389 spc_next = (c1 == ' ' || c1 == '\t'); |
|
1390 break; |
|
1391 |
|
1392 case '*': |
|
1393 if (c1 == '*') |
|
1394 spc_next = next_char_is_space (); |
|
1395 else |
|
1396 spc_next = (c1 == ' ' || c1 == '\t'); |
|
1397 break; |
|
1398 |
|
1399 case '|': |
|
1400 if (c1 == '|') |
|
1401 spc_next = next_char_is_space (); |
|
1402 else |
|
1403 spc_next = (c1 == ' ' || c1 == '\t'); |
|
1404 break; |
|
1405 |
|
1406 case '<': |
|
1407 if (c1 == '=' || c1 == '>') |
|
1408 spc_next = next_char_is_space (); |
|
1409 else |
|
1410 spc_next = (c1 == ' ' || c1 == '\t'); |
|
1411 break; |
|
1412 |
|
1413 case '>': |
|
1414 if (c1 == '=') |
|
1415 spc_next = next_char_is_space (); |
|
1416 else |
|
1417 spc_next = (c1 == ' ' || c1 == '\t'); |
|
1418 break; |
|
1419 |
777
|
1420 case '~': |
|
1421 case '!': |
|
1422 case '=': |
1
|
1423 if (c1 == '=') |
|
1424 spc_next = next_char_is_space (); |
|
1425 else |
|
1426 goto done; |
|
1427 break; |
|
1428 |
|
1429 case '.': |
|
1430 if (c1 == '*') |
|
1431 { |
|
1432 int c2 = yyinput (); |
|
1433 if (c2 == '*') |
|
1434 spc_next = next_char_is_space (); |
|
1435 else |
|
1436 spc_next = (c2 == ' ' || c2 == '\t'); |
|
1437 yyunput (c2, yytext); |
|
1438 } |
|
1439 else if (c1 == '/' || c1 == '\\' || c1 == '^') |
|
1440 spc_next = next_char_is_space (); |
|
1441 else |
|
1442 goto done; |
|
1443 break; |
|
1444 |
|
1445 default: |
|
1446 goto done; |
|
1447 } |
|
1448 |
|
1449 bin_op = looks_like_bin_op (spc_prev, spc_next); |
|
1450 |
|
1451 done: |
|
1452 yyunput (c1, yytext); |
|
1453 yyunput (c0, yytext); |
|
1454 |
|
1455 return bin_op; |
|
1456 } |
|
1457 |
767
|
1458 // Used to delete trailing white space from tokens. |
|
1459 |
146
|
1460 static char * |
1
|
1461 strip_trailing_whitespace (char *s) |
|
1462 { |
|
1463 char *retval = strsave (s); |
|
1464 |
|
1465 char *t = strchr (retval, ' '); |
522
|
1466 if (t) |
1
|
1467 *t = '\0'; |
|
1468 |
|
1469 t = strchr (retval, '\t'); |
522
|
1470 if (t) |
1
|
1471 *t = '\0'; |
|
1472 |
|
1473 return retval; |
|
1474 } |
|
1475 |
767
|
1476 // Figure out exactly what kind of token to return when we have seen |
|
1477 // an identifier. Handles keywords. |
|
1478 |
146
|
1479 static int |
|
1480 handle_identifier (char *tok, int next_tok_is_eq) |
|
1481 { |
883
|
1482 // It is almost always an error for an identifier to be followed |
|
1483 // directly by another identifier. Special cases are handled below. |
|
1484 |
|
1485 cant_be_identifier = 1; |
|
1486 |
747
|
1487 // If we are expecting a structure element, we just want to return |
|
1488 // TEXT_ID, which is a string that is also a valid identifier. |
|
1489 |
|
1490 if (looking_at_indirect_ref) |
|
1491 { |
|
1492 yylval.tok_val = new token (tok); |
|
1493 token_stack.push (yylval.tok_val); |
|
1494 TOK_RETURN (TEXT_ID); |
|
1495 } |
|
1496 |
883
|
1497 // If we have a regular keyword, or a plot STYLE, return it. Keywords |
|
1498 // can be followed by identifiers (TOK_RETURN handles that). |
146
|
1499 |
|
1500 int kw_token = is_keyword (tok); |
|
1501 if (kw_token) |
|
1502 { |
|
1503 if (kw_token == STYLE) |
|
1504 { |
|
1505 current_input_column += yyleng; |
|
1506 quote_is_transpose = 0; |
|
1507 convert_spaces_to_comma = 1; |
|
1508 return kw_token; |
|
1509 } |
|
1510 else |
|
1511 TOK_RETURN (kw_token); |
|
1512 } |
|
1513 |
883
|
1514 // See if we have a plot keyword (title, using, with, or clear). |
146
|
1515 |
941
|
1516 if (plotting) |
|
1517 { |
146
|
1518 // Yes, we really do need both of these plot_range variables. One |
|
1519 // is used to mark when we are past all possiblity of a plot range, |
|
1520 // the other is used to mark when we are actually between the square |
|
1521 // brackets that surround the range. |
|
1522 |
941
|
1523 if (! in_plot_range) |
|
1524 past_plot_range = 1; |
|
1525 |
|
1526 int plot_option_kw = is_plot_keyword (tok); |
|
1527 |
|
1528 if (cant_be_identifier && plot_option_kw) |
|
1529 TOK_RETURN (plot_option_kw); |
|
1530 } |
146
|
1531 |
|
1532 // If we are looking at a text style function, set up to gobble its |
|
1533 // arguments. These are also reserved words, but only because it |
|
1534 // would be very difficult to do anything intelligent with them if |
|
1535 // they were not reserved. |
|
1536 |
|
1537 if (is_text_function_name (tok)) |
|
1538 { |
|
1539 BEGIN TEXT_FCN; |
|
1540 |
169
|
1541 if (strcmp (tok, "help") == 0) |
146
|
1542 BEGIN HELP_FCN; |
|
1543 else if (strcmp (tok, "set") == 0) |
|
1544 doing_set = 1; |
|
1545 } |
|
1546 |
|
1547 // Make sure we put the return values of a function in the symbol |
|
1548 // table that is local to the function. |
|
1549 |
|
1550 if (next_tok_is_eq && defining_func && maybe_screwed) |
|
1551 curr_sym_tab = tmp_local_sym_tab; |
|
1552 |
|
1553 // Find the token in the symbol table. |
|
1554 |
|
1555 yylval.tok_val = new token (lookup_identifier (tok), |
|
1556 input_line_number, |
|
1557 current_input_column); |
|
1558 |
|
1559 token_stack.push (yylval.tok_val); |
|
1560 |
|
1561 // After seeing an identifer, it is ok to convert spaces to a comma |
|
1562 // (if needed). |
|
1563 |
|
1564 convert_spaces_to_comma = 1; |
|
1565 current_input_column += yyleng; |
|
1566 |
|
1567 // If we are defining a function and we have not seen the parameter |
|
1568 // list yet and the next token is `=', return a token that represents |
|
1569 // the only return value for the function. For example, |
|
1570 // |
|
1571 // function SCREW = f (args); |
|
1572 // |
|
1573 // The variable maybe_screwed is reset in parse.y. |
|
1574 |
|
1575 if (next_tok_is_eq) |
|
1576 { |
|
1577 if (defining_func && maybe_screwed) |
|
1578 return SCREW; |
|
1579 else |
|
1580 return NAME; |
|
1581 } |
|
1582 |
|
1583 // At this point, we are only dealing with identifiers that are not |
|
1584 // followed by `=' (if the next token is `=', there is no need to |
|
1585 // check to see if we should insert a comma (invalid syntax), or allow |
|
1586 // a following `'' to be treated as a transpose (the next token is |
|
1587 // `=', so it can't be `''. |
|
1588 |
|
1589 quote_is_transpose = 1; |
|
1590 do_comma_insert_check (); |
|
1591 |
|
1592 // Check to see if we should insert a comma. |
|
1593 |
430
|
1594 if (user_pref.commas_in_literal_matrix != 2 |
|
1595 && ! in_brace_or_paren.empty () |
|
1596 && in_brace_or_paren.top ()) |
146
|
1597 { |
|
1598 int c0 = yytext[yyleng-1]; |
|
1599 int spc_prev = (c0 == ' ' || c0 == '\t'); |
|
1600 int bin_op = next_token_is_bin_op (spc_prev, yytext); |
|
1601 |
|
1602 int postfix_un_op = next_token_is_postfix_unary_op (spc_prev, |
|
1603 yytext); |
|
1604 |
|
1605 int c1 = yyinput (); |
|
1606 unput (c1); |
747
|
1607 int other_op = match_any (c1, ".,;\n]"); |
430
|
1608 int index_op = (c1 == '(' |
|
1609 && (user_pref.commas_in_literal_matrix == 0 |
|
1610 || ! spc_prev)); |
412
|
1611 |
|
1612 if (! (postfix_un_op || bin_op || other_op || index_op)) |
430
|
1613 unput (','); |
146
|
1614 } |
|
1615 |
|
1616 return NAME; |
|
1617 } |
|
1618 |
767
|
1619 // Print a warning if a function file that defines a function has |
|
1620 // anything other than comments and whitespace following the END token |
|
1621 // that matches the FUNCTION statement. |
|
1622 |
1
|
1623 void |
|
1624 check_for_garbage_after_fcn_def (void) |
|
1625 { |
|
1626 // By making a newline be the next character to be read, we will force |
|
1627 // the parser to return after reading the function. Calling yyunput |
|
1628 // with EOF seems not to work... |
|
1629 |
|
1630 int in_comment = 0; |
|
1631 int lineno = input_line_number; |
|
1632 int c; |
|
1633 while ((c = yyinput ()) != EOF) |
|
1634 { |
|
1635 switch (c) |
|
1636 { |
|
1637 case ' ': |
|
1638 case '\t': |
|
1639 case ';': |
|
1640 case ',': |
|
1641 break; |
777
|
1642 |
1
|
1643 case '\n': |
|
1644 if (in_comment) |
|
1645 in_comment = 0; |
|
1646 break; |
777
|
1647 |
1
|
1648 case '%': |
|
1649 case '#': |
|
1650 in_comment = 1; |
|
1651 break; |
777
|
1652 |
1
|
1653 default: |
|
1654 if (in_comment) |
|
1655 break; |
|
1656 else |
|
1657 { |
|
1658 warning ("ignoring trailing garbage after end of function\n\ |
341
|
1659 near line %d of file `%s.m'", lineno, curr_fcn_file_name); |
1
|
1660 |
|
1661 yyunput ('\n', yytext); |
|
1662 return; |
|
1663 } |
|
1664 } |
|
1665 } |
|
1666 yyunput ('\n', yytext); |
|
1667 } |
|
1668 |
767
|
1669 /* |
|
1670 |
|
1671 Maybe someday... |
1
|
1672 |
|
1673 "+=" return ADD_EQ; |
|
1674 "-=" return SUB_EQ; |
|
1675 "*=" return MUL_EQ; |
|
1676 "/=" return DIV_EQ; |
|
1677 "\\=" return LEFTDIV_EQ; |
|
1678 ".+=" return ADD_EQ; |
|
1679 ".-=" return SUB_EQ; |
|
1680 ".*=" return EMUL_EQ; |
|
1681 "./=" return EDIV_EQ; |
|
1682 ".\\=" return ELEFTDIV_EQ; |
|
1683 |
|
1684 */ |