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