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