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); |
|
173 static bool next_token_is_bin_op (int spc_prev, char *yytext); |
|
174 static bool next_token_is_postfix_unary_op (int spc_prev, char *yytext); |
1823
|
175 static string strip_trailing_whitespace (char *s); |
972
|
176 static void handle_number (char *yytext); |
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}+) |
968
|
204 NUMBER (({D}+\.?{D}*{EXPON}?)|(\.{D}+{EXPON}?)) |
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++; |
|
239 return handle_string (yytext[0], 1); |
|
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 (); |
967
|
297 int bin_op = next_token_is_bin_op (1, yytext); |
|
298 int postfix_un_op = next_token_is_postfix_unary_op (1, yytext); |
|
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 |
|
335 <MATRIX>{SNLCMT}*\n{SNLCMT}* { |
1082
|
336 fixup_column_count (yytext); |
1088
|
337 eat_whitespace (); |
2167
|
338 if (Vwhitespace_in_literal_matrix != 2) |
985
|
339 { |
2857
|
340 lexer_flags.quote_is_transpose = false; |
|
341 lexer_flags.cant_be_identifier = false; |
|
342 lexer_flags.convert_spaces_to_comma = true; |
985
|
343 |
1826
|
344 if (nesting_level.none ()) |
985
|
345 return LEXICAL_ERROR; |
|
346 |
1826
|
347 if (nesting_level.is_brace ()) |
985
|
348 return ';'; |
|
349 } |
|
350 } |
|
351 |
|
352 %{ |
968
|
353 // Open and close brace are handled differently if we are in the range |
|
354 // part of a plot command. |
975
|
355 // |
968
|
356 %} |
1
|
357 |
967
|
358 \[{S}* { |
1826
|
359 nesting_level.brace (); |
975
|
360 |
1082
|
361 current_input_column += yyleng; |
2857
|
362 lexer_flags.quote_is_transpose = false; |
|
363 lexer_flags.cant_be_identifier = false; |
|
364 lexer_flags.convert_spaces_to_comma = true; |
975
|
365 |
|
366 promptflag--; |
|
367 eat_whitespace (); |
|
368 |
1826
|
369 if (lexer_flags.plotting && ! lexer_flags.past_plot_range) |
967
|
370 { |
2857
|
371 lexer_flags.in_plot_range = true; |
1082
|
372 return OPEN_BRACE; |
967
|
373 } |
|
374 else |
|
375 { |
1826
|
376 lexer_flags.braceflag++; |
975
|
377 BEGIN MATRIX; |
1082
|
378 return '['; |
967
|
379 } |
|
380 } |
1
|
381 |
968
|
382 \] { |
1826
|
383 nesting_level.remove (); |
968
|
384 |
1826
|
385 if (lexer_flags.plotting && ! lexer_flags.past_plot_range) |
968
|
386 { |
2857
|
387 lexer_flags.in_plot_range = false; |
968
|
388 TOK_RETURN (CLOSE_BRACE); |
|
389 } |
|
390 else |
|
391 TOK_RETURN (']'); |
|
392 } |
|
393 |
|
394 %{ |
|
395 // Imaginary numbers. |
|
396 %} |
|
397 |
|
398 {NUMBER}{Im} { |
972
|
399 handle_number (yytext); |
968
|
400 return IMAG_NUM; |
|
401 } |
|
402 |
|
403 %{ |
|
404 // Real numbers. Don't grab the `.' part of a dot operator as part of |
|
405 // the constant. |
|
406 %} |
|
407 |
|
408 {D}+/\.[\*/\\^'] | |
|
409 {NUMBER} { |
972
|
410 handle_number (yytext); |
968
|
411 return NUM; |
|
412 } |
|
413 |
|
414 %{ |
|
415 // Eat whitespace. Whitespace inside matrix constants is handled by |
|
416 // the <MATRIX> start state code above. |
|
417 %} |
|
418 |
967
|
419 {S}* { |
|
420 current_input_column += yyleng; |
|
421 } |
|
422 |
968
|
423 %{ |
|
424 // Continuation lines. Allow comments after continuations. |
|
425 %} |
|
426 |
967
|
427 {CONT}{S}*{NL} | |
|
428 {CONT}{S}*{COMMENT} { |
|
429 promptflag--; |
|
430 current_input_column = 1; |
|
431 } |
1
|
432 |
968
|
433 %{ |
|
434 // An ellipsis not at the end of a line is not a continuation, but |
|
435 // does have another meaning. |
|
436 %} |
|
437 |
967
|
438 {EL} { |
|
439 return ELLIPSIS; |
|
440 } |
1
|
441 |
968
|
442 %{ |
|
443 // End of file. |
|
444 %} |
|
445 |
967
|
446 <<EOF>> { |
|
447 TOK_RETURN (END_OF_INPUT); |
|
448 } |
1
|
449 |
968
|
450 %{ |
970
|
451 // Identifiers. Truncate the token at the first space or tab but |
|
452 // don't write directly on yytext. |
968
|
453 %} |
|
454 |
967
|
455 {IDENT}{S}* { |
1823
|
456 string tok = strip_trailing_whitespace (yytext); |
1001
|
457 int c = yytext[yyleng-1]; |
|
458 int cont_is_spc = eat_continuation (); |
|
459 int spc_gobbled = (cont_is_spc || c == ' ' || c == '\t'); |
|
460 return handle_identifier (tok, spc_gobbled); |
967
|
461 } |
1
|
462 |
968
|
463 %{ |
|
464 // A new line character. New line characters inside matrix constants |
985
|
465 // are handled by the <MATRIX> start state code above. If closest |
|
466 // nesting is inside parentheses, don't return a row separator. |
968
|
467 %} |
|
468 |
967
|
469 {NL} { |
|
470 current_input_column = 1; |
2857
|
471 lexer_flags.quote_is_transpose = false; |
|
472 lexer_flags.cant_be_identifier = false; |
|
473 lexer_flags.convert_spaces_to_comma = true; |
985
|
474 |
1826
|
475 if (nesting_level.none ()) |
985
|
476 return '\n'; |
|
477 |
1826
|
478 if (nesting_level.is_brace ()) |
985
|
479 return LEXICAL_ERROR; |
967
|
480 } |
1
|
481 |
968
|
482 %{ |
|
483 // Single quote can either be the beginning of a string or a transpose |
|
484 // operator. |
|
485 %} |
|
486 |
967
|
487 "'" { |
|
488 current_input_column++; |
2857
|
489 lexer_flags.convert_spaces_to_comma = true; |
1
|
490 |
1826
|
491 if (lexer_flags.quote_is_transpose) |
967
|
492 { |
|
493 do_comma_insert_check (); |
|
494 return QUOTE; |
|
495 } |
|
496 else |
973
|
497 return handle_string ('\''); |
967
|
498 } |
1
|
499 |
968
|
500 %{ |
971
|
501 // Double quotes always begin strings. |
|
502 %} |
|
503 |
973
|
504 \" { |
|
505 current_input_column++; |
|
506 return handle_string ('"'); |
|
507 } |
971
|
508 |
|
509 %{ |
|
510 // The colon operator is handled differently if we are in the range |
|
511 // part of a plot command. |
968
|
512 %} |
|
513 |
967
|
514 ":" { |
1826
|
515 if (lexer_flags.plotting |
|
516 && (lexer_flags.in_plot_range || lexer_flags.in_plot_using)) |
2857
|
517 BIN_OP_RETURN (COLON, true); |
967
|
518 else |
2857
|
519 BIN_OP_RETURN (':', false); |
967
|
520 } |
1
|
521 |
968
|
522 %{ |
985
|
523 // Gobble comments. If closest nesting is inside parentheses, don't |
|
524 // return a new line. |
|
525 %} |
968
|
526 |
967
|
527 {CCHAR} { |
1826
|
528 if (help_buf.empty () |
|
529 && lexer_flags.beginning_of_function |
|
530 && nesting_level.none ()) |
967
|
531 { |
|
532 grab_help_text (); |
2857
|
533 lexer_flags.beginning_of_function = false; |
967
|
534 } |
|
535 else |
|
536 { |
|
537 int c; |
|
538 while ((c = yyinput ()) != EOF && c != '\n') |
|
539 ; // Eat comment. |
|
540 } |
440
|
541 |
967
|
542 current_input_column = 1; |
2857
|
543 lexer_flags.quote_is_transpose = false; |
|
544 lexer_flags.cant_be_identifier = false; |
|
545 lexer_flags.convert_spaces_to_comma = true; |
985
|
546 |
1826
|
547 if (nesting_level.none ()) |
985
|
548 return '\n'; |
1826
|
549 else if (nesting_level.is_brace ()) |
1566
|
550 return ';'; |
967
|
551 } |
440
|
552 |
968
|
553 %{ |
|
554 // Other operators. |
|
555 %} |
|
556 |
2857
|
557 ".+" { BIN_OP_RETURN (EPLUS, false); } |
|
558 ".-" { BIN_OP_RETURN (EMINUS, false); } |
|
559 ".*" { BIN_OP_RETURN (EMUL, false); } |
|
560 "./" { BIN_OP_RETURN (EDIV, false); } |
|
561 ".\\" { BIN_OP_RETURN (ELEFTDIV, false); } |
|
562 {EPOW} { BIN_OP_RETURN (EPOW, false); } |
|
563 ".'" { do_comma_insert_check (); BIN_OP_RETURN (TRANSPOSE, true); } |
|
564 "++" { do_comma_insert_check (); BIN_OP_RETURN (PLUS_PLUS, true); } |
|
565 "--" { do_comma_insert_check (); BIN_OP_RETURN (MINUS_MINUS, true); } |
|
566 "<=" { BIN_OP_RETURN (EXPR_LE, false); } |
|
567 "==" { BIN_OP_RETURN (EXPR_EQ, false); } |
|
568 {NOTEQ} { BIN_OP_RETURN (EXPR_NE, false); } |
|
569 ">=" { BIN_OP_RETURN (EXPR_GE, false); } |
2877
|
570 "&" { BIN_OP_RETURN (EXPR_AND, false); } |
2857
|
571 "|" { BIN_OP_RETURN (EXPR_OR, false); } |
|
572 "<" { BIN_OP_RETURN (EXPR_LT, false); } |
|
573 ">" { BIN_OP_RETURN (EXPR_GT, false); } |
|
574 "*" { BIN_OP_RETURN ('*', false); } |
|
575 "/" { BIN_OP_RETURN ('/', false); } |
|
576 "\\" { BIN_OP_RETURN (LEFTDIV, false); } |
|
577 ";" { BIN_OP_RETURN (';', true); } |
|
578 "," { BIN_OP_RETURN (',', true); } |
|
579 {POW} { BIN_OP_RETURN (POW, false); } |
|
580 "=" { BIN_OP_RETURN ('=', true); } |
2877
|
581 "&&" { BIN_OP_RETURN (EXPR_AND_AND, false); } |
2857
|
582 "||" { BIN_OP_RETURN (EXPR_OR_OR, false); } |
2900
|
583 "<<" { BIN_OP_RETURN (LSHIFT, false); } |
|
584 ">>" { BIN_OP_RETURN (RSHIFT, false); } |
967
|
585 |
|
586 {NOT} { |
1826
|
587 if (lexer_flags.plotting && ! lexer_flags.in_plot_range) |
2857
|
588 lexer_flags.past_plot_range = true; |
|
589 BIN_OP_RETURN (EXPR_NOT, false); |
967
|
590 } |
1
|
591 |
1276
|
592 "+" { |
1826
|
593 if (lexer_flags.plotting && ! lexer_flags.in_plot_range) |
2857
|
594 lexer_flags.past_plot_range = true; |
|
595 BIN_OP_RETURN ('+', false); |
967
|
596 } |
|
597 |
1276
|
598 "-" { |
1826
|
599 if (lexer_flags.plotting && ! lexer_flags.in_plot_range) |
2857
|
600 lexer_flags.past_plot_range = true; |
|
601 BIN_OP_RETURN ('-', false); |
967
|
602 } |
|
603 |
|
604 "(" { |
1826
|
605 if (lexer_flags.plotting && ! lexer_flags.in_plot_range) |
2857
|
606 lexer_flags.past_plot_range = true; |
1826
|
607 nesting_level.paren (); |
985
|
608 promptflag--; |
967
|
609 TOK_RETURN ('('); |
|
610 } |
|
611 |
|
612 ")" { |
1826
|
613 nesting_level.remove (); |
1001
|
614 |
967
|
615 current_input_column++; |
2857
|
616 lexer_flags.cant_be_identifier = true; |
|
617 lexer_flags.quote_is_transpose = true; |
1826
|
618 lexer_flags.convert_spaces_to_comma = nesting_level.is_brace (); |
1001
|
619 do_comma_insert_check (); |
967
|
620 return ')'; |
|
621 } |
|
622 |
2066
|
623 "." { |
|
624 TOK_RETURN ('.'); |
|
625 } |
|
626 |
2877
|
627 "+=" { BIN_OP_RETURN (ADD_EQ, false); } |
|
628 "-=" { BIN_OP_RETURN (SUB_EQ, false); } |
|
629 "*=" { BIN_OP_RETURN (MUL_EQ, false); } |
|
630 "/=" { BIN_OP_RETURN (DIV_EQ, false); } |
|
631 ".+=" { BIN_OP_RETURN (ADD_EQ, false); } |
|
632 ".-=" { BIN_OP_RETURN (SUB_EQ, false); } |
|
633 ".*=" { BIN_OP_RETURN (EMUL_EQ, false); } |
|
634 "./=" { BIN_OP_RETURN (EDIV_EQ, false); } |
|
635 "&=" { BIN_OP_RETURN (AND_EQ, false); } |
|
636 "|=" { BIN_OP_RETURN (OR_EQ, false); } |
2900
|
637 "<<=" { BIN_OP_RETURN (LSHIFT_EQ, false); } |
|
638 ">>=" { BIN_OP_RETURN (RSHIFT_EQ, false); } |
2877
|
639 |
968
|
640 %{ |
2066
|
641 // Unrecognized input is a lexical error. |
968
|
642 %} |
1
|
643 |
2042
|
644 . { |
2066
|
645 current_input_column++; |
|
646 |
|
647 error ("invalid character `%s' near line %d, column %d", |
|
648 undo_string_escape (yytext[0]), input_line_number, |
|
649 current_input_column); |
|
650 |
|
651 return LEXICAL_ERROR; |
|
652 } |
1
|
653 |
|
654 %% |
|
655 |
767
|
656 // GAG. |
|
657 // |
|
658 // If we're reading a matrix and the next character is '[', make sure |
|
659 // that we insert a comma ahead of it. |
|
660 |
146
|
661 void |
1
|
662 do_comma_insert_check (void) |
|
663 { |
1001
|
664 int spc_gobbled = eat_continuation (); |
2970
|
665 |
1
|
666 int c = yyinput (); |
2970
|
667 |
146
|
668 yyunput (c, yytext); |
2970
|
669 |
1001
|
670 if (spc_gobbled) |
|
671 yyunput (' ', yytext); |
2970
|
672 |
1826
|
673 lexer_flags.do_comma_insert = (lexer_flags.braceflag && c == '['); |
1
|
674 } |
|
675 |
767
|
676 // Fix things up for errors or interrupts. The parser is never called |
|
677 // recursively, so it is always safe to reinitialize its state before |
|
678 // doing any parsing. |
|
679 |
1
|
680 void |
|
681 reset_parser (void) |
|
682 { |
1826
|
683 // Start off on the right foot. |
1
|
684 BEGIN 0; |
166
|
685 error_state = 0; |
287
|
686 |
1826
|
687 // We do want a prompt by default. |
1
|
688 promptflag = 1; |
287
|
689 |
1826
|
690 // Error may have occurred inside some parentheses or braces. |
985
|
691 nesting_level.clear (); |
287
|
692 |
1826
|
693 // Clear out the stack of token info used to track line and column |
|
694 // numbers. |
143
|
695 while (! token_stack.empty ()) |
|
696 delete token_stack.pop (); |
287
|
697 |
1826
|
698 // Can be reset by defining a function. |
985
|
699 if (! (reading_script_file || reading_fcn_file)) |
|
700 { |
|
701 current_input_column = 1; |
2926
|
702 input_line_number = command_editor::current_command_number () - 1; |
985
|
703 } |
287
|
704 |
1826
|
705 // Only ask for input from stdin if we are expecting interactive |
|
706 // input. |
338
|
707 if (interactive && ! (reading_fcn_file || get_input_from_eval_string)) |
287
|
708 yyrestart (stdin); |
991
|
709 |
1826
|
710 // Clear the buffer for help text. |
|
711 help_buf.resize (0); |
1755
|
712 |
1826
|
713 // Reset other flags. |
|
714 lexer_flags.init (); |
1
|
715 } |
|
716 |
767
|
717 // If we read some newlines, we need figure out what column we're |
|
718 // really looking at. |
|
719 |
1
|
720 static void |
|
721 fixup_column_count (char *s) |
|
722 { |
|
723 char c; |
|
724 while ((c = *s++) != '\0') |
|
725 { |
|
726 if (c == '\n') |
143
|
727 current_input_column = 1; |
1
|
728 else |
|
729 current_input_column++; |
|
730 } |
|
731 } |
|
732 |
767
|
733 // Include these so that we don't have to link to libfl.a. |
246
|
734 |
1
|
735 #ifdef yywrap |
|
736 #undef yywrap |
|
737 #endif |
246
|
738 static int |
1
|
739 yywrap (void) |
|
740 { |
287
|
741 return 1; |
1
|
742 } |
|
743 |
767
|
744 // Tell us all what the current buffer is. |
|
745 |
1
|
746 YY_BUFFER_STATE |
|
747 current_buffer (void) |
|
748 { |
|
749 return YY_CURRENT_BUFFER; |
|
750 } |
|
751 |
767
|
752 // Create a new buffer. |
|
753 |
1
|
754 YY_BUFFER_STATE |
|
755 create_buffer (FILE *f) |
|
756 { |
|
757 return yy_create_buffer (f, YY_BUF_SIZE); |
|
758 } |
|
759 |
767
|
760 // Start reading a new buffer. |
|
761 |
1
|
762 void |
|
763 switch_to_buffer (YY_BUFFER_STATE buf) |
|
764 { |
|
765 yy_switch_to_buffer (buf); |
|
766 } |
|
767 |
767
|
768 // Delete a buffer. |
|
769 |
1
|
770 void |
|
771 delete_buffer (YY_BUFFER_STATE buf) |
|
772 { |
|
773 yy_delete_buffer (buf); |
|
774 } |
|
775 |
767
|
776 // Restore a buffer (for unwind-prot). |
|
777 |
1
|
778 void |
|
779 restore_input_buffer (void *buf) |
|
780 { |
2861
|
781 switch_to_buffer (static_cast<YY_BUFFER_STATE> (buf)); |
1
|
782 } |
|
783 |
767
|
784 // Delete a buffer (for unwind-prot). |
|
785 |
1
|
786 void |
|
787 delete_input_buffer (void *buf) |
|
788 { |
2861
|
789 delete_buffer (static_cast<YY_BUFFER_STATE> (buf)); |
1
|
790 } |
|
791 |
767
|
792 // Check to see if a character string matches any of the possible line |
|
793 // styles for plots. |
|
794 |
1823
|
795 static string |
|
796 plot_style_token (const string& s) |
1
|
797 { |
1823
|
798 string retval; |
|
799 |
2804
|
800 static const char *plot_styles[] = |
146
|
801 { |
925
|
802 "boxes", |
924
|
803 "boxerrorbars", |
2542
|
804 "boxxyerrorbars", |
|
805 "candlesticks", |
146
|
806 "dots", |
|
807 "errorbars", |
2542
|
808 "financebars", |
|
809 "fsteps", |
|
810 "histeps", |
146
|
811 "impulses", |
|
812 "lines", |
|
813 "linespoints", |
|
814 "points", |
926
|
815 "steps", |
2542
|
816 "vector", |
|
817 "xerrorbars", |
|
818 "xyerrorbars", |
|
819 "yerrorbars", |
522
|
820 0, |
146
|
821 }; |
|
822 |
2804
|
823 const char * const *tmp = plot_styles; |
522
|
824 while (*tmp) |
1
|
825 { |
1823
|
826 if (almost_match (*tmp, s.c_str ())) |
|
827 { |
|
828 retval = *tmp; |
|
829 break; |
|
830 } |
1
|
831 |
|
832 tmp++; |
|
833 } |
|
834 |
1823
|
835 return retval; |
1
|
836 } |
|
837 |
767
|
838 // Check to see if a character string matches any one of the plot |
941
|
839 // option keywords. Don't match abbreviations for clear, since that's |
|
840 // not a gnuplot keyword (users will probably only expect to be able |
|
841 // to abbreviate actual gnuplot keywords). |
767
|
842 |
1
|
843 static int |
1823
|
844 is_plot_keyword (const string& s) |
1
|
845 { |
1823
|
846 const char *t = s.c_str (); |
|
847 if (almost_match ("title", t)) |
146
|
848 { |
|
849 return TITLE; |
|
850 } |
1823
|
851 else if (almost_match ("using", t)) |
146
|
852 { |
2857
|
853 lexer_flags.in_plot_using = true; |
146
|
854 return USING; |
|
855 } |
1823
|
856 else if (almost_match ("with", t)) |
146
|
857 { |
2857
|
858 lexer_flags.in_plot_style = true; |
146
|
859 return WITH; |
|
860 } |
1823
|
861 else if (strcmp ("clear", t) == 0) |
883
|
862 { |
|
863 return CLEAR; |
|
864 } |
1
|
865 else |
146
|
866 { |
|
867 return 0; |
|
868 } |
1
|
869 } |
|
870 |
2877
|
871 // Handle keywords. |
767
|
872 |
1
|
873 static int |
1823
|
874 is_keyword (const string& s) |
1
|
875 { |
1826
|
876 if (lexer_flags.plotting && lexer_flags.in_plot_style) |
1
|
877 { |
1823
|
878 string sty = plot_style_token (s); |
|
879 |
|
880 if (! sty.empty ()) |
1
|
881 { |
2857
|
882 lexer_flags.in_plot_style = false; |
143
|
883 yylval.tok_val = new token (sty); |
|
884 token_stack.push (yylval.tok_val); |
1
|
885 return STYLE; |
|
886 } |
|
887 } |
|
888 |
143
|
889 int l = input_line_number; |
|
890 int c = current_input_column; |
|
891 |
1823
|
892 int len = s.length (); |
922
|
893 |
1823
|
894 const octave_kw *kw = octave_kw_lookup (s.c_str (), len); |
191
|
895 |
1497
|
896 if (kw) |
143
|
897 { |
1497
|
898 yylval.tok_val = 0; |
|
899 |
|
900 switch (kw->kw_id) |
|
901 { |
|
902 case all_va_args_kw: |
|
903 case break_kw: |
2764
|
904 case case_kw: |
1497
|
905 case catch_kw: |
|
906 case continue_kw: |
|
907 case else_kw: |
|
908 case elseif_kw: |
|
909 case global_kw: |
2764
|
910 case otherwise_kw: |
1497
|
911 case return_kw: |
2846
|
912 case static_kw: |
1497
|
913 case unwind_protect_cleanup_kw: |
|
914 break; |
|
915 |
|
916 case end_kw: |
|
917 yylval.tok_val = new token (token::simple_end, l, c); |
|
918 break; |
|
919 |
|
920 case end_try_catch_kw: |
|
921 yylval.tok_val = new token (token::try_catch_end, l, c); |
|
922 break; |
|
923 |
|
924 case end_unwind_protect_kw: |
|
925 yylval.tok_val = new token (token::unwind_protect_end, l, c); |
|
926 break; |
|
927 |
|
928 case endfor_kw: |
|
929 yylval.tok_val = new token (token::for_end, l, c); |
|
930 break; |
|
931 |
|
932 case endfunction_kw: |
|
933 yylval.tok_val = new token (token::function_end, l, c); |
|
934 break; |
|
935 |
|
936 case endif_kw: |
|
937 yylval.tok_val = new token (token::if_end, l, c); |
|
938 break; |
|
939 |
2764
|
940 case endswitch_kw: |
|
941 yylval.tok_val = new token (token::switch_end, l, c); |
|
942 break; |
|
943 |
1497
|
944 case endwhile_kw: |
|
945 yylval.tok_val = new token (token::while_end, l, c); |
|
946 break; |
|
947 |
|
948 case for_kw: |
|
949 case while_kw: |
|
950 promptflag--; |
1826
|
951 lexer_flags.looping++; |
1497
|
952 break; |
|
953 |
|
954 case if_kw: |
|
955 case try_kw: |
2764
|
956 case switch_kw: |
1497
|
957 case unwind_protect_kw: |
|
958 promptflag--; |
|
959 break; |
|
960 |
|
961 case gplot_kw: |
2857
|
962 lexer_flags.plotting = true; |
1497
|
963 yylval.tok_val = new token (token::two_dee, l, c); |
|
964 break; |
|
965 |
|
966 case gsplot_kw: |
2857
|
967 lexer_flags.plotting = true; |
1497
|
968 yylval.tok_val = new token (token::three_dee, l, c); |
|
969 break; |
|
970 |
|
971 case replot_kw: |
2857
|
972 lexer_flags.plotting = true; |
1497
|
973 yylval.tok_val = new token (token::replot, l, c); |
|
974 break; |
|
975 |
|
976 case function_kw: |
1826
|
977 if (lexer_flags.defining_func) |
1497
|
978 { |
|
979 error ("function keyword invalid within a function body"); |
|
980 |
|
981 if ((reading_fcn_file || reading_script_file) |
1755
|
982 && ! curr_fcn_file_name.empty ()) |
1497
|
983 error ("defining new function near line %d of file `%s.m'", |
1755
|
984 input_line_number, curr_fcn_file_name.c_str ()); |
1497
|
985 else |
1755
|
986 error ("defining new function near line %d", |
|
987 input_line_number); |
1497
|
988 |
|
989 return LEXICAL_ERROR; |
|
990 } |
|
991 else |
|
992 { |
2877
|
993 // Prepare for local symbols. |
|
994 |
1497
|
995 tmp_local_sym_tab = new symbol_table (); |
2877
|
996 |
|
997 promptflag--; |
|
998 |
2857
|
999 lexer_flags.defining_func = true; |
2877
|
1000 lexer_flags.parsed_function_name = false; |
2857
|
1001 lexer_flags.beginning_of_function = true; |
2877
|
1002 |
1497
|
1003 if (! (reading_fcn_file || reading_script_file)) |
|
1004 input_line_number = 1; |
|
1005 } |
|
1006 break; |
|
1007 |
|
1008 default: |
|
1009 panic_impossible (); |
|
1010 } |
|
1011 |
|
1012 if (! yylval.tok_val) |
|
1013 yylval.tok_val = new token (l, c); |
|
1014 |
476
|
1015 token_stack.push (yylval.tok_val); |
1497
|
1016 |
|
1017 return kw->tok; |
143
|
1018 } |
1
|
1019 |
|
1020 return 0; |
|
1021 } |
|
1022 |
767
|
1023 // Try to find an identifier. All binding to global or builtin |
|
1024 // variables occurs when expressions are evaluated. |
|
1025 |
1
|
1026 static symbol_record * |
1823
|
1027 lookup_identifier (const string& name) |
1
|
1028 { |
2856
|
1029 return curr_sym_tab->lookup (name, true); |
1
|
1030 } |
|
1031 |
2702
|
1032 static bool |
|
1033 is_variable (const string& name) |
|
1034 { |
2856
|
1035 symbol_record *sr = curr_sym_tab->lookup (name); |
2702
|
1036 |
|
1037 return sr && sr->is_variable (); |
|
1038 } |
|
1039 |
|
1040 static void |
|
1041 force_local_variable (const string& name) |
|
1042 { |
2856
|
1043 symbol_record *sr = curr_sym_tab->lookup (name, true); |
2702
|
1044 |
|
1045 if (sr) |
|
1046 sr->define (octave_value ()); |
|
1047 } |
|
1048 |
1019
|
1049 // Grab the help text from an function file. Always overwrites the |
|
1050 // current contents of help_buf. |
767
|
1051 |
2300
|
1052 // XXX FIXME XXX -- gobble_leading_white_space() in variables.cc |
|
1053 // duplicates some of this code! |
|
1054 |
1
|
1055 static void |
|
1056 grab_help_text (void) |
|
1057 { |
1755
|
1058 help_buf.resize (0); |
1019
|
1059 |
2300
|
1060 bool begin_comment = true; |
|
1061 bool in_comment = true; |
1019
|
1062 int c = 0; |
1
|
1063 |
1019
|
1064 while ((c = yyinput ()) != EOF) |
|
1065 { |
2300
|
1066 if (begin_comment) |
|
1067 { |
|
1068 if (c == '%' || c == '#') |
|
1069 continue; |
|
1070 else |
|
1071 begin_comment = false; |
|
1072 } |
|
1073 |
1019
|
1074 if (in_comment) |
1
|
1075 { |
1755
|
1076 help_buf += (char) c; |
|
1077 |
1019
|
1078 if (c == '\n') |
2300
|
1079 in_comment = false; |
1019
|
1080 } |
|
1081 else |
|
1082 { |
|
1083 switch (c) |
991
|
1084 { |
1019
|
1085 case '%': |
|
1086 case '#': |
2300
|
1087 in_comment = true; |
|
1088 begin_comment = true; |
1019
|
1089 break; |
777
|
1090 |
1019
|
1091 case ' ': |
|
1092 case '\t': |
|
1093 break; |
777
|
1094 |
1019
|
1095 default: |
|
1096 goto done; |
1
|
1097 } |
|
1098 } |
1019
|
1099 } |
991
|
1100 |
1019
|
1101 done: |
991
|
1102 |
1019
|
1103 if (c) |
|
1104 yyunput (c, yytext); |
1
|
1105 } |
|
1106 |
767
|
1107 // Return 1 if the given character matches any character in the given |
|
1108 // string. |
|
1109 |
2857
|
1110 static bool |
2804
|
1111 match_any (char c, const char *s) |
1
|
1112 { |
|
1113 char tmp; |
|
1114 while ((tmp = *s++) != '\0') |
|
1115 { |
|
1116 if (c == tmp) |
2857
|
1117 return true; |
1
|
1118 } |
2857
|
1119 return false; |
1
|
1120 } |
|
1121 |
767
|
1122 // Given information about the spacing surrounding an operator, |
|
1123 // return 1 if it looks like it should be treated as a binary |
|
1124 // operator. For example, |
|
1125 // |
|
1126 // [ 1 + 2 ] or [ 1+ 2] or [ 1+2 ] ==> binary |
|
1127 |
2857
|
1128 static bool |
1
|
1129 looks_like_bin_op (int spc_prev, int spc_next) |
|
1130 { |
608
|
1131 return ((spc_prev && spc_next) || ! spc_prev); |
1
|
1132 } |
|
1133 |
767
|
1134 // Try to determine if the next token should be treated as a postfix |
|
1135 // unary operator. This is ugly, but it seems to do the right thing. |
|
1136 |
2857
|
1137 static bool |
1
|
1138 next_token_is_postfix_unary_op (int spc_prev, char *yytext) |
|
1139 { |
2857
|
1140 bool un_op = false; |
1
|
1141 |
|
1142 int c0 = yyinput (); |
|
1143 int c1 = yyinput (); |
|
1144 |
|
1145 yyunput (c1, yytext); |
|
1146 yyunput (c0, yytext); |
|
1147 |
|
1148 int transpose = (c0 == '.' && c1 == '\''); |
|
1149 int hermitian = (c0 == '\''); |
|
1150 |
|
1151 un_op = (transpose || (hermitian && ! spc_prev)); |
|
1152 |
|
1153 return un_op; |
|
1154 } |
|
1155 |
767
|
1156 // Try to determine if the next token should be treated as a binary |
|
1157 // operator. This is even uglier, but it also seems to do the right |
1276
|
1158 // thing. Note that it is only necessary to check the spacing for `+' |
|
1159 // and `-', since those are the only tokens that can appear as unary |
|
1160 // ops too. |
1521
|
1161 // |
|
1162 // Note that this never returns true for `.', even though it can be a |
|
1163 // binary operator (the structure reference thing). The only time |
|
1164 // this appears to matter is for things like |
|
1165 // |
|
1166 // [ a . b ] |
|
1167 // |
|
1168 // which probably doesn't occur that often, can be worked around by |
|
1169 // eliminating the whitespace, putting the expression in parentheses, |
|
1170 // or using `whitespace_in_literal_matrix = "ignored"', so I think it |
|
1171 // is an acceptable change. It would be quite a bit harder to `fix' |
|
1172 // this. (Well, maybe not. the best fix would be to do away with the |
|
1173 // specialness of whitespace inside of `[ ... ]'). |
1554
|
1174 // |
|
1175 // However, we still do check for `.+', `.*', etc. |
767
|
1176 |
2857
|
1177 static bool |
1
|
1178 next_token_is_bin_op (int spc_prev, char *yytext) |
|
1179 { |
2857
|
1180 bool bin_op = false; |
1
|
1181 |
|
1182 int c0 = yyinput (); |
|
1183 |
|
1184 switch (c0) |
|
1185 { |
777
|
1186 case '+': |
|
1187 case '-': |
1276
|
1188 { |
|
1189 int c1 = yyinput (); |
|
1190 yyunput (c1, yytext); |
|
1191 int spc_next = (c1 == ' ' || c1 == '\t'); |
|
1192 bin_op = looks_like_bin_op (spc_prev, spc_next); |
|
1193 } |
|
1194 break; |
|
1195 |
1554
|
1196 case '.': |
|
1197 { |
|
1198 int c1 = yyinput (); |
|
1199 yyunput (c1, yytext); |
|
1200 bin_op = match_any (c1, "+-*/\\^"); |
|
1201 } |
|
1202 break; |
|
1203 |
777
|
1204 case '/': |
|
1205 case ':': |
|
1206 case '\\': |
|
1207 case '^': |
1
|
1208 case '&': |
|
1209 case '*': |
|
1210 case '|': |
|
1211 case '<': |
|
1212 case '>': |
777
|
1213 case '~': |
|
1214 case '!': |
|
1215 case '=': |
2857
|
1216 bin_op = true; |
1
|
1217 break; |
|
1218 |
|
1219 default: |
1276
|
1220 break; |
1
|
1221 } |
|
1222 |
|
1223 yyunput (c0, yytext); |
|
1224 |
|
1225 return bin_op; |
|
1226 } |
|
1227 |
767
|
1228 // Used to delete trailing white space from tokens. |
|
1229 |
1823
|
1230 static string |
1
|
1231 strip_trailing_whitespace (char *s) |
|
1232 { |
1823
|
1233 string retval = s; |
1
|
1234 |
1823
|
1235 size_t pos = retval.find_first_of (" \t"); |
1
|
1236 |
1823
|
1237 if (pos != NPOS) |
|
1238 retval.resize (pos); |
1
|
1239 |
|
1240 return retval; |
|
1241 } |
|
1242 |
1001
|
1243 // Discard whitespace, including comments and continuations. |
1088
|
1244 // |
|
1245 // Return value is logical OR of the following values: |
|
1246 // |
1826
|
1247 // ATE_NOTHING : no spaces to eat |
1088
|
1248 // ATE_SPACE_OR_TAB : space or tab in input |
|
1249 // ATE_NEWLINE : bare new line in input |
1001
|
1250 |
1826
|
1251 static yum_yum |
975
|
1252 eat_whitespace (void) |
|
1253 { |
1826
|
1254 yum_yum retval = ATE_NOTHING; |
2857
|
1255 bool in_comment = false; |
975
|
1256 int c; |
|
1257 while ((c = yyinput ()) != EOF) |
|
1258 { |
|
1259 current_input_column++; |
|
1260 |
|
1261 switch (c) |
|
1262 { |
|
1263 case ' ': |
|
1264 case '\t': |
1088
|
1265 retval |= ATE_SPACE_OR_TAB; |
975
|
1266 break; |
|
1267 |
|
1268 case '\n': |
1088
|
1269 retval |= ATE_NEWLINE; |
2857
|
1270 in_comment = false; |
975
|
1271 current_input_column = 0; |
|
1272 break; |
|
1273 |
|
1274 case '#': |
|
1275 case '%': |
2857
|
1276 in_comment = true; |
975
|
1277 break; |
|
1278 |
1001
|
1279 case '.': |
|
1280 if (in_comment) |
|
1281 break; |
|
1282 else |
|
1283 { |
|
1284 if (have_ellipsis_continuation ()) |
|
1285 break; |
|
1286 else |
|
1287 goto done; |
|
1288 } |
|
1289 |
|
1290 case '\\': |
|
1291 if (in_comment) |
|
1292 break; |
|
1293 else |
|
1294 { |
3105
|
1295 if (have_continuation ()) |
1001
|
1296 break; |
|
1297 else |
|
1298 goto done; |
|
1299 } |
|
1300 |
975
|
1301 default: |
|
1302 if (in_comment) |
|
1303 break; |
|
1304 else |
|
1305 goto done; |
|
1306 } |
|
1307 } |
|
1308 |
|
1309 done: |
|
1310 yyunput (c, yytext); |
1082
|
1311 current_input_column--; |
1001
|
1312 return retval; |
975
|
1313 } |
|
1314 |
|
1315 static void |
972
|
1316 handle_number (char *yytext) |
|
1317 { |
2621
|
1318 char *tmp = strsave (yytext); |
|
1319 |
|
1320 char *idx = strpbrk (tmp, "Dd"); |
|
1321 |
|
1322 if (idx) |
|
1323 *idx = 'e'; |
|
1324 |
972
|
1325 double value; |
2621
|
1326 int nread = sscanf (tmp, "%lf", &value); |
|
1327 |
|
1328 delete [] tmp; |
972
|
1329 |
1826
|
1330 // If yytext doesn't contain a valid number, we are in deep doo doo. |
985
|
1331 |
972
|
1332 assert (nread == 1); |
|
1333 |
1826
|
1334 lexer_flags.quote_is_transpose = 1; |
|
1335 lexer_flags.cant_be_identifier = 1; |
|
1336 lexer_flags.convert_spaces_to_comma = 1; |
972
|
1337 |
1826
|
1338 if (lexer_flags.plotting && ! lexer_flags.in_plot_range) |
|
1339 lexer_flags.past_plot_range = 1; |
972
|
1340 |
|
1341 yylval.tok_val = new token (value, yytext, input_line_number, |
|
1342 current_input_column); |
|
1343 |
|
1344 token_stack.push (yylval.tok_val); |
|
1345 |
|
1346 current_input_column += yyleng; |
|
1347 |
|
1348 do_comma_insert_check (); |
|
1349 } |
|
1350 |
1001
|
1351 // We have seen a backslash and need to find out if it should be |
|
1352 // treated as a continuation character. If so, this eats it, up to |
|
1353 // and including the new line character. |
|
1354 // |
973
|
1355 // Match whitespace only, followed by a comment character or newline. |
|
1356 // Once a comment character is found, discard all input until newline. |
|
1357 // If non-whitespace characters are found before comment |
|
1358 // characters, return 0. Otherwise, return 1. |
|
1359 |
2857
|
1360 static bool |
3096
|
1361 have_continuation (bool trailing_comments_ok) |
973
|
1362 { |
|
1363 ostrstream buf; |
|
1364 |
2857
|
1365 bool in_comment = false; |
973
|
1366 char c; |
|
1367 while ((c = yyinput ()) != EOF) |
|
1368 { |
|
1369 buf << (char) c; |
|
1370 |
|
1371 switch (c) |
|
1372 { |
|
1373 case ' ': |
|
1374 case '\t': |
|
1375 break; |
|
1376 |
|
1377 case '%': |
|
1378 case '#': |
1091
|
1379 if (trailing_comments_ok) |
2857
|
1380 in_comment = true; |
1091
|
1381 else |
|
1382 goto cleanup; |
973
|
1383 break; |
|
1384 |
|
1385 case '\n': |
975
|
1386 current_input_column = 0; |
1001
|
1387 promptflag--; |
2857
|
1388 return true; |
973
|
1389 |
|
1390 default: |
1091
|
1391 if (! in_comment) |
|
1392 goto cleanup; |
|
1393 break; |
973
|
1394 } |
|
1395 } |
|
1396 |
|
1397 yyunput (c, yytext); |
2857
|
1398 return false; |
973
|
1399 |
3096
|
1400 cleanup: |
1091
|
1401 buf << ends; |
|
1402 char *s = buf.str (); |
|
1403 if (s) |
|
1404 { |
|
1405 int len = strlen (s); |
|
1406 while (len--) |
|
1407 yyunput (s[len], yytext); |
|
1408 } |
|
1409 delete [] s; |
3096
|
1410 |
2857
|
1411 return false; |
973
|
1412 } |
|
1413 |
1001
|
1414 // We have seen a `.' and need to see if it is the start of a |
|
1415 // continuation. If so, this eats it, up to and including the new |
|
1416 // line character. |
|
1417 |
2857
|
1418 static bool |
3096
|
1419 have_ellipsis_continuation (bool trailing_comments_ok) |
973
|
1420 { |
|
1421 char c1 = yyinput (); |
|
1422 if (c1 == '.') |
|
1423 { |
|
1424 char c2 = yyinput (); |
1091
|
1425 if (c2 == '.' && have_continuation (trailing_comments_ok)) |
2857
|
1426 return true; |
973
|
1427 else |
|
1428 { |
|
1429 yyunput (c2, yytext); |
|
1430 yyunput (c1, yytext); |
|
1431 } |
|
1432 } |
|
1433 else |
|
1434 yyunput (c1, yytext); |
|
1435 |
2857
|
1436 return false; |
973
|
1437 } |
|
1438 |
1001
|
1439 // See if we have a continuation line. If so, eat it and the leading |
|
1440 // whitespace on the next line. |
1088
|
1441 // |
|
1442 // Return value is the same as described for eat_whitespace(). |
1001
|
1443 |
1826
|
1444 static yum_yum |
1001
|
1445 eat_continuation (void) |
|
1446 { |
1826
|
1447 int retval = ATE_NOTHING; |
1001
|
1448 int c = yyinput (); |
|
1449 if ((c == '.' && have_ellipsis_continuation ()) |
3105
|
1450 || (c == '\\' && have_continuation ())) |
1001
|
1451 retval = eat_whitespace (); |
|
1452 else |
|
1453 yyunput (c, yytext); |
|
1454 |
|
1455 return retval; |
|
1456 } |
|
1457 |
973
|
1458 static int |
975
|
1459 handle_string (char delim, int text_style) |
973
|
1460 { |
|
1461 ostrstream buf; |
|
1462 |
|
1463 int c; |
1031
|
1464 int escape_pending = 0; |
973
|
1465 |
|
1466 while ((c = yyinput ()) != EOF) |
|
1467 { |
|
1468 current_input_column++; |
|
1469 |
3105
|
1470 if (c == '\\') |
973
|
1471 { |
1053
|
1472 if (escape_pending) |
|
1473 { |
|
1474 buf << (char) c; |
|
1475 escape_pending = 0; |
|
1476 } |
|
1477 else |
|
1478 { |
3096
|
1479 if (have_continuation (false)) |
1053
|
1480 escape_pending = 0; |
|
1481 else |
|
1482 { |
|
1483 buf << (char) c; |
|
1484 escape_pending = 1; |
|
1485 } |
|
1486 } |
1031
|
1487 continue; |
973
|
1488 } |
|
1489 else if (c == '.') |
|
1490 { |
3096
|
1491 if (! have_ellipsis_continuation (false)) |
973
|
1492 buf << (char) c; |
|
1493 } |
|
1494 else if (c == '\n') |
|
1495 { |
1053
|
1496 error ("unterminated string constant"); |
973
|
1497 break; |
|
1498 } |
|
1499 else if (c == delim) |
|
1500 { |
1031
|
1501 if (escape_pending) |
973
|
1502 buf << (char) c; |
|
1503 else |
|
1504 { |
|
1505 c = yyinput (); |
|
1506 if (c == delim) |
|
1507 buf << (char) c; |
|
1508 else |
|
1509 { |
|
1510 yyunput (c, yytext); |
|
1511 buf << ends; |
3105
|
1512 char *t = buf.str (); |
|
1513 string s = do_string_escapes (t); |
|
1514 delete [] t; |
975
|
1515 |
1826
|
1516 if (text_style && lexer_flags.doing_set) |
975
|
1517 { |
3105
|
1518 if (! s.empty ()) |
|
1519 s = string (delim, 1) + s + string (delim, 1); |
975
|
1520 } |
|
1521 else |
|
1522 { |
2857
|
1523 lexer_flags.quote_is_transpose = true; |
|
1524 lexer_flags.cant_be_identifier = true; |
|
1525 lexer_flags.convert_spaces_to_comma = true; |
975
|
1526 } |
|
1527 |
3105
|
1528 yylval.tok_val = new token (s); |
973
|
1529 token_stack.push (yylval.tok_val); |
|
1530 return TEXT; |
|
1531 } |
|
1532 } |
|
1533 } |
|
1534 else |
|
1535 { |
|
1536 buf << (char) c; |
|
1537 } |
|
1538 |
1031
|
1539 escape_pending = 0; |
973
|
1540 } |
|
1541 |
|
1542 return LEXICAL_ERROR; |
|
1543 } |
|
1544 |
971
|
1545 static int |
1001
|
1546 handle_close_brace (int spc_gobbled) |
971
|
1547 { |
1826
|
1548 if (! nesting_level.none ()) |
971
|
1549 { |
1826
|
1550 nesting_level.remove (); |
|
1551 lexer_flags.braceflag--; |
971
|
1552 } |
|
1553 |
1826
|
1554 if (lexer_flags.braceflag == 0) |
1001
|
1555 BEGIN 0; |
|
1556 |
2970
|
1557 // XXX FIXME XXX -- this needs to handle +=, -=, etc. |
|
1558 |
971
|
1559 int c1 = yyinput (); |
|
1560 if (c1 == '=') |
|
1561 { |
2857
|
1562 lexer_flags.quote_is_transpose = false; |
|
1563 lexer_flags.cant_be_identifier = false; |
|
1564 lexer_flags.convert_spaces_to_comma = true; |
971
|
1565 |
|
1566 int c2 = yyinput (); |
|
1567 unput (c2); |
|
1568 unput (c1); |
|
1569 |
2970
|
1570 if (c2 == '=' || lexer_flags.looking_at_return_list) |
|
1571 return ']'; |
971
|
1572 else |
2970
|
1573 return CLOSE_BRACE; |
971
|
1574 } |
|
1575 else |
|
1576 { |
|
1577 unput (c1); |
|
1578 |
2167
|
1579 if (lexer_flags.braceflag && Vwhitespace_in_literal_matrix != 2) |
971
|
1580 { |
1001
|
1581 int bin_op = next_token_is_bin_op (spc_gobbled, yytext); |
971
|
1582 int postfix_un_op = next_token_is_postfix_unary_op |
1001
|
1583 (spc_gobbled, yytext); |
971
|
1584 |
|
1585 int other_op = match_any (c1, ",;\n]"); |
|
1586 |
1826
|
1587 if (! (postfix_un_op || bin_op || other_op) |
|
1588 && nesting_level.is_brace () |
|
1589 && lexer_flags.convert_spaces_to_comma) |
971
|
1590 { |
|
1591 unput (','); |
|
1592 return ']'; |
|
1593 } |
|
1594 } |
|
1595 } |
|
1596 |
2857
|
1597 lexer_flags.quote_is_transpose = true; |
|
1598 lexer_flags.cant_be_identifier = false; |
|
1599 lexer_flags.convert_spaces_to_comma = true; |
971
|
1600 return ']'; |
|
1601 } |
|
1602 |
1072
|
1603 static void |
|
1604 maybe_unput_comma (int spc_gobbled) |
|
1605 { |
2970
|
1606 if (Vwhitespace_in_literal_matrix != 2 && nesting_level.is_brace ()) |
1072
|
1607 { |
|
1608 int bin_op = next_token_is_bin_op (spc_gobbled, yytext); |
|
1609 |
2970
|
1610 int postfix_un_op |
|
1611 = next_token_is_postfix_unary_op (spc_gobbled, yytext); |
1072
|
1612 |
|
1613 int c1 = yyinput (); |
|
1614 int c2 = yyinput (); |
2970
|
1615 |
1072
|
1616 unput (c2); |
|
1617 unput (c1); |
2970
|
1618 |
1072
|
1619 int sep_op = match_any (c1, ",;\n]"); |
2970
|
1620 |
1072
|
1621 int dot_op = (c1 == '.' |
|
1622 && (isalpha (c2) || isspace (c2) || c2 == '_')); |
2970
|
1623 |
1072
|
1624 int index_op = (c1 == '(' |
2167
|
1625 && (Vwhitespace_in_literal_matrix == 0 |
1072
|
1626 || ! spc_gobbled)); |
|
1627 |
|
1628 if (! (postfix_un_op || bin_op || sep_op || dot_op || index_op)) |
|
1629 unput (','); |
|
1630 } |
|
1631 } |
|
1632 |
767
|
1633 // Figure out exactly what kind of token to return when we have seen |
|
1634 // an identifier. Handles keywords. |
|
1635 |
146
|
1636 static int |
1823
|
1637 handle_identifier (const string& tok, int spc_gobbled) |
146
|
1638 { |
1826
|
1639 // It is almost always an error for an identifier to be followed |
|
1640 // directly by another identifier. Special cases are handled |
|
1641 // below. |
883
|
1642 |
2857
|
1643 lexer_flags.cant_be_identifier = true; |
883
|
1644 |
2970
|
1645 // If we are expecting a structure element, avoid recognizing |
|
1646 // keywords and other special names and return STRUCT_ELT, which is |
|
1647 // a string that is also a valid identifier. But first, we have to |
|
1648 // decide whether to insert a comma. |
747
|
1649 |
1826
|
1650 if (lexer_flags.looking_at_indirect_ref) |
1072
|
1651 { |
2970
|
1652 do_comma_insert_check (); |
|
1653 |
1072
|
1654 maybe_unput_comma (spc_gobbled); |
2819
|
1655 |
|
1656 yylval.tok_val = new token (tok, input_line_number, |
|
1657 current_input_column); |
|
1658 |
|
1659 token_stack.push (yylval.tok_val); |
|
1660 |
2857
|
1661 lexer_flags.cant_be_identifier = false; |
|
1662 lexer_flags.quote_is_transpose = true; |
|
1663 lexer_flags.convert_spaces_to_comma = true; |
2819
|
1664 |
|
1665 current_input_column += yyleng; |
|
1666 |
2970
|
1667 return STRUCT_ELT; |
1072
|
1668 } |
747
|
1669 |
1826
|
1670 // If we have a regular keyword, or a plot STYLE, return it. |
|
1671 // Keywords can be followed by identifiers (TOK_RETURN handles |
|
1672 // that). |
146
|
1673 |
|
1674 int kw_token = is_keyword (tok); |
2970
|
1675 |
146
|
1676 if (kw_token) |
|
1677 { |
|
1678 if (kw_token == STYLE) |
|
1679 { |
1060
|
1680 current_input_column += yyleng; |
2857
|
1681 lexer_flags.quote_is_transpose = false; |
|
1682 lexer_flags.convert_spaces_to_comma = true; |
146
|
1683 return kw_token; |
|
1684 } |
|
1685 else |
|
1686 TOK_RETURN (kw_token); |
|
1687 } |
|
1688 |
1826
|
1689 // See if we have a plot keyword (title, using, with, or clear). |
146
|
1690 |
1826
|
1691 if (lexer_flags.plotting) |
941
|
1692 { |
1826
|
1693 // Yes, we really do need both of these plot_range variables. |
|
1694 // One is used to mark when we are past all possiblity of a plot |
|
1695 // range, the other is used to mark when we are actually between |
|
1696 // the square brackets that surround the range. |
146
|
1697 |
1826
|
1698 if (! lexer_flags.in_plot_range) |
2857
|
1699 lexer_flags.past_plot_range = true; |
941
|
1700 |
1273
|
1701 // Option keywords can't appear in parentheses or braces. |
|
1702 |
|
1703 int plot_option_kw = 0; |
2970
|
1704 |
1826
|
1705 if (nesting_level.none ()) |
1273
|
1706 plot_option_kw = is_plot_keyword (tok); |
941
|
1707 |
1826
|
1708 if (lexer_flags.cant_be_identifier && plot_option_kw) |
941
|
1709 TOK_RETURN (plot_option_kw); |
|
1710 } |
146
|
1711 |
1001
|
1712 int c = yyinput (); |
|
1713 yyunput (c, yytext); |
2702
|
1714 bool next_tok_is_eq = (c == '='); |
|
1715 bool next_tok_is_paren = (c == '('); |
1001
|
1716 |
1826
|
1717 // Make sure we put the return values of a function in the symbol |
|
1718 // table that is local to the function. |
146
|
1719 |
2877
|
1720 // If we are defining a function and we have not seen the function |
|
1721 // name yet and the next token is `=', then this identifier must be |
|
1722 // the only return value for the function and it belongs in the |
|
1723 // local symbol table. |
|
1724 |
1826
|
1725 if (next_tok_is_eq |
2877
|
1726 && lexer_flags.defining_func |
|
1727 && ! lexer_flags.parsed_function_name) |
146
|
1728 curr_sym_tab = tmp_local_sym_tab; |
|
1729 |
2702
|
1730 // Kluge alert. |
|
1731 // |
|
1732 // If we are looking at a text style function, set up to gobble its |
2745
|
1733 // arguments. |
|
1734 // |
|
1735 // If the following token is `=', or if we are parsing a function |
|
1736 // return list or function parameter list, force the symbol to be |
2702
|
1737 // inserted as a variable in the current symbol table. |
|
1738 |
|
1739 if (is_text_function_name (tok) && ! is_variable (tok)) |
|
1740 { |
2745
|
1741 if (next_tok_is_eq |
|
1742 || lexer_flags.looking_at_return_list |
|
1743 || lexer_flags.looking_at_parameter_list) |
|
1744 { |
|
1745 force_local_variable (tok); |
|
1746 } |
2702
|
1747 else if (! next_tok_is_paren) |
|
1748 { |
|
1749 if (tok == "gset") |
2857
|
1750 lexer_flags.doing_set = true; |
2702
|
1751 |
|
1752 BEGIN TEXT_FCN; |
|
1753 } |
|
1754 } |
|
1755 |
1826
|
1756 // Find the token in the symbol table. |
146
|
1757 |
|
1758 yylval.tok_val = new token (lookup_identifier (tok), |
|
1759 input_line_number, |
|
1760 current_input_column); |
|
1761 |
|
1762 token_stack.push (yylval.tok_val); |
|
1763 |
1826
|
1764 // After seeing an identifer, it is ok to convert spaces to a comma |
|
1765 // (if needed). |
146
|
1766 |
2857
|
1767 lexer_flags.convert_spaces_to_comma = true; |
146
|
1768 |
2877
|
1769 if (! next_tok_is_eq) |
|
1770 { |
|
1771 lexer_flags.quote_is_transpose = true; |
146
|
1772 |
2877
|
1773 do_comma_insert_check (); |
|
1774 |
|
1775 maybe_unput_comma (spc_gobbled); |
146
|
1776 } |
|
1777 |
2877
|
1778 current_input_column += yyleng; |
146
|
1779 |
|
1780 return NAME; |
|
1781 } |
|
1782 |
767
|
1783 // Print a warning if a function file that defines a function has |
|
1784 // anything other than comments and whitespace following the END token |
|
1785 // that matches the FUNCTION statement. |
|
1786 |
1
|
1787 void |
|
1788 check_for_garbage_after_fcn_def (void) |
|
1789 { |
1826
|
1790 // By making a newline be the next character to be read, we will |
|
1791 // force the parser to return after reading the function. Calling |
|
1792 // yyunput with EOF seems not to work... |
1
|
1793 |
2857
|
1794 bool in_comment = false; |
1
|
1795 int lineno = input_line_number; |
|
1796 int c; |
|
1797 while ((c = yyinput ()) != EOF) |
|
1798 { |
|
1799 switch (c) |
|
1800 { |
|
1801 case ' ': |
|
1802 case '\t': |
|
1803 case ';': |
|
1804 case ',': |
|
1805 break; |
777
|
1806 |
1
|
1807 case '\n': |
|
1808 if (in_comment) |
2857
|
1809 in_comment = false; |
1
|
1810 break; |
777
|
1811 |
1
|
1812 case '%': |
|
1813 case '#': |
2857
|
1814 in_comment = true; |
1
|
1815 break; |
777
|
1816 |
1
|
1817 default: |
|
1818 if (in_comment) |
|
1819 break; |
|
1820 else |
|
1821 { |
|
1822 warning ("ignoring trailing garbage after end of function\n\ |
1755
|
1823 near line %d of file `%s.m'", lineno, curr_fcn_file_name.c_str ()); |
1
|
1824 |
|
1825 yyunput ('\n', yytext); |
|
1826 return; |
|
1827 } |
|
1828 } |
|
1829 } |
|
1830 yyunput ('\n', yytext); |
|
1831 } |
|
1832 |
1826
|
1833 void |
|
1834 lexical_feedback::init (void) |
|
1835 { |
|
1836 // Not initially defining a matrix list. |
|
1837 braceflag = 0; |
|
1838 |
|
1839 // Not initially inside a loop or if statement. |
|
1840 looping = 0; |
|
1841 |
2857
|
1842 // Not initially defining a function. |
|
1843 beginning_of_function = false; |
|
1844 defining_func = false; |
2877
|
1845 parsed_function_name = false; |
2857
|
1846 |
|
1847 // Not parsing a function return or parameter list. |
|
1848 looking_at_return_list = false; |
|
1849 looking_at_parameter_list = false; |
|
1850 |
|
1851 // Next token can be identifier. |
|
1852 cant_be_identifier = false; |
|
1853 |
|
1854 // No need to do comma insert or convert spaces to comma at |
|
1855 // beginning of input. |
|
1856 convert_spaces_to_comma = true; |
|
1857 do_comma_insert = false; |
|
1858 |
|
1859 // Not initially doing any plotting or setting of plot attributes. |
|
1860 doing_set = false; |
|
1861 in_plot_range = false; |
|
1862 in_plot_style = false; |
|
1863 in_plot_using = false; |
|
1864 past_plot_range = false; |
|
1865 plotting = false; |
|
1866 |
1826
|
1867 // Not initially looking at indirect references. |
2857
|
1868 looking_at_indirect_ref = false; |
1826
|
1869 |
|
1870 // Quote marks strings intially. |
2857
|
1871 quote_is_transpose = false; |
1826
|
1872 } |
|
1873 |
2167
|
1874 int |
|
1875 whitespace_in_literal_matrix (void) |
|
1876 { |
|
1877 int pref = 0; |
3009
|
1878 |
2167
|
1879 string val = builtin_string_variable ("whitespace_in_literal_matrix"); |
3009
|
1880 |
2167
|
1881 if (! val.empty ()) |
|
1882 { |
|
1883 if (val.compare ("ignore", 0, 6) == 0) |
|
1884 pref = 2; |
|
1885 else if (val.compare ("traditional", 0, 11) == 0) |
|
1886 pref = 1; |
|
1887 } |
3009
|
1888 |
2167
|
1889 Vwhitespace_in_literal_matrix = pref; |
3009
|
1890 |
2167
|
1891 return 0; |
|
1892 } |
|
1893 |
|
1894 void |
|
1895 symbols_of_lex (void) |
|
1896 { |
|
1897 DEFVAR (whitespace_in_literal_matrix, "", 0, whitespace_in_literal_matrix, |
|
1898 "control auto-insertion of commas and semicolons in literal matrices"); |
|
1899 } |
|
1900 |
1994
|
1901 /* |
|
1902 ;;; Local Variables: *** |
|
1903 ;;; mode: C++ *** |
|
1904 ;;; End: *** |
|
1905 */ |