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