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