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