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