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