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