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 } |
1489
|
1095 else if (strcmp ("try", s) == 0) |
|
1096 { |
|
1097 promptflag--; |
|
1098 yylval.tok_val = new token (l, c); |
|
1099 token_stack.push (yylval.tok_val); |
|
1100 return TRY; |
|
1101 } |
|
1102 else if (strcmp ("catch", s) == 0) |
|
1103 { |
|
1104 yylval.tok_val = new token (l, c); |
|
1105 token_stack.push (yylval.tok_val); |
|
1106 return CATCH; |
|
1107 } |
|
1108 else if (strcmp ("end_try_catch", s) == 0) |
|
1109 { |
|
1110 end_found = 1; |
|
1111 yylval.tok_val = new token (token::try_catch_end, l, c); |
|
1112 token_stack.push (yylval.tok_val); |
|
1113 } |
922
|
1114 else if (strcmp ("all_va_args", s) == 0) |
|
1115 { |
|
1116 yylval.tok_val = new token (l, c); |
|
1117 token_stack.push (yylval.tok_val); |
|
1118 return ALL_VA_ARGS; |
|
1119 } |
1
|
1120 |
|
1121 if (end_found) |
1001
|
1122 return END; |
1
|
1123 |
|
1124 return 0; |
|
1125 } |
|
1126 |
767
|
1127 // Try to find an identifier. All binding to global or builtin |
|
1128 // variables occurs when expressions are evaluated. |
|
1129 |
1
|
1130 static symbol_record * |
|
1131 lookup_identifier (char *name) |
|
1132 { |
|
1133 return curr_sym_tab->lookup (name, 1, 0); |
|
1134 } |
|
1135 |
1019
|
1136 // Grab the help text from an function file. Always overwrites the |
|
1137 // current contents of help_buf. |
767
|
1138 |
1
|
1139 static void |
|
1140 grab_help_text (void) |
|
1141 { |
1019
|
1142 delete [] help_buf; |
|
1143 help_buf = 0; |
|
1144 |
|
1145 ostrstream buf; |
|
1146 |
|
1147 int in_comment = 1; |
|
1148 int c = 0; |
1
|
1149 |
1019
|
1150 while ((c = yyinput ()) != EOF) |
|
1151 { |
|
1152 if (in_comment) |
1
|
1153 { |
1019
|
1154 buf << (char) c; |
|
1155 if (c == '\n') |
|
1156 in_comment = 0; |
|
1157 } |
|
1158 else |
|
1159 { |
|
1160 switch (c) |
991
|
1161 { |
1019
|
1162 case '%': |
|
1163 case '#': |
|
1164 in_comment = 1; |
|
1165 break; |
777
|
1166 |
1019
|
1167 case ' ': |
|
1168 case '\t': |
|
1169 break; |
777
|
1170 |
1019
|
1171 default: |
|
1172 goto done; |
1
|
1173 } |
|
1174 } |
1019
|
1175 } |
991
|
1176 |
1019
|
1177 done: |
991
|
1178 |
1019
|
1179 if (c) |
|
1180 yyunput (c, yytext); |
991
|
1181 |
1019
|
1182 buf << ends; |
|
1183 |
|
1184 help_buf = buf.str (); |
991
|
1185 |
1019
|
1186 if (! help_buf || ! *help_buf) |
|
1187 { |
|
1188 delete [] help_buf; |
|
1189 help_buf = 0; |
1
|
1190 } |
|
1191 } |
|
1192 |
767
|
1193 // Return 1 if the given character matches any character in the given |
|
1194 // string. |
|
1195 |
1
|
1196 static int |
|
1197 match_any (char c, char *s) |
|
1198 { |
|
1199 char tmp; |
|
1200 while ((tmp = *s++) != '\0') |
|
1201 { |
|
1202 if (c == tmp) |
|
1203 return 1; |
|
1204 } |
|
1205 return 0; |
|
1206 } |
|
1207 |
767
|
1208 // Given information about the spacing surrounding an operator, |
|
1209 // return 1 if it looks like it should be treated as a binary |
|
1210 // operator. For example, |
|
1211 // |
|
1212 // [ 1 + 2 ] or [ 1+ 2] or [ 1+2 ] ==> binary |
|
1213 |
1
|
1214 static int |
|
1215 looks_like_bin_op (int spc_prev, int spc_next) |
|
1216 { |
608
|
1217 return ((spc_prev && spc_next) || ! spc_prev); |
1
|
1218 } |
|
1219 |
1488
|
1220 // Duh. This seems to no longer be needed. |
|
1221 #if 0 |
1
|
1222 static int |
|
1223 next_char_is_space (void) |
|
1224 { |
|
1225 int c = yyinput (); |
|
1226 yyunput (c, yytext); |
|
1227 return (c == ' ' || c == '\t'); |
|
1228 } |
1488
|
1229 #endif |
1
|
1230 |
767
|
1231 // Try to determine if the next token should be treated as a postfix |
|
1232 // unary operator. This is ugly, but it seems to do the right thing. |
|
1233 |
1
|
1234 static int |
|
1235 next_token_is_postfix_unary_op (int spc_prev, char *yytext) |
|
1236 { |
|
1237 int un_op = 0; |
|
1238 |
|
1239 int c0 = yyinput (); |
|
1240 int c1 = yyinput (); |
|
1241 |
|
1242 yyunput (c1, yytext); |
|
1243 yyunput (c0, yytext); |
|
1244 |
|
1245 int transpose = (c0 == '.' && c1 == '\''); |
|
1246 int hermitian = (c0 == '\''); |
|
1247 |
|
1248 un_op = (transpose || (hermitian && ! spc_prev)); |
|
1249 |
|
1250 return un_op; |
|
1251 } |
|
1252 |
767
|
1253 // Try to determine if the next token should be treated as a binary |
|
1254 // operator. This is even uglier, but it also seems to do the right |
1276
|
1255 // thing. Note that it is only necessary to check the spacing for `+' |
|
1256 // and `-', since those are the only tokens that can appear as unary |
|
1257 // ops too. |
767
|
1258 |
1
|
1259 static int |
|
1260 next_token_is_bin_op (int spc_prev, char *yytext) |
|
1261 { |
|
1262 int bin_op = 0; |
|
1263 |
|
1264 int c0 = yyinput (); |
|
1265 |
|
1266 switch (c0) |
|
1267 { |
777
|
1268 case '+': |
|
1269 case '-': |
1276
|
1270 { |
|
1271 int c1 = yyinput (); |
|
1272 yyunput (c1, yytext); |
|
1273 int spc_next = (c1 == ' ' || c1 == '\t'); |
|
1274 bin_op = looks_like_bin_op (spc_prev, spc_next); |
|
1275 } |
|
1276 break; |
|
1277 |
777
|
1278 case '/': |
|
1279 case ':': |
|
1280 case '\\': |
|
1281 case '^': |
1
|
1282 case '&': |
|
1283 case '*': |
|
1284 case '|': |
|
1285 case '<': |
|
1286 case '>': |
777
|
1287 case '~': |
|
1288 case '!': |
|
1289 case '=': |
1
|
1290 case '.': |
1276
|
1291 bin_op = 1; |
1
|
1292 break; |
|
1293 |
|
1294 default: |
1276
|
1295 break; |
1
|
1296 } |
|
1297 |
|
1298 yyunput (c0, yytext); |
|
1299 |
|
1300 return bin_op; |
|
1301 } |
|
1302 |
767
|
1303 // Used to delete trailing white space from tokens. |
|
1304 |
146
|
1305 static char * |
1
|
1306 strip_trailing_whitespace (char *s) |
|
1307 { |
1266
|
1308 static char *retval = 0; |
|
1309 |
|
1310 delete [] retval; |
|
1311 |
|
1312 retval = strsave (s); |
1
|
1313 |
|
1314 char *t = strchr (retval, ' '); |
522
|
1315 if (t) |
1
|
1316 *t = '\0'; |
|
1317 |
|
1318 t = strchr (retval, '\t'); |
522
|
1319 if (t) |
1
|
1320 *t = '\0'; |
|
1321 |
|
1322 return retval; |
|
1323 } |
|
1324 |
1001
|
1325 // Discard whitespace, including comments and continuations. |
1088
|
1326 // |
|
1327 // Return value is logical OR of the following values: |
|
1328 // |
|
1329 // ATE_SPACE_OR_TAB : space or tab in input |
|
1330 // ATE_NEWLINE : bare new line in input |
1001
|
1331 |
|
1332 static int |
975
|
1333 eat_whitespace (void) |
|
1334 { |
1001
|
1335 int retval = 0; |
975
|
1336 int in_comment = 0; |
|
1337 int c; |
|
1338 while ((c = yyinput ()) != EOF) |
|
1339 { |
|
1340 current_input_column++; |
|
1341 |
|
1342 switch (c) |
|
1343 { |
|
1344 case ' ': |
|
1345 case '\t': |
1088
|
1346 retval |= ATE_SPACE_OR_TAB; |
975
|
1347 break; |
|
1348 |
|
1349 case '\n': |
1088
|
1350 retval |= ATE_NEWLINE; |
975
|
1351 in_comment = 0; |
|
1352 current_input_column = 0; |
|
1353 break; |
|
1354 |
|
1355 case '#': |
|
1356 case '%': |
|
1357 in_comment = 1; |
|
1358 break; |
|
1359 |
1001
|
1360 case '.': |
|
1361 if (in_comment) |
|
1362 break; |
|
1363 else |
|
1364 { |
|
1365 if (have_ellipsis_continuation ()) |
|
1366 break; |
|
1367 else |
|
1368 goto done; |
|
1369 } |
|
1370 |
|
1371 case '\\': |
|
1372 if (in_comment) |
|
1373 break; |
|
1374 else |
|
1375 { |
|
1376 if (have_continuation ()) |
|
1377 break; |
|
1378 else |
|
1379 goto done; |
|
1380 } |
|
1381 |
975
|
1382 default: |
|
1383 if (in_comment) |
|
1384 break; |
|
1385 else |
|
1386 goto done; |
|
1387 } |
|
1388 } |
|
1389 |
|
1390 done: |
|
1391 yyunput (c, yytext); |
1082
|
1392 current_input_column--; |
1001
|
1393 return retval; |
975
|
1394 } |
|
1395 |
|
1396 static void |
972
|
1397 handle_number (char *yytext) |
|
1398 { |
|
1399 double value; |
|
1400 int nread = sscanf (yytext, "%lf", &value); |
|
1401 |
985
|
1402 // If yytext doesn't contain a valid number, we are in deep doo doo. |
|
1403 |
972
|
1404 assert (nread == 1); |
|
1405 |
|
1406 quote_is_transpose = 1; |
|
1407 cant_be_identifier = 1; |
|
1408 convert_spaces_to_comma = 1; |
|
1409 |
|
1410 if (plotting && ! in_plot_range) |
|
1411 past_plot_range = 1; |
|
1412 |
|
1413 yylval.tok_val = new token (value, yytext, input_line_number, |
|
1414 current_input_column); |
|
1415 |
|
1416 token_stack.push (yylval.tok_val); |
|
1417 |
|
1418 current_input_column += yyleng; |
|
1419 |
|
1420 do_comma_insert_check (); |
|
1421 } |
|
1422 |
1001
|
1423 // We have seen a backslash and need to find out if it should be |
|
1424 // treated as a continuation character. If so, this eats it, up to |
|
1425 // and including the new line character. |
|
1426 // |
973
|
1427 // Match whitespace only, followed by a comment character or newline. |
|
1428 // Once a comment character is found, discard all input until newline. |
|
1429 // If non-whitespace characters are found before comment |
|
1430 // characters, return 0. Otherwise, return 1. |
|
1431 |
|
1432 static int |
1091
|
1433 have_continuation (int trailing_comments_ok) |
973
|
1434 { |
|
1435 ostrstream buf; |
|
1436 |
|
1437 int in_comment = 0; |
|
1438 char c; |
|
1439 while ((c = yyinput ()) != EOF) |
|
1440 { |
|
1441 buf << (char) c; |
|
1442 |
|
1443 switch (c) |
|
1444 { |
|
1445 case ' ': |
|
1446 case '\t': |
|
1447 break; |
|
1448 |
|
1449 case '%': |
|
1450 case '#': |
1091
|
1451 if (trailing_comments_ok) |
|
1452 in_comment = 1; |
|
1453 else |
|
1454 goto cleanup; |
973
|
1455 break; |
|
1456 |
|
1457 case '\n': |
975
|
1458 current_input_column = 0; |
1001
|
1459 promptflag--; |
973
|
1460 return 1; |
|
1461 |
|
1462 default: |
1091
|
1463 if (! in_comment) |
|
1464 goto cleanup; |
|
1465 break; |
973
|
1466 } |
|
1467 } |
|
1468 |
|
1469 yyunput (c, yytext); |
1091
|
1470 return 0; |
973
|
1471 |
1091
|
1472 cleanup: |
|
1473 buf << ends; |
|
1474 char *s = buf.str (); |
|
1475 if (s) |
|
1476 { |
|
1477 int len = strlen (s); |
|
1478 while (len--) |
|
1479 yyunput (s[len], yytext); |
|
1480 } |
|
1481 delete [] s; |
973
|
1482 return 0; |
|
1483 } |
|
1484 |
1001
|
1485 // We have seen a `.' and need to see if it is the start of a |
|
1486 // continuation. If so, this eats it, up to and including the new |
|
1487 // line character. |
|
1488 |
973
|
1489 static int |
1091
|
1490 have_ellipsis_continuation (int trailing_comments_ok) |
973
|
1491 { |
|
1492 char c1 = yyinput (); |
|
1493 if (c1 == '.') |
|
1494 { |
|
1495 char c2 = yyinput (); |
1091
|
1496 if (c2 == '.' && have_continuation (trailing_comments_ok)) |
973
|
1497 return 1; |
|
1498 else |
|
1499 { |
|
1500 yyunput (c2, yytext); |
|
1501 yyunput (c1, yytext); |
|
1502 } |
|
1503 } |
|
1504 else |
|
1505 yyunput (c1, yytext); |
|
1506 |
|
1507 return 0; |
|
1508 } |
|
1509 |
1001
|
1510 // See if we have a continuation line. If so, eat it and the leading |
|
1511 // whitespace on the next line. |
1088
|
1512 // |
|
1513 // Return value is the same as described for eat_whitespace(). |
1001
|
1514 |
|
1515 static int |
|
1516 eat_continuation (void) |
|
1517 { |
|
1518 int retval = 0; |
|
1519 int c = yyinput (); |
|
1520 if ((c == '.' && have_ellipsis_continuation ()) |
|
1521 || (c == '\\' && have_continuation ())) |
|
1522 retval = eat_whitespace (); |
|
1523 else |
|
1524 yyunput (c, yytext); |
|
1525 |
|
1526 return retval; |
|
1527 } |
|
1528 |
973
|
1529 static int |
975
|
1530 handle_string (char delim, int text_style) |
973
|
1531 { |
|
1532 ostrstream buf; |
|
1533 |
|
1534 int c; |
1031
|
1535 int escape_pending = 0; |
973
|
1536 |
|
1537 while ((c = yyinput ()) != EOF) |
|
1538 { |
|
1539 current_input_column++; |
|
1540 |
|
1541 if (c == '\\') |
|
1542 { |
1053
|
1543 if (escape_pending) |
|
1544 { |
|
1545 buf << (char) c; |
|
1546 escape_pending = 0; |
|
1547 } |
|
1548 else |
|
1549 { |
1091
|
1550 if (have_continuation (0)) |
1053
|
1551 escape_pending = 0; |
|
1552 else |
|
1553 { |
|
1554 buf << (char) c; |
|
1555 escape_pending = 1; |
|
1556 } |
|
1557 } |
1031
|
1558 continue; |
973
|
1559 } |
|
1560 else if (c == '.') |
|
1561 { |
1091
|
1562 if (! have_ellipsis_continuation (0)) |
973
|
1563 buf << (char) c; |
|
1564 } |
|
1565 else if (c == '\n') |
|
1566 { |
1053
|
1567 error ("unterminated string constant"); |
973
|
1568 break; |
|
1569 } |
|
1570 else if (c == delim) |
|
1571 { |
1031
|
1572 if (escape_pending) |
973
|
1573 buf << (char) c; |
|
1574 else |
|
1575 { |
|
1576 c = yyinput (); |
|
1577 if (c == delim) |
|
1578 buf << (char) c; |
|
1579 else |
|
1580 { |
|
1581 yyunput (c, yytext); |
|
1582 buf << ends; |
|
1583 char *tok = buf.str (); |
|
1584 do_string_escapes (tok); |
975
|
1585 |
|
1586 if (text_style && doing_set) |
|
1587 { |
|
1588 if (tok) |
|
1589 { |
|
1590 int len = strlen (tok) + 3; |
|
1591 char *tmp = tok; |
|
1592 tok = new char [len]; |
|
1593 tok[0] = delim; |
|
1594 strcpy (tok+1, tmp); |
|
1595 tok[len-2] = delim; |
|
1596 tok[len-1] = '\0'; |
|
1597 delete [] tmp; |
|
1598 } |
|
1599 } |
|
1600 else |
|
1601 { |
|
1602 quote_is_transpose = 1; |
|
1603 cant_be_identifier = 1; |
|
1604 convert_spaces_to_comma = 1; |
|
1605 } |
|
1606 |
973
|
1607 yylval.tok_val = new token (tok); |
|
1608 delete [] tok; |
|
1609 token_stack.push (yylval.tok_val); |
|
1610 return TEXT; |
|
1611 } |
|
1612 } |
|
1613 } |
|
1614 else |
|
1615 { |
|
1616 buf << (char) c; |
|
1617 } |
|
1618 |
1031
|
1619 escape_pending = 0; |
973
|
1620 } |
|
1621 |
|
1622 return LEXICAL_ERROR; |
|
1623 } |
|
1624 |
971
|
1625 static int |
1001
|
1626 handle_close_brace (int spc_gobbled) |
971
|
1627 { |
985
|
1628 if (! nesting_level.empty ()) |
971
|
1629 { |
985
|
1630 nesting_level.pop (); |
971
|
1631 braceflag--; |
|
1632 } |
|
1633 |
|
1634 if (braceflag == 0) |
1001
|
1635 BEGIN 0; |
|
1636 |
971
|
1637 int c1 = yyinput (); |
|
1638 if (c1 == '=') |
|
1639 { |
|
1640 quote_is_transpose = 0; |
|
1641 cant_be_identifier = 0; |
|
1642 convert_spaces_to_comma = 1; |
|
1643 |
|
1644 int c2 = yyinput (); |
|
1645 unput (c2); |
|
1646 unput (c1); |
|
1647 |
|
1648 if (c2 != '=' && maybe_screwed_again) |
|
1649 return SCREW_TWO; |
|
1650 else |
|
1651 return ']'; |
|
1652 } |
|
1653 else |
|
1654 { |
|
1655 unput (c1); |
|
1656 |
985
|
1657 if (braceflag && user_pref.whitespace_in_literal_matrix != 2) |
971
|
1658 { |
1001
|
1659 int bin_op = next_token_is_bin_op (spc_gobbled, yytext); |
971
|
1660 int postfix_un_op = next_token_is_postfix_unary_op |
1001
|
1661 (spc_gobbled, yytext); |
971
|
1662 |
|
1663 int other_op = match_any (c1, ",;\n]"); |
|
1664 |
|
1665 if (! (postfix_un_op || bin_op || other_op |
985
|
1666 || nesting_level.empty ()) |
|
1667 && nesting_level.top () == BRACE |
971
|
1668 && convert_spaces_to_comma) |
|
1669 { |
|
1670 unput (','); |
|
1671 return ']'; |
|
1672 } |
|
1673 } |
|
1674 } |
|
1675 |
|
1676 quote_is_transpose = 1; |
|
1677 cant_be_identifier = 0; |
|
1678 convert_spaces_to_comma = 1; |
|
1679 return ']'; |
|
1680 } |
|
1681 |
1072
|
1682 static void |
|
1683 maybe_unput_comma (int spc_gobbled) |
|
1684 { |
|
1685 if (user_pref.whitespace_in_literal_matrix != 2 |
|
1686 && ! nesting_level.empty () |
|
1687 && nesting_level.top () == BRACE) |
|
1688 { |
|
1689 int bin_op = next_token_is_bin_op (spc_gobbled, yytext); |
|
1690 |
|
1691 int postfix_un_op = next_token_is_postfix_unary_op (spc_gobbled, |
|
1692 yytext); |
|
1693 |
|
1694 int c1 = yyinput (); |
|
1695 int c2 = yyinput (); |
|
1696 unput (c2); |
|
1697 unput (c1); |
|
1698 int sep_op = match_any (c1, ",;\n]"); |
|
1699 int dot_op = (c1 == '.' |
|
1700 && (isalpha (c2) || isspace (c2) || c2 == '_')); |
|
1701 int index_op = (c1 == '(' |
|
1702 && (user_pref.whitespace_in_literal_matrix == 0 |
|
1703 || ! spc_gobbled)); |
|
1704 |
|
1705 if (! (postfix_un_op || bin_op || sep_op || dot_op || index_op)) |
|
1706 unput (','); |
|
1707 } |
|
1708 } |
|
1709 |
767
|
1710 // Figure out exactly what kind of token to return when we have seen |
|
1711 // an identifier. Handles keywords. |
|
1712 |
146
|
1713 static int |
1001
|
1714 handle_identifier (char *tok, int spc_gobbled) |
146
|
1715 { |
883
|
1716 // It is almost always an error for an identifier to be followed |
|
1717 // directly by another identifier. Special cases are handled below. |
|
1718 |
|
1719 cant_be_identifier = 1; |
|
1720 |
747
|
1721 // If we are expecting a structure element, we just want to return |
1072
|
1722 // TEXT_ID, which is a string that is also a valid identifier. But |
|
1723 // first, we have to decide whether to insert a comma. |
747
|
1724 |
|
1725 if (looking_at_indirect_ref) |
1072
|
1726 { |
|
1727 maybe_unput_comma (spc_gobbled); |
|
1728 TOK_PUSH_AND_RETURN (tok, TEXT_ID); |
|
1729 } |
747
|
1730 |
883
|
1731 // If we have a regular keyword, or a plot STYLE, return it. Keywords |
|
1732 // can be followed by identifiers (TOK_RETURN handles that). |
146
|
1733 |
|
1734 int kw_token = is_keyword (tok); |
|
1735 if (kw_token) |
|
1736 { |
|
1737 if (kw_token == STYLE) |
|
1738 { |
1060
|
1739 current_input_column += yyleng; |
146
|
1740 quote_is_transpose = 0; |
|
1741 convert_spaces_to_comma = 1; |
|
1742 return kw_token; |
|
1743 } |
|
1744 else |
|
1745 TOK_RETURN (kw_token); |
|
1746 } |
|
1747 |
883
|
1748 // See if we have a plot keyword (title, using, with, or clear). |
146
|
1749 |
941
|
1750 if (plotting) |
|
1751 { |
146
|
1752 // Yes, we really do need both of these plot_range variables. One |
|
1753 // is used to mark when we are past all possiblity of a plot range, |
|
1754 // the other is used to mark when we are actually between the square |
|
1755 // brackets that surround the range. |
|
1756 |
941
|
1757 if (! in_plot_range) |
|
1758 past_plot_range = 1; |
|
1759 |
1273
|
1760 // Option keywords can't appear in parentheses or braces. |
|
1761 |
|
1762 int plot_option_kw = 0; |
|
1763 if (nesting_level.empty ()) |
|
1764 plot_option_kw = is_plot_keyword (tok); |
941
|
1765 |
|
1766 if (cant_be_identifier && plot_option_kw) |
|
1767 TOK_RETURN (plot_option_kw); |
|
1768 } |
146
|
1769 |
|
1770 // If we are looking at a text style function, set up to gobble its |
|
1771 // arguments. These are also reserved words, but only because it |
|
1772 // would be very difficult to do anything intelligent with them if |
|
1773 // they were not reserved. |
|
1774 |
|
1775 if (is_text_function_name (tok)) |
|
1776 { |
|
1777 BEGIN TEXT_FCN; |
|
1778 |
1420
|
1779 if (strcmp (tok, "set") == 0) |
146
|
1780 doing_set = 1; |
|
1781 } |
|
1782 |
1001
|
1783 int c = yyinput (); |
|
1784 yyunput (c, yytext); |
|
1785 int next_tok_is_eq = (c == '='); |
|
1786 |
146
|
1787 // Make sure we put the return values of a function in the symbol |
|
1788 // table that is local to the function. |
|
1789 |
|
1790 if (next_tok_is_eq && defining_func && maybe_screwed) |
|
1791 curr_sym_tab = tmp_local_sym_tab; |
|
1792 |
|
1793 // Find the token in the symbol table. |
|
1794 |
|
1795 yylval.tok_val = new token (lookup_identifier (tok), |
|
1796 input_line_number, |
|
1797 current_input_column); |
|
1798 |
|
1799 token_stack.push (yylval.tok_val); |
|
1800 |
|
1801 // After seeing an identifer, it is ok to convert spaces to a comma |
|
1802 // (if needed). |
|
1803 |
|
1804 convert_spaces_to_comma = 1; |
|
1805 |
|
1806 // If we are defining a function and we have not seen the parameter |
|
1807 // list yet and the next token is `=', return a token that represents |
|
1808 // the only return value for the function. For example, |
|
1809 // |
|
1810 // function SCREW = f (args); |
|
1811 // |
|
1812 // The variable maybe_screwed is reset in parse.y. |
|
1813 |
|
1814 if (next_tok_is_eq) |
|
1815 { |
1060
|
1816 current_input_column += yyleng; |
146
|
1817 if (defining_func && maybe_screwed) |
|
1818 return SCREW; |
|
1819 else |
|
1820 return NAME; |
|
1821 } |
|
1822 |
|
1823 // At this point, we are only dealing with identifiers that are not |
|
1824 // followed by `=' (if the next token is `=', there is no need to |
|
1825 // check to see if we should insert a comma (invalid syntax), or allow |
|
1826 // a following `'' to be treated as a transpose (the next token is |
|
1827 // `=', so it can't be `''. |
|
1828 |
|
1829 quote_is_transpose = 1; |
|
1830 do_comma_insert_check (); |
|
1831 |
1072
|
1832 maybe_unput_comma (spc_gobbled); |
146
|
1833 |
1060
|
1834 current_input_column += yyleng; |
146
|
1835 return NAME; |
|
1836 } |
|
1837 |
767
|
1838 // Print a warning if a function file that defines a function has |
|
1839 // anything other than comments and whitespace following the END token |
|
1840 // that matches the FUNCTION statement. |
|
1841 |
1
|
1842 void |
|
1843 check_for_garbage_after_fcn_def (void) |
|
1844 { |
|
1845 // By making a newline be the next character to be read, we will force |
|
1846 // the parser to return after reading the function. Calling yyunput |
|
1847 // with EOF seems not to work... |
|
1848 |
|
1849 int in_comment = 0; |
|
1850 int lineno = input_line_number; |
|
1851 int c; |
|
1852 while ((c = yyinput ()) != EOF) |
|
1853 { |
|
1854 switch (c) |
|
1855 { |
|
1856 case ' ': |
|
1857 case '\t': |
|
1858 case ';': |
|
1859 case ',': |
|
1860 break; |
777
|
1861 |
1
|
1862 case '\n': |
|
1863 if (in_comment) |
|
1864 in_comment = 0; |
|
1865 break; |
777
|
1866 |
1
|
1867 case '%': |
|
1868 case '#': |
|
1869 in_comment = 1; |
|
1870 break; |
777
|
1871 |
1
|
1872 default: |
|
1873 if (in_comment) |
|
1874 break; |
|
1875 else |
|
1876 { |
|
1877 warning ("ignoring trailing garbage after end of function\n\ |
341
|
1878 near line %d of file `%s.m'", lineno, curr_fcn_file_name); |
1
|
1879 |
|
1880 yyunput ('\n', yytext); |
|
1881 return; |
|
1882 } |
|
1883 } |
|
1884 } |
|
1885 yyunput ('\n', yytext); |
|
1886 } |
|
1887 |
767
|
1888 /* |
|
1889 |
|
1890 Maybe someday... |
1
|
1891 |
|
1892 "+=" return ADD_EQ; |
|
1893 "-=" return SUB_EQ; |
|
1894 "*=" return MUL_EQ; |
|
1895 "/=" return DIV_EQ; |
|
1896 "\\=" return LEFTDIV_EQ; |
|
1897 ".+=" return ADD_EQ; |
|
1898 ".-=" return SUB_EQ; |
|
1899 ".*=" return EMUL_EQ; |
|
1900 "./=" return EDIV_EQ; |
|
1901 ".\\=" return ELEFTDIV_EQ; |
|
1902 |
|
1903 */ |