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