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