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