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