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