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