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