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