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