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 |
3427
|
1176 // XXX FIXME XXX -- gobble_leading_white_space() in parse.y |
2300
|
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; |
3427
|
1186 bool discard_space = true; |
1019
|
1187 int c = 0; |
1
|
1188 |
1019
|
1189 while ((c = yyinput ()) != EOF) |
|
1190 { |
2300
|
1191 if (begin_comment) |
|
1192 { |
|
1193 if (c == '%' || c == '#') |
|
1194 continue; |
3427
|
1195 else if (discard_space && c == ' ') |
|
1196 { |
|
1197 discard_space = false; |
|
1198 continue; |
|
1199 } |
2300
|
1200 else |
|
1201 begin_comment = false; |
|
1202 } |
|
1203 |
1019
|
1204 if (in_comment) |
1
|
1205 { |
1755
|
1206 help_buf += (char) c; |
|
1207 |
1019
|
1208 if (c == '\n') |
3427
|
1209 { |
|
1210 in_comment = false; |
|
1211 discard_space = true; |
|
1212 } |
1019
|
1213 } |
|
1214 else |
|
1215 { |
|
1216 switch (c) |
991
|
1217 { |
1019
|
1218 case '%': |
|
1219 case '#': |
2300
|
1220 in_comment = true; |
|
1221 begin_comment = true; |
1019
|
1222 break; |
777
|
1223 |
1019
|
1224 case ' ': |
|
1225 case '\t': |
|
1226 break; |
777
|
1227 |
1019
|
1228 default: |
|
1229 goto done; |
1
|
1230 } |
|
1231 } |
1019
|
1232 } |
991
|
1233 |
1019
|
1234 done: |
991
|
1235 |
1019
|
1236 if (c) |
3246
|
1237 unput (c); |
1
|
1238 } |
|
1239 |
767
|
1240 // Return 1 if the given character matches any character in the given |
|
1241 // string. |
|
1242 |
2857
|
1243 static bool |
2804
|
1244 match_any (char c, const char *s) |
1
|
1245 { |
|
1246 char tmp; |
|
1247 while ((tmp = *s++) != '\0') |
|
1248 { |
|
1249 if (c == tmp) |
2857
|
1250 return true; |
1
|
1251 } |
2857
|
1252 return false; |
1
|
1253 } |
|
1254 |
767
|
1255 // Given information about the spacing surrounding an operator, |
|
1256 // return 1 if it looks like it should be treated as a binary |
|
1257 // operator. For example, |
|
1258 // |
|
1259 // [ 1 + 2 ] or [ 1+ 2] or [ 1+2 ] ==> binary |
|
1260 |
2857
|
1261 static bool |
3246
|
1262 looks_like_bin_op (bool spc_prev, int next_char) |
1
|
1263 { |
3246
|
1264 bool spc_next = (next_char == ' ' || next_char == '\t'); |
|
1265 |
608
|
1266 return ((spc_prev && spc_next) || ! spc_prev); |
1
|
1267 } |
|
1268 |
3263
|
1269 // Recognize separators. If the separator is a CRLF pair, it is |
|
1270 // replaced by a single LF. |
|
1271 |
|
1272 static bool |
|
1273 next_token_is_sep_op (void) |
|
1274 { |
|
1275 bool retval = false; |
|
1276 |
|
1277 int c1 = yyinput (); |
|
1278 |
|
1279 if (c1 == '\r') |
|
1280 { |
|
1281 int c2 = yyinput (); |
|
1282 |
|
1283 if (c2 == '\n') |
|
1284 { |
|
1285 c1 = '\n'; |
|
1286 |
|
1287 retval = true; |
|
1288 } |
|
1289 else |
|
1290 unput (c2); |
|
1291 } |
|
1292 else |
|
1293 retval = match_any (c1, ",;\n]"); |
|
1294 |
|
1295 unput (c1); |
|
1296 |
|
1297 return retval; |
|
1298 } |
|
1299 |
767
|
1300 // Try to determine if the next token should be treated as a postfix |
|
1301 // unary operator. This is ugly, but it seems to do the right thing. |
|
1302 |
2857
|
1303 static bool |
3246
|
1304 next_token_is_postfix_unary_op (bool spc_prev) |
1
|
1305 { |
2857
|
1306 bool un_op = false; |
1
|
1307 |
|
1308 int c0 = yyinput (); |
|
1309 |
3246
|
1310 if (c0 == '\'' && ! spc_prev) |
|
1311 { |
|
1312 un_op = true; |
|
1313 } |
|
1314 else if (c0 == '.') |
|
1315 { |
|
1316 int c1 = yyinput (); |
|
1317 un_op = (c1 == '\''); |
|
1318 unput (c1); |
|
1319 } |
1
|
1320 |
3246
|
1321 unput (c0); |
1
|
1322 |
|
1323 return un_op; |
|
1324 } |
|
1325 |
767
|
1326 // Try to determine if the next token should be treated as a binary |
3246
|
1327 // operator. |
1521
|
1328 // |
3246
|
1329 // This kluge exists because whitespace is not always ignored inside |
|
1330 // the square brackets that are used to create matrix objects. |
1521
|
1331 // |
3246
|
1332 // Line continuations directly after the operator will cause this |
|
1333 // function to return FALSE. |
767
|
1334 |
2857
|
1335 static bool |
3246
|
1336 next_token_is_bin_op (bool spc_prev) |
1
|
1337 { |
2857
|
1338 bool bin_op = false; |
1
|
1339 |
|
1340 int c0 = yyinput (); |
|
1341 |
|
1342 switch (c0) |
|
1343 { |
3246
|
1344 case ':': |
777
|
1345 case '+': |
|
1346 case '-': |
3246
|
1347 case '/': |
|
1348 case '\\': |
|
1349 case '^': |
1276
|
1350 { |
|
1351 int c1 = yyinput (); |
3246
|
1352 bin_op = looks_like_bin_op (spc_prev, c1); |
|
1353 unput (c1); |
1276
|
1354 } |
|
1355 break; |
|
1356 |
3246
|
1357 // .+ .- ./ .\ .^ .* .** |
1554
|
1358 case '.': |
|
1359 { |
|
1360 int c1 = yyinput (); |
3246
|
1361 |
|
1362 if (match_any (c1, "+-/\\^")) |
|
1363 { |
|
1364 int c2 = yyinput (); |
|
1365 bin_op = looks_like_bin_op (spc_prev, c2); |
|
1366 unput (c2); |
|
1367 } |
|
1368 else if (c1 == '*') |
|
1369 { |
|
1370 int c2 = yyinput (); |
|
1371 |
|
1372 if (c2 == '*') |
|
1373 { |
|
1374 int c3 = yyinput (); |
|
1375 bin_op = looks_like_bin_op (spc_prev, c3); |
|
1376 unput (c3); |
|
1377 } |
|
1378 else |
|
1379 bin_op = looks_like_bin_op (spc_prev, c2); |
|
1380 |
|
1381 unput (c2); |
|
1382 } |
|
1383 else if (! isdigit (c1) && c1 != ' ' && c1 != '\t') |
|
1384 { |
|
1385 bin_op = true; |
|
1386 } |
|
1387 |
|
1388 unput (c1); |
1554
|
1389 } |
|
1390 break; |
|
1391 |
3246
|
1392 // = == & && | || * ** |
|
1393 case '=': |
1
|
1394 case '&': |
3246
|
1395 case '|': |
1
|
1396 case '*': |
3246
|
1397 { |
|
1398 int c1 = yyinput (); |
|
1399 |
|
1400 if (c1 == c0) |
|
1401 { |
|
1402 int c2 = yyinput (); |
|
1403 bin_op = looks_like_bin_op (spc_prev, c2); |
|
1404 unput (c2); |
|
1405 } |
|
1406 else |
|
1407 bin_op = looks_like_bin_op (spc_prev, c1); |
|
1408 |
|
1409 unput (c1); |
|
1410 } |
|
1411 break; |
|
1412 |
|
1413 // <= >= <> ~= != < > |
1
|
1414 case '<': |
|
1415 case '>': |
777
|
1416 case '~': |
|
1417 case '!': |
3246
|
1418 { |
|
1419 int c1 = yyinput (); |
|
1420 |
|
1421 if ((c1 == '=') || (c1 == '<' && c1 == '>')) |
|
1422 { |
|
1423 int c2 = yyinput (); |
|
1424 bin_op = looks_like_bin_op (spc_prev, c2); |
|
1425 unput (c2); |
|
1426 } |
|
1427 else if (c1 != '~' && c1 != '!') |
|
1428 bin_op = looks_like_bin_op (spc_prev, c1); |
|
1429 |
|
1430 unput (c1); |
|
1431 } |
1
|
1432 break; |
|
1433 |
|
1434 default: |
1276
|
1435 break; |
1
|
1436 } |
|
1437 |
3246
|
1438 unput (c0); |
1
|
1439 |
|
1440 return bin_op; |
|
1441 } |
|
1442 |
767
|
1443 // Used to delete trailing white space from tokens. |
|
1444 |
1823
|
1445 static string |
1
|
1446 strip_trailing_whitespace (char *s) |
|
1447 { |
1823
|
1448 string retval = s; |
1
|
1449 |
1823
|
1450 size_t pos = retval.find_first_of (" \t"); |
1
|
1451 |
1823
|
1452 if (pos != NPOS) |
|
1453 retval.resize (pos); |
1
|
1454 |
|
1455 return retval; |
|
1456 } |
|
1457 |
1001
|
1458 // Discard whitespace, including comments and continuations. |
1088
|
1459 // |
|
1460 // Return value is logical OR of the following values: |
|
1461 // |
1826
|
1462 // ATE_NOTHING : no spaces to eat |
1088
|
1463 // ATE_SPACE_OR_TAB : space or tab in input |
|
1464 // ATE_NEWLINE : bare new line in input |
1001
|
1465 |
1826
|
1466 static yum_yum |
975
|
1467 eat_whitespace (void) |
|
1468 { |
1826
|
1469 yum_yum retval = ATE_NOTHING; |
2857
|
1470 bool in_comment = false; |
975
|
1471 int c; |
|
1472 while ((c = yyinput ()) != EOF) |
|
1473 { |
|
1474 current_input_column++; |
|
1475 |
|
1476 switch (c) |
|
1477 { |
|
1478 case ' ': |
|
1479 case '\t': |
1088
|
1480 retval |= ATE_SPACE_OR_TAB; |
975
|
1481 break; |
|
1482 |
|
1483 case '\n': |
1088
|
1484 retval |= ATE_NEWLINE; |
2857
|
1485 in_comment = false; |
975
|
1486 current_input_column = 0; |
|
1487 break; |
|
1488 |
|
1489 case '#': |
|
1490 case '%': |
2857
|
1491 in_comment = true; |
975
|
1492 break; |
|
1493 |
1001
|
1494 case '.': |
|
1495 if (in_comment) |
|
1496 break; |
|
1497 else |
|
1498 { |
|
1499 if (have_ellipsis_continuation ()) |
|
1500 break; |
|
1501 else |
|
1502 goto done; |
|
1503 } |
|
1504 |
|
1505 case '\\': |
|
1506 if (in_comment) |
|
1507 break; |
|
1508 else |
|
1509 { |
3105
|
1510 if (have_continuation ()) |
1001
|
1511 break; |
|
1512 else |
|
1513 goto done; |
|
1514 } |
|
1515 |
975
|
1516 default: |
|
1517 if (in_comment) |
|
1518 break; |
|
1519 else |
|
1520 goto done; |
|
1521 } |
|
1522 } |
|
1523 |
|
1524 done: |
3246
|
1525 unput (c); |
1082
|
1526 current_input_column--; |
1001
|
1527 return retval; |
975
|
1528 } |
|
1529 |
3220
|
1530 static inline bool |
|
1531 looks_like_hex (const char *s, int len) |
|
1532 { |
|
1533 return (len > 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X')); |
|
1534 } |
|
1535 |
975
|
1536 static void |
3246
|
1537 handle_number (void) |
972
|
1538 { |
2621
|
1539 char *tmp = strsave (yytext); |
|
1540 |
|
1541 char *idx = strpbrk (tmp, "Dd"); |
|
1542 |
|
1543 if (idx) |
|
1544 *idx = 'e'; |
|
1545 |
3220
|
1546 double value = 0.0; |
|
1547 int nread = 0; |
|
1548 |
|
1549 if (looks_like_hex (tmp, strlen (tmp))) |
|
1550 { |
|
1551 unsigned long ival; |
|
1552 nread = sscanf (tmp, "%lx", &ival); |
|
1553 value = static_cast<double> (ival); |
|
1554 } |
|
1555 else |
|
1556 nread = sscanf (tmp, "%lf", &value); |
2621
|
1557 |
|
1558 delete [] tmp; |
972
|
1559 |
1826
|
1560 // If yytext doesn't contain a valid number, we are in deep doo doo. |
985
|
1561 |
972
|
1562 assert (nread == 1); |
|
1563 |
1826
|
1564 lexer_flags.quote_is_transpose = 1; |
|
1565 lexer_flags.cant_be_identifier = 1; |
|
1566 lexer_flags.convert_spaces_to_comma = 1; |
972
|
1567 |
1826
|
1568 if (lexer_flags.plotting && ! lexer_flags.in_plot_range) |
|
1569 lexer_flags.past_plot_range = 1; |
972
|
1570 |
|
1571 yylval.tok_val = new token (value, yytext, input_line_number, |
|
1572 current_input_column); |
|
1573 |
|
1574 token_stack.push (yylval.tok_val); |
|
1575 |
|
1576 current_input_column += yyleng; |
|
1577 |
|
1578 do_comma_insert_check (); |
|
1579 } |
|
1580 |
1001
|
1581 // We have seen a backslash and need to find out if it should be |
|
1582 // treated as a continuation character. If so, this eats it, up to |
|
1583 // and including the new line character. |
|
1584 // |
973
|
1585 // Match whitespace only, followed by a comment character or newline. |
|
1586 // Once a comment character is found, discard all input until newline. |
|
1587 // If non-whitespace characters are found before comment |
|
1588 // characters, return 0. Otherwise, return 1. |
|
1589 |
2857
|
1590 static bool |
3096
|
1591 have_continuation (bool trailing_comments_ok) |
973
|
1592 { |
|
1593 ostrstream buf; |
|
1594 |
2857
|
1595 bool in_comment = false; |
3332
|
1596 int c; |
973
|
1597 while ((c = yyinput ()) != EOF) |
|
1598 { |
|
1599 buf << (char) c; |
|
1600 |
|
1601 switch (c) |
|
1602 { |
|
1603 case ' ': |
|
1604 case '\t': |
|
1605 break; |
|
1606 |
|
1607 case '%': |
|
1608 case '#': |
1091
|
1609 if (trailing_comments_ok) |
2857
|
1610 in_comment = true; |
1091
|
1611 else |
|
1612 goto cleanup; |
973
|
1613 break; |
|
1614 |
|
1615 case '\n': |
975
|
1616 current_input_column = 0; |
1001
|
1617 promptflag--; |
2857
|
1618 return true; |
973
|
1619 |
3263
|
1620 case '\r': |
|
1621 c = yyinput (); |
|
1622 if (c == EOF) |
|
1623 break; |
|
1624 else if (c == '\n') |
|
1625 { |
|
1626 current_input_column = 0; |
|
1627 promptflag--; |
|
1628 return true; |
|
1629 } |
|
1630 |
973
|
1631 default: |
1091
|
1632 if (! in_comment) |
|
1633 goto cleanup; |
|
1634 break; |
973
|
1635 } |
|
1636 } |
|
1637 |
3246
|
1638 unput (c); |
2857
|
1639 return false; |
973
|
1640 |
3096
|
1641 cleanup: |
1091
|
1642 buf << ends; |
|
1643 char *s = buf.str (); |
|
1644 if (s) |
|
1645 { |
|
1646 int len = strlen (s); |
|
1647 while (len--) |
3246
|
1648 unput (s[len]); |
1091
|
1649 } |
|
1650 delete [] s; |
3096
|
1651 |
2857
|
1652 return false; |
973
|
1653 } |
|
1654 |
1001
|
1655 // We have seen a `.' and need to see if it is the start of a |
|
1656 // continuation. If so, this eats it, up to and including the new |
|
1657 // line character. |
|
1658 |
2857
|
1659 static bool |
3096
|
1660 have_ellipsis_continuation (bool trailing_comments_ok) |
973
|
1661 { |
|
1662 char c1 = yyinput (); |
|
1663 if (c1 == '.') |
|
1664 { |
|
1665 char c2 = yyinput (); |
1091
|
1666 if (c2 == '.' && have_continuation (trailing_comments_ok)) |
2857
|
1667 return true; |
973
|
1668 else |
|
1669 { |
3246
|
1670 unput (c2); |
|
1671 unput (c1); |
973
|
1672 } |
|
1673 } |
|
1674 else |
3246
|
1675 unput (c1); |
973
|
1676 |
2857
|
1677 return false; |
973
|
1678 } |
|
1679 |
1001
|
1680 // See if we have a continuation line. If so, eat it and the leading |
|
1681 // whitespace on the next line. |
1088
|
1682 // |
|
1683 // Return value is the same as described for eat_whitespace(). |
1001
|
1684 |
1826
|
1685 static yum_yum |
1001
|
1686 eat_continuation (void) |
|
1687 { |
1826
|
1688 int retval = ATE_NOTHING; |
1001
|
1689 int c = yyinput (); |
|
1690 if ((c == '.' && have_ellipsis_continuation ()) |
3105
|
1691 || (c == '\\' && have_continuation ())) |
1001
|
1692 retval = eat_whitespace (); |
|
1693 else |
3246
|
1694 unput (c); |
1001
|
1695 |
|
1696 return retval; |
|
1697 } |
|
1698 |
973
|
1699 static int |
975
|
1700 handle_string (char delim, int text_style) |
973
|
1701 { |
|
1702 ostrstream buf; |
|
1703 |
|
1704 int c; |
1031
|
1705 int escape_pending = 0; |
973
|
1706 |
|
1707 while ((c = yyinput ()) != EOF) |
|
1708 { |
|
1709 current_input_column++; |
|
1710 |
3105
|
1711 if (c == '\\') |
973
|
1712 { |
1053
|
1713 if (escape_pending) |
|
1714 { |
|
1715 buf << (char) c; |
|
1716 escape_pending = 0; |
|
1717 } |
|
1718 else |
|
1719 { |
3096
|
1720 if (have_continuation (false)) |
1053
|
1721 escape_pending = 0; |
|
1722 else |
|
1723 { |
|
1724 buf << (char) c; |
|
1725 escape_pending = 1; |
|
1726 } |
|
1727 } |
1031
|
1728 continue; |
973
|
1729 } |
|
1730 else if (c == '.') |
|
1731 { |
3096
|
1732 if (! have_ellipsis_continuation (false)) |
973
|
1733 buf << (char) c; |
|
1734 } |
|
1735 else if (c == '\n') |
|
1736 { |
1053
|
1737 error ("unterminated string constant"); |
973
|
1738 break; |
|
1739 } |
|
1740 else if (c == delim) |
|
1741 { |
1031
|
1742 if (escape_pending) |
973
|
1743 buf << (char) c; |
|
1744 else |
|
1745 { |
|
1746 c = yyinput (); |
|
1747 if (c == delim) |
|
1748 buf << (char) c; |
|
1749 else |
|
1750 { |
3246
|
1751 unput (c); |
973
|
1752 buf << ends; |
3105
|
1753 char *t = buf.str (); |
|
1754 string s = do_string_escapes (t); |
|
1755 delete [] t; |
975
|
1756 |
1826
|
1757 if (text_style && lexer_flags.doing_set) |
975
|
1758 { |
3238
|
1759 s = string (1, delim) + s + string (1, delim); |
975
|
1760 } |
|
1761 else |
|
1762 { |
2857
|
1763 lexer_flags.quote_is_transpose = true; |
|
1764 lexer_flags.cant_be_identifier = true; |
|
1765 lexer_flags.convert_spaces_to_comma = true; |
975
|
1766 } |
|
1767 |
3105
|
1768 yylval.tok_val = new token (s); |
973
|
1769 token_stack.push (yylval.tok_val); |
3400
|
1770 |
|
1771 if (delim == '\'') |
|
1772 gripe_single_quote_string (); |
|
1773 |
973
|
1774 return TEXT; |
|
1775 } |
|
1776 } |
|
1777 } |
|
1778 else |
|
1779 { |
|
1780 buf << (char) c; |
|
1781 } |
|
1782 |
1031
|
1783 escape_pending = 0; |
973
|
1784 } |
|
1785 |
|
1786 return LEXICAL_ERROR; |
|
1787 } |
|
1788 |
3208
|
1789 static bool |
|
1790 next_token_is_assign_op (void) |
|
1791 { |
|
1792 bool retval = false; |
|
1793 |
|
1794 int c0 = yyinput (); |
|
1795 |
|
1796 switch (c0) |
|
1797 { |
|
1798 case '=': |
|
1799 { |
|
1800 int c1 = yyinput (); |
|
1801 unput (c1); |
|
1802 if (c1 != '=') |
|
1803 retval = true; |
|
1804 } |
|
1805 break; |
|
1806 |
|
1807 case '+': |
|
1808 case '-': |
|
1809 case '*': |
|
1810 case '/': |
|
1811 case '\\': |
|
1812 case '&': |
|
1813 case '|': |
|
1814 { |
|
1815 int c1 = yyinput (); |
|
1816 unput (c1); |
|
1817 if (c1 == '=') |
|
1818 retval = true; |
|
1819 } |
|
1820 break; |
|
1821 |
|
1822 case '.': |
|
1823 { |
|
1824 int c1 = yyinput (); |
|
1825 if (match_any (c1, "+-*/\\")) |
|
1826 { |
|
1827 int c2 = yyinput (); |
|
1828 unput (c2); |
|
1829 if (c2 == '=') |
|
1830 retval = true; |
|
1831 } |
|
1832 unput (c1); |
|
1833 } |
|
1834 break; |
|
1835 |
|
1836 case '>': |
|
1837 { |
|
1838 int c1 = yyinput (); |
|
1839 if (c1 == '>') |
|
1840 { |
|
1841 int c2 = yyinput (); |
|
1842 unput (c2); |
|
1843 if (c2 == '=') |
|
1844 retval = true; |
|
1845 } |
|
1846 unput (c1); |
|
1847 } |
|
1848 break; |
|
1849 |
|
1850 case '<': |
|
1851 { |
|
1852 int c1 = yyinput (); |
|
1853 if (c1 == '<') |
|
1854 { |
|
1855 int c2 = yyinput (); |
|
1856 unput (c2); |
|
1857 if (c2 == '=') |
|
1858 retval = true; |
|
1859 } |
|
1860 unput (c1); |
|
1861 } |
|
1862 break; |
|
1863 |
|
1864 default: |
|
1865 break; |
|
1866 } |
|
1867 |
|
1868 unput (c0); |
|
1869 |
|
1870 return retval; |
|
1871 } |
|
1872 |
971
|
1873 static int |
3351
|
1874 handle_close_bracket (int spc_gobbled) |
971
|
1875 { |
3208
|
1876 int retval = ']'; |
|
1877 |
1826
|
1878 if (! nesting_level.none ()) |
971
|
1879 { |
1826
|
1880 nesting_level.remove (); |
3351
|
1881 lexer_flags.bracketflag--; |
971
|
1882 } |
|
1883 |
3351
|
1884 if (lexer_flags.bracketflag == 0) |
1001
|
1885 BEGIN 0; |
|
1886 |
3208
|
1887 if (next_token_is_assign_op () && ! lexer_flags.looking_at_return_list) |
971
|
1888 { |
3208
|
1889 retval = CLOSE_BRACE; |
971
|
1890 } |
|
1891 else |
|
1892 { |
3208
|
1893 int c1 = yyinput (); |
971
|
1894 unput (c1); |
|
1895 |
3388
|
1896 if (lexer_flags.bracketflag) |
971
|
1897 { |
3246
|
1898 int bin_op = next_token_is_bin_op (spc_gobbled); |
3263
|
1899 |
3246
|
1900 int postfix_un_op = next_token_is_postfix_unary_op (spc_gobbled); |
971
|
1901 |
3263
|
1902 int sep_op = next_token_is_sep_op (); |
971
|
1903 |
3263
|
1904 if (! (postfix_un_op || bin_op || sep_op) |
3351
|
1905 && nesting_level.is_bracket () |
1826
|
1906 && lexer_flags.convert_spaces_to_comma) |
971
|
1907 { |
3388
|
1908 maybe_warn_separator_insert (','); |
|
1909 |
|
1910 if (Vwhitespace_in_literal_matrix != 2) |
|
1911 { |
|
1912 unput (','); |
|
1913 return ']'; |
|
1914 } |
971
|
1915 } |
|
1916 } |
|
1917 } |
|
1918 |
2857
|
1919 lexer_flags.quote_is_transpose = true; |
|
1920 lexer_flags.cant_be_identifier = false; |
|
1921 lexer_flags.convert_spaces_to_comma = true; |
3208
|
1922 |
|
1923 return retval; |
971
|
1924 } |
|
1925 |
1072
|
1926 static void |
|
1927 maybe_unput_comma (int spc_gobbled) |
|
1928 { |
3388
|
1929 if (nesting_level.is_bracket ()) |
1072
|
1930 { |
3246
|
1931 int bin_op = next_token_is_bin_op (spc_gobbled); |
1072
|
1932 |
3246
|
1933 int postfix_un_op = next_token_is_postfix_unary_op (spc_gobbled); |
1072
|
1934 |
|
1935 int c1 = yyinput (); |
|
1936 int c2 = yyinput (); |
2970
|
1937 |
1072
|
1938 unput (c2); |
|
1939 unput (c1); |
2970
|
1940 |
3263
|
1941 int sep_op = next_token_is_sep_op (); |
2970
|
1942 |
1072
|
1943 int dot_op = (c1 == '.' |
|
1944 && (isalpha (c2) || isspace (c2) || c2 == '_')); |
2970
|
1945 |
3388
|
1946 if (postfix_un_op || bin_op || sep_op || dot_op) |
|
1947 return; |
|
1948 |
|
1949 int index_op = (c1 == '('); |
|
1950 |
|
1951 if (index_op) |
|
1952 { |
|
1953 // If there is no space before the '(', we don't insert a comma. |
|
1954 if (! spc_gobbled) |
|
1955 return; |
|
1956 |
|
1957 maybe_warn_separator_insert (','); |
1072
|
1958 |
3388
|
1959 // If there is a space, we only insert a comma if we are |
|
1960 // trying to be Matlab-like. |
|
1961 if (Vwhitespace_in_literal_matrix == 1) |
|
1962 unput (','); |
|
1963 } |
|
1964 else |
|
1965 { |
|
1966 maybe_warn_separator_insert (','); |
|
1967 |
|
1968 if (Vwhitespace_in_literal_matrix != 2) |
|
1969 unput (','); |
|
1970 } |
1072
|
1971 } |
|
1972 } |
|
1973 |
767
|
1974 // Figure out exactly what kind of token to return when we have seen |
|
1975 // an identifier. Handles keywords. |
|
1976 |
146
|
1977 static int |
1823
|
1978 handle_identifier (const string& tok, int spc_gobbled) |
146
|
1979 { |
1826
|
1980 // It is almost always an error for an identifier to be followed |
|
1981 // directly by another identifier. Special cases are handled |
|
1982 // below. |
883
|
1983 |
2857
|
1984 lexer_flags.cant_be_identifier = true; |
883
|
1985 |
2970
|
1986 // If we are expecting a structure element, avoid recognizing |
|
1987 // keywords and other special names and return STRUCT_ELT, which is |
|
1988 // a string that is also a valid identifier. But first, we have to |
|
1989 // decide whether to insert a comma. |
747
|
1990 |
1826
|
1991 if (lexer_flags.looking_at_indirect_ref) |
1072
|
1992 { |
2970
|
1993 do_comma_insert_check (); |
|
1994 |
1072
|
1995 maybe_unput_comma (spc_gobbled); |
2819
|
1996 |
|
1997 yylval.tok_val = new token (tok, input_line_number, |
|
1998 current_input_column); |
|
1999 |
|
2000 token_stack.push (yylval.tok_val); |
|
2001 |
2857
|
2002 lexer_flags.cant_be_identifier = false; |
|
2003 lexer_flags.quote_is_transpose = true; |
|
2004 lexer_flags.convert_spaces_to_comma = true; |
2819
|
2005 |
|
2006 current_input_column += yyleng; |
|
2007 |
2970
|
2008 return STRUCT_ELT; |
1072
|
2009 } |
747
|
2010 |
1826
|
2011 // If we have a regular keyword, or a plot STYLE, return it. |
|
2012 // Keywords can be followed by identifiers (TOK_RETURN handles |
|
2013 // that). |
146
|
2014 |
|
2015 int kw_token = is_keyword (tok); |
2970
|
2016 |
146
|
2017 if (kw_token) |
|
2018 { |
|
2019 if (kw_token == STYLE) |
|
2020 { |
1060
|
2021 current_input_column += yyleng; |
2857
|
2022 lexer_flags.quote_is_transpose = false; |
|
2023 lexer_flags.convert_spaces_to_comma = true; |
146
|
2024 return kw_token; |
|
2025 } |
|
2026 else |
|
2027 TOK_RETURN (kw_token); |
|
2028 } |
|
2029 |
1826
|
2030 // See if we have a plot keyword (title, using, with, or clear). |
146
|
2031 |
1826
|
2032 if (lexer_flags.plotting) |
941
|
2033 { |
1826
|
2034 // Yes, we really do need both of these plot_range variables. |
|
2035 // One is used to mark when we are past all possiblity of a plot |
|
2036 // range, the other is used to mark when we are actually between |
|
2037 // the square brackets that surround the range. |
146
|
2038 |
1826
|
2039 if (! lexer_flags.in_plot_range) |
2857
|
2040 lexer_flags.past_plot_range = true; |
941
|
2041 |
3351
|
2042 // Option keywords can't appear in brackets, braces, or parentheses. |
1273
|
2043 |
|
2044 int plot_option_kw = 0; |
2970
|
2045 |
1826
|
2046 if (nesting_level.none ()) |
1273
|
2047 plot_option_kw = is_plot_keyword (tok); |
941
|
2048 |
1826
|
2049 if (lexer_flags.cant_be_identifier && plot_option_kw) |
941
|
2050 TOK_RETURN (plot_option_kw); |
|
2051 } |
146
|
2052 |
3480
|
2053 int c1 = yyinput (); |
|
2054 |
|
2055 bool next_tok_is_dot = (c1 == '.'); |
|
2056 bool next_tok_is_paren = (c1 == '('); |
|
2057 |
|
2058 bool next_tok_is_eq = false; |
|
2059 if (c1 == '=') |
|
2060 { |
|
2061 int c2 = yyinput (); |
|
2062 unput (c2); |
|
2063 |
|
2064 if (c2 != '=') |
|
2065 next_tok_is_eq = true; |
|
2066 } |
|
2067 |
|
2068 unput (c1); |
1001
|
2069 |
1826
|
2070 // Make sure we put the return values of a function in the symbol |
|
2071 // table that is local to the function. |
146
|
2072 |
2877
|
2073 // If we are defining a function and we have not seen the function |
|
2074 // name yet and the next token is `=', then this identifier must be |
|
2075 // the only return value for the function and it belongs in the |
|
2076 // local symbol table. |
|
2077 |
1826
|
2078 if (next_tok_is_eq |
2877
|
2079 && lexer_flags.defining_func |
|
2080 && ! lexer_flags.parsed_function_name) |
146
|
2081 curr_sym_tab = tmp_local_sym_tab; |
|
2082 |
2702
|
2083 // Kluge alert. |
|
2084 // |
|
2085 // If we are looking at a text style function, set up to gobble its |
2745
|
2086 // arguments. |
|
2087 // |
|
2088 // If the following token is `=', or if we are parsing a function |
3189
|
2089 // return list or function parameter list, or if we are looking at |
|
2090 // something like [ab,cd] = foo (), force the symbol to be inserted |
|
2091 // as a variable in the current symbol table. |
2702
|
2092 |
|
2093 if (is_text_function_name (tok) && ! is_variable (tok)) |
|
2094 { |
2745
|
2095 if (next_tok_is_eq |
|
2096 || lexer_flags.looking_at_return_list |
3189
|
2097 || lexer_flags.looking_at_parameter_list |
3246
|
2098 || lexer_flags.looking_at_matrix_or_assign_lhs |
|
2099 || (next_tok_is_dot && next_token_is_bin_op (spc_gobbled))) |
2745
|
2100 { |
|
2101 force_local_variable (tok); |
|
2102 } |
2702
|
2103 else if (! next_tok_is_paren) |
|
2104 { |
|
2105 if (tok == "gset") |
2857
|
2106 lexer_flags.doing_set = true; |
2702
|
2107 |
|
2108 BEGIN TEXT_FCN; |
|
2109 } |
|
2110 } |
|
2111 |
1826
|
2112 // Find the token in the symbol table. |
146
|
2113 |
|
2114 yylval.tok_val = new token (lookup_identifier (tok), |
|
2115 input_line_number, |
|
2116 current_input_column); |
|
2117 |
|
2118 token_stack.push (yylval.tok_val); |
|
2119 |
1826
|
2120 // After seeing an identifer, it is ok to convert spaces to a comma |
|
2121 // (if needed). |
146
|
2122 |
2857
|
2123 lexer_flags.convert_spaces_to_comma = true; |
146
|
2124 |
2877
|
2125 if (! next_tok_is_eq) |
|
2126 { |
|
2127 lexer_flags.quote_is_transpose = true; |
146
|
2128 |
2877
|
2129 do_comma_insert_check (); |
|
2130 |
|
2131 maybe_unput_comma (spc_gobbled); |
146
|
2132 } |
|
2133 |
2877
|
2134 current_input_column += yyleng; |
146
|
2135 |
|
2136 return NAME; |
|
2137 } |
|
2138 |
767
|
2139 // Print a warning if a function file that defines a function has |
|
2140 // anything other than comments and whitespace following the END token |
|
2141 // that matches the FUNCTION statement. |
|
2142 |
1
|
2143 void |
|
2144 check_for_garbage_after_fcn_def (void) |
|
2145 { |
1826
|
2146 // By making a newline be the next character to be read, we will |
|
2147 // force the parser to return after reading the function. Calling |
3246
|
2148 // unput with EOF does not work. |
1
|
2149 |
2857
|
2150 bool in_comment = false; |
1
|
2151 int lineno = input_line_number; |
|
2152 int c; |
|
2153 while ((c = yyinput ()) != EOF) |
|
2154 { |
|
2155 switch (c) |
|
2156 { |
|
2157 case ' ': |
|
2158 case '\t': |
|
2159 case ';': |
|
2160 case ',': |
|
2161 break; |
777
|
2162 |
1
|
2163 case '\n': |
|
2164 if (in_comment) |
2857
|
2165 in_comment = false; |
1
|
2166 break; |
777
|
2167 |
1
|
2168 case '%': |
|
2169 case '#': |
2857
|
2170 in_comment = true; |
1
|
2171 break; |
777
|
2172 |
1
|
2173 default: |
|
2174 if (in_comment) |
|
2175 break; |
|
2176 else |
|
2177 { |
|
2178 warning ("ignoring trailing garbage after end of function\n\ |
1755
|
2179 near line %d of file `%s.m'", lineno, curr_fcn_file_name.c_str ()); |
1
|
2180 |
3246
|
2181 unput ('\n'); |
1
|
2182 return; |
|
2183 } |
|
2184 } |
|
2185 } |
3246
|
2186 unput ('\n'); |
1
|
2187 } |
|
2188 |
1826
|
2189 void |
|
2190 lexical_feedback::init (void) |
|
2191 { |
|
2192 // Not initially defining a matrix list. |
3351
|
2193 bracketflag = 0; |
1826
|
2194 |
|
2195 // Not initially inside a loop or if statement. |
|
2196 looping = 0; |
|
2197 |
2857
|
2198 // Not initially defining a function. |
|
2199 beginning_of_function = false; |
|
2200 defining_func = false; |
2877
|
2201 parsed_function_name = false; |
2857
|
2202 |
|
2203 // Not parsing a function return or parameter list. |
|
2204 looking_at_return_list = false; |
|
2205 looking_at_parameter_list = false; |
|
2206 |
|
2207 // Next token can be identifier. |
|
2208 cant_be_identifier = false; |
|
2209 |
|
2210 // No need to do comma insert or convert spaces to comma at |
|
2211 // beginning of input. |
|
2212 convert_spaces_to_comma = true; |
|
2213 do_comma_insert = false; |
|
2214 |
|
2215 // Not initially doing any plotting or setting of plot attributes. |
|
2216 doing_set = false; |
|
2217 in_plot_range = false; |
|
2218 in_plot_style = false; |
3165
|
2219 in_plot_axes = false; |
2857
|
2220 in_plot_using = false; |
|
2221 past_plot_range = false; |
|
2222 plotting = false; |
|
2223 |
1826
|
2224 // Not initially looking at indirect references. |
2857
|
2225 looking_at_indirect_ref = false; |
1826
|
2226 |
|
2227 // Quote marks strings intially. |
2857
|
2228 quote_is_transpose = false; |
1826
|
2229 } |
|
2230 |
3388
|
2231 static void |
|
2232 maybe_warn_separator_insert (char sep) |
|
2233 { |
|
2234 string nm = curr_fcn_file_full_name; |
|
2235 |
|
2236 if (Vwarn_separator_insert) |
|
2237 { |
|
2238 if (nm.empty ()) |
|
2239 warning ("potential auto-insertion of `%c' near line %d", |
|
2240 sep, input_line_number); |
|
2241 else |
|
2242 warning ("potential auto-insertion of `%c' near line %d of file %s", |
|
2243 sep, input_line_number, nm.c_str ()); |
|
2244 } |
|
2245 } |
|
2246 |
3400
|
2247 static void |
|
2248 gripe_single_quote_string (void) |
|
2249 { |
|
2250 string nm = curr_fcn_file_full_name; |
|
2251 |
|
2252 if (Vwarn_single_quote_string) |
|
2253 { |
|
2254 if (nm.empty ()) |
|
2255 warning ("single quote delimited string near line %d", |
|
2256 input_line_number); |
|
2257 else |
|
2258 warning ("single quote delimited string near line %d of file %s", |
|
2259 input_line_number, nm.c_str ()); |
|
2260 } |
|
2261 } |
|
2262 |
3388
|
2263 static int |
|
2264 warn_separator_insert (void) |
|
2265 { |
|
2266 Vwarn_separator_insert = check_preference ("warn_separator_insert"); |
|
2267 |
|
2268 return 0; |
|
2269 } |
|
2270 |
3246
|
2271 static int |
3400
|
2272 warn_single_quote_string (void) |
|
2273 { |
|
2274 Vwarn_single_quote_string = check_preference ("warn_single_quote_string"); |
|
2275 |
|
2276 return 0; |
|
2277 } |
|
2278 |
|
2279 static int |
2167
|
2280 whitespace_in_literal_matrix (void) |
|
2281 { |
|
2282 int pref = 0; |
3009
|
2283 |
2167
|
2284 string val = builtin_string_variable ("whitespace_in_literal_matrix"); |
3009
|
2285 |
2167
|
2286 if (! val.empty ()) |
|
2287 { |
|
2288 if (val.compare ("ignore", 0, 6) == 0) |
|
2289 pref = 2; |
|
2290 else if (val.compare ("traditional", 0, 11) == 0) |
|
2291 pref = 1; |
|
2292 } |
3009
|
2293 |
2167
|
2294 Vwhitespace_in_literal_matrix = pref; |
3009
|
2295 |
2167
|
2296 return 0; |
|
2297 } |
|
2298 |
|
2299 void |
|
2300 symbols_of_lex (void) |
|
2301 { |
3388
|
2302 DEFVAR (warn_separator_insert, 0.0, warn_separator_insert, |
3428
|
2303 "-*- texinfo -*-\n\ |
|
2304 @defvr {Built-in Variable} warn_separator_insert\n\ |
|
2305 Print warning if commas or semicolons that might be inserted\n\ |
|
2306 automatically in literal matrices.\n\ |
|
2307 @end defvr"); |
3388
|
2308 |
3400
|
2309 DEFVAR (warn_single_quote_string, 0.0, warn_single_quote_string, |
3428
|
2310 "-*- texinfo -*-\n\ |
|
2311 @defvr {Built-in Variable} warn_single_quote_string\n\ |
|
2312 Print warning if a signle quote character is used to introduce a\n\ |
|
2313 string constant.\n\ |
|
2314 @end defvr"); |
3400
|
2315 |
3258
|
2316 DEFVAR (whitespace_in_literal_matrix, "", whitespace_in_literal_matrix, |
3428
|
2317 "-*- texinfo -*-\n\ |
|
2318 @defvr {Built-in Variable} whitespace_in_literal_matrix\n\ |
|
2319 Control auto-insertion of commas and semicolons in literal matrices.\n\ |
|
2320 @end defvr"); |
|
2321 |
2167
|
2322 } |
|
2323 |
1994
|
2324 /* |
|
2325 ;;; Local Variables: *** |
|
2326 ;;; mode: C++ *** |
|
2327 ;;; End: *** |
|
2328 */ |