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