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