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