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 |
3575
|
858 // XXX FIXME XXX -- specify minimum match length for these. |
2804
|
859 static const char *plot_styles[] = |
146
|
860 { |
925
|
861 "boxes", |
924
|
862 "boxerrorbars", |
2542
|
863 "boxxyerrorbars", |
|
864 "candlesticks", |
146
|
865 "dots", |
|
866 "errorbars", |
2542
|
867 "financebars", |
|
868 "fsteps", |
|
869 "histeps", |
146
|
870 "impulses", |
|
871 "lines", |
|
872 "linespoints", |
|
873 "points", |
926
|
874 "steps", |
2542
|
875 "vector", |
|
876 "xerrorbars", |
|
877 "xyerrorbars", |
|
878 "yerrorbars", |
522
|
879 0, |
146
|
880 }; |
|
881 |
2804
|
882 const char * const *tmp = plot_styles; |
522
|
883 while (*tmp) |
1
|
884 { |
1823
|
885 if (almost_match (*tmp, s.c_str ())) |
|
886 { |
|
887 retval = *tmp; |
|
888 break; |
|
889 } |
1
|
890 |
|
891 tmp++; |
|
892 } |
|
893 |
1823
|
894 return retval; |
1
|
895 } |
|
896 |
3165
|
897 // Check to see if a character string matches any of the possible axes |
|
898 // tags for plots. |
|
899 |
3536
|
900 static std::string |
3523
|
901 plot_axes_token (const std::string& s) |
3165
|
902 { |
3523
|
903 std::string retval; |
3165
|
904 |
3575
|
905 // XXX FIXME XXX -- specify minimum match length for these. |
3561
|
906 static const char *plot_axes[] = |
3165
|
907 { |
|
908 "x1y1", |
|
909 "x1y2", |
|
910 "x2y1", |
|
911 "x2y2", |
|
912 0, |
|
913 }; |
|
914 |
3561
|
915 const char **tmp = plot_axes; |
3165
|
916 while (*tmp) |
|
917 { |
|
918 if (almost_match (*tmp, s.c_str ())) |
|
919 { |
|
920 retval = *tmp; |
|
921 break; |
|
922 } |
|
923 |
|
924 tmp++; |
|
925 } |
|
926 |
|
927 return retval; |
|
928 } |
|
929 |
767
|
930 // Check to see if a character string matches any one of the plot |
941
|
931 // option keywords. Don't match abbreviations for clear, since that's |
|
932 // not a gnuplot keyword (users will probably only expect to be able |
|
933 // to abbreviate actual gnuplot keywords). |
767
|
934 |
1
|
935 static int |
3523
|
936 is_plot_keyword (const std::string& s) |
1
|
937 { |
1823
|
938 const char *t = s.c_str (); |
3575
|
939 if (almost_match ("title", t, 1)) |
146
|
940 { |
|
941 return TITLE; |
|
942 } |
3575
|
943 else if (almost_match ("using", t, 1)) |
146
|
944 { |
2857
|
945 lexer_flags.in_plot_using = true; |
146
|
946 return USING; |
|
947 } |
3575
|
948 else if (almost_match ("with", t, 1)) |
146
|
949 { |
2857
|
950 lexer_flags.in_plot_style = true; |
146
|
951 return WITH; |
|
952 } |
3575
|
953 else if (almost_match ("axes", t, 2) || almost_match ("axis", t, 2)) |
3165
|
954 { |
|
955 lexer_flags.in_plot_axes = true; |
|
956 return AXES; |
|
957 } |
1823
|
958 else if (strcmp ("clear", t) == 0) |
883
|
959 { |
|
960 return CLEAR; |
|
961 } |
1
|
962 else |
146
|
963 { |
|
964 return 0; |
|
965 } |
1
|
966 } |
|
967 |
2877
|
968 // Handle keywords. |
767
|
969 |
1
|
970 static int |
3523
|
971 is_keyword (const std::string& s) |
1
|
972 { |
3165
|
973 if (lexer_flags.plotting) |
1
|
974 { |
3165
|
975 if (lexer_flags.in_plot_style) |
1
|
976 { |
3523
|
977 std::string sty = plot_style_token (s); |
3165
|
978 |
|
979 if (! sty.empty ()) |
|
980 { |
|
981 lexer_flags.in_plot_style = false; |
|
982 yylval.tok_val = new token (sty); |
|
983 token_stack.push (yylval.tok_val); |
|
984 return STYLE; |
|
985 } |
1
|
986 } |
3165
|
987 else if (lexer_flags.in_plot_axes) |
|
988 { |
3523
|
989 std::string axes = plot_axes_token (s); |
3165
|
990 |
|
991 if (! axes.empty ()) |
|
992 { |
|
993 lexer_flags.in_plot_axes = false; |
|
994 yylval.tok_val = new token (axes); |
|
995 token_stack.push (yylval.tok_val); |
|
996 return AXES_TAG; |
|
997 } |
|
998 } |
1
|
999 } |
|
1000 |
143
|
1001 int l = input_line_number; |
|
1002 int c = current_input_column; |
|
1003 |
1823
|
1004 int len = s.length (); |
922
|
1005 |
1823
|
1006 const octave_kw *kw = octave_kw_lookup (s.c_str (), len); |
191
|
1007 |
1497
|
1008 if (kw) |
143
|
1009 { |
1497
|
1010 yylval.tok_val = 0; |
|
1011 |
|
1012 switch (kw->kw_id) |
|
1013 { |
|
1014 case all_va_args_kw: |
|
1015 case break_kw: |
2764
|
1016 case case_kw: |
1497
|
1017 case catch_kw: |
|
1018 case continue_kw: |
|
1019 case else_kw: |
|
1020 case elseif_kw: |
|
1021 case global_kw: |
2764
|
1022 case otherwise_kw: |
1497
|
1023 case return_kw: |
2846
|
1024 case static_kw: |
3484
|
1025 case until_kw: |
1497
|
1026 case unwind_protect_cleanup_kw: |
|
1027 break; |
|
1028 |
|
1029 case end_kw: |
|
1030 yylval.tok_val = new token (token::simple_end, l, c); |
|
1031 break; |
|
1032 |
|
1033 case end_try_catch_kw: |
|
1034 yylval.tok_val = new token (token::try_catch_end, l, c); |
|
1035 break; |
|
1036 |
|
1037 case end_unwind_protect_kw: |
|
1038 yylval.tok_val = new token (token::unwind_protect_end, l, c); |
|
1039 break; |
|
1040 |
|
1041 case endfor_kw: |
|
1042 yylval.tok_val = new token (token::for_end, l, c); |
|
1043 break; |
|
1044 |
|
1045 case endfunction_kw: |
|
1046 yylval.tok_val = new token (token::function_end, l, c); |
|
1047 break; |
|
1048 |
|
1049 case endif_kw: |
|
1050 yylval.tok_val = new token (token::if_end, l, c); |
|
1051 break; |
|
1052 |
2764
|
1053 case endswitch_kw: |
|
1054 yylval.tok_val = new token (token::switch_end, l, c); |
|
1055 break; |
|
1056 |
1497
|
1057 case endwhile_kw: |
|
1058 yylval.tok_val = new token (token::while_end, l, c); |
|
1059 break; |
|
1060 |
3484
|
1061 case do_kw: |
1497
|
1062 case for_kw: |
|
1063 case while_kw: |
|
1064 promptflag--; |
1826
|
1065 lexer_flags.looping++; |
1497
|
1066 break; |
|
1067 |
|
1068 case if_kw: |
|
1069 case try_kw: |
2764
|
1070 case switch_kw: |
1497
|
1071 case unwind_protect_kw: |
|
1072 promptflag--; |
|
1073 break; |
|
1074 |
|
1075 case gplot_kw: |
2857
|
1076 lexer_flags.plotting = true; |
1497
|
1077 yylval.tok_val = new token (token::two_dee, l, c); |
|
1078 break; |
|
1079 |
|
1080 case gsplot_kw: |
2857
|
1081 lexer_flags.plotting = true; |
1497
|
1082 yylval.tok_val = new token (token::three_dee, l, c); |
|
1083 break; |
|
1084 |
|
1085 case replot_kw: |
2857
|
1086 lexer_flags.plotting = true; |
1497
|
1087 yylval.tok_val = new token (token::replot, l, c); |
|
1088 break; |
|
1089 |
|
1090 case function_kw: |
1826
|
1091 if (lexer_flags.defining_func) |
1497
|
1092 { |
|
1093 error ("function keyword invalid within a function body"); |
|
1094 |
|
1095 if ((reading_fcn_file || reading_script_file) |
1755
|
1096 && ! curr_fcn_file_name.empty ()) |
1497
|
1097 error ("defining new function near line %d of file `%s.m'", |
1755
|
1098 input_line_number, curr_fcn_file_name.c_str ()); |
1497
|
1099 else |
1755
|
1100 error ("defining new function near line %d", |
|
1101 input_line_number); |
1497
|
1102 |
|
1103 return LEXICAL_ERROR; |
|
1104 } |
|
1105 else |
|
1106 { |
2877
|
1107 // Prepare for local symbols. |
|
1108 |
1497
|
1109 tmp_local_sym_tab = new symbol_table (); |
2877
|
1110 |
|
1111 promptflag--; |
|
1112 |
2857
|
1113 lexer_flags.defining_func = true; |
2877
|
1114 lexer_flags.parsed_function_name = false; |
2857
|
1115 lexer_flags.beginning_of_function = true; |
2877
|
1116 |
1497
|
1117 if (! (reading_fcn_file || reading_script_file)) |
|
1118 input_line_number = 1; |
|
1119 } |
|
1120 break; |
|
1121 |
3174
|
1122 case magic_file_kw: |
|
1123 { |
|
1124 if ((reading_fcn_file || reading_script_file) |
|
1125 && ! curr_fcn_file_full_name.empty ()) |
|
1126 yylval.tok_val = new token (curr_fcn_file_full_name, l, c); |
|
1127 else |
|
1128 yylval.tok_val = new token ("stdin", l, c); |
|
1129 } |
|
1130 break; |
|
1131 |
|
1132 case magic_line_kw: |
|
1133 yylval.tok_val = new token (static_cast<double> (l), "", l, c); |
|
1134 break; |
|
1135 |
1497
|
1136 default: |
|
1137 panic_impossible (); |
|
1138 } |
|
1139 |
|
1140 if (! yylval.tok_val) |
|
1141 yylval.tok_val = new token (l, c); |
|
1142 |
476
|
1143 token_stack.push (yylval.tok_val); |
1497
|
1144 |
|
1145 return kw->tok; |
143
|
1146 } |
1
|
1147 |
|
1148 return 0; |
|
1149 } |
|
1150 |
767
|
1151 // Try to find an identifier. All binding to global or builtin |
|
1152 // variables occurs when expressions are evaluated. |
|
1153 |
1
|
1154 static symbol_record * |
3523
|
1155 lookup_identifier (const std::string& name) |
1
|
1156 { |
2856
|
1157 return curr_sym_tab->lookup (name, true); |
1
|
1158 } |
|
1159 |
2702
|
1160 static bool |
3523
|
1161 is_variable (const std::string& name) |
2702
|
1162 { |
2856
|
1163 symbol_record *sr = curr_sym_tab->lookup (name); |
2702
|
1164 |
|
1165 return sr && sr->is_variable (); |
|
1166 } |
|
1167 |
|
1168 static void |
3523
|
1169 force_local_variable (const std::string& name) |
2702
|
1170 { |
2856
|
1171 symbol_record *sr = curr_sym_tab->lookup (name, true); |
2702
|
1172 |
|
1173 if (sr) |
|
1174 sr->define (octave_value ()); |
|
1175 } |
|
1176 |
1019
|
1177 // Grab the help text from an function file. Always overwrites the |
|
1178 // current contents of help_buf. |
767
|
1179 |
3427
|
1180 // XXX FIXME XXX -- gobble_leading_white_space() in parse.y |
2300
|
1181 // duplicates some of this code! |
|
1182 |
1
|
1183 static void |
|
1184 grab_help_text (void) |
|
1185 { |
1755
|
1186 help_buf.resize (0); |
1019
|
1187 |
2300
|
1188 bool begin_comment = true; |
|
1189 bool in_comment = true; |
3427
|
1190 bool discard_space = true; |
1019
|
1191 int c = 0; |
1
|
1192 |
1019
|
1193 while ((c = yyinput ()) != EOF) |
|
1194 { |
2300
|
1195 if (begin_comment) |
|
1196 { |
|
1197 if (c == '%' || c == '#') |
|
1198 continue; |
3427
|
1199 else if (discard_space && c == ' ') |
|
1200 { |
|
1201 discard_space = false; |
|
1202 continue; |
|
1203 } |
2300
|
1204 else |
|
1205 begin_comment = false; |
|
1206 } |
|
1207 |
1019
|
1208 if (in_comment) |
1
|
1209 { |
1755
|
1210 help_buf += (char) c; |
|
1211 |
1019
|
1212 if (c == '\n') |
3427
|
1213 { |
|
1214 in_comment = false; |
|
1215 discard_space = true; |
|
1216 } |
1019
|
1217 } |
|
1218 else |
|
1219 { |
|
1220 switch (c) |
991
|
1221 { |
1019
|
1222 case '%': |
|
1223 case '#': |
2300
|
1224 in_comment = true; |
|
1225 begin_comment = true; |
1019
|
1226 break; |
777
|
1227 |
1019
|
1228 case ' ': |
|
1229 case '\t': |
|
1230 break; |
777
|
1231 |
1019
|
1232 default: |
|
1233 goto done; |
1
|
1234 } |
|
1235 } |
1019
|
1236 } |
991
|
1237 |
1019
|
1238 done: |
991
|
1239 |
1019
|
1240 if (c) |
3246
|
1241 unput (c); |
1
|
1242 } |
|
1243 |
767
|
1244 // Return 1 if the given character matches any character in the given |
|
1245 // string. |
|
1246 |
2857
|
1247 static bool |
2804
|
1248 match_any (char c, const char *s) |
1
|
1249 { |
|
1250 char tmp; |
|
1251 while ((tmp = *s++) != '\0') |
|
1252 { |
|
1253 if (c == tmp) |
2857
|
1254 return true; |
1
|
1255 } |
2857
|
1256 return false; |
1
|
1257 } |
|
1258 |
767
|
1259 // Given information about the spacing surrounding an operator, |
|
1260 // return 1 if it looks like it should be treated as a binary |
|
1261 // operator. For example, |
|
1262 // |
|
1263 // [ 1 + 2 ] or [ 1+ 2] or [ 1+2 ] ==> binary |
|
1264 |
2857
|
1265 static bool |
3246
|
1266 looks_like_bin_op (bool spc_prev, int next_char) |
1
|
1267 { |
3246
|
1268 bool spc_next = (next_char == ' ' || next_char == '\t'); |
|
1269 |
608
|
1270 return ((spc_prev && spc_next) || ! spc_prev); |
1
|
1271 } |
|
1272 |
3263
|
1273 // Recognize separators. If the separator is a CRLF pair, it is |
|
1274 // replaced by a single LF. |
|
1275 |
|
1276 static bool |
|
1277 next_token_is_sep_op (void) |
|
1278 { |
|
1279 bool retval = false; |
|
1280 |
|
1281 int c1 = yyinput (); |
|
1282 |
|
1283 if (c1 == '\r') |
|
1284 { |
|
1285 int c2 = yyinput (); |
|
1286 |
|
1287 if (c2 == '\n') |
|
1288 { |
|
1289 c1 = '\n'; |
|
1290 |
|
1291 retval = true; |
|
1292 } |
|
1293 else |
|
1294 unput (c2); |
|
1295 } |
|
1296 else |
|
1297 retval = match_any (c1, ",;\n]"); |
|
1298 |
|
1299 unput (c1); |
|
1300 |
|
1301 return retval; |
|
1302 } |
|
1303 |
767
|
1304 // Try to determine if the next token should be treated as a postfix |
|
1305 // unary operator. This is ugly, but it seems to do the right thing. |
|
1306 |
2857
|
1307 static bool |
3246
|
1308 next_token_is_postfix_unary_op (bool spc_prev) |
1
|
1309 { |
2857
|
1310 bool un_op = false; |
1
|
1311 |
|
1312 int c0 = yyinput (); |
|
1313 |
3246
|
1314 if (c0 == '\'' && ! spc_prev) |
|
1315 { |
|
1316 un_op = true; |
|
1317 } |
|
1318 else if (c0 == '.') |
|
1319 { |
|
1320 int c1 = yyinput (); |
|
1321 un_op = (c1 == '\''); |
|
1322 unput (c1); |
|
1323 } |
1
|
1324 |
3246
|
1325 unput (c0); |
1
|
1326 |
|
1327 return un_op; |
|
1328 } |
|
1329 |
767
|
1330 // Try to determine if the next token should be treated as a binary |
3246
|
1331 // operator. |
1521
|
1332 // |
3246
|
1333 // This kluge exists because whitespace is not always ignored inside |
|
1334 // the square brackets that are used to create matrix objects. |
1521
|
1335 // |
3246
|
1336 // Line continuations directly after the operator will cause this |
|
1337 // function to return FALSE. |
767
|
1338 |
2857
|
1339 static bool |
3246
|
1340 next_token_is_bin_op (bool spc_prev) |
1
|
1341 { |
2857
|
1342 bool bin_op = false; |
1
|
1343 |
|
1344 int c0 = yyinput (); |
|
1345 |
|
1346 switch (c0) |
|
1347 { |
3246
|
1348 case ':': |
777
|
1349 case '+': |
|
1350 case '-': |
3246
|
1351 case '/': |
|
1352 case '\\': |
|
1353 case '^': |
1276
|
1354 { |
|
1355 int c1 = yyinput (); |
3246
|
1356 bin_op = looks_like_bin_op (spc_prev, c1); |
|
1357 unput (c1); |
1276
|
1358 } |
|
1359 break; |
|
1360 |
3246
|
1361 // .+ .- ./ .\ .^ .* .** |
1554
|
1362 case '.': |
|
1363 { |
|
1364 int c1 = yyinput (); |
3246
|
1365 |
|
1366 if (match_any (c1, "+-/\\^")) |
|
1367 { |
|
1368 int c2 = yyinput (); |
|
1369 bin_op = looks_like_bin_op (spc_prev, c2); |
|
1370 unput (c2); |
|
1371 } |
|
1372 else if (c1 == '*') |
|
1373 { |
|
1374 int c2 = yyinput (); |
|
1375 |
|
1376 if (c2 == '*') |
|
1377 { |
|
1378 int c3 = yyinput (); |
|
1379 bin_op = looks_like_bin_op (spc_prev, c3); |
|
1380 unput (c3); |
|
1381 } |
|
1382 else |
|
1383 bin_op = looks_like_bin_op (spc_prev, c2); |
|
1384 |
|
1385 unput (c2); |
|
1386 } |
|
1387 else if (! isdigit (c1) && c1 != ' ' && c1 != '\t') |
|
1388 { |
|
1389 bin_op = true; |
|
1390 } |
|
1391 |
|
1392 unput (c1); |
1554
|
1393 } |
|
1394 break; |
|
1395 |
3246
|
1396 // = == & && | || * ** |
|
1397 case '=': |
1
|
1398 case '&': |
3246
|
1399 case '|': |
1
|
1400 case '*': |
3246
|
1401 { |
|
1402 int c1 = yyinput (); |
|
1403 |
|
1404 if (c1 == c0) |
|
1405 { |
|
1406 int c2 = yyinput (); |
|
1407 bin_op = looks_like_bin_op (spc_prev, c2); |
|
1408 unput (c2); |
|
1409 } |
|
1410 else |
|
1411 bin_op = looks_like_bin_op (spc_prev, c1); |
|
1412 |
|
1413 unput (c1); |
|
1414 } |
|
1415 break; |
|
1416 |
|
1417 // <= >= <> ~= != < > |
1
|
1418 case '<': |
|
1419 case '>': |
777
|
1420 case '~': |
|
1421 case '!': |
3246
|
1422 { |
|
1423 int c1 = yyinput (); |
|
1424 |
|
1425 if ((c1 == '=') || (c1 == '<' && c1 == '>')) |
|
1426 { |
|
1427 int c2 = yyinput (); |
|
1428 bin_op = looks_like_bin_op (spc_prev, c2); |
|
1429 unput (c2); |
|
1430 } |
|
1431 else if (c1 != '~' && c1 != '!') |
|
1432 bin_op = looks_like_bin_op (spc_prev, c1); |
|
1433 |
|
1434 unput (c1); |
|
1435 } |
1
|
1436 break; |
|
1437 |
|
1438 default: |
1276
|
1439 break; |
1
|
1440 } |
|
1441 |
3246
|
1442 unput (c0); |
1
|
1443 |
|
1444 return bin_op; |
|
1445 } |
|
1446 |
767
|
1447 // Used to delete trailing white space from tokens. |
|
1448 |
3536
|
1449 static std::string |
1
|
1450 strip_trailing_whitespace (char *s) |
|
1451 { |
3523
|
1452 std::string retval = s; |
1
|
1453 |
1823
|
1454 size_t pos = retval.find_first_of (" \t"); |
1
|
1455 |
1823
|
1456 if (pos != NPOS) |
|
1457 retval.resize (pos); |
1
|
1458 |
|
1459 return retval; |
|
1460 } |
|
1461 |
1001
|
1462 // Discard whitespace, including comments and continuations. |
1088
|
1463 // |
|
1464 // Return value is logical OR of the following values: |
|
1465 // |
1826
|
1466 // ATE_NOTHING : no spaces to eat |
1088
|
1467 // ATE_SPACE_OR_TAB : space or tab in input |
|
1468 // ATE_NEWLINE : bare new line in input |
1001
|
1469 |
1826
|
1470 static yum_yum |
975
|
1471 eat_whitespace (void) |
|
1472 { |
1826
|
1473 yum_yum retval = ATE_NOTHING; |
2857
|
1474 bool in_comment = false; |
975
|
1475 int c; |
|
1476 while ((c = yyinput ()) != EOF) |
|
1477 { |
|
1478 current_input_column++; |
|
1479 |
|
1480 switch (c) |
|
1481 { |
|
1482 case ' ': |
|
1483 case '\t': |
1088
|
1484 retval |= ATE_SPACE_OR_TAB; |
975
|
1485 break; |
|
1486 |
|
1487 case '\n': |
1088
|
1488 retval |= ATE_NEWLINE; |
2857
|
1489 in_comment = false; |
975
|
1490 current_input_column = 0; |
|
1491 break; |
|
1492 |
|
1493 case '#': |
|
1494 case '%': |
2857
|
1495 in_comment = true; |
975
|
1496 break; |
|
1497 |
1001
|
1498 case '.': |
|
1499 if (in_comment) |
|
1500 break; |
|
1501 else |
|
1502 { |
|
1503 if (have_ellipsis_continuation ()) |
|
1504 break; |
|
1505 else |
|
1506 goto done; |
|
1507 } |
|
1508 |
|
1509 case '\\': |
|
1510 if (in_comment) |
|
1511 break; |
|
1512 else |
|
1513 { |
3105
|
1514 if (have_continuation ()) |
1001
|
1515 break; |
|
1516 else |
|
1517 goto done; |
|
1518 } |
|
1519 |
975
|
1520 default: |
|
1521 if (in_comment) |
|
1522 break; |
|
1523 else |
|
1524 goto done; |
|
1525 } |
|
1526 } |
|
1527 |
|
1528 done: |
3246
|
1529 unput (c); |
1082
|
1530 current_input_column--; |
1001
|
1531 return retval; |
975
|
1532 } |
|
1533 |
3220
|
1534 static inline bool |
|
1535 looks_like_hex (const char *s, int len) |
|
1536 { |
|
1537 return (len > 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X')); |
|
1538 } |
|
1539 |
975
|
1540 static void |
3246
|
1541 handle_number (void) |
972
|
1542 { |
2621
|
1543 char *tmp = strsave (yytext); |
|
1544 |
|
1545 char *idx = strpbrk (tmp, "Dd"); |
|
1546 |
|
1547 if (idx) |
|
1548 *idx = 'e'; |
|
1549 |
3220
|
1550 double value = 0.0; |
|
1551 int nread = 0; |
|
1552 |
|
1553 if (looks_like_hex (tmp, strlen (tmp))) |
|
1554 { |
|
1555 unsigned long ival; |
|
1556 nread = sscanf (tmp, "%lx", &ival); |
|
1557 value = static_cast<double> (ival); |
|
1558 } |
|
1559 else |
|
1560 nread = sscanf (tmp, "%lf", &value); |
2621
|
1561 |
|
1562 delete [] tmp; |
972
|
1563 |
1826
|
1564 // If yytext doesn't contain a valid number, we are in deep doo doo. |
985
|
1565 |
972
|
1566 assert (nread == 1); |
|
1567 |
1826
|
1568 lexer_flags.quote_is_transpose = 1; |
|
1569 lexer_flags.cant_be_identifier = 1; |
|
1570 lexer_flags.convert_spaces_to_comma = 1; |
972
|
1571 |
1826
|
1572 if (lexer_flags.plotting && ! lexer_flags.in_plot_range) |
3575
|
1573 lexer_flags.past_plot_range = true; |
972
|
1574 |
|
1575 yylval.tok_val = new token (value, yytext, input_line_number, |
|
1576 current_input_column); |
|
1577 |
|
1578 token_stack.push (yylval.tok_val); |
|
1579 |
|
1580 current_input_column += yyleng; |
|
1581 |
|
1582 do_comma_insert_check (); |
|
1583 } |
|
1584 |
1001
|
1585 // We have seen a backslash and need to find out if it should be |
|
1586 // treated as a continuation character. If so, this eats it, up to |
|
1587 // and including the new line character. |
|
1588 // |
973
|
1589 // Match whitespace only, followed by a comment character or newline. |
|
1590 // Once a comment character is found, discard all input until newline. |
|
1591 // If non-whitespace characters are found before comment |
|
1592 // characters, return 0. Otherwise, return 1. |
|
1593 |
2857
|
1594 static bool |
3096
|
1595 have_continuation (bool trailing_comments_ok) |
973
|
1596 { |
3523
|
1597 std::ostrstream buf; |
973
|
1598 |
2857
|
1599 bool in_comment = false; |
3332
|
1600 int c; |
973
|
1601 while ((c = yyinput ()) != EOF) |
|
1602 { |
|
1603 buf << (char) c; |
|
1604 |
|
1605 switch (c) |
|
1606 { |
|
1607 case ' ': |
|
1608 case '\t': |
|
1609 break; |
|
1610 |
|
1611 case '%': |
|
1612 case '#': |
1091
|
1613 if (trailing_comments_ok) |
2857
|
1614 in_comment = true; |
1091
|
1615 else |
|
1616 goto cleanup; |
973
|
1617 break; |
|
1618 |
|
1619 case '\n': |
975
|
1620 current_input_column = 0; |
1001
|
1621 promptflag--; |
2857
|
1622 return true; |
973
|
1623 |
3263
|
1624 case '\r': |
|
1625 c = yyinput (); |
|
1626 if (c == EOF) |
|
1627 break; |
|
1628 else if (c == '\n') |
|
1629 { |
|
1630 current_input_column = 0; |
|
1631 promptflag--; |
|
1632 return true; |
|
1633 } |
|
1634 |
973
|
1635 default: |
1091
|
1636 if (! in_comment) |
|
1637 goto cleanup; |
|
1638 break; |
973
|
1639 } |
|
1640 } |
|
1641 |
3246
|
1642 unput (c); |
2857
|
1643 return false; |
973
|
1644 |
3096
|
1645 cleanup: |
3538
|
1646 buf << std::ends; |
1091
|
1647 char *s = buf.str (); |
|
1648 if (s) |
|
1649 { |
|
1650 int len = strlen (s); |
|
1651 while (len--) |
3246
|
1652 unput (s[len]); |
1091
|
1653 } |
|
1654 delete [] s; |
3096
|
1655 |
2857
|
1656 return false; |
973
|
1657 } |
|
1658 |
1001
|
1659 // We have seen a `.' and need to see if it is the start of a |
|
1660 // continuation. If so, this eats it, up to and including the new |
|
1661 // line character. |
|
1662 |
2857
|
1663 static bool |
3096
|
1664 have_ellipsis_continuation (bool trailing_comments_ok) |
973
|
1665 { |
|
1666 char c1 = yyinput (); |
|
1667 if (c1 == '.') |
|
1668 { |
|
1669 char c2 = yyinput (); |
1091
|
1670 if (c2 == '.' && have_continuation (trailing_comments_ok)) |
2857
|
1671 return true; |
973
|
1672 else |
|
1673 { |
3246
|
1674 unput (c2); |
|
1675 unput (c1); |
973
|
1676 } |
|
1677 } |
|
1678 else |
3246
|
1679 unput (c1); |
973
|
1680 |
2857
|
1681 return false; |
973
|
1682 } |
|
1683 |
1001
|
1684 // See if we have a continuation line. If so, eat it and the leading |
|
1685 // whitespace on the next line. |
1088
|
1686 // |
|
1687 // Return value is the same as described for eat_whitespace(). |
1001
|
1688 |
1826
|
1689 static yum_yum |
1001
|
1690 eat_continuation (void) |
|
1691 { |
1826
|
1692 int retval = ATE_NOTHING; |
1001
|
1693 int c = yyinput (); |
|
1694 if ((c == '.' && have_ellipsis_continuation ()) |
3105
|
1695 || (c == '\\' && have_continuation ())) |
1001
|
1696 retval = eat_whitespace (); |
|
1697 else |
3246
|
1698 unput (c); |
1001
|
1699 |
|
1700 return retval; |
|
1701 } |
|
1702 |
973
|
1703 static int |
975
|
1704 handle_string (char delim, int text_style) |
973
|
1705 { |
3523
|
1706 std::ostrstream buf; |
973
|
1707 |
|
1708 int c; |
1031
|
1709 int escape_pending = 0; |
973
|
1710 |
|
1711 while ((c = yyinput ()) != EOF) |
|
1712 { |
|
1713 current_input_column++; |
|
1714 |
3105
|
1715 if (c == '\\') |
973
|
1716 { |
1053
|
1717 if (escape_pending) |
|
1718 { |
|
1719 buf << (char) c; |
|
1720 escape_pending = 0; |
|
1721 } |
|
1722 else |
|
1723 { |
3096
|
1724 if (have_continuation (false)) |
1053
|
1725 escape_pending = 0; |
|
1726 else |
|
1727 { |
|
1728 buf << (char) c; |
|
1729 escape_pending = 1; |
|
1730 } |
|
1731 } |
1031
|
1732 continue; |
973
|
1733 } |
|
1734 else if (c == '.') |
|
1735 { |
3096
|
1736 if (! have_ellipsis_continuation (false)) |
973
|
1737 buf << (char) c; |
|
1738 } |
|
1739 else if (c == '\n') |
|
1740 { |
1053
|
1741 error ("unterminated string constant"); |
973
|
1742 break; |
|
1743 } |
|
1744 else if (c == delim) |
|
1745 { |
1031
|
1746 if (escape_pending) |
973
|
1747 buf << (char) c; |
|
1748 else |
|
1749 { |
|
1750 c = yyinput (); |
|
1751 if (c == delim) |
|
1752 buf << (char) c; |
|
1753 else |
|
1754 { |
3246
|
1755 unput (c); |
3538
|
1756 buf << std::ends; |
3105
|
1757 char *t = buf.str (); |
3523
|
1758 std::string s = do_string_escapes (t); |
3105
|
1759 delete [] t; |
975
|
1760 |
1826
|
1761 if (text_style && lexer_flags.doing_set) |
975
|
1762 { |
3523
|
1763 s = std::string (1, delim) + s + std::string (1, delim); |
975
|
1764 } |
|
1765 else |
|
1766 { |
2857
|
1767 lexer_flags.quote_is_transpose = true; |
|
1768 lexer_flags.cant_be_identifier = true; |
|
1769 lexer_flags.convert_spaces_to_comma = true; |
975
|
1770 } |
|
1771 |
3105
|
1772 yylval.tok_val = new token (s); |
973
|
1773 token_stack.push (yylval.tok_val); |
3400
|
1774 |
|
1775 if (delim == '\'') |
|
1776 gripe_single_quote_string (); |
|
1777 |
973
|
1778 return TEXT; |
|
1779 } |
|
1780 } |
|
1781 } |
|
1782 else |
|
1783 { |
|
1784 buf << (char) c; |
|
1785 } |
|
1786 |
1031
|
1787 escape_pending = 0; |
973
|
1788 } |
|
1789 |
|
1790 return LEXICAL_ERROR; |
|
1791 } |
|
1792 |
3208
|
1793 static bool |
|
1794 next_token_is_assign_op (void) |
|
1795 { |
|
1796 bool retval = false; |
|
1797 |
|
1798 int c0 = yyinput (); |
|
1799 |
|
1800 switch (c0) |
|
1801 { |
|
1802 case '=': |
|
1803 { |
|
1804 int c1 = yyinput (); |
|
1805 unput (c1); |
|
1806 if (c1 != '=') |
|
1807 retval = true; |
|
1808 } |
|
1809 break; |
|
1810 |
|
1811 case '+': |
|
1812 case '-': |
|
1813 case '*': |
|
1814 case '/': |
|
1815 case '\\': |
|
1816 case '&': |
|
1817 case '|': |
|
1818 { |
|
1819 int c1 = yyinput (); |
|
1820 unput (c1); |
|
1821 if (c1 == '=') |
|
1822 retval = true; |
|
1823 } |
|
1824 break; |
|
1825 |
|
1826 case '.': |
|
1827 { |
|
1828 int c1 = yyinput (); |
|
1829 if (match_any (c1, "+-*/\\")) |
|
1830 { |
|
1831 int c2 = yyinput (); |
|
1832 unput (c2); |
|
1833 if (c2 == '=') |
|
1834 retval = true; |
|
1835 } |
|
1836 unput (c1); |
|
1837 } |
|
1838 break; |
|
1839 |
|
1840 case '>': |
|
1841 { |
|
1842 int c1 = yyinput (); |
|
1843 if (c1 == '>') |
|
1844 { |
|
1845 int c2 = yyinput (); |
|
1846 unput (c2); |
|
1847 if (c2 == '=') |
|
1848 retval = true; |
|
1849 } |
|
1850 unput (c1); |
|
1851 } |
|
1852 break; |
|
1853 |
|
1854 case '<': |
|
1855 { |
|
1856 int c1 = yyinput (); |
|
1857 if (c1 == '<') |
|
1858 { |
|
1859 int c2 = yyinput (); |
|
1860 unput (c2); |
|
1861 if (c2 == '=') |
|
1862 retval = true; |
|
1863 } |
|
1864 unput (c1); |
|
1865 } |
|
1866 break; |
|
1867 |
|
1868 default: |
|
1869 break; |
|
1870 } |
|
1871 |
|
1872 unput (c0); |
|
1873 |
|
1874 return retval; |
|
1875 } |
|
1876 |
971
|
1877 static int |
3351
|
1878 handle_close_bracket (int spc_gobbled) |
971
|
1879 { |
3208
|
1880 int retval = ']'; |
|
1881 |
1826
|
1882 if (! nesting_level.none ()) |
971
|
1883 { |
1826
|
1884 nesting_level.remove (); |
3351
|
1885 lexer_flags.bracketflag--; |
971
|
1886 } |
|
1887 |
3351
|
1888 if (lexer_flags.bracketflag == 0) |
1001
|
1889 BEGIN 0; |
|
1890 |
3208
|
1891 if (next_token_is_assign_op () && ! lexer_flags.looking_at_return_list) |
971
|
1892 { |
3208
|
1893 retval = CLOSE_BRACE; |
971
|
1894 } |
|
1895 else |
|
1896 { |
3208
|
1897 int c1 = yyinput (); |
971
|
1898 unput (c1); |
|
1899 |
3388
|
1900 if (lexer_flags.bracketflag) |
971
|
1901 { |
3246
|
1902 int bin_op = next_token_is_bin_op (spc_gobbled); |
3263
|
1903 |
3246
|
1904 int postfix_un_op = next_token_is_postfix_unary_op (spc_gobbled); |
971
|
1905 |
3263
|
1906 int sep_op = next_token_is_sep_op (); |
971
|
1907 |
3263
|
1908 if (! (postfix_un_op || bin_op || sep_op) |
3351
|
1909 && nesting_level.is_bracket () |
1826
|
1910 && lexer_flags.convert_spaces_to_comma) |
971
|
1911 { |
3388
|
1912 maybe_warn_separator_insert (','); |
|
1913 |
|
1914 if (Vwhitespace_in_literal_matrix != 2) |
|
1915 { |
|
1916 unput (','); |
|
1917 return ']'; |
|
1918 } |
971
|
1919 } |
|
1920 } |
|
1921 } |
|
1922 |
2857
|
1923 lexer_flags.quote_is_transpose = true; |
|
1924 lexer_flags.cant_be_identifier = false; |
|
1925 lexer_flags.convert_spaces_to_comma = true; |
3208
|
1926 |
|
1927 return retval; |
971
|
1928 } |
|
1929 |
1072
|
1930 static void |
|
1931 maybe_unput_comma (int spc_gobbled) |
|
1932 { |
3388
|
1933 if (nesting_level.is_bracket ()) |
1072
|
1934 { |
3246
|
1935 int bin_op = next_token_is_bin_op (spc_gobbled); |
1072
|
1936 |
3246
|
1937 int postfix_un_op = next_token_is_postfix_unary_op (spc_gobbled); |
1072
|
1938 |
|
1939 int c1 = yyinput (); |
|
1940 int c2 = yyinput (); |
2970
|
1941 |
1072
|
1942 unput (c2); |
|
1943 unput (c1); |
2970
|
1944 |
3263
|
1945 int sep_op = next_token_is_sep_op (); |
2970
|
1946 |
1072
|
1947 int dot_op = (c1 == '.' |
|
1948 && (isalpha (c2) || isspace (c2) || c2 == '_')); |
2970
|
1949 |
3388
|
1950 if (postfix_un_op || bin_op || sep_op || dot_op) |
|
1951 return; |
|
1952 |
|
1953 int index_op = (c1 == '('); |
|
1954 |
|
1955 if (index_op) |
|
1956 { |
|
1957 // If there is no space before the '(', we don't insert a comma. |
|
1958 if (! spc_gobbled) |
|
1959 return; |
|
1960 |
|
1961 maybe_warn_separator_insert (','); |
1072
|
1962 |
3388
|
1963 // If there is a space, we only insert a comma if we are |
|
1964 // trying to be Matlab-like. |
|
1965 if (Vwhitespace_in_literal_matrix == 1) |
|
1966 unput (','); |
|
1967 } |
|
1968 else |
|
1969 { |
|
1970 maybe_warn_separator_insert (','); |
|
1971 |
|
1972 if (Vwhitespace_in_literal_matrix != 2) |
|
1973 unput (','); |
|
1974 } |
1072
|
1975 } |
|
1976 } |
|
1977 |
767
|
1978 // Figure out exactly what kind of token to return when we have seen |
|
1979 // an identifier. Handles keywords. |
|
1980 |
146
|
1981 static int |
3523
|
1982 handle_identifier (const std::string& tok, int spc_gobbled) |
146
|
1983 { |
1826
|
1984 // It is almost always an error for an identifier to be followed |
|
1985 // directly by another identifier. Special cases are handled |
|
1986 // below. |
883
|
1987 |
2857
|
1988 lexer_flags.cant_be_identifier = true; |
883
|
1989 |
2970
|
1990 // If we are expecting a structure element, avoid recognizing |
|
1991 // keywords and other special names and return STRUCT_ELT, which is |
|
1992 // a string that is also a valid identifier. But first, we have to |
|
1993 // decide whether to insert a comma. |
747
|
1994 |
1826
|
1995 if (lexer_flags.looking_at_indirect_ref) |
1072
|
1996 { |
2970
|
1997 do_comma_insert_check (); |
|
1998 |
1072
|
1999 maybe_unput_comma (spc_gobbled); |
2819
|
2000 |
|
2001 yylval.tok_val = new token (tok, input_line_number, |
|
2002 current_input_column); |
|
2003 |
|
2004 token_stack.push (yylval.tok_val); |
|
2005 |
2857
|
2006 lexer_flags.cant_be_identifier = false; |
|
2007 lexer_flags.quote_is_transpose = true; |
|
2008 lexer_flags.convert_spaces_to_comma = true; |
2819
|
2009 |
|
2010 current_input_column += yyleng; |
|
2011 |
2970
|
2012 return STRUCT_ELT; |
1072
|
2013 } |
747
|
2014 |
1826
|
2015 // If we have a regular keyword, or a plot STYLE, return it. |
|
2016 // Keywords can be followed by identifiers (TOK_RETURN handles |
|
2017 // that). |
146
|
2018 |
|
2019 int kw_token = is_keyword (tok); |
2970
|
2020 |
146
|
2021 if (kw_token) |
|
2022 { |
|
2023 if (kw_token == STYLE) |
|
2024 { |
1060
|
2025 current_input_column += yyleng; |
2857
|
2026 lexer_flags.quote_is_transpose = false; |
|
2027 lexer_flags.convert_spaces_to_comma = true; |
146
|
2028 return kw_token; |
|
2029 } |
|
2030 else |
|
2031 TOK_RETURN (kw_token); |
|
2032 } |
|
2033 |
1826
|
2034 // See if we have a plot keyword (title, using, with, or clear). |
146
|
2035 |
1826
|
2036 if (lexer_flags.plotting) |
941
|
2037 { |
1826
|
2038 // Yes, we really do need both of these plot_range variables. |
|
2039 // One is used to mark when we are past all possiblity of a plot |
|
2040 // range, the other is used to mark when we are actually between |
|
2041 // the square brackets that surround the range. |
146
|
2042 |
1826
|
2043 if (! lexer_flags.in_plot_range) |
2857
|
2044 lexer_flags.past_plot_range = true; |
941
|
2045 |
3351
|
2046 // Option keywords can't appear in brackets, braces, or parentheses. |
1273
|
2047 |
|
2048 int plot_option_kw = 0; |
2970
|
2049 |
1826
|
2050 if (nesting_level.none ()) |
1273
|
2051 plot_option_kw = is_plot_keyword (tok); |
3575
|
2052 |
1826
|
2053 if (lexer_flags.cant_be_identifier && plot_option_kw) |
941
|
2054 TOK_RETURN (plot_option_kw); |
|
2055 } |
146
|
2056 |
3480
|
2057 int c1 = yyinput (); |
|
2058 |
|
2059 bool next_tok_is_dot = (c1 == '.'); |
|
2060 bool next_tok_is_paren = (c1 == '('); |
|
2061 |
|
2062 bool next_tok_is_eq = false; |
|
2063 if (c1 == '=') |
|
2064 { |
|
2065 int c2 = yyinput (); |
|
2066 unput (c2); |
|
2067 |
|
2068 if (c2 != '=') |
|
2069 next_tok_is_eq = true; |
|
2070 } |
|
2071 |
|
2072 unput (c1); |
1001
|
2073 |
1826
|
2074 // Make sure we put the return values of a function in the symbol |
|
2075 // table that is local to the function. |
146
|
2076 |
2877
|
2077 // If we are defining a function and we have not seen the function |
|
2078 // name yet and the next token is `=', then this identifier must be |
|
2079 // the only return value for the function and it belongs in the |
|
2080 // local symbol table. |
|
2081 |
1826
|
2082 if (next_tok_is_eq |
2877
|
2083 && lexer_flags.defining_func |
|
2084 && ! lexer_flags.parsed_function_name) |
146
|
2085 curr_sym_tab = tmp_local_sym_tab; |
|
2086 |
2702
|
2087 // Kluge alert. |
|
2088 // |
|
2089 // If we are looking at a text style function, set up to gobble its |
2745
|
2090 // arguments. |
|
2091 // |
|
2092 // If the following token is `=', or if we are parsing a function |
3189
|
2093 // return list or function parameter list, or if we are looking at |
|
2094 // something like [ab,cd] = foo (), force the symbol to be inserted |
|
2095 // as a variable in the current symbol table. |
2702
|
2096 |
|
2097 if (is_text_function_name (tok) && ! is_variable (tok)) |
|
2098 { |
2745
|
2099 if (next_tok_is_eq |
|
2100 || lexer_flags.looking_at_return_list |
3189
|
2101 || lexer_flags.looking_at_parameter_list |
3246
|
2102 || lexer_flags.looking_at_matrix_or_assign_lhs |
|
2103 || (next_tok_is_dot && next_token_is_bin_op (spc_gobbled))) |
2745
|
2104 { |
|
2105 force_local_variable (tok); |
|
2106 } |
2702
|
2107 else if (! next_tok_is_paren) |
|
2108 { |
|
2109 if (tok == "gset") |
2857
|
2110 lexer_flags.doing_set = true; |
2702
|
2111 |
|
2112 BEGIN TEXT_FCN; |
|
2113 } |
|
2114 } |
|
2115 |
1826
|
2116 // Find the token in the symbol table. |
146
|
2117 |
|
2118 yylval.tok_val = new token (lookup_identifier (tok), |
|
2119 input_line_number, |
|
2120 current_input_column); |
|
2121 |
|
2122 token_stack.push (yylval.tok_val); |
|
2123 |
1826
|
2124 // After seeing an identifer, it is ok to convert spaces to a comma |
|
2125 // (if needed). |
146
|
2126 |
2857
|
2127 lexer_flags.convert_spaces_to_comma = true; |
146
|
2128 |
2877
|
2129 if (! next_tok_is_eq) |
|
2130 { |
|
2131 lexer_flags.quote_is_transpose = true; |
146
|
2132 |
2877
|
2133 do_comma_insert_check (); |
|
2134 |
|
2135 maybe_unput_comma (spc_gobbled); |
146
|
2136 } |
|
2137 |
2877
|
2138 current_input_column += yyleng; |
146
|
2139 |
|
2140 return NAME; |
|
2141 } |
|
2142 |
767
|
2143 // Print a warning if a function file that defines a function has |
|
2144 // anything other than comments and whitespace following the END token |
|
2145 // that matches the FUNCTION statement. |
|
2146 |
1
|
2147 void |
|
2148 check_for_garbage_after_fcn_def (void) |
|
2149 { |
1826
|
2150 // By making a newline be the next character to be read, we will |
|
2151 // force the parser to return after reading the function. Calling |
3246
|
2152 // unput with EOF does not work. |
1
|
2153 |
2857
|
2154 bool in_comment = false; |
1
|
2155 int lineno = input_line_number; |
|
2156 int c; |
|
2157 while ((c = yyinput ()) != EOF) |
|
2158 { |
|
2159 switch (c) |
|
2160 { |
|
2161 case ' ': |
|
2162 case '\t': |
|
2163 case ';': |
|
2164 case ',': |
|
2165 break; |
777
|
2166 |
1
|
2167 case '\n': |
|
2168 if (in_comment) |
2857
|
2169 in_comment = false; |
1
|
2170 break; |
777
|
2171 |
1
|
2172 case '%': |
|
2173 case '#': |
2857
|
2174 in_comment = true; |
1
|
2175 break; |
777
|
2176 |
1
|
2177 default: |
|
2178 if (in_comment) |
|
2179 break; |
|
2180 else |
|
2181 { |
|
2182 warning ("ignoring trailing garbage after end of function\n\ |
1755
|
2183 near line %d of file `%s.m'", lineno, curr_fcn_file_name.c_str ()); |
1
|
2184 |
3246
|
2185 unput ('\n'); |
1
|
2186 return; |
|
2187 } |
|
2188 } |
|
2189 } |
3246
|
2190 unput ('\n'); |
1
|
2191 } |
|
2192 |
1826
|
2193 void |
|
2194 lexical_feedback::init (void) |
|
2195 { |
|
2196 // Not initially defining a matrix list. |
3351
|
2197 bracketflag = 0; |
1826
|
2198 |
|
2199 // Not initially inside a loop or if statement. |
|
2200 looping = 0; |
|
2201 |
2857
|
2202 // Not initially defining a function. |
|
2203 beginning_of_function = false; |
|
2204 defining_func = false; |
2877
|
2205 parsed_function_name = false; |
2857
|
2206 |
|
2207 // Not parsing a function return or parameter list. |
|
2208 looking_at_return_list = false; |
|
2209 looking_at_parameter_list = false; |
|
2210 |
|
2211 // Next token can be identifier. |
|
2212 cant_be_identifier = false; |
|
2213 |
|
2214 // No need to do comma insert or convert spaces to comma at |
|
2215 // beginning of input. |
|
2216 convert_spaces_to_comma = true; |
|
2217 do_comma_insert = false; |
|
2218 |
|
2219 // Not initially doing any plotting or setting of plot attributes. |
|
2220 doing_set = false; |
|
2221 in_plot_range = false; |
|
2222 in_plot_style = false; |
3165
|
2223 in_plot_axes = false; |
2857
|
2224 in_plot_using = false; |
|
2225 past_plot_range = false; |
|
2226 plotting = false; |
|
2227 |
1826
|
2228 // Not initially looking at indirect references. |
2857
|
2229 looking_at_indirect_ref = false; |
1826
|
2230 |
|
2231 // Quote marks strings intially. |
2857
|
2232 quote_is_transpose = false; |
1826
|
2233 } |
|
2234 |
3388
|
2235 static void |
|
2236 maybe_warn_separator_insert (char sep) |
|
2237 { |
3523
|
2238 std::string nm = curr_fcn_file_full_name; |
3388
|
2239 |
|
2240 if (Vwarn_separator_insert) |
|
2241 { |
|
2242 if (nm.empty ()) |
|
2243 warning ("potential auto-insertion of `%c' near line %d", |
|
2244 sep, input_line_number); |
|
2245 else |
|
2246 warning ("potential auto-insertion of `%c' near line %d of file %s", |
|
2247 sep, input_line_number, nm.c_str ()); |
|
2248 } |
|
2249 } |
|
2250 |
3400
|
2251 static void |
|
2252 gripe_single_quote_string (void) |
|
2253 { |
3523
|
2254 std::string nm = curr_fcn_file_full_name; |
3400
|
2255 |
|
2256 if (Vwarn_single_quote_string) |
|
2257 { |
|
2258 if (nm.empty ()) |
|
2259 warning ("single quote delimited string near line %d", |
|
2260 input_line_number); |
|
2261 else |
|
2262 warning ("single quote delimited string near line %d of file %s", |
|
2263 input_line_number, nm.c_str ()); |
|
2264 } |
|
2265 } |
|
2266 |
3388
|
2267 static int |
|
2268 warn_separator_insert (void) |
|
2269 { |
|
2270 Vwarn_separator_insert = check_preference ("warn_separator_insert"); |
|
2271 |
|
2272 return 0; |
|
2273 } |
|
2274 |
3246
|
2275 static int |
3400
|
2276 warn_single_quote_string (void) |
|
2277 { |
|
2278 Vwarn_single_quote_string = check_preference ("warn_single_quote_string"); |
|
2279 |
|
2280 return 0; |
|
2281 } |
|
2282 |
|
2283 static int |
2167
|
2284 whitespace_in_literal_matrix (void) |
|
2285 { |
|
2286 int pref = 0; |
3009
|
2287 |
3523
|
2288 std::string val = builtin_string_variable ("whitespace_in_literal_matrix"); |
3009
|
2289 |
2167
|
2290 if (! val.empty ()) |
|
2291 { |
3565
|
2292 if (val == "ignore") |
2167
|
2293 pref = 2; |
3565
|
2294 else if (val == "traditional") |
2167
|
2295 pref = 1; |
|
2296 } |
3009
|
2297 |
2167
|
2298 Vwhitespace_in_literal_matrix = pref; |
3009
|
2299 |
2167
|
2300 return 0; |
|
2301 } |
|
2302 |
|
2303 void |
|
2304 symbols_of_lex (void) |
|
2305 { |
3388
|
2306 DEFVAR (warn_separator_insert, 0.0, warn_separator_insert, |
3428
|
2307 "-*- texinfo -*-\n\ |
|
2308 @defvr {Built-in Variable} warn_separator_insert\n\ |
3567
|
2309 Print warning if commas or semicolons might be inserted\n\ |
3428
|
2310 automatically in literal matrices.\n\ |
|
2311 @end defvr"); |
3388
|
2312 |
3400
|
2313 DEFVAR (warn_single_quote_string, 0.0, warn_single_quote_string, |
3428
|
2314 "-*- texinfo -*-\n\ |
|
2315 @defvr {Built-in Variable} warn_single_quote_string\n\ |
|
2316 Print warning if a signle quote character is used to introduce a\n\ |
|
2317 string constant.\n\ |
|
2318 @end defvr"); |
3400
|
2319 |
3258
|
2320 DEFVAR (whitespace_in_literal_matrix, "", whitespace_in_literal_matrix, |
3428
|
2321 "-*- texinfo -*-\n\ |
|
2322 @defvr {Built-in Variable} whitespace_in_literal_matrix\n\ |
|
2323 Control auto-insertion of commas and semicolons in literal matrices.\n\ |
|
2324 @end defvr"); |
|
2325 |
2167
|
2326 } |
|
2327 |
1994
|
2328 /* |
|
2329 ;;; Local Variables: *** |
|
2330 ;;; mode: C++ *** |
|
2331 ;;; End: *** |
|
2332 */ |