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