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