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