1994
|
1 /* |
1
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with GNU CC; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
|
23 %s TEXT_FCN |
|
24 %s MATRIX |
|
25 |
|
26 %{ |
240
|
27 #ifdef HAVE_CONFIG_H |
1220
|
28 #include <config.h> |
240
|
29 #endif |
|
30 |
1341
|
31 #include <cctype> |
|
32 #include <cstring> |
|
33 |
1823
|
34 #include <string> |
|
35 |
973
|
36 #include <strstream.h> |
522
|
37 |
1
|
38 #include "SLStack.h" |
|
39 |
2926
|
40 #include "cmd-edit.h" |
|
41 |
1497
|
42 // These would be alphabetical, but y.tab.h must be included before |
|
43 // oct-gperf.h and y.tab.h must be included after token.h and the tree |
|
44 // class declarations. We can't include y.tab.h in oct-gperf.h |
|
45 // because it may not be protected to allow it to be included multiple |
|
46 // times. |
|
47 |
2181
|
48 #include "defun.h" |
1355
|
49 #include "error.h" |
1351
|
50 #include "input.h" |
1355
|
51 #include "lex.h" |
2891
|
52 #include "ov.h" |
1355
|
53 #include "parse.h" |
2987
|
54 #include "pt-all.h" |
2891
|
55 #include "symtab.h" |
|
56 #include "token.h" |
|
57 #include "toplev.h" |
1355
|
58 #include "utils.h" |
|
59 #include "variables.h" |
2492
|
60 #include <y.tab.h> |
|
61 #include <oct-gperf.h> |
1
|
62 |
2716
|
63 #if ! (defined (FLEX_SCANNER) \ |
|
64 && defined (YY_FLEX_MAJOR_VERSION) && YY_FLEX_MAJOR_VERSION >= 2 \ |
|
65 && defined (YY_FLEX_MINOR_VERSION) && YY_FLEX_MINOR_VERSION >= 5) |
|
66 #error lex.l requires flex version 2.5.4 or later |
|
67 #endif |
|
68 |
1826
|
69 // Flags that need to be shared between the lexer and parser. |
|
70 lexical_feedback lexer_flags; |
|
71 |
1351
|
72 // Stack to hold tokens so that we can delete them when the parser is |
|
73 // reset and avoid growing forever just because we are stashing some |
|
74 // information. This has to appear before lex.h is included, because |
|
75 // one of the macros defined there uses token_stack. |
2614
|
76 // |
|
77 // XXX FIXME XXX -- this should really be static, but that causes |
|
78 // problems on some systems. |
|
79 SLStack <token*> token_stack; |
1351
|
80 |
1826
|
81 // Did eat_whitespace() eat a space or tab, or a newline, or both? |
1
|
82 |
1826
|
83 typedef int yum_yum; |
1
|
84 |
1826
|
85 const yum_yum ATE_NOTHING = 0; |
|
86 const yum_yum ATE_SPACE_OR_TAB = 1; |
|
87 const yum_yum ATE_NEWLINE = 2; |
1088
|
88 |
3351
|
89 // Is the closest nesting level a square bracket, squiggly brace or a paren? |
1826
|
90 |
3351
|
91 class bracket_brace_paren_nesting_level : public SLStack <int> |
1826
|
92 { |
|
93 public: |
|
94 |
3351
|
95 bracket_brace_paren_nesting_level (void) : SLStack<int> () { } |
1826
|
96 |
3351
|
97 ~bracket_brace_paren_nesting_level (void) { } |
|
98 |
|
99 void bracket (void) { push (BRACKET); } |
|
100 bool is_bracket (void) { return ! empty () && top () == BRACKET; } |
1826
|
101 |
|
102 void brace (void) { push (BRACE); } |
|
103 bool is_brace (void) { return ! empty () && top () == BRACE; } |
|
104 |
|
105 void paren (void) { push (PAREN); } |
|
106 bool is_paren (void) { return ! empty () && top () == PAREN; } |
985
|
107 |
1826
|
108 bool none (void) { return empty (); } |
|
109 |
|
110 void remove (void) { if (! empty ()) SLStack<int>::pop (); } |
|
111 |
|
112 private: |
|
113 |
3351
|
114 enum { BRACKET = 1, BRACE = 2, PAREN = 3 }; |
1826
|
115 |
3351
|
116 bracket_brace_paren_nesting_level (const bracket_brace_paren_nesting_level&); |
1826
|
117 |
3351
|
118 bracket_brace_paren_nesting_level& |
|
119 operator = (const bracket_brace_paren_nesting_level&); |
1826
|
120 }; |
|
121 |
3351
|
122 static bracket_brace_paren_nesting_level nesting_level; |
1
|
123 |
2167
|
124 // Should whitespace in a literal matrix list be automatically |
|
125 // converted to commas and semicolons? |
|
126 // |
|
127 // user specifies value of var |
|
128 // -------------- ------------ |
|
129 // "ignore" 2 |
|
130 // "traditional" 1 |
|
131 // anything else 0 |
|
132 // |
|
133 // Octave will never insert a comma in a literal matrix list if the |
|
134 // user specifies "ignore". For example, the statement [1 2] will |
|
135 // result in an error instead of being treated the same as [1, 2], and |
|
136 // the statement |
|
137 // |
|
138 // [ 1, 2, |
|
139 // 3, 4 ] |
|
140 // |
|
141 // will result in the vector [1 2 3 4] instead of a matrix. |
|
142 // |
|
143 // Traditional behavior makes Octave convert spaces to a comma between |
|
144 // identifiers and `('. For example, the statement |
|
145 // |
|
146 // [eye (2)] |
|
147 // |
|
148 // will be parsed as |
|
149 // |
|
150 // [eye, (2)] |
|
151 // |
|
152 // and will result in an error since the `eye' function will be |
|
153 // called with no arguments. To get around this, you would have to |
|
154 // omit the space between `eye' and the `('. |
|
155 // |
|
156 // The default value is 0, which results in behavior that is the same |
|
157 // as traditional, except that Octave does not convert spaces to a |
|
158 // comma between identifiers and `('. For example, the statement |
|
159 // |
|
160 // [eye (2)] |
|
161 // |
|
162 // will result in a call to `eye' with the argument `2'. |
|
163 |
|
164 static int Vwhitespace_in_literal_matrix; |
|
165 |
3388
|
166 static bool Vwarn_separator_insert = false; |
|
167 |
3400
|
168 static bool Vwarn_single_quote_string = false; |
|
169 |
146
|
170 // Forward declarations for functions defined at the bottom of this |
|
171 // file. |
|
172 |
1
|
173 static void fixup_column_count (char *s); |
146
|
174 static void do_comma_insert_check (void); |
1823
|
175 static int is_plot_keyword (const string& s); |
|
176 static int is_keyword (const string& s); |
|
177 static string plot_style_token (const string& s); |
|
178 static symbol_record *lookup_identifier (const string& s); |
1
|
179 static void grab_help_text (void); |
2857
|
180 static bool match_any (char c, const char *s); |
3263
|
181 static bool next_token_is_sep_op (void); |
3246
|
182 static bool next_token_is_bin_op (bool spc_prev); |
|
183 static bool next_token_is_postfix_unary_op (bool spc_prev); |
1823
|
184 static string strip_trailing_whitespace (char *s); |
3246
|
185 static void handle_number (void); |
975
|
186 static int handle_string (char delim, int text_style = 0); |
3351
|
187 static int handle_close_bracket (int spc_gobbled); |
1823
|
188 static int handle_identifier (const string& tok, int spc_gobbled); |
3096
|
189 static bool have_continuation (bool trailing_comments_ok = true); |
|
190 static bool have_ellipsis_continuation (bool trailing_comments_ok = true); |
1826
|
191 static yum_yum eat_whitespace (void); |
|
192 static yum_yum eat_continuation (void); |
3388
|
193 static void maybe_warn_separator_insert (char sep); |
3400
|
194 static void gripe_single_quote_string (void); |
1
|
195 |
|
196 %} |
|
197 |
|
198 D [0-9] |
|
199 S [ \t] |
2042
|
200 NL ((\n)|(\r\n)) |
|
201 SNL ({S}|{NL}) |
1
|
202 EL (\.\.\.) |
967
|
203 BS (\\) |
|
204 CONT ({EL}|{BS}) |
1
|
205 Im [iIjJ] |
967
|
206 CCHAR [#%] |
|
207 COMMENT ({CCHAR}.*{NL}) |
|
208 SNLCMT ({SNL}|{COMMENT}) |
|
209 NOTEQ ((~=)|(!=)|(<>)) |
|
210 POW ((\*\*)|(\^)) |
|
211 EPOW (\.{POW}) |
|
212 NOT ((\~)|(\!)) |
1
|
213 IDENT ([_a-zA-Z][_a-zA-Z0-9]*) |
|
214 EXPON ([DdEe][+-]?{D}+) |
3220
|
215 NUMBER (({D}+\.?{D}*{EXPON}?)|(\.{D}+{EXPON}?)|(0[xX][0-9a-fA-F]+)) |
1
|
216 %% |
|
217 |
968
|
218 %{ |
|
219 // Help and other text-style functions are a pain in the ass. This |
|
220 // stuff needs to be simplified. May require some changes in the |
|
221 // parser too. |
|
222 %} |
|
223 |
967
|
224 <TEXT_FCN>{NL} { |
|
225 BEGIN 0; |
|
226 current_input_column = 1; |
2857
|
227 lexer_flags.quote_is_transpose = false; |
|
228 lexer_flags.cant_be_identifier = false; |
|
229 lexer_flags.convert_spaces_to_comma = true; |
967
|
230 return '\n'; |
|
231 } |
1
|
232 |
967
|
233 <TEXT_FCN>[\;\,] { |
1826
|
234 if (lexer_flags.doing_set && strcmp (yytext, ",") == 0) |
967
|
235 { |
1060
|
236 TOK_PUSH_AND_RETURN (yytext, TEXT); |
967
|
237 } |
|
238 else |
|
239 { |
|
240 BEGIN 0; |
|
241 if (strcmp (yytext, ",") == 0) |
|
242 TOK_RETURN (','); |
|
243 else |
|
244 TOK_RETURN (';'); |
|
245 } |
|
246 } |
1
|
247 |
975
|
248 <TEXT_FCN>[\"\'] { |
|
249 current_input_column++; |
3246
|
250 return handle_string (yytext[0], true); |
975
|
251 } |
|
252 |
3079
|
253 <TEXT_FCN>[^ \t\n\;\,\"\'][^ \t\n\;\,]*{S}* { |
1823
|
254 string tok = strip_trailing_whitespace (yytext); |
1060
|
255 TOK_PUSH_AND_RETURN (tok, TEXT); |
967
|
256 } |
1
|
257 |
968
|
258 %{ |
1
|
259 // For this and the next two rules, we're looking at ']', and we |
971
|
260 // need to know if the next token is `=' or `=='. |
1
|
261 // |
|
262 // It would have been so much easier if the delimiters were simply |
|
263 // different for the expression on the left hand side of the equals |
|
264 // operator. |
971
|
265 // |
|
266 // It's also a pain in the ass to decide whether to insert a comma |
|
267 // after seeing a ']' character... |
968
|
268 %} |
|
269 |
2119
|
270 <MATRIX>{SNLCMT}*\]{S}* { |
1001
|
271 fixup_column_count (yytext); |
|
272 int c = yytext[yyleng-1]; |
|
273 int cont_is_spc = eat_continuation (); |
|
274 int spc_gobbled = (cont_is_spc || c == ' ' || c == '\t'); |
3351
|
275 return handle_close_bracket (spc_gobbled); |
967
|
276 } |
1
|
277 |
968
|
278 %{ |
1088
|
279 // Commas are element separators in matrix constants. If we don't |
|
280 // check for continuations here we can end up inserting too many |
|
281 // commas. |
968
|
282 %} |
|
283 |
967
|
284 <MATRIX>{S}*\,{S}* { |
1088
|
285 current_input_column += yyleng; |
3388
|
286 |
1088
|
287 int tmp = eat_continuation (); |
3388
|
288 |
2857
|
289 lexer_flags.quote_is_transpose = false; |
|
290 lexer_flags.cant_be_identifier = false; |
|
291 lexer_flags.convert_spaces_to_comma = true; |
3388
|
292 |
|
293 if ((tmp & ATE_NEWLINE) == ATE_NEWLINE) |
|
294 { |
|
295 maybe_warn_separator_insert (';'); |
|
296 |
|
297 if (Vwhitespace_in_literal_matrix != 2) |
|
298 unput (';'); |
|
299 } |
|
300 |
1088
|
301 return (','); |
967
|
302 } |
1
|
303 |
968
|
304 %{ |
|
305 // In some cases, spaces in matrix constants can turn into commas. |
|
306 // If commas are required, spaces are not important in matrix |
1088
|
307 // constants so we just eat them. If we don't check for continuations |
|
308 // here we can end up inserting too many commas. |
968
|
309 %} |
430
|
310 |
968
|
311 <MATRIX>{S}+ { |
1088
|
312 current_input_column += yyleng; |
3388
|
313 |
|
314 int tmp = eat_continuation (); |
|
315 int bin_op = next_token_is_bin_op (true); |
|
316 int postfix_un_op = next_token_is_postfix_unary_op (true); |
|
317 |
|
318 if (! (postfix_un_op || bin_op) |
|
319 && nesting_level.is_bracket () |
|
320 && lexer_flags.convert_spaces_to_comma) |
967
|
321 { |
3388
|
322 if ((tmp & ATE_NEWLINE) == ATE_NEWLINE) |
|
323 { |
|
324 maybe_warn_separator_insert (';'); |
967
|
325 |
3388
|
326 if (Vwhitespace_in_literal_matrix != 2) |
|
327 unput (';'); |
|
328 } |
|
329 |
|
330 if (Vwhitespace_in_literal_matrix != 2) |
1088
|
331 { |
2857
|
332 lexer_flags.quote_is_transpose = false; |
|
333 lexer_flags.cant_be_identifier = false; |
|
334 lexer_flags.convert_spaces_to_comma = true; |
3388
|
335 |
|
336 maybe_warn_separator_insert (','); |
|
337 |
1088
|
338 return (','); |
|
339 } |
967
|
340 } |
|
341 } |
430
|
342 |
968
|
343 %{ |
1088
|
344 // Semicolons are handled as row seprators in matrix constants. If we |
|
345 // don't eat whitespace here we can end up inserting too many |
|
346 // semicolons. |
968
|
347 %} |
|
348 |
985
|
349 <MATRIX>{SNLCMT}*;{SNLCMT}* { |
967
|
350 fixup_column_count (yytext); |
1001
|
351 eat_whitespace (); |
2857
|
352 lexer_flags.quote_is_transpose = false; |
|
353 lexer_flags.cant_be_identifier = false; |
|
354 lexer_flags.convert_spaces_to_comma = true; |
967
|
355 return ';'; |
|
356 } |
|
357 |
968
|
358 %{ |
1088
|
359 // In some cases, new lines can also become row separators. If we |
|
360 // don't eat whitespace here we can end up inserting too many |
|
361 // semicolons. |
985
|
362 %} |
|
363 |
3180
|
364 <MATRIX>{S}*{COMMENT}{SNLCMT}* | |
|
365 <MATRIX>{S}*{NL}{SNLCMT}* { |
1082
|
366 fixup_column_count (yytext); |
1088
|
367 eat_whitespace (); |
3388
|
368 |
2167
|
369 if (Vwhitespace_in_literal_matrix != 2) |
985
|
370 { |
2857
|
371 lexer_flags.quote_is_transpose = false; |
|
372 lexer_flags.cant_be_identifier = false; |
|
373 lexer_flags.convert_spaces_to_comma = true; |
985
|
374 |
1826
|
375 if (nesting_level.none ()) |
985
|
376 return LEXICAL_ERROR; |
3388
|
377 } |
985
|
378 |
3388
|
379 if (nesting_level.is_bracket ()) |
|
380 { |
|
381 maybe_warn_separator_insert (';'); |
|
382 |
|
383 if (Vwhitespace_in_literal_matrix != 2) |
985
|
384 return ';'; |
|
385 } |
|
386 } |
|
387 |
|
388 %{ |
3351
|
389 // Open and close bracket are handled differently if we are in the range |
968
|
390 // part of a plot command. |
975
|
391 // |
968
|
392 %} |
1
|
393 |
967
|
394 \[{S}* { |
3351
|
395 nesting_level.bracket (); |
975
|
396 |
1082
|
397 current_input_column += yyleng; |
2857
|
398 lexer_flags.quote_is_transpose = false; |
|
399 lexer_flags.cant_be_identifier = false; |
|
400 lexer_flags.convert_spaces_to_comma = true; |
975
|
401 |
|
402 promptflag--; |
|
403 eat_whitespace (); |
|
404 |
1826
|
405 if (lexer_flags.plotting && ! lexer_flags.past_plot_range) |
967
|
406 { |
2857
|
407 lexer_flags.in_plot_range = true; |
1082
|
408 return OPEN_BRACE; |
967
|
409 } |
|
410 else |
|
411 { |
3351
|
412 lexer_flags.bracketflag++; |
975
|
413 BEGIN MATRIX; |
1082
|
414 return '['; |
967
|
415 } |
|
416 } |
1
|
417 |
968
|
418 \] { |
1826
|
419 nesting_level.remove (); |
968
|
420 |
1826
|
421 if (lexer_flags.plotting && ! lexer_flags.past_plot_range) |
968
|
422 { |
2857
|
423 lexer_flags.in_plot_range = false; |
968
|
424 TOK_RETURN (CLOSE_BRACE); |
|
425 } |
|
426 else |
|
427 TOK_RETURN (']'); |
|
428 } |
|
429 |
|
430 %{ |
|
431 // Imaginary numbers. |
|
432 %} |
|
433 |
|
434 {NUMBER}{Im} { |
3246
|
435 handle_number (); |
968
|
436 return IMAG_NUM; |
|
437 } |
|
438 |
|
439 %{ |
|
440 // Real numbers. Don't grab the `.' part of a dot operator as part of |
|
441 // the constant. |
|
442 %} |
|
443 |
|
444 {D}+/\.[\*/\\^'] | |
|
445 {NUMBER} { |
3246
|
446 handle_number (); |
968
|
447 return NUM; |
|
448 } |
|
449 |
|
450 %{ |
|
451 // Eat whitespace. Whitespace inside matrix constants is handled by |
|
452 // the <MATRIX> start state code above. |
|
453 %} |
|
454 |
967
|
455 {S}* { |
|
456 current_input_column += yyleng; |
|
457 } |
|
458 |
968
|
459 %{ |
|
460 // Continuation lines. Allow comments after continuations. |
|
461 %} |
|
462 |
967
|
463 {CONT}{S}*{NL} | |
|
464 {CONT}{S}*{COMMENT} { |
|
465 promptflag--; |
|
466 current_input_column = 1; |
|
467 } |
1
|
468 |
968
|
469 %{ |
|
470 // An ellipsis not at the end of a line is not a continuation, but |
|
471 // does have another meaning. |
|
472 %} |
|
473 |
967
|
474 {EL} { |
|
475 return ELLIPSIS; |
|
476 } |
1
|
477 |
968
|
478 %{ |
|
479 // End of file. |
|
480 %} |
|
481 |
967
|
482 <<EOF>> { |
|
483 TOK_RETURN (END_OF_INPUT); |
|
484 } |
1
|
485 |
968
|
486 %{ |
970
|
487 // Identifiers. Truncate the token at the first space or tab but |
|
488 // don't write directly on yytext. |
968
|
489 %} |
|
490 |
967
|
491 {IDENT}{S}* { |
1823
|
492 string tok = strip_trailing_whitespace (yytext); |
1001
|
493 int c = yytext[yyleng-1]; |
|
494 int cont_is_spc = eat_continuation (); |
|
495 int spc_gobbled = (cont_is_spc || c == ' ' || c == '\t'); |
|
496 return handle_identifier (tok, spc_gobbled); |
967
|
497 } |
1
|
498 |
968
|
499 %{ |
|
500 // A new line character. New line characters inside matrix constants |
985
|
501 // are handled by the <MATRIX> start state code above. If closest |
|
502 // nesting is inside parentheses, don't return a row separator. |
968
|
503 %} |
|
504 |
967
|
505 {NL} { |
|
506 current_input_column = 1; |
2857
|
507 lexer_flags.quote_is_transpose = false; |
|
508 lexer_flags.cant_be_identifier = false; |
|
509 lexer_flags.convert_spaces_to_comma = true; |
985
|
510 |
1826
|
511 if (nesting_level.none ()) |
985
|
512 return '\n'; |
|
513 |
3351
|
514 if (nesting_level.is_bracket ()) |
985
|
515 return LEXICAL_ERROR; |
967
|
516 } |
1
|
517 |
968
|
518 %{ |
|
519 // Single quote can either be the beginning of a string or a transpose |
|
520 // operator. |
|
521 %} |
|
522 |
967
|
523 "'" { |
|
524 current_input_column++; |
2857
|
525 lexer_flags.convert_spaces_to_comma = true; |
1
|
526 |
1826
|
527 if (lexer_flags.quote_is_transpose) |
967
|
528 { |
|
529 do_comma_insert_check (); |
|
530 return QUOTE; |
|
531 } |
|
532 else |
973
|
533 return handle_string ('\''); |
967
|
534 } |
1
|
535 |
968
|
536 %{ |
971
|
537 // Double quotes always begin strings. |
|
538 %} |
|
539 |
973
|
540 \" { |
|
541 current_input_column++; |
|
542 return handle_string ('"'); |
|
543 } |
971
|
544 |
|
545 %{ |
|
546 // The colon operator is handled differently if we are in the range |
|
547 // part of a plot command. |
968
|
548 %} |
|
549 |
967
|
550 ":" { |
1826
|
551 if (lexer_flags.plotting |
|
552 && (lexer_flags.in_plot_range || lexer_flags.in_plot_using)) |
2857
|
553 BIN_OP_RETURN (COLON, true); |
967
|
554 else |
2857
|
555 BIN_OP_RETURN (':', false); |
967
|
556 } |
1
|
557 |
968
|
558 %{ |
985
|
559 // Gobble comments. If closest nesting is inside parentheses, don't |
|
560 // return a new line. |
|
561 %} |
968
|
562 |
967
|
563 {CCHAR} { |
1826
|
564 if (help_buf.empty () |
|
565 && lexer_flags.beginning_of_function |
|
566 && nesting_level.none ()) |
967
|
567 { |
|
568 grab_help_text (); |
2857
|
569 lexer_flags.beginning_of_function = false; |
967
|
570 } |
|
571 else |
|
572 { |
|
573 int c; |
|
574 while ((c = yyinput ()) != EOF && c != '\n') |
|
575 ; // Eat comment. |
|
576 } |
440
|
577 |
967
|
578 current_input_column = 1; |
2857
|
579 lexer_flags.quote_is_transpose = false; |
|
580 lexer_flags.cant_be_identifier = false; |
|
581 lexer_flags.convert_spaces_to_comma = true; |
985
|
582 |
1826
|
583 if (nesting_level.none ()) |
985
|
584 return '\n'; |
3351
|
585 else if (nesting_level.is_bracket ()) |
1566
|
586 return ';'; |
967
|
587 } |
440
|
588 |
968
|
589 %{ |
|
590 // Other operators. |
|
591 %} |
|
592 |
2857
|
593 ".+" { BIN_OP_RETURN (EPLUS, false); } |
|
594 ".-" { BIN_OP_RETURN (EMINUS, false); } |
|
595 ".*" { BIN_OP_RETURN (EMUL, false); } |
|
596 "./" { BIN_OP_RETURN (EDIV, false); } |
|
597 ".\\" { BIN_OP_RETURN (ELEFTDIV, false); } |
|
598 {EPOW} { BIN_OP_RETURN (EPOW, false); } |
|
599 ".'" { do_comma_insert_check (); BIN_OP_RETURN (TRANSPOSE, true); } |
|
600 "++" { do_comma_insert_check (); BIN_OP_RETURN (PLUS_PLUS, true); } |
|
601 "--" { do_comma_insert_check (); BIN_OP_RETURN (MINUS_MINUS, true); } |
|
602 "<=" { BIN_OP_RETURN (EXPR_LE, false); } |
|
603 "==" { BIN_OP_RETURN (EXPR_EQ, false); } |
|
604 {NOTEQ} { BIN_OP_RETURN (EXPR_NE, false); } |
|
605 ">=" { BIN_OP_RETURN (EXPR_GE, false); } |
2877
|
606 "&" { BIN_OP_RETURN (EXPR_AND, false); } |
2857
|
607 "|" { BIN_OP_RETURN (EXPR_OR, false); } |
|
608 "<" { BIN_OP_RETURN (EXPR_LT, false); } |
|
609 ">" { BIN_OP_RETURN (EXPR_GT, false); } |
|
610 "*" { BIN_OP_RETURN ('*', false); } |
|
611 "/" { BIN_OP_RETURN ('/', false); } |
|
612 "\\" { BIN_OP_RETURN (LEFTDIV, false); } |
|
613 ";" { BIN_OP_RETURN (';', true); } |
|
614 "," { BIN_OP_RETURN (',', true); } |
|
615 {POW} { BIN_OP_RETURN (POW, false); } |
|
616 "=" { BIN_OP_RETURN ('=', true); } |
2877
|
617 "&&" { BIN_OP_RETURN (EXPR_AND_AND, false); } |
2857
|
618 "||" { BIN_OP_RETURN (EXPR_OR_OR, false); } |
2900
|
619 "<<" { BIN_OP_RETURN (LSHIFT, false); } |
|
620 ">>" { BIN_OP_RETURN (RSHIFT, false); } |
967
|
621 |
|
622 {NOT} { |
1826
|
623 if (lexer_flags.plotting && ! lexer_flags.in_plot_range) |
2857
|
624 lexer_flags.past_plot_range = true; |
|
625 BIN_OP_RETURN (EXPR_NOT, false); |
967
|
626 } |
1
|
627 |
1276
|
628 "+" { |
1826
|
629 if (lexer_flags.plotting && ! lexer_flags.in_plot_range) |
2857
|
630 lexer_flags.past_plot_range = true; |
|
631 BIN_OP_RETURN ('+', false); |
967
|
632 } |
|
633 |
1276
|
634 "-" { |
1826
|
635 if (lexer_flags.plotting && ! lexer_flags.in_plot_range) |
2857
|
636 lexer_flags.past_plot_range = true; |
|
637 BIN_OP_RETURN ('-', false); |
967
|
638 } |
|
639 |
|
640 "(" { |
1826
|
641 if (lexer_flags.plotting && ! lexer_flags.in_plot_range) |
2857
|
642 lexer_flags.past_plot_range = true; |
1826
|
643 nesting_level.paren (); |
985
|
644 promptflag--; |
967
|
645 TOK_RETURN ('('); |
|
646 } |
|
647 |
|
648 ")" { |
1826
|
649 nesting_level.remove (); |
1001
|
650 |
967
|
651 current_input_column++; |
2857
|
652 lexer_flags.cant_be_identifier = true; |
|
653 lexer_flags.quote_is_transpose = true; |
3351
|
654 lexer_flags.convert_spaces_to_comma = nesting_level.is_bracket (); |
1001
|
655 do_comma_insert_check (); |
967
|
656 return ')'; |
|
657 } |
|
658 |
2066
|
659 "." { |
|
660 TOK_RETURN ('.'); |
|
661 } |
|
662 |
2877
|
663 "+=" { BIN_OP_RETURN (ADD_EQ, false); } |
|
664 "-=" { BIN_OP_RETURN (SUB_EQ, false); } |
|
665 "*=" { BIN_OP_RETURN (MUL_EQ, false); } |
|
666 "/=" { BIN_OP_RETURN (DIV_EQ, false); } |
3204
|
667 "\\=" { BIN_OP_RETURN (LEFTDIV_EQ, false); } |
2877
|
668 ".+=" { BIN_OP_RETURN (ADD_EQ, false); } |
|
669 ".-=" { BIN_OP_RETURN (SUB_EQ, false); } |
|
670 ".*=" { BIN_OP_RETURN (EMUL_EQ, false); } |
|
671 "./=" { BIN_OP_RETURN (EDIV_EQ, false); } |
3204
|
672 ".\\=" { BIN_OP_RETURN (ELEFTDIV_EQ, false); } |
2877
|
673 "&=" { BIN_OP_RETURN (AND_EQ, false); } |
|
674 "|=" { BIN_OP_RETURN (OR_EQ, false); } |
2900
|
675 "<<=" { BIN_OP_RETURN (LSHIFT_EQ, false); } |
|
676 ">>=" { BIN_OP_RETURN (RSHIFT_EQ, false); } |
2877
|
677 |
3351
|
678 "{" { |
|
679 nesting_level.brace (); |
|
680 promptflag--; |
|
681 TOK_RETURN ('{'); |
|
682 } |
|
683 |
|
684 "}" { |
|
685 nesting_level.remove (); |
|
686 |
|
687 current_input_column++; |
|
688 lexer_flags.cant_be_identifier = true; |
|
689 lexer_flags.quote_is_transpose = true; |
|
690 lexer_flags.convert_spaces_to_comma = nesting_level.is_bracket (); |
|
691 do_comma_insert_check (); // Is this really necessary? |
|
692 |
|
693 return '}'; |
|
694 } |
|
695 |
968
|
696 %{ |
2066
|
697 // Unrecognized input is a lexical error. |
968
|
698 %} |
1
|
699 |
2042
|
700 . { |
2066
|
701 current_input_column++; |
|
702 |
|
703 error ("invalid character `%s' near line %d, column %d", |
|
704 undo_string_escape (yytext[0]), input_line_number, |
|
705 current_input_column); |
|
706 |
|
707 return LEXICAL_ERROR; |
|
708 } |
1
|
709 |
|
710 %% |
|
711 |
767
|
712 // GAG. |
|
713 // |
|
714 // If we're reading a matrix and the next character is '[', make sure |
|
715 // that we insert a comma ahead of it. |
|
716 |
146
|
717 void |
1
|
718 do_comma_insert_check (void) |
|
719 { |
1001
|
720 int spc_gobbled = eat_continuation (); |
2970
|
721 |
1
|
722 int c = yyinput (); |
2970
|
723 |
3246
|
724 unput (c); |
2970
|
725 |
1001
|
726 if (spc_gobbled) |
3246
|
727 unput (' '); |
2970
|
728 |
3351
|
729 lexer_flags.do_comma_insert = (lexer_flags.bracketflag && c == '['); |
1
|
730 } |
|
731 |
767
|
732 // Fix things up for errors or interrupts. The parser is never called |
|
733 // recursively, so it is always safe to reinitialize its state before |
|
734 // doing any parsing. |
|
735 |
1
|
736 void |
|
737 reset_parser (void) |
|
738 { |
1826
|
739 // Start off on the right foot. |
1
|
740 BEGIN 0; |
166
|
741 error_state = 0; |
287
|
742 |
1826
|
743 // We do want a prompt by default. |
1
|
744 promptflag = 1; |
287
|
745 |
3351
|
746 // Error may have occurred inside some brackets, braces, or parentheses. |
985
|
747 nesting_level.clear (); |
287
|
748 |
1826
|
749 // Clear out the stack of token info used to track line and column |
|
750 // numbers. |
143
|
751 while (! token_stack.empty ()) |
|
752 delete token_stack.pop (); |
287
|
753 |
1826
|
754 // Can be reset by defining a function. |
985
|
755 if (! (reading_script_file || reading_fcn_file)) |
|
756 { |
|
757 current_input_column = 1; |
2926
|
758 input_line_number = command_editor::current_command_number () - 1; |
985
|
759 } |
287
|
760 |
1826
|
761 // Only ask for input from stdin if we are expecting interactive |
|
762 // input. |
3174
|
763 if ((interactive || forced_interactive) |
|
764 && ! (reading_fcn_file || get_input_from_eval_string |
|
765 || input_from_startup_file)) |
287
|
766 yyrestart (stdin); |
991
|
767 |
1826
|
768 // Clear the buffer for help text. |
|
769 help_buf.resize (0); |
1755
|
770 |
1826
|
771 // Reset other flags. |
|
772 lexer_flags.init (); |
1
|
773 } |
|
774 |
767
|
775 // If we read some newlines, we need figure out what column we're |
|
776 // really looking at. |
|
777 |
1
|
778 static void |
|
779 fixup_column_count (char *s) |
|
780 { |
|
781 char c; |
|
782 while ((c = *s++) != '\0') |
|
783 { |
|
784 if (c == '\n') |
143
|
785 current_input_column = 1; |
1
|
786 else |
|
787 current_input_column++; |
|
788 } |
|
789 } |
|
790 |
767
|
791 // Include these so that we don't have to link to libfl.a. |
246
|
792 |
1
|
793 #ifdef yywrap |
|
794 #undef yywrap |
|
795 #endif |
3332
|
796 int |
1
|
797 yywrap (void) |
|
798 { |
287
|
799 return 1; |
1
|
800 } |
|
801 |
767
|
802 // Tell us all what the current buffer is. |
|
803 |
1
|
804 YY_BUFFER_STATE |
|
805 current_buffer (void) |
|
806 { |
|
807 return YY_CURRENT_BUFFER; |
|
808 } |
|
809 |
767
|
810 // Create a new buffer. |
|
811 |
1
|
812 YY_BUFFER_STATE |
|
813 create_buffer (FILE *f) |
|
814 { |
|
815 return yy_create_buffer (f, YY_BUF_SIZE); |
|
816 } |
|
817 |
767
|
818 // Start reading a new buffer. |
|
819 |
1
|
820 void |
|
821 switch_to_buffer (YY_BUFFER_STATE buf) |
|
822 { |
|
823 yy_switch_to_buffer (buf); |
|
824 } |
|
825 |
767
|
826 // Delete a buffer. |
|
827 |
1
|
828 void |
|
829 delete_buffer (YY_BUFFER_STATE buf) |
|
830 { |
|
831 yy_delete_buffer (buf); |
|
832 } |
|
833 |
767
|
834 // Restore a buffer (for unwind-prot). |
|
835 |
1
|
836 void |
|
837 restore_input_buffer (void *buf) |
|
838 { |
2861
|
839 switch_to_buffer (static_cast<YY_BUFFER_STATE> (buf)); |
1
|
840 } |
|
841 |
767
|
842 // Delete a buffer (for unwind-prot). |
|
843 |
1
|
844 void |
|
845 delete_input_buffer (void *buf) |
|
846 { |
2861
|
847 delete_buffer (static_cast<YY_BUFFER_STATE> (buf)); |
1
|
848 } |
|
849 |
767
|
850 // Check to see if a character string matches any of the possible line |
|
851 // styles for plots. |
|
852 |
1823
|
853 static string |
|
854 plot_style_token (const string& s) |
1
|
855 { |
1823
|
856 string retval; |
|
857 |
2804
|
858 static const char *plot_styles[] = |
146
|
859 { |
925
|
860 "boxes", |
924
|
861 "boxerrorbars", |
2542
|
862 "boxxyerrorbars", |
|
863 "candlesticks", |
146
|
864 "dots", |
|
865 "errorbars", |
2542
|
866 "financebars", |
|
867 "fsteps", |
|
868 "histeps", |
146
|
869 "impulses", |
|
870 "lines", |
|
871 "linespoints", |
|
872 "points", |
926
|
873 "steps", |
2542
|
874 "vector", |
|
875 "xerrorbars", |
|
876 "xyerrorbars", |
|
877 "yerrorbars", |
522
|
878 0, |
146
|
879 }; |
|
880 |
2804
|
881 const char * const *tmp = plot_styles; |
522
|
882 while (*tmp) |
1
|
883 { |
1823
|
884 if (almost_match (*tmp, s.c_str ())) |
|
885 { |
|
886 retval = *tmp; |
|
887 break; |
|
888 } |
1
|
889 |
|
890 tmp++; |
|
891 } |
|
892 |
1823
|
893 return retval; |
1
|
894 } |
|
895 |
3165
|
896 // Check to see if a character string matches any of the possible axes |
|
897 // tags for plots. |
|
898 |
|
899 static string |
|
900 plot_axes_token (const string& s) |
|
901 { |
|
902 string retval; |
|
903 |
|
904 static char *plot_axes[] = |
|
905 { |
|
906 "x1y1", |
|
907 "x1y2", |
|
908 "x2y1", |
|
909 "x2y2", |
|
910 0, |
|
911 }; |
|
912 |
|
913 char **tmp = plot_axes; |
|
914 while (*tmp) |
|
915 { |
|
916 if (almost_match (*tmp, s.c_str ())) |
|
917 { |
|
918 retval = *tmp; |
|
919 break; |
|
920 } |
|
921 |
|
922 tmp++; |
|
923 } |
|
924 |
|
925 return retval; |
|
926 } |
|
927 |
767
|
928 // Check to see if a character string matches any one of the plot |
941
|
929 // option keywords. Don't match abbreviations for clear, since that's |
|
930 // not a gnuplot keyword (users will probably only expect to be able |
|
931 // to abbreviate actual gnuplot keywords). |
767
|
932 |
1
|
933 static int |
1823
|
934 is_plot_keyword (const string& s) |
1
|
935 { |
1823
|
936 const char *t = s.c_str (); |
|
937 if (almost_match ("title", t)) |
146
|
938 { |
|
939 return TITLE; |
|
940 } |
1823
|
941 else if (almost_match ("using", t)) |
146
|
942 { |
2857
|
943 lexer_flags.in_plot_using = true; |
146
|
944 return USING; |
|
945 } |
1823
|
946 else if (almost_match ("with", t)) |
146
|
947 { |
2857
|
948 lexer_flags.in_plot_style = true; |
146
|
949 return WITH; |
|
950 } |
3165
|
951 else if (almost_match ("axes", t) || almost_match ("axis", t)) |
|
952 { |
|
953 lexer_flags.in_plot_axes = true; |
|
954 return AXES; |
|
955 } |
1823
|
956 else if (strcmp ("clear", t) == 0) |
883
|
957 { |
|
958 return CLEAR; |
|
959 } |
1
|
960 else |
146
|
961 { |
|
962 return 0; |
|
963 } |
1
|
964 } |
|
965 |
2877
|
966 // Handle keywords. |
767
|
967 |
1
|
968 static int |
1823
|
969 is_keyword (const string& s) |
1
|
970 { |
3165
|
971 if (lexer_flags.plotting) |
1
|
972 { |
3165
|
973 if (lexer_flags.in_plot_style) |
1
|
974 { |
3165
|
975 string sty = plot_style_token (s); |
|
976 |
|
977 if (! sty.empty ()) |
|
978 { |
|
979 lexer_flags.in_plot_style = false; |
|
980 yylval.tok_val = new token (sty); |
|
981 token_stack.push (yylval.tok_val); |
|
982 return STYLE; |
|
983 } |
1
|
984 } |
3165
|
985 else if (lexer_flags.in_plot_axes) |
|
986 { |
|
987 string axes = plot_axes_token (s); |
|
988 |
|
989 if (! axes.empty ()) |
|
990 { |
|
991 lexer_flags.in_plot_axes = false; |
|
992 yylval.tok_val = new token (axes); |
|
993 token_stack.push (yylval.tok_val); |
|
994 return AXES_TAG; |
|
995 } |
|
996 } |
1
|
997 } |
|
998 |
143
|
999 int l = input_line_number; |
|
1000 int c = current_input_column; |
|
1001 |
1823
|
1002 int len = s.length (); |
922
|
1003 |
1823
|
1004 const octave_kw *kw = octave_kw_lookup (s.c_str (), len); |
191
|
1005 |
1497
|
1006 if (kw) |
143
|
1007 { |
1497
|
1008 yylval.tok_val = 0; |
|
1009 |
|
1010 switch (kw->kw_id) |
|
1011 { |
|
1012 case all_va_args_kw: |
|
1013 case break_kw: |
2764
|
1014 case case_kw: |
1497
|
1015 case catch_kw: |
|
1016 case continue_kw: |
|
1017 case else_kw: |
|
1018 case elseif_kw: |
|
1019 case global_kw: |
2764
|
1020 case otherwise_kw: |
1497
|
1021 case return_kw: |
2846
|
1022 case static_kw: |
1497
|
1023 case unwind_protect_cleanup_kw: |
|
1024 break; |
|
1025 |
|
1026 case end_kw: |
|
1027 yylval.tok_val = new token (token::simple_end, l, c); |
|
1028 break; |
|
1029 |
|
1030 case end_try_catch_kw: |
|
1031 yylval.tok_val = new token (token::try_catch_end, l, c); |
|
1032 break; |
|
1033 |
|
1034 case end_unwind_protect_kw: |
|
1035 yylval.tok_val = new token (token::unwind_protect_end, l, c); |
|
1036 break; |
|
1037 |
|
1038 case endfor_kw: |
|
1039 yylval.tok_val = new token (token::for_end, l, c); |
|
1040 break; |
|
1041 |
|
1042 case endfunction_kw: |
|
1043 yylval.tok_val = new token (token::function_end, l, c); |
|
1044 break; |
|
1045 |
|
1046 case endif_kw: |
|
1047 yylval.tok_val = new token (token::if_end, l, c); |
|
1048 break; |
|
1049 |
2764
|
1050 case endswitch_kw: |
|
1051 yylval.tok_val = new token (token::switch_end, l, c); |
|
1052 break; |
|
1053 |
1497
|
1054 case endwhile_kw: |
|
1055 yylval.tok_val = new token (token::while_end, l, c); |
|
1056 break; |
|
1057 |
|
1058 case for_kw: |
|
1059 case while_kw: |
|
1060 promptflag--; |
1826
|
1061 lexer_flags.looping++; |
1497
|
1062 break; |
|
1063 |
|
1064 case if_kw: |
|
1065 case try_kw: |
2764
|
1066 case switch_kw: |
1497
|
1067 case unwind_protect_kw: |
|
1068 promptflag--; |
|
1069 break; |
|
1070 |
|
1071 case gplot_kw: |
2857
|
1072 lexer_flags.plotting = true; |
1497
|
1073 yylval.tok_val = new token (token::two_dee, l, c); |
|
1074 break; |
|
1075 |
|
1076 case gsplot_kw: |
2857
|
1077 lexer_flags.plotting = true; |
1497
|
1078 yylval.tok_val = new token (token::three_dee, l, c); |
|
1079 break; |
|
1080 |
|
1081 case replot_kw: |
2857
|
1082 lexer_flags.plotting = true; |
1497
|
1083 yylval.tok_val = new token (token::replot, l, c); |
|
1084 break; |
|
1085 |
|
1086 case function_kw: |
1826
|
1087 if (lexer_flags.defining_func) |
1497
|
1088 { |
|
1089 error ("function keyword invalid within a function body"); |
|
1090 |
|
1091 if ((reading_fcn_file || reading_script_file) |
1755
|
1092 && ! curr_fcn_file_name.empty ()) |
1497
|
1093 error ("defining new function near line %d of file `%s.m'", |
1755
|
1094 input_line_number, curr_fcn_file_name.c_str ()); |
1497
|
1095 else |
1755
|
1096 error ("defining new function near line %d", |
|
1097 input_line_number); |
1497
|
1098 |
|
1099 return LEXICAL_ERROR; |
|
1100 } |
|
1101 else |
|
1102 { |
2877
|
1103 // Prepare for local symbols. |
|
1104 |
1497
|
1105 tmp_local_sym_tab = new symbol_table (); |
2877
|
1106 |
|
1107 promptflag--; |
|
1108 |
2857
|
1109 lexer_flags.defining_func = true; |
2877
|
1110 lexer_flags.parsed_function_name = false; |
2857
|
1111 lexer_flags.beginning_of_function = true; |
2877
|
1112 |
1497
|
1113 if (! (reading_fcn_file || reading_script_file)) |
|
1114 input_line_number = 1; |
|
1115 } |
|
1116 break; |
|
1117 |
3174
|
1118 case magic_file_kw: |
|
1119 { |
|
1120 if ((reading_fcn_file || reading_script_file) |
|
1121 && ! curr_fcn_file_full_name.empty ()) |
|
1122 yylval.tok_val = new token (curr_fcn_file_full_name, l, c); |
|
1123 else |
|
1124 yylval.tok_val = new token ("stdin", l, c); |
|
1125 } |
|
1126 break; |
|
1127 |
|
1128 case magic_line_kw: |
|
1129 yylval.tok_val = new token (static_cast<double> (l), "", l, c); |
|
1130 break; |
|
1131 |
1497
|
1132 default: |
|
1133 panic_impossible (); |
|
1134 } |
|
1135 |
|
1136 if (! yylval.tok_val) |
|
1137 yylval.tok_val = new token (l, c); |
|
1138 |
476
|
1139 token_stack.push (yylval.tok_val); |
1497
|
1140 |
|
1141 return kw->tok; |
143
|
1142 } |
1
|
1143 |
|
1144 return 0; |
|
1145 } |
|
1146 |
767
|
1147 // Try to find an identifier. All binding to global or builtin |
|
1148 // variables occurs when expressions are evaluated. |
|
1149 |
1
|
1150 static symbol_record * |
1823
|
1151 lookup_identifier (const string& name) |
1
|
1152 { |
2856
|
1153 return curr_sym_tab->lookup (name, true); |
1
|
1154 } |
|
1155 |
2702
|
1156 static bool |
|
1157 is_variable (const string& name) |
|
1158 { |
2856
|
1159 symbol_record *sr = curr_sym_tab->lookup (name); |
2702
|
1160 |
|
1161 return sr && sr->is_variable (); |
|
1162 } |
|
1163 |
|
1164 static void |
|
1165 force_local_variable (const string& name) |
|
1166 { |
2856
|
1167 symbol_record *sr = curr_sym_tab->lookup (name, true); |
2702
|
1168 |
|
1169 if (sr) |
|
1170 sr->define (octave_value ()); |
|
1171 } |
|
1172 |
1019
|
1173 // Grab the help text from an function file. Always overwrites the |
|
1174 // current contents of help_buf. |
767
|
1175 |
2300
|
1176 // XXX FIXME XXX -- gobble_leading_white_space() in variables.cc |
|
1177 // duplicates some of this code! |
|
1178 |
1
|
1179 static void |
|
1180 grab_help_text (void) |
|
1181 { |
1755
|
1182 help_buf.resize (0); |
1019
|
1183 |
2300
|
1184 bool begin_comment = true; |
|
1185 bool in_comment = true; |
1019
|
1186 int c = 0; |
1
|
1187 |
1019
|
1188 while ((c = yyinput ()) != EOF) |
|
1189 { |
2300
|
1190 if (begin_comment) |
|
1191 { |
|
1192 if (c == '%' || c == '#') |
|
1193 continue; |
|
1194 else |
|
1195 begin_comment = false; |
|
1196 } |
|
1197 |
1019
|
1198 if (in_comment) |
1
|
1199 { |
1755
|
1200 help_buf += (char) c; |
|
1201 |
1019
|
1202 if (c == '\n') |
2300
|
1203 in_comment = false; |
1019
|
1204 } |
|
1205 else |
|
1206 { |
|
1207 switch (c) |
991
|
1208 { |
1019
|
1209 case '%': |
|
1210 case '#': |
2300
|
1211 in_comment = true; |
|
1212 begin_comment = true; |
1019
|
1213 break; |
777
|
1214 |
1019
|
1215 case ' ': |
|
1216 case '\t': |
|
1217 break; |
777
|
1218 |
1019
|
1219 default: |
|
1220 goto done; |
1
|
1221 } |
|
1222 } |
1019
|
1223 } |
991
|
1224 |
1019
|
1225 done: |
991
|
1226 |
1019
|
1227 if (c) |
3246
|
1228 unput (c); |
1
|
1229 } |
|
1230 |
767
|
1231 // Return 1 if the given character matches any character in the given |
|
1232 // string. |
|
1233 |
2857
|
1234 static bool |
2804
|
1235 match_any (char c, const char *s) |
1
|
1236 { |
|
1237 char tmp; |
|
1238 while ((tmp = *s++) != '\0') |
|
1239 { |
|
1240 if (c == tmp) |
2857
|
1241 return true; |
1
|
1242 } |
2857
|
1243 return false; |
1
|
1244 } |
|
1245 |
767
|
1246 // Given information about the spacing surrounding an operator, |
|
1247 // return 1 if it looks like it should be treated as a binary |
|
1248 // operator. For example, |
|
1249 // |
|
1250 // [ 1 + 2 ] or [ 1+ 2] or [ 1+2 ] ==> binary |
|
1251 |
2857
|
1252 static bool |
3246
|
1253 looks_like_bin_op (bool spc_prev, int next_char) |
1
|
1254 { |
3246
|
1255 bool spc_next = (next_char == ' ' || next_char == '\t'); |
|
1256 |
608
|
1257 return ((spc_prev && spc_next) || ! spc_prev); |
1
|
1258 } |
|
1259 |
3263
|
1260 // Recognize separators. If the separator is a CRLF pair, it is |
|
1261 // replaced by a single LF. |
|
1262 |
|
1263 static bool |
|
1264 next_token_is_sep_op (void) |
|
1265 { |
|
1266 bool retval = false; |
|
1267 |
|
1268 int c1 = yyinput (); |
|
1269 |
|
1270 if (c1 == '\r') |
|
1271 { |
|
1272 int c2 = yyinput (); |
|
1273 |
|
1274 if (c2 == '\n') |
|
1275 { |
|
1276 c1 = '\n'; |
|
1277 |
|
1278 retval = true; |
|
1279 } |
|
1280 else |
|
1281 unput (c2); |
|
1282 } |
|
1283 else |
|
1284 retval = match_any (c1, ",;\n]"); |
|
1285 |
|
1286 unput (c1); |
|
1287 |
|
1288 return retval; |
|
1289 } |
|
1290 |
767
|
1291 // Try to determine if the next token should be treated as a postfix |
|
1292 // unary operator. This is ugly, but it seems to do the right thing. |
|
1293 |
2857
|
1294 static bool |
3246
|
1295 next_token_is_postfix_unary_op (bool spc_prev) |
1
|
1296 { |
2857
|
1297 bool un_op = false; |
1
|
1298 |
|
1299 int c0 = yyinput (); |
|
1300 |
3246
|
1301 if (c0 == '\'' && ! spc_prev) |
|
1302 { |
|
1303 un_op = true; |
|
1304 } |
|
1305 else if (c0 == '.') |
|
1306 { |
|
1307 int c1 = yyinput (); |
|
1308 un_op = (c1 == '\''); |
|
1309 unput (c1); |
|
1310 } |
1
|
1311 |
3246
|
1312 unput (c0); |
1
|
1313 |
|
1314 return un_op; |
|
1315 } |
|
1316 |
767
|
1317 // Try to determine if the next token should be treated as a binary |
3246
|
1318 // operator. |
1521
|
1319 // |
3246
|
1320 // This kluge exists because whitespace is not always ignored inside |
|
1321 // the square brackets that are used to create matrix objects. |
1521
|
1322 // |
3246
|
1323 // Line continuations directly after the operator will cause this |
|
1324 // function to return FALSE. |
767
|
1325 |
2857
|
1326 static bool |
3246
|
1327 next_token_is_bin_op (bool spc_prev) |
1
|
1328 { |
2857
|
1329 bool bin_op = false; |
1
|
1330 |
|
1331 int c0 = yyinput (); |
|
1332 |
|
1333 switch (c0) |
|
1334 { |
3246
|
1335 case ':': |
777
|
1336 case '+': |
|
1337 case '-': |
3246
|
1338 case '/': |
|
1339 case '\\': |
|
1340 case '^': |
1276
|
1341 { |
|
1342 int c1 = yyinput (); |
3246
|
1343 bin_op = looks_like_bin_op (spc_prev, c1); |
|
1344 unput (c1); |
1276
|
1345 } |
|
1346 break; |
|
1347 |
3246
|
1348 // .+ .- ./ .\ .^ .* .** |
1554
|
1349 case '.': |
|
1350 { |
|
1351 int c1 = yyinput (); |
3246
|
1352 |
|
1353 if (match_any (c1, "+-/\\^")) |
|
1354 { |
|
1355 int c2 = yyinput (); |
|
1356 bin_op = looks_like_bin_op (spc_prev, c2); |
|
1357 unput (c2); |
|
1358 } |
|
1359 else if (c1 == '*') |
|
1360 { |
|
1361 int c2 = yyinput (); |
|
1362 |
|
1363 if (c2 == '*') |
|
1364 { |
|
1365 int c3 = yyinput (); |
|
1366 bin_op = looks_like_bin_op (spc_prev, c3); |
|
1367 unput (c3); |
|
1368 } |
|
1369 else |
|
1370 bin_op = looks_like_bin_op (spc_prev, c2); |
|
1371 |
|
1372 unput (c2); |
|
1373 } |
|
1374 else if (! isdigit (c1) && c1 != ' ' && c1 != '\t') |
|
1375 { |
|
1376 bin_op = true; |
|
1377 } |
|
1378 |
|
1379 unput (c1); |
1554
|
1380 } |
|
1381 break; |
|
1382 |
3246
|
1383 // = == & && | || * ** |
|
1384 case '=': |
1
|
1385 case '&': |
3246
|
1386 case '|': |
1
|
1387 case '*': |
3246
|
1388 { |
|
1389 int c1 = yyinput (); |
|
1390 |
|
1391 if (c1 == c0) |
|
1392 { |
|
1393 int c2 = yyinput (); |
|
1394 bin_op = looks_like_bin_op (spc_prev, c2); |
|
1395 unput (c2); |
|
1396 } |
|
1397 else |
|
1398 bin_op = looks_like_bin_op (spc_prev, c1); |
|
1399 |
|
1400 unput (c1); |
|
1401 } |
|
1402 break; |
|
1403 |
|
1404 // <= >= <> ~= != < > |
1
|
1405 case '<': |
|
1406 case '>': |
777
|
1407 case '~': |
|
1408 case '!': |
3246
|
1409 { |
|
1410 int c1 = yyinput (); |
|
1411 |
|
1412 if ((c1 == '=') || (c1 == '<' && c1 == '>')) |
|
1413 { |
|
1414 int c2 = yyinput (); |
|
1415 bin_op = looks_like_bin_op (spc_prev, c2); |
|
1416 unput (c2); |
|
1417 } |
|
1418 else if (c1 != '~' && c1 != '!') |
|
1419 bin_op = looks_like_bin_op (spc_prev, c1); |
|
1420 |
|
1421 unput (c1); |
|
1422 } |
1
|
1423 break; |
|
1424 |
|
1425 default: |
1276
|
1426 break; |
1
|
1427 } |
|
1428 |
3246
|
1429 unput (c0); |
1
|
1430 |
|
1431 return bin_op; |
|
1432 } |
|
1433 |
767
|
1434 // Used to delete trailing white space from tokens. |
|
1435 |
1823
|
1436 static string |
1
|
1437 strip_trailing_whitespace (char *s) |
|
1438 { |
1823
|
1439 string retval = s; |
1
|
1440 |
1823
|
1441 size_t pos = retval.find_first_of (" \t"); |
1
|
1442 |
1823
|
1443 if (pos != NPOS) |
|
1444 retval.resize (pos); |
1
|
1445 |
|
1446 return retval; |
|
1447 } |
|
1448 |
1001
|
1449 // Discard whitespace, including comments and continuations. |
1088
|
1450 // |
|
1451 // Return value is logical OR of the following values: |
|
1452 // |
1826
|
1453 // ATE_NOTHING : no spaces to eat |
1088
|
1454 // ATE_SPACE_OR_TAB : space or tab in input |
|
1455 // ATE_NEWLINE : bare new line in input |
1001
|
1456 |
1826
|
1457 static yum_yum |
975
|
1458 eat_whitespace (void) |
|
1459 { |
1826
|
1460 yum_yum retval = ATE_NOTHING; |
2857
|
1461 bool in_comment = false; |
975
|
1462 int c; |
|
1463 while ((c = yyinput ()) != EOF) |
|
1464 { |
|
1465 current_input_column++; |
|
1466 |
|
1467 switch (c) |
|
1468 { |
|
1469 case ' ': |
|
1470 case '\t': |
1088
|
1471 retval |= ATE_SPACE_OR_TAB; |
975
|
1472 break; |
|
1473 |
|
1474 case '\n': |
1088
|
1475 retval |= ATE_NEWLINE; |
2857
|
1476 in_comment = false; |
975
|
1477 current_input_column = 0; |
|
1478 break; |
|
1479 |
|
1480 case '#': |
|
1481 case '%': |
2857
|
1482 in_comment = true; |
975
|
1483 break; |
|
1484 |
1001
|
1485 case '.': |
|
1486 if (in_comment) |
|
1487 break; |
|
1488 else |
|
1489 { |
|
1490 if (have_ellipsis_continuation ()) |
|
1491 break; |
|
1492 else |
|
1493 goto done; |
|
1494 } |
|
1495 |
|
1496 case '\\': |
|
1497 if (in_comment) |
|
1498 break; |
|
1499 else |
|
1500 { |
3105
|
1501 if (have_continuation ()) |
1001
|
1502 break; |
|
1503 else |
|
1504 goto done; |
|
1505 } |
|
1506 |
975
|
1507 default: |
|
1508 if (in_comment) |
|
1509 break; |
|
1510 else |
|
1511 goto done; |
|
1512 } |
|
1513 } |
|
1514 |
|
1515 done: |
3246
|
1516 unput (c); |
1082
|
1517 current_input_column--; |
1001
|
1518 return retval; |
975
|
1519 } |
|
1520 |
3220
|
1521 static inline bool |
|
1522 looks_like_hex (const char *s, int len) |
|
1523 { |
|
1524 return (len > 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X')); |
|
1525 } |
|
1526 |
975
|
1527 static void |
3246
|
1528 handle_number (void) |
972
|
1529 { |
2621
|
1530 char *tmp = strsave (yytext); |
|
1531 |
|
1532 char *idx = strpbrk (tmp, "Dd"); |
|
1533 |
|
1534 if (idx) |
|
1535 *idx = 'e'; |
|
1536 |
3220
|
1537 double value = 0.0; |
|
1538 int nread = 0; |
|
1539 |
|
1540 if (looks_like_hex (tmp, strlen (tmp))) |
|
1541 { |
|
1542 unsigned long ival; |
|
1543 nread = sscanf (tmp, "%lx", &ival); |
|
1544 value = static_cast<double> (ival); |
|
1545 } |
|
1546 else |
|
1547 nread = sscanf (tmp, "%lf", &value); |
2621
|
1548 |
|
1549 delete [] tmp; |
972
|
1550 |
1826
|
1551 // If yytext doesn't contain a valid number, we are in deep doo doo. |
985
|
1552 |
972
|
1553 assert (nread == 1); |
|
1554 |
1826
|
1555 lexer_flags.quote_is_transpose = 1; |
|
1556 lexer_flags.cant_be_identifier = 1; |
|
1557 lexer_flags.convert_spaces_to_comma = 1; |
972
|
1558 |
1826
|
1559 if (lexer_flags.plotting && ! lexer_flags.in_plot_range) |
|
1560 lexer_flags.past_plot_range = 1; |
972
|
1561 |
|
1562 yylval.tok_val = new token (value, yytext, input_line_number, |
|
1563 current_input_column); |
|
1564 |
|
1565 token_stack.push (yylval.tok_val); |
|
1566 |
|
1567 current_input_column += yyleng; |
|
1568 |
|
1569 do_comma_insert_check (); |
|
1570 } |
|
1571 |
1001
|
1572 // We have seen a backslash and need to find out if it should be |
|
1573 // treated as a continuation character. If so, this eats it, up to |
|
1574 // and including the new line character. |
|
1575 // |
973
|
1576 // Match whitespace only, followed by a comment character or newline. |
|
1577 // Once a comment character is found, discard all input until newline. |
|
1578 // If non-whitespace characters are found before comment |
|
1579 // characters, return 0. Otherwise, return 1. |
|
1580 |
2857
|
1581 static bool |
3096
|
1582 have_continuation (bool trailing_comments_ok) |
973
|
1583 { |
|
1584 ostrstream buf; |
|
1585 |
2857
|
1586 bool in_comment = false; |
3332
|
1587 int c; |
973
|
1588 while ((c = yyinput ()) != EOF) |
|
1589 { |
|
1590 buf << (char) c; |
|
1591 |
|
1592 switch (c) |
|
1593 { |
|
1594 case ' ': |
|
1595 case '\t': |
|
1596 break; |
|
1597 |
|
1598 case '%': |
|
1599 case '#': |
1091
|
1600 if (trailing_comments_ok) |
2857
|
1601 in_comment = true; |
1091
|
1602 else |
|
1603 goto cleanup; |
973
|
1604 break; |
|
1605 |
|
1606 case '\n': |
975
|
1607 current_input_column = 0; |
1001
|
1608 promptflag--; |
2857
|
1609 return true; |
973
|
1610 |
3263
|
1611 case '\r': |
|
1612 c = yyinput (); |
|
1613 if (c == EOF) |
|
1614 break; |
|
1615 else if (c == '\n') |
|
1616 { |
|
1617 current_input_column = 0; |
|
1618 promptflag--; |
|
1619 return true; |
|
1620 } |
|
1621 |
973
|
1622 default: |
1091
|
1623 if (! in_comment) |
|
1624 goto cleanup; |
|
1625 break; |
973
|
1626 } |
|
1627 } |
|
1628 |
3246
|
1629 unput (c); |
2857
|
1630 return false; |
973
|
1631 |
3096
|
1632 cleanup: |
1091
|
1633 buf << ends; |
|
1634 char *s = buf.str (); |
|
1635 if (s) |
|
1636 { |
|
1637 int len = strlen (s); |
|
1638 while (len--) |
3246
|
1639 unput (s[len]); |
1091
|
1640 } |
|
1641 delete [] s; |
3096
|
1642 |
2857
|
1643 return false; |
973
|
1644 } |
|
1645 |
1001
|
1646 // We have seen a `.' and need to see if it is the start of a |
|
1647 // continuation. If so, this eats it, up to and including the new |
|
1648 // line character. |
|
1649 |
2857
|
1650 static bool |
3096
|
1651 have_ellipsis_continuation (bool trailing_comments_ok) |
973
|
1652 { |
|
1653 char c1 = yyinput (); |
|
1654 if (c1 == '.') |
|
1655 { |
|
1656 char c2 = yyinput (); |
1091
|
1657 if (c2 == '.' && have_continuation (trailing_comments_ok)) |
2857
|
1658 return true; |
973
|
1659 else |
|
1660 { |
3246
|
1661 unput (c2); |
|
1662 unput (c1); |
973
|
1663 } |
|
1664 } |
|
1665 else |
3246
|
1666 unput (c1); |
973
|
1667 |
2857
|
1668 return false; |
973
|
1669 } |
|
1670 |
1001
|
1671 // See if we have a continuation line. If so, eat it and the leading |
|
1672 // whitespace on the next line. |
1088
|
1673 // |
|
1674 // Return value is the same as described for eat_whitespace(). |
1001
|
1675 |
1826
|
1676 static yum_yum |
1001
|
1677 eat_continuation (void) |
|
1678 { |
1826
|
1679 int retval = ATE_NOTHING; |
1001
|
1680 int c = yyinput (); |
|
1681 if ((c == '.' && have_ellipsis_continuation ()) |
3105
|
1682 || (c == '\\' && have_continuation ())) |
1001
|
1683 retval = eat_whitespace (); |
|
1684 else |
3246
|
1685 unput (c); |
1001
|
1686 |
|
1687 return retval; |
|
1688 } |
|
1689 |
973
|
1690 static int |
975
|
1691 handle_string (char delim, int text_style) |
973
|
1692 { |
|
1693 ostrstream buf; |
|
1694 |
|
1695 int c; |
1031
|
1696 int escape_pending = 0; |
973
|
1697 |
|
1698 while ((c = yyinput ()) != EOF) |
|
1699 { |
|
1700 current_input_column++; |
|
1701 |
3105
|
1702 if (c == '\\') |
973
|
1703 { |
1053
|
1704 if (escape_pending) |
|
1705 { |
|
1706 buf << (char) c; |
|
1707 escape_pending = 0; |
|
1708 } |
|
1709 else |
|
1710 { |
3096
|
1711 if (have_continuation (false)) |
1053
|
1712 escape_pending = 0; |
|
1713 else |
|
1714 { |
|
1715 buf << (char) c; |
|
1716 escape_pending = 1; |
|
1717 } |
|
1718 } |
1031
|
1719 continue; |
973
|
1720 } |
|
1721 else if (c == '.') |
|
1722 { |
3096
|
1723 if (! have_ellipsis_continuation (false)) |
973
|
1724 buf << (char) c; |
|
1725 } |
|
1726 else if (c == '\n') |
|
1727 { |
1053
|
1728 error ("unterminated string constant"); |
973
|
1729 break; |
|
1730 } |
|
1731 else if (c == delim) |
|
1732 { |
1031
|
1733 if (escape_pending) |
973
|
1734 buf << (char) c; |
|
1735 else |
|
1736 { |
|
1737 c = yyinput (); |
|
1738 if (c == delim) |
|
1739 buf << (char) c; |
|
1740 else |
|
1741 { |
3246
|
1742 unput (c); |
973
|
1743 buf << ends; |
3105
|
1744 char *t = buf.str (); |
|
1745 string s = do_string_escapes (t); |
|
1746 delete [] t; |
975
|
1747 |
1826
|
1748 if (text_style && lexer_flags.doing_set) |
975
|
1749 { |
3238
|
1750 s = string (1, delim) + s + string (1, delim); |
975
|
1751 } |
|
1752 else |
|
1753 { |
2857
|
1754 lexer_flags.quote_is_transpose = true; |
|
1755 lexer_flags.cant_be_identifier = true; |
|
1756 lexer_flags.convert_spaces_to_comma = true; |
975
|
1757 } |
|
1758 |
3105
|
1759 yylval.tok_val = new token (s); |
973
|
1760 token_stack.push (yylval.tok_val); |
3400
|
1761 |
|
1762 if (delim == '\'') |
|
1763 gripe_single_quote_string (); |
|
1764 |
973
|
1765 return TEXT; |
|
1766 } |
|
1767 } |
|
1768 } |
|
1769 else |
|
1770 { |
|
1771 buf << (char) c; |
|
1772 } |
|
1773 |
1031
|
1774 escape_pending = 0; |
973
|
1775 } |
|
1776 |
|
1777 return LEXICAL_ERROR; |
|
1778 } |
|
1779 |
3208
|
1780 static bool |
|
1781 next_token_is_assign_op (void) |
|
1782 { |
|
1783 bool retval = false; |
|
1784 |
|
1785 int c0 = yyinput (); |
|
1786 |
|
1787 switch (c0) |
|
1788 { |
|
1789 case '=': |
|
1790 { |
|
1791 int c1 = yyinput (); |
|
1792 unput (c1); |
|
1793 if (c1 != '=') |
|
1794 retval = true; |
|
1795 } |
|
1796 break; |
|
1797 |
|
1798 case '+': |
|
1799 case '-': |
|
1800 case '*': |
|
1801 case '/': |
|
1802 case '\\': |
|
1803 case '&': |
|
1804 case '|': |
|
1805 { |
|
1806 int c1 = yyinput (); |
|
1807 unput (c1); |
|
1808 if (c1 == '=') |
|
1809 retval = true; |
|
1810 } |
|
1811 break; |
|
1812 |
|
1813 case '.': |
|
1814 { |
|
1815 int c1 = yyinput (); |
|
1816 if (match_any (c1, "+-*/\\")) |
|
1817 { |
|
1818 int c2 = yyinput (); |
|
1819 unput (c2); |
|
1820 if (c2 == '=') |
|
1821 retval = true; |
|
1822 } |
|
1823 unput (c1); |
|
1824 } |
|
1825 break; |
|
1826 |
|
1827 case '>': |
|
1828 { |
|
1829 int c1 = yyinput (); |
|
1830 if (c1 == '>') |
|
1831 { |
|
1832 int c2 = yyinput (); |
|
1833 unput (c2); |
|
1834 if (c2 == '=') |
|
1835 retval = true; |
|
1836 } |
|
1837 unput (c1); |
|
1838 } |
|
1839 break; |
|
1840 |
|
1841 case '<': |
|
1842 { |
|
1843 int c1 = yyinput (); |
|
1844 if (c1 == '<') |
|
1845 { |
|
1846 int c2 = yyinput (); |
|
1847 unput (c2); |
|
1848 if (c2 == '=') |
|
1849 retval = true; |
|
1850 } |
|
1851 unput (c1); |
|
1852 } |
|
1853 break; |
|
1854 |
|
1855 default: |
|
1856 break; |
|
1857 } |
|
1858 |
|
1859 unput (c0); |
|
1860 |
|
1861 return retval; |
|
1862 } |
|
1863 |
971
|
1864 static int |
3351
|
1865 handle_close_bracket (int spc_gobbled) |
971
|
1866 { |
3208
|
1867 int retval = ']'; |
|
1868 |
1826
|
1869 if (! nesting_level.none ()) |
971
|
1870 { |
1826
|
1871 nesting_level.remove (); |
3351
|
1872 lexer_flags.bracketflag--; |
971
|
1873 } |
|
1874 |
3351
|
1875 if (lexer_flags.bracketflag == 0) |
1001
|
1876 BEGIN 0; |
|
1877 |
3208
|
1878 if (next_token_is_assign_op () && ! lexer_flags.looking_at_return_list) |
971
|
1879 { |
3208
|
1880 retval = CLOSE_BRACE; |
971
|
1881 } |
|
1882 else |
|
1883 { |
3208
|
1884 int c1 = yyinput (); |
971
|
1885 unput (c1); |
|
1886 |
3388
|
1887 if (lexer_flags.bracketflag) |
971
|
1888 { |
3246
|
1889 int bin_op = next_token_is_bin_op (spc_gobbled); |
3263
|
1890 |
3246
|
1891 int postfix_un_op = next_token_is_postfix_unary_op (spc_gobbled); |
971
|
1892 |
3263
|
1893 int sep_op = next_token_is_sep_op (); |
971
|
1894 |
3263
|
1895 if (! (postfix_un_op || bin_op || sep_op) |
3351
|
1896 && nesting_level.is_bracket () |
1826
|
1897 && lexer_flags.convert_spaces_to_comma) |
971
|
1898 { |
3388
|
1899 maybe_warn_separator_insert (','); |
|
1900 |
|
1901 if (Vwhitespace_in_literal_matrix != 2) |
|
1902 { |
|
1903 unput (','); |
|
1904 return ']'; |
|
1905 } |
971
|
1906 } |
|
1907 } |
|
1908 } |
|
1909 |
2857
|
1910 lexer_flags.quote_is_transpose = true; |
|
1911 lexer_flags.cant_be_identifier = false; |
|
1912 lexer_flags.convert_spaces_to_comma = true; |
3208
|
1913 |
|
1914 return retval; |
971
|
1915 } |
|
1916 |
1072
|
1917 static void |
|
1918 maybe_unput_comma (int spc_gobbled) |
|
1919 { |
3388
|
1920 if (nesting_level.is_bracket ()) |
1072
|
1921 { |
3246
|
1922 int bin_op = next_token_is_bin_op (spc_gobbled); |
1072
|
1923 |
3246
|
1924 int postfix_un_op = next_token_is_postfix_unary_op (spc_gobbled); |
1072
|
1925 |
|
1926 int c1 = yyinput (); |
|
1927 int c2 = yyinput (); |
2970
|
1928 |
1072
|
1929 unput (c2); |
|
1930 unput (c1); |
2970
|
1931 |
3263
|
1932 int sep_op = next_token_is_sep_op (); |
2970
|
1933 |
1072
|
1934 int dot_op = (c1 == '.' |
|
1935 && (isalpha (c2) || isspace (c2) || c2 == '_')); |
2970
|
1936 |
3388
|
1937 if (postfix_un_op || bin_op || sep_op || dot_op) |
|
1938 return; |
|
1939 |
|
1940 int index_op = (c1 == '('); |
|
1941 |
|
1942 if (index_op) |
|
1943 { |
|
1944 // If there is no space before the '(', we don't insert a comma. |
|
1945 if (! spc_gobbled) |
|
1946 return; |
|
1947 |
|
1948 maybe_warn_separator_insert (','); |
1072
|
1949 |
3388
|
1950 // If there is a space, we only insert a comma if we are |
|
1951 // trying to be Matlab-like. |
|
1952 if (Vwhitespace_in_literal_matrix == 1) |
|
1953 unput (','); |
|
1954 } |
|
1955 else |
|
1956 { |
|
1957 maybe_warn_separator_insert (','); |
|
1958 |
|
1959 if (Vwhitespace_in_literal_matrix != 2) |
|
1960 unput (','); |
|
1961 } |
1072
|
1962 } |
|
1963 } |
|
1964 |
767
|
1965 // Figure out exactly what kind of token to return when we have seen |
|
1966 // an identifier. Handles keywords. |
|
1967 |
146
|
1968 static int |
1823
|
1969 handle_identifier (const string& tok, int spc_gobbled) |
146
|
1970 { |
1826
|
1971 // It is almost always an error for an identifier to be followed |
|
1972 // directly by another identifier. Special cases are handled |
|
1973 // below. |
883
|
1974 |
2857
|
1975 lexer_flags.cant_be_identifier = true; |
883
|
1976 |
2970
|
1977 // If we are expecting a structure element, avoid recognizing |
|
1978 // keywords and other special names and return STRUCT_ELT, which is |
|
1979 // a string that is also a valid identifier. But first, we have to |
|
1980 // decide whether to insert a comma. |
747
|
1981 |
1826
|
1982 if (lexer_flags.looking_at_indirect_ref) |
1072
|
1983 { |
2970
|
1984 do_comma_insert_check (); |
|
1985 |
1072
|
1986 maybe_unput_comma (spc_gobbled); |
2819
|
1987 |
|
1988 yylval.tok_val = new token (tok, input_line_number, |
|
1989 current_input_column); |
|
1990 |
|
1991 token_stack.push (yylval.tok_val); |
|
1992 |
2857
|
1993 lexer_flags.cant_be_identifier = false; |
|
1994 lexer_flags.quote_is_transpose = true; |
|
1995 lexer_flags.convert_spaces_to_comma = true; |
2819
|
1996 |
|
1997 current_input_column += yyleng; |
|
1998 |
2970
|
1999 return STRUCT_ELT; |
1072
|
2000 } |
747
|
2001 |
1826
|
2002 // If we have a regular keyword, or a plot STYLE, return it. |
|
2003 // Keywords can be followed by identifiers (TOK_RETURN handles |
|
2004 // that). |
146
|
2005 |
|
2006 int kw_token = is_keyword (tok); |
2970
|
2007 |
146
|
2008 if (kw_token) |
|
2009 { |
|
2010 if (kw_token == STYLE) |
|
2011 { |
1060
|
2012 current_input_column += yyleng; |
2857
|
2013 lexer_flags.quote_is_transpose = false; |
|
2014 lexer_flags.convert_spaces_to_comma = true; |
146
|
2015 return kw_token; |
|
2016 } |
|
2017 else |
|
2018 TOK_RETURN (kw_token); |
|
2019 } |
|
2020 |
1826
|
2021 // See if we have a plot keyword (title, using, with, or clear). |
146
|
2022 |
1826
|
2023 if (lexer_flags.plotting) |
941
|
2024 { |
1826
|
2025 // Yes, we really do need both of these plot_range variables. |
|
2026 // One is used to mark when we are past all possiblity of a plot |
|
2027 // range, the other is used to mark when we are actually between |
|
2028 // the square brackets that surround the range. |
146
|
2029 |
1826
|
2030 if (! lexer_flags.in_plot_range) |
2857
|
2031 lexer_flags.past_plot_range = true; |
941
|
2032 |
3351
|
2033 // Option keywords can't appear in brackets, braces, or parentheses. |
1273
|
2034 |
|
2035 int plot_option_kw = 0; |
2970
|
2036 |
1826
|
2037 if (nesting_level.none ()) |
1273
|
2038 plot_option_kw = is_plot_keyword (tok); |
941
|
2039 |
1826
|
2040 if (lexer_flags.cant_be_identifier && plot_option_kw) |
941
|
2041 TOK_RETURN (plot_option_kw); |
|
2042 } |
146
|
2043 |
1001
|
2044 int c = yyinput (); |
3246
|
2045 unput (c); |
2702
|
2046 bool next_tok_is_eq = (c == '='); |
3246
|
2047 bool next_tok_is_dot = (c == '.'); |
2702
|
2048 bool next_tok_is_paren = (c == '('); |
1001
|
2049 |
1826
|
2050 // Make sure we put the return values of a function in the symbol |
|
2051 // table that is local to the function. |
146
|
2052 |
2877
|
2053 // If we are defining a function and we have not seen the function |
|
2054 // name yet and the next token is `=', then this identifier must be |
|
2055 // the only return value for the function and it belongs in the |
|
2056 // local symbol table. |
|
2057 |
1826
|
2058 if (next_tok_is_eq |
2877
|
2059 && lexer_flags.defining_func |
|
2060 && ! lexer_flags.parsed_function_name) |
146
|
2061 curr_sym_tab = tmp_local_sym_tab; |
|
2062 |
2702
|
2063 // Kluge alert. |
|
2064 // |
|
2065 // If we are looking at a text style function, set up to gobble its |
2745
|
2066 // arguments. |
|
2067 // |
|
2068 // If the following token is `=', or if we are parsing a function |
3189
|
2069 // return list or function parameter list, or if we are looking at |
|
2070 // something like [ab,cd] = foo (), force the symbol to be inserted |
|
2071 // as a variable in the current symbol table. |
2702
|
2072 |
|
2073 if (is_text_function_name (tok) && ! is_variable (tok)) |
|
2074 { |
2745
|
2075 if (next_tok_is_eq |
|
2076 || lexer_flags.looking_at_return_list |
3189
|
2077 || lexer_flags.looking_at_parameter_list |
3246
|
2078 || lexer_flags.looking_at_matrix_or_assign_lhs |
|
2079 || (next_tok_is_dot && next_token_is_bin_op (spc_gobbled))) |
2745
|
2080 { |
|
2081 force_local_variable (tok); |
|
2082 } |
2702
|
2083 else if (! next_tok_is_paren) |
|
2084 { |
|
2085 if (tok == "gset") |
2857
|
2086 lexer_flags.doing_set = true; |
2702
|
2087 |
|
2088 BEGIN TEXT_FCN; |
|
2089 } |
|
2090 } |
|
2091 |
1826
|
2092 // Find the token in the symbol table. |
146
|
2093 |
|
2094 yylval.tok_val = new token (lookup_identifier (tok), |
|
2095 input_line_number, |
|
2096 current_input_column); |
|
2097 |
|
2098 token_stack.push (yylval.tok_val); |
|
2099 |
1826
|
2100 // After seeing an identifer, it is ok to convert spaces to a comma |
|
2101 // (if needed). |
146
|
2102 |
2857
|
2103 lexer_flags.convert_spaces_to_comma = true; |
146
|
2104 |
2877
|
2105 if (! next_tok_is_eq) |
|
2106 { |
|
2107 lexer_flags.quote_is_transpose = true; |
146
|
2108 |
2877
|
2109 do_comma_insert_check (); |
|
2110 |
|
2111 maybe_unput_comma (spc_gobbled); |
146
|
2112 } |
|
2113 |
2877
|
2114 current_input_column += yyleng; |
146
|
2115 |
|
2116 return NAME; |
|
2117 } |
|
2118 |
767
|
2119 // Print a warning if a function file that defines a function has |
|
2120 // anything other than comments and whitespace following the END token |
|
2121 // that matches the FUNCTION statement. |
|
2122 |
1
|
2123 void |
|
2124 check_for_garbage_after_fcn_def (void) |
|
2125 { |
1826
|
2126 // By making a newline be the next character to be read, we will |
|
2127 // force the parser to return after reading the function. Calling |
3246
|
2128 // unput with EOF does not work. |
1
|
2129 |
2857
|
2130 bool in_comment = false; |
1
|
2131 int lineno = input_line_number; |
|
2132 int c; |
|
2133 while ((c = yyinput ()) != EOF) |
|
2134 { |
|
2135 switch (c) |
|
2136 { |
|
2137 case ' ': |
|
2138 case '\t': |
|
2139 case ';': |
|
2140 case ',': |
|
2141 break; |
777
|
2142 |
1
|
2143 case '\n': |
|
2144 if (in_comment) |
2857
|
2145 in_comment = false; |
1
|
2146 break; |
777
|
2147 |
1
|
2148 case '%': |
|
2149 case '#': |
2857
|
2150 in_comment = true; |
1
|
2151 break; |
777
|
2152 |
1
|
2153 default: |
|
2154 if (in_comment) |
|
2155 break; |
|
2156 else |
|
2157 { |
|
2158 warning ("ignoring trailing garbage after end of function\n\ |
1755
|
2159 near line %d of file `%s.m'", lineno, curr_fcn_file_name.c_str ()); |
1
|
2160 |
3246
|
2161 unput ('\n'); |
1
|
2162 return; |
|
2163 } |
|
2164 } |
|
2165 } |
3246
|
2166 unput ('\n'); |
1
|
2167 } |
|
2168 |
1826
|
2169 void |
|
2170 lexical_feedback::init (void) |
|
2171 { |
|
2172 // Not initially defining a matrix list. |
3351
|
2173 bracketflag = 0; |
1826
|
2174 |
|
2175 // Not initially inside a loop or if statement. |
|
2176 looping = 0; |
|
2177 |
2857
|
2178 // Not initially defining a function. |
|
2179 beginning_of_function = false; |
|
2180 defining_func = false; |
2877
|
2181 parsed_function_name = false; |
2857
|
2182 |
|
2183 // Not parsing a function return or parameter list. |
|
2184 looking_at_return_list = false; |
|
2185 looking_at_parameter_list = false; |
|
2186 |
|
2187 // Next token can be identifier. |
|
2188 cant_be_identifier = false; |
|
2189 |
|
2190 // No need to do comma insert or convert spaces to comma at |
|
2191 // beginning of input. |
|
2192 convert_spaces_to_comma = true; |
|
2193 do_comma_insert = false; |
|
2194 |
|
2195 // Not initially doing any plotting or setting of plot attributes. |
|
2196 doing_set = false; |
|
2197 in_plot_range = false; |
|
2198 in_plot_style = false; |
3165
|
2199 in_plot_axes = false; |
2857
|
2200 in_plot_using = false; |
|
2201 past_plot_range = false; |
|
2202 plotting = false; |
|
2203 |
1826
|
2204 // Not initially looking at indirect references. |
2857
|
2205 looking_at_indirect_ref = false; |
1826
|
2206 |
|
2207 // Quote marks strings intially. |
2857
|
2208 quote_is_transpose = false; |
1826
|
2209 } |
|
2210 |
3388
|
2211 static void |
|
2212 maybe_warn_separator_insert (char sep) |
|
2213 { |
|
2214 string nm = curr_fcn_file_full_name; |
|
2215 |
|
2216 if (Vwarn_separator_insert) |
|
2217 { |
|
2218 if (nm.empty ()) |
|
2219 warning ("potential auto-insertion of `%c' near line %d", |
|
2220 sep, input_line_number); |
|
2221 else |
|
2222 warning ("potential auto-insertion of `%c' near line %d of file %s", |
|
2223 sep, input_line_number, nm.c_str ()); |
|
2224 } |
|
2225 } |
|
2226 |
3400
|
2227 static void |
|
2228 gripe_single_quote_string (void) |
|
2229 { |
|
2230 string nm = curr_fcn_file_full_name; |
|
2231 |
|
2232 if (Vwarn_single_quote_string) |
|
2233 { |
|
2234 if (nm.empty ()) |
|
2235 warning ("single quote delimited string near line %d", |
|
2236 input_line_number); |
|
2237 else |
|
2238 warning ("single quote delimited string near line %d of file %s", |
|
2239 input_line_number, nm.c_str ()); |
|
2240 } |
|
2241 } |
|
2242 |
3388
|
2243 static int |
|
2244 warn_separator_insert (void) |
|
2245 { |
|
2246 Vwarn_separator_insert = check_preference ("warn_separator_insert"); |
|
2247 |
|
2248 return 0; |
|
2249 } |
|
2250 |
3246
|
2251 static int |
3400
|
2252 warn_single_quote_string (void) |
|
2253 { |
|
2254 Vwarn_single_quote_string = check_preference ("warn_single_quote_string"); |
|
2255 |
|
2256 return 0; |
|
2257 } |
|
2258 |
|
2259 static int |
2167
|
2260 whitespace_in_literal_matrix (void) |
|
2261 { |
|
2262 int pref = 0; |
3009
|
2263 |
2167
|
2264 string val = builtin_string_variable ("whitespace_in_literal_matrix"); |
3009
|
2265 |
2167
|
2266 if (! val.empty ()) |
|
2267 { |
|
2268 if (val.compare ("ignore", 0, 6) == 0) |
|
2269 pref = 2; |
|
2270 else if (val.compare ("traditional", 0, 11) == 0) |
|
2271 pref = 1; |
|
2272 } |
3009
|
2273 |
2167
|
2274 Vwhitespace_in_literal_matrix = pref; |
3009
|
2275 |
2167
|
2276 return 0; |
|
2277 } |
|
2278 |
|
2279 void |
|
2280 symbols_of_lex (void) |
|
2281 { |
3388
|
2282 DEFVAR (warn_separator_insert, 0.0, warn_separator_insert, |
|
2283 "print warning if commas or semicolons that might be inserted\n\ |
|
2284 automatically in literal matrices"); |
|
2285 |
3400
|
2286 DEFVAR (warn_single_quote_string, 0.0, warn_single_quote_string, |
|
2287 "print warning if a signle quote character is used to introduce a\n\ |
|
2288 string constant"); |
|
2289 |
3258
|
2290 DEFVAR (whitespace_in_literal_matrix, "", whitespace_in_literal_matrix, |
2167
|
2291 "control auto-insertion of commas and semicolons in literal matrices"); |
|
2292 } |
|
2293 |
1994
|
2294 /* |
|
2295 ;;; Local Variables: *** |
|
2296 ;;; mode: C++ *** |
|
2297 ;;; End: *** |
|
2298 */ |