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", |
2542
|
853 "boxxyerrorbars", |
|
854 "candlesticks", |
146
|
855 "dots", |
|
856 "errorbars", |
2542
|
857 "financebars", |
|
858 "fsteps", |
|
859 "histeps", |
146
|
860 "impulses", |
|
861 "lines", |
|
862 "linespoints", |
|
863 "points", |
926
|
864 "steps", |
2542
|
865 "vector", |
|
866 "xerrorbars", |
|
867 "xyerrorbars", |
|
868 "yerrorbars", |
522
|
869 0, |
146
|
870 }; |
|
871 |
1
|
872 char **tmp = plot_styles; |
522
|
873 while (*tmp) |
1
|
874 { |
1823
|
875 if (almost_match (*tmp, s.c_str ())) |
|
876 { |
|
877 retval = *tmp; |
|
878 break; |
|
879 } |
1
|
880 |
|
881 tmp++; |
|
882 } |
|
883 |
1823
|
884 return retval; |
1
|
885 } |
|
886 |
767
|
887 // Check to see if a character string matches any one of the plot |
941
|
888 // option keywords. Don't match abbreviations for clear, since that's |
|
889 // not a gnuplot keyword (users will probably only expect to be able |
|
890 // to abbreviate actual gnuplot keywords). |
767
|
891 |
1
|
892 static int |
1823
|
893 is_plot_keyword (const string& s) |
1
|
894 { |
1823
|
895 const char *t = s.c_str (); |
|
896 if (almost_match ("title", t)) |
146
|
897 { |
|
898 return TITLE; |
|
899 } |
1823
|
900 else if (almost_match ("using", t)) |
146
|
901 { |
1826
|
902 lexer_flags.in_plot_using = 1; |
146
|
903 return USING; |
|
904 } |
1823
|
905 else if (almost_match ("with", t)) |
146
|
906 { |
1826
|
907 lexer_flags.in_plot_style = 1; |
146
|
908 return WITH; |
|
909 } |
1823
|
910 else if (strcmp ("clear", t) == 0) |
883
|
911 { |
|
912 return CLEAR; |
|
913 } |
1
|
914 else |
146
|
915 { |
|
916 return 0; |
|
917 } |
1
|
918 } |
|
919 |
767
|
920 // Handle keywords. Could probably be more efficient... |
|
921 |
1
|
922 static int |
1823
|
923 is_keyword (const string& s) |
1
|
924 { |
1826
|
925 if (lexer_flags.plotting && lexer_flags.in_plot_style) |
1
|
926 { |
1823
|
927 string sty = plot_style_token (s); |
|
928 |
|
929 if (! sty.empty ()) |
1
|
930 { |
1826
|
931 lexer_flags.in_plot_style = 0; |
143
|
932 yylval.tok_val = new token (sty); |
|
933 token_stack.push (yylval.tok_val); |
1
|
934 return STYLE; |
|
935 } |
|
936 } |
|
937 |
143
|
938 int l = input_line_number; |
|
939 int c = current_input_column; |
|
940 |
1823
|
941 int len = s.length (); |
922
|
942 |
1823
|
943 const octave_kw *kw = octave_kw_lookup (s.c_str (), len); |
191
|
944 |
1497
|
945 if (kw) |
143
|
946 { |
1497
|
947 yylval.tok_val = 0; |
|
948 |
|
949 switch (kw->kw_id) |
|
950 { |
|
951 case all_va_args_kw: |
|
952 case break_kw: |
|
953 case catch_kw: |
|
954 case continue_kw: |
|
955 case else_kw: |
|
956 case elseif_kw: |
|
957 case global_kw: |
|
958 case return_kw: |
|
959 case unwind_protect_cleanup_kw: |
|
960 break; |
|
961 |
|
962 case end_kw: |
|
963 yylval.tok_val = new token (token::simple_end, l, c); |
|
964 break; |
|
965 |
|
966 case end_try_catch_kw: |
|
967 yylval.tok_val = new token (token::try_catch_end, l, c); |
|
968 break; |
|
969 |
|
970 case end_unwind_protect_kw: |
|
971 yylval.tok_val = new token (token::unwind_protect_end, l, c); |
|
972 break; |
|
973 |
|
974 case endfor_kw: |
|
975 yylval.tok_val = new token (token::for_end, l, c); |
|
976 break; |
|
977 |
|
978 case endfunction_kw: |
|
979 yylval.tok_val = new token (token::function_end, l, c); |
|
980 break; |
|
981 |
|
982 case endif_kw: |
|
983 yylval.tok_val = new token (token::if_end, l, c); |
|
984 break; |
|
985 |
|
986 case endwhile_kw: |
|
987 yylval.tok_val = new token (token::while_end, l, c); |
|
988 break; |
|
989 |
|
990 case for_kw: |
|
991 case while_kw: |
|
992 promptflag--; |
1826
|
993 lexer_flags.looping++; |
1497
|
994 break; |
|
995 |
|
996 case if_kw: |
|
997 promptflag--; |
1826
|
998 lexer_flags.iffing++; |
1497
|
999 break; |
|
1000 |
|
1001 case try_kw: |
|
1002 case unwind_protect_kw: |
|
1003 promptflag--; |
|
1004 break; |
|
1005 |
|
1006 case gplot_kw: |
1826
|
1007 lexer_flags.plotting = 1; |
1497
|
1008 yylval.tok_val = new token (token::two_dee, l, c); |
|
1009 break; |
|
1010 |
|
1011 case gsplot_kw: |
1826
|
1012 lexer_flags.plotting = 1; |
1497
|
1013 yylval.tok_val = new token (token::three_dee, l, c); |
|
1014 break; |
|
1015 |
|
1016 case replot_kw: |
1826
|
1017 lexer_flags.plotting = 1; |
1497
|
1018 yylval.tok_val = new token (token::replot, l, c); |
|
1019 break; |
|
1020 |
|
1021 case function_kw: |
1826
|
1022 if (lexer_flags.defining_func) |
1497
|
1023 { |
|
1024 error ("function keyword invalid within a function body"); |
|
1025 |
|
1026 if ((reading_fcn_file || reading_script_file) |
1755
|
1027 && ! curr_fcn_file_name.empty ()) |
1497
|
1028 error ("defining new function near line %d of file `%s.m'", |
1755
|
1029 input_line_number, curr_fcn_file_name.c_str ()); |
1497
|
1030 else |
1755
|
1031 error ("defining new function near line %d", |
|
1032 input_line_number); |
1497
|
1033 |
|
1034 return LEXICAL_ERROR; |
|
1035 } |
|
1036 else |
|
1037 { |
|
1038 tmp_local_sym_tab = new symbol_table (); |
|
1039 curr_sym_tab = tmp_local_sym_tab; |
1826
|
1040 lexer_flags.defining_func = 1; |
1497
|
1041 promptflag--; |
1826
|
1042 lexer_flags.beginning_of_function = 1; |
1497
|
1043 if (! (reading_fcn_file || reading_script_file)) |
|
1044 input_line_number = 1; |
|
1045 } |
|
1046 break; |
|
1047 |
|
1048 default: |
|
1049 panic_impossible (); |
|
1050 } |
|
1051 |
|
1052 if (! yylval.tok_val) |
|
1053 yylval.tok_val = new token (l, c); |
|
1054 |
476
|
1055 token_stack.push (yylval.tok_val); |
1497
|
1056 |
|
1057 return kw->tok; |
143
|
1058 } |
1
|
1059 |
|
1060 return 0; |
|
1061 } |
|
1062 |
767
|
1063 // Try to find an identifier. All binding to global or builtin |
|
1064 // variables occurs when expressions are evaluated. |
|
1065 |
1
|
1066 static symbol_record * |
1823
|
1067 lookup_identifier (const string& name) |
1
|
1068 { |
|
1069 return curr_sym_tab->lookup (name, 1, 0); |
|
1070 } |
|
1071 |
1019
|
1072 // Grab the help text from an function file. Always overwrites the |
|
1073 // current contents of help_buf. |
767
|
1074 |
2300
|
1075 // XXX FIXME XXX -- gobble_leading_white_space() in variables.cc |
|
1076 // duplicates some of this code! |
|
1077 |
1
|
1078 static void |
|
1079 grab_help_text (void) |
|
1080 { |
1755
|
1081 help_buf.resize (0); |
1019
|
1082 |
2300
|
1083 bool begin_comment = true; |
|
1084 bool in_comment = true; |
1019
|
1085 int c = 0; |
1
|
1086 |
1019
|
1087 while ((c = yyinput ()) != EOF) |
|
1088 { |
2300
|
1089 if (begin_comment) |
|
1090 { |
|
1091 if (c == '%' || c == '#') |
|
1092 continue; |
|
1093 else |
|
1094 begin_comment = false; |
|
1095 } |
|
1096 |
1019
|
1097 if (in_comment) |
1
|
1098 { |
1755
|
1099 help_buf += (char) c; |
|
1100 |
1019
|
1101 if (c == '\n') |
2300
|
1102 in_comment = false; |
1019
|
1103 } |
|
1104 else |
|
1105 { |
|
1106 switch (c) |
991
|
1107 { |
1019
|
1108 case '%': |
|
1109 case '#': |
2300
|
1110 in_comment = true; |
|
1111 begin_comment = true; |
1019
|
1112 break; |
777
|
1113 |
1019
|
1114 case ' ': |
|
1115 case '\t': |
|
1116 break; |
777
|
1117 |
1019
|
1118 default: |
|
1119 goto done; |
1
|
1120 } |
|
1121 } |
1019
|
1122 } |
991
|
1123 |
1019
|
1124 done: |
991
|
1125 |
1019
|
1126 if (c) |
|
1127 yyunput (c, yytext); |
1
|
1128 } |
|
1129 |
767
|
1130 // Return 1 if the given character matches any character in the given |
|
1131 // string. |
|
1132 |
1
|
1133 static int |
|
1134 match_any (char c, char *s) |
|
1135 { |
|
1136 char tmp; |
|
1137 while ((tmp = *s++) != '\0') |
|
1138 { |
|
1139 if (c == tmp) |
|
1140 return 1; |
|
1141 } |
|
1142 return 0; |
|
1143 } |
|
1144 |
767
|
1145 // Given information about the spacing surrounding an operator, |
|
1146 // return 1 if it looks like it should be treated as a binary |
|
1147 // operator. For example, |
|
1148 // |
|
1149 // [ 1 + 2 ] or [ 1+ 2] or [ 1+2 ] ==> binary |
|
1150 |
1
|
1151 static int |
|
1152 looks_like_bin_op (int spc_prev, int spc_next) |
|
1153 { |
608
|
1154 return ((spc_prev && spc_next) || ! spc_prev); |
1
|
1155 } |
|
1156 |
767
|
1157 // Try to determine if the next token should be treated as a postfix |
|
1158 // unary operator. This is ugly, but it seems to do the right thing. |
|
1159 |
1
|
1160 static int |
|
1161 next_token_is_postfix_unary_op (int spc_prev, char *yytext) |
|
1162 { |
|
1163 int un_op = 0; |
|
1164 |
|
1165 int c0 = yyinput (); |
|
1166 int c1 = yyinput (); |
|
1167 |
|
1168 yyunput (c1, yytext); |
|
1169 yyunput (c0, yytext); |
|
1170 |
|
1171 int transpose = (c0 == '.' && c1 == '\''); |
|
1172 int hermitian = (c0 == '\''); |
|
1173 |
|
1174 un_op = (transpose || (hermitian && ! spc_prev)); |
|
1175 |
|
1176 return un_op; |
|
1177 } |
|
1178 |
767
|
1179 // Try to determine if the next token should be treated as a binary |
|
1180 // operator. This is even uglier, but it also seems to do the right |
1276
|
1181 // thing. Note that it is only necessary to check the spacing for `+' |
|
1182 // and `-', since those are the only tokens that can appear as unary |
|
1183 // ops too. |
1521
|
1184 // |
|
1185 // Note that this never returns true for `.', even though it can be a |
|
1186 // binary operator (the structure reference thing). The only time |
|
1187 // this appears to matter is for things like |
|
1188 // |
|
1189 // [ a . b ] |
|
1190 // |
|
1191 // which probably doesn't occur that often, can be worked around by |
|
1192 // eliminating the whitespace, putting the expression in parentheses, |
|
1193 // or using `whitespace_in_literal_matrix = "ignored"', so I think it |
|
1194 // is an acceptable change. It would be quite a bit harder to `fix' |
|
1195 // this. (Well, maybe not. the best fix would be to do away with the |
|
1196 // specialness of whitespace inside of `[ ... ]'). |
1554
|
1197 // |
|
1198 // However, we still do check for `.+', `.*', etc. |
767
|
1199 |
1
|
1200 static int |
|
1201 next_token_is_bin_op (int spc_prev, char *yytext) |
|
1202 { |
|
1203 int bin_op = 0; |
|
1204 |
|
1205 int c0 = yyinput (); |
|
1206 |
|
1207 switch (c0) |
|
1208 { |
777
|
1209 case '+': |
|
1210 case '-': |
1276
|
1211 { |
|
1212 int c1 = yyinput (); |
|
1213 yyunput (c1, yytext); |
|
1214 int spc_next = (c1 == ' ' || c1 == '\t'); |
|
1215 bin_op = looks_like_bin_op (spc_prev, spc_next); |
|
1216 } |
|
1217 break; |
|
1218 |
1554
|
1219 case '.': |
|
1220 { |
|
1221 int c1 = yyinput (); |
|
1222 yyunput (c1, yytext); |
|
1223 bin_op = match_any (c1, "+-*/\\^"); |
|
1224 } |
|
1225 break; |
|
1226 |
777
|
1227 case '/': |
|
1228 case ':': |
|
1229 case '\\': |
|
1230 case '^': |
1
|
1231 case '&': |
|
1232 case '*': |
|
1233 case '|': |
|
1234 case '<': |
|
1235 case '>': |
777
|
1236 case '~': |
|
1237 case '!': |
|
1238 case '=': |
1276
|
1239 bin_op = 1; |
1
|
1240 break; |
|
1241 |
|
1242 default: |
1276
|
1243 break; |
1
|
1244 } |
|
1245 |
|
1246 yyunput (c0, yytext); |
|
1247 |
|
1248 return bin_op; |
|
1249 } |
|
1250 |
767
|
1251 // Used to delete trailing white space from tokens. |
|
1252 |
1823
|
1253 static string |
1
|
1254 strip_trailing_whitespace (char *s) |
|
1255 { |
1823
|
1256 string retval = s; |
1
|
1257 |
1823
|
1258 size_t pos = retval.find_first_of (" \t"); |
1
|
1259 |
1823
|
1260 if (pos != NPOS) |
|
1261 retval.resize (pos); |
1
|
1262 |
|
1263 return retval; |
|
1264 } |
|
1265 |
1001
|
1266 // Discard whitespace, including comments and continuations. |
1088
|
1267 // |
|
1268 // Return value is logical OR of the following values: |
|
1269 // |
1826
|
1270 // ATE_NOTHING : no spaces to eat |
1088
|
1271 // ATE_SPACE_OR_TAB : space or tab in input |
|
1272 // ATE_NEWLINE : bare new line in input |
1001
|
1273 |
1826
|
1274 static yum_yum |
975
|
1275 eat_whitespace (void) |
|
1276 { |
1826
|
1277 yum_yum retval = ATE_NOTHING; |
975
|
1278 int in_comment = 0; |
|
1279 int c; |
|
1280 while ((c = yyinput ()) != EOF) |
|
1281 { |
|
1282 current_input_column++; |
|
1283 |
|
1284 switch (c) |
|
1285 { |
|
1286 case ' ': |
|
1287 case '\t': |
1088
|
1288 retval |= ATE_SPACE_OR_TAB; |
975
|
1289 break; |
|
1290 |
|
1291 case '\n': |
1088
|
1292 retval |= ATE_NEWLINE; |
975
|
1293 in_comment = 0; |
|
1294 current_input_column = 0; |
|
1295 break; |
|
1296 |
|
1297 case '#': |
|
1298 case '%': |
|
1299 in_comment = 1; |
|
1300 break; |
|
1301 |
1001
|
1302 case '.': |
|
1303 if (in_comment) |
|
1304 break; |
|
1305 else |
|
1306 { |
|
1307 if (have_ellipsis_continuation ()) |
|
1308 break; |
|
1309 else |
|
1310 goto done; |
|
1311 } |
|
1312 |
|
1313 case '\\': |
|
1314 if (in_comment) |
|
1315 break; |
|
1316 else |
|
1317 { |
|
1318 if (have_continuation ()) |
|
1319 break; |
|
1320 else |
|
1321 goto done; |
|
1322 } |
|
1323 |
975
|
1324 default: |
|
1325 if (in_comment) |
|
1326 break; |
|
1327 else |
|
1328 goto done; |
|
1329 } |
|
1330 } |
|
1331 |
|
1332 done: |
|
1333 yyunput (c, yytext); |
1082
|
1334 current_input_column--; |
1001
|
1335 return retval; |
975
|
1336 } |
|
1337 |
|
1338 static void |
972
|
1339 handle_number (char *yytext) |
|
1340 { |
|
1341 double value; |
|
1342 int nread = sscanf (yytext, "%lf", &value); |
|
1343 |
1826
|
1344 // If yytext doesn't contain a valid number, we are in deep doo doo. |
985
|
1345 |
972
|
1346 assert (nread == 1); |
|
1347 |
1826
|
1348 lexer_flags.quote_is_transpose = 1; |
|
1349 lexer_flags.cant_be_identifier = 1; |
|
1350 lexer_flags.convert_spaces_to_comma = 1; |
972
|
1351 |
1826
|
1352 if (lexer_flags.plotting && ! lexer_flags.in_plot_range) |
|
1353 lexer_flags.past_plot_range = 1; |
972
|
1354 |
|
1355 yylval.tok_val = new token (value, yytext, input_line_number, |
|
1356 current_input_column); |
|
1357 |
|
1358 token_stack.push (yylval.tok_val); |
|
1359 |
|
1360 current_input_column += yyleng; |
|
1361 |
|
1362 do_comma_insert_check (); |
|
1363 } |
|
1364 |
1001
|
1365 // We have seen a backslash and need to find out if it should be |
|
1366 // treated as a continuation character. If so, this eats it, up to |
|
1367 // and including the new line character. |
|
1368 // |
973
|
1369 // Match whitespace only, followed by a comment character or newline. |
|
1370 // Once a comment character is found, discard all input until newline. |
|
1371 // If non-whitespace characters are found before comment |
|
1372 // characters, return 0. Otherwise, return 1. |
|
1373 |
|
1374 static int |
1091
|
1375 have_continuation (int trailing_comments_ok) |
973
|
1376 { |
|
1377 ostrstream buf; |
|
1378 |
|
1379 int in_comment = 0; |
|
1380 char c; |
|
1381 while ((c = yyinput ()) != EOF) |
|
1382 { |
|
1383 buf << (char) c; |
|
1384 |
|
1385 switch (c) |
|
1386 { |
|
1387 case ' ': |
|
1388 case '\t': |
|
1389 break; |
|
1390 |
|
1391 case '%': |
|
1392 case '#': |
1091
|
1393 if (trailing_comments_ok) |
|
1394 in_comment = 1; |
|
1395 else |
|
1396 goto cleanup; |
973
|
1397 break; |
|
1398 |
|
1399 case '\n': |
975
|
1400 current_input_column = 0; |
1001
|
1401 promptflag--; |
973
|
1402 return 1; |
|
1403 |
|
1404 default: |
1091
|
1405 if (! in_comment) |
|
1406 goto cleanup; |
|
1407 break; |
973
|
1408 } |
|
1409 } |
|
1410 |
|
1411 yyunput (c, yytext); |
1091
|
1412 return 0; |
973
|
1413 |
1091
|
1414 cleanup: |
|
1415 buf << ends; |
|
1416 char *s = buf.str (); |
|
1417 if (s) |
|
1418 { |
|
1419 int len = strlen (s); |
|
1420 while (len--) |
|
1421 yyunput (s[len], yytext); |
|
1422 } |
|
1423 delete [] s; |
973
|
1424 return 0; |
|
1425 } |
|
1426 |
1001
|
1427 // We have seen a `.' and need to see if it is the start of a |
|
1428 // continuation. If so, this eats it, up to and including the new |
|
1429 // line character. |
|
1430 |
973
|
1431 static int |
1091
|
1432 have_ellipsis_continuation (int trailing_comments_ok) |
973
|
1433 { |
|
1434 char c1 = yyinput (); |
|
1435 if (c1 == '.') |
|
1436 { |
|
1437 char c2 = yyinput (); |
1091
|
1438 if (c2 == '.' && have_continuation (trailing_comments_ok)) |
973
|
1439 return 1; |
|
1440 else |
|
1441 { |
|
1442 yyunput (c2, yytext); |
|
1443 yyunput (c1, yytext); |
|
1444 } |
|
1445 } |
|
1446 else |
|
1447 yyunput (c1, yytext); |
|
1448 |
|
1449 return 0; |
|
1450 } |
|
1451 |
1001
|
1452 // See if we have a continuation line. If so, eat it and the leading |
|
1453 // whitespace on the next line. |
1088
|
1454 // |
|
1455 // Return value is the same as described for eat_whitespace(). |
1001
|
1456 |
1826
|
1457 static yum_yum |
1001
|
1458 eat_continuation (void) |
|
1459 { |
1826
|
1460 int retval = ATE_NOTHING; |
1001
|
1461 int c = yyinput (); |
|
1462 if ((c == '.' && have_ellipsis_continuation ()) |
|
1463 || (c == '\\' && have_continuation ())) |
|
1464 retval = eat_whitespace (); |
|
1465 else |
|
1466 yyunput (c, yytext); |
|
1467 |
|
1468 return retval; |
|
1469 } |
|
1470 |
973
|
1471 static int |
975
|
1472 handle_string (char delim, int text_style) |
973
|
1473 { |
|
1474 ostrstream buf; |
|
1475 |
|
1476 int c; |
1031
|
1477 int escape_pending = 0; |
973
|
1478 |
|
1479 while ((c = yyinput ()) != EOF) |
|
1480 { |
|
1481 current_input_column++; |
|
1482 |
|
1483 if (c == '\\') |
|
1484 { |
1053
|
1485 if (escape_pending) |
|
1486 { |
|
1487 buf << (char) c; |
|
1488 escape_pending = 0; |
|
1489 } |
|
1490 else |
|
1491 { |
1091
|
1492 if (have_continuation (0)) |
1053
|
1493 escape_pending = 0; |
|
1494 else |
|
1495 { |
|
1496 buf << (char) c; |
|
1497 escape_pending = 1; |
|
1498 } |
|
1499 } |
1031
|
1500 continue; |
973
|
1501 } |
|
1502 else if (c == '.') |
|
1503 { |
1091
|
1504 if (! have_ellipsis_continuation (0)) |
973
|
1505 buf << (char) c; |
|
1506 } |
|
1507 else if (c == '\n') |
|
1508 { |
1053
|
1509 error ("unterminated string constant"); |
973
|
1510 break; |
|
1511 } |
|
1512 else if (c == delim) |
|
1513 { |
1031
|
1514 if (escape_pending) |
973
|
1515 buf << (char) c; |
|
1516 else |
|
1517 { |
|
1518 c = yyinput (); |
|
1519 if (c == delim) |
|
1520 buf << (char) c; |
|
1521 else |
|
1522 { |
|
1523 yyunput (c, yytext); |
|
1524 buf << ends; |
|
1525 char *tok = buf.str (); |
|
1526 do_string_escapes (tok); |
975
|
1527 |
1826
|
1528 if (text_style && lexer_flags.doing_set) |
975
|
1529 { |
|
1530 if (tok) |
|
1531 { |
|
1532 int len = strlen (tok) + 3; |
|
1533 char *tmp = tok; |
|
1534 tok = new char [len]; |
|
1535 tok[0] = delim; |
|
1536 strcpy (tok+1, tmp); |
|
1537 tok[len-2] = delim; |
|
1538 tok[len-1] = '\0'; |
|
1539 delete [] tmp; |
|
1540 } |
|
1541 } |
|
1542 else |
|
1543 { |
1826
|
1544 lexer_flags.quote_is_transpose = 1; |
|
1545 lexer_flags.cant_be_identifier = 1; |
|
1546 lexer_flags.convert_spaces_to_comma = 1; |
975
|
1547 } |
|
1548 |
973
|
1549 yylval.tok_val = new token (tok); |
|
1550 delete [] tok; |
|
1551 token_stack.push (yylval.tok_val); |
|
1552 return TEXT; |
|
1553 } |
|
1554 } |
|
1555 } |
|
1556 else |
|
1557 { |
|
1558 buf << (char) c; |
|
1559 } |
|
1560 |
1031
|
1561 escape_pending = 0; |
973
|
1562 } |
|
1563 |
|
1564 return LEXICAL_ERROR; |
|
1565 } |
|
1566 |
971
|
1567 static int |
1001
|
1568 handle_close_brace (int spc_gobbled) |
971
|
1569 { |
1826
|
1570 if (! nesting_level.none ()) |
971
|
1571 { |
1826
|
1572 nesting_level.remove (); |
|
1573 lexer_flags.braceflag--; |
971
|
1574 } |
|
1575 |
1826
|
1576 if (lexer_flags.braceflag == 0) |
1001
|
1577 BEGIN 0; |
|
1578 |
971
|
1579 int c1 = yyinput (); |
|
1580 if (c1 == '=') |
|
1581 { |
1826
|
1582 lexer_flags.quote_is_transpose = 0; |
|
1583 lexer_flags.cant_be_identifier = 0; |
|
1584 lexer_flags.convert_spaces_to_comma = 1; |
971
|
1585 |
|
1586 int c2 = yyinput (); |
|
1587 unput (c2); |
|
1588 unput (c1); |
|
1589 |
1826
|
1590 if (c2 != '=' && lexer_flags.maybe_screwed_again) |
971
|
1591 return SCREW_TWO; |
|
1592 else |
|
1593 return ']'; |
|
1594 } |
|
1595 else |
|
1596 { |
|
1597 unput (c1); |
|
1598 |
2167
|
1599 if (lexer_flags.braceflag && Vwhitespace_in_literal_matrix != 2) |
971
|
1600 { |
1001
|
1601 int bin_op = next_token_is_bin_op (spc_gobbled, yytext); |
971
|
1602 int postfix_un_op = next_token_is_postfix_unary_op |
1001
|
1603 (spc_gobbled, yytext); |
971
|
1604 |
|
1605 int other_op = match_any (c1, ",;\n]"); |
|
1606 |
1826
|
1607 if (! (postfix_un_op || bin_op || other_op) |
|
1608 && nesting_level.is_brace () |
|
1609 && lexer_flags.convert_spaces_to_comma) |
971
|
1610 { |
|
1611 unput (','); |
|
1612 return ']'; |
|
1613 } |
|
1614 } |
|
1615 } |
|
1616 |
1826
|
1617 lexer_flags.quote_is_transpose = 1; |
|
1618 lexer_flags.cant_be_identifier = 0; |
|
1619 lexer_flags.convert_spaces_to_comma = 1; |
971
|
1620 return ']'; |
|
1621 } |
|
1622 |
1072
|
1623 static void |
|
1624 maybe_unput_comma (int spc_gobbled) |
|
1625 { |
2167
|
1626 if (Vwhitespace_in_literal_matrix != 2 |
1826
|
1627 && nesting_level.is_brace ()) |
1072
|
1628 { |
|
1629 int bin_op = next_token_is_bin_op (spc_gobbled, yytext); |
|
1630 |
|
1631 int postfix_un_op = next_token_is_postfix_unary_op (spc_gobbled, |
|
1632 yytext); |
|
1633 |
|
1634 int c1 = yyinput (); |
|
1635 int c2 = yyinput (); |
|
1636 unput (c2); |
|
1637 unput (c1); |
|
1638 int sep_op = match_any (c1, ",;\n]"); |
|
1639 int dot_op = (c1 == '.' |
|
1640 && (isalpha (c2) || isspace (c2) || c2 == '_')); |
|
1641 int index_op = (c1 == '(' |
2167
|
1642 && (Vwhitespace_in_literal_matrix == 0 |
1072
|
1643 || ! spc_gobbled)); |
|
1644 |
|
1645 if (! (postfix_un_op || bin_op || sep_op || dot_op || index_op)) |
|
1646 unput (','); |
|
1647 } |
|
1648 } |
|
1649 |
767
|
1650 // Figure out exactly what kind of token to return when we have seen |
|
1651 // an identifier. Handles keywords. |
|
1652 |
146
|
1653 static int |
1823
|
1654 handle_identifier (const string& tok, int spc_gobbled) |
146
|
1655 { |
1826
|
1656 // It is almost always an error for an identifier to be followed |
|
1657 // directly by another identifier. Special cases are handled |
|
1658 // below. |
883
|
1659 |
1826
|
1660 lexer_flags.cant_be_identifier = 1; |
883
|
1661 |
1826
|
1662 // If we are expecting a structure element, we just want to return |
|
1663 // TEXT_ID, which is a string that is also a valid identifier. But |
|
1664 // first, we have to decide whether to insert a comma. |
747
|
1665 |
1826
|
1666 if (lexer_flags.looking_at_indirect_ref) |
1072
|
1667 { |
|
1668 maybe_unput_comma (spc_gobbled); |
|
1669 TOK_PUSH_AND_RETURN (tok, TEXT_ID); |
|
1670 } |
747
|
1671 |
1826
|
1672 // If we have a regular keyword, or a plot STYLE, return it. |
|
1673 // Keywords can be followed by identifiers (TOK_RETURN handles |
|
1674 // that). |
146
|
1675 |
|
1676 int kw_token = is_keyword (tok); |
|
1677 if (kw_token) |
|
1678 { |
|
1679 if (kw_token == STYLE) |
|
1680 { |
1060
|
1681 current_input_column += yyleng; |
1826
|
1682 lexer_flags.quote_is_transpose = 0; |
|
1683 lexer_flags.convert_spaces_to_comma = 1; |
146
|
1684 return kw_token; |
|
1685 } |
|
1686 else |
|
1687 TOK_RETURN (kw_token); |
|
1688 } |
|
1689 |
1826
|
1690 // See if we have a plot keyword (title, using, with, or clear). |
146
|
1691 |
1826
|
1692 if (lexer_flags.plotting) |
941
|
1693 { |
1826
|
1694 // Yes, we really do need both of these plot_range variables. |
|
1695 // One is used to mark when we are past all possiblity of a plot |
|
1696 // range, the other is used to mark when we are actually between |
|
1697 // the square brackets that surround the range. |
146
|
1698 |
1826
|
1699 if (! lexer_flags.in_plot_range) |
|
1700 lexer_flags.past_plot_range = 1; |
941
|
1701 |
1273
|
1702 // Option keywords can't appear in parentheses or braces. |
|
1703 |
|
1704 int plot_option_kw = 0; |
1826
|
1705 if (nesting_level.none ()) |
1273
|
1706 plot_option_kw = is_plot_keyword (tok); |
941
|
1707 |
1826
|
1708 if (lexer_flags.cant_be_identifier && plot_option_kw) |
941
|
1709 TOK_RETURN (plot_option_kw); |
|
1710 } |
146
|
1711 |
1826
|
1712 // If we are looking at a text style function, set up to gobble its |
|
1713 // arguments. These are also reserved words, but only because it |
|
1714 // would be very difficult to do anything intelligent with them if |
|
1715 // they were not reserved. |
146
|
1716 |
|
1717 if (is_text_function_name (tok)) |
|
1718 { |
|
1719 BEGIN TEXT_FCN; |
|
1720 |
2524
|
1721 if (tok == "gset") |
1826
|
1722 lexer_flags.doing_set = 1; |
146
|
1723 } |
|
1724 |
1001
|
1725 int c = yyinput (); |
|
1726 yyunput (c, yytext); |
|
1727 int next_tok_is_eq = (c == '='); |
|
1728 |
1826
|
1729 // Make sure we put the return values of a function in the symbol |
|
1730 // table that is local to the function. |
146
|
1731 |
1826
|
1732 if (next_tok_is_eq |
|
1733 && lexer_flags.defining_func && lexer_flags.maybe_screwed) |
146
|
1734 curr_sym_tab = tmp_local_sym_tab; |
|
1735 |
1826
|
1736 // Find the token in the symbol table. |
146
|
1737 |
|
1738 yylval.tok_val = new token (lookup_identifier (tok), |
|
1739 input_line_number, |
|
1740 current_input_column); |
|
1741 |
|
1742 token_stack.push (yylval.tok_val); |
|
1743 |
1826
|
1744 // After seeing an identifer, it is ok to convert spaces to a comma |
|
1745 // (if needed). |
146
|
1746 |
1826
|
1747 lexer_flags.convert_spaces_to_comma = 1; |
146
|
1748 |
1826
|
1749 // If we are defining a function and we have not seen the parameter |
|
1750 // list yet and the next token is `=', return a token that |
|
1751 // represents the only return value for the function. For example, |
|
1752 // |
|
1753 // function SCREW = f (args); |
|
1754 // |
|
1755 // The variable maybe_screwed is reset in parse.y. |
146
|
1756 |
|
1757 if (next_tok_is_eq) |
|
1758 { |
1060
|
1759 current_input_column += yyleng; |
1826
|
1760 if (lexer_flags.defining_func && lexer_flags.maybe_screwed) |
146
|
1761 return SCREW; |
|
1762 else |
|
1763 return NAME; |
|
1764 } |
|
1765 |
1826
|
1766 // At this point, we are only dealing with identifiers that are not |
|
1767 // followed by `=' (if the next token is `=', there is no need to |
|
1768 // check to see if we should insert a comma (invalid syntax), or |
|
1769 // allow a following `'' to be treated as a transpose (the next |
|
1770 // token is `=', so it can't be `''. |
146
|
1771 |
1826
|
1772 lexer_flags.quote_is_transpose = 1; |
146
|
1773 do_comma_insert_check (); |
|
1774 |
1072
|
1775 maybe_unput_comma (spc_gobbled); |
146
|
1776 |
1060
|
1777 current_input_column += yyleng; |
146
|
1778 return NAME; |
|
1779 } |
|
1780 |
767
|
1781 // Print a warning if a function file that defines a function has |
|
1782 // anything other than comments and whitespace following the END token |
|
1783 // that matches the FUNCTION statement. |
|
1784 |
1
|
1785 void |
|
1786 check_for_garbage_after_fcn_def (void) |
|
1787 { |
1826
|
1788 // By making a newline be the next character to be read, we will |
|
1789 // force the parser to return after reading the function. Calling |
|
1790 // yyunput with EOF seems not to work... |
1
|
1791 |
|
1792 int in_comment = 0; |
|
1793 int lineno = input_line_number; |
|
1794 int c; |
|
1795 while ((c = yyinput ()) != EOF) |
|
1796 { |
|
1797 switch (c) |
|
1798 { |
|
1799 case ' ': |
|
1800 case '\t': |
|
1801 case ';': |
|
1802 case ',': |
|
1803 break; |
777
|
1804 |
1
|
1805 case '\n': |
|
1806 if (in_comment) |
|
1807 in_comment = 0; |
|
1808 break; |
777
|
1809 |
1
|
1810 case '%': |
|
1811 case '#': |
|
1812 in_comment = 1; |
|
1813 break; |
777
|
1814 |
1
|
1815 default: |
|
1816 if (in_comment) |
|
1817 break; |
|
1818 else |
|
1819 { |
|
1820 warning ("ignoring trailing garbage after end of function\n\ |
1755
|
1821 near line %d of file `%s.m'", lineno, curr_fcn_file_name.c_str ()); |
1
|
1822 |
|
1823 yyunput ('\n', yytext); |
|
1824 return; |
|
1825 } |
|
1826 } |
|
1827 } |
|
1828 yyunput ('\n', yytext); |
|
1829 } |
|
1830 |
1826
|
1831 void |
|
1832 lexical_feedback::init (void) |
|
1833 { |
|
1834 // Not initially defining a function. |
|
1835 beginning_of_function = 0; |
|
1836 defining_func = 0; |
|
1837 |
|
1838 // Not initially defining a matrix list. |
|
1839 braceflag = 0; |
|
1840 |
|
1841 // Next token can be identifier. |
|
1842 cant_be_identifier = 0; |
767
|
1843 |
1826
|
1844 // No need to do comma insert or convert spaces to comma at |
|
1845 // beginning of input. |
|
1846 convert_spaces_to_comma = 1; |
|
1847 do_comma_insert = 0; |
|
1848 |
|
1849 // Not initially doing any plotting or setting of plot attributes. |
|
1850 doing_set = 0; |
|
1851 in_plot_range = 0; |
|
1852 in_plot_style = 0; |
|
1853 in_plot_using = 0; |
|
1854 past_plot_range = 0; |
|
1855 plotting = 0; |
1
|
1856 |
1826
|
1857 // Not initially inside a loop or if statement. |
|
1858 iffing = 0; |
|
1859 looping = 0; |
|
1860 |
|
1861 // Not initially looking at indirect references. |
|
1862 looking_at_indirect_ref = 0; |
|
1863 |
|
1864 // Not initially screwed by `function [...] = f (...)' syntax. |
|
1865 maybe_screwed = 0; |
|
1866 maybe_screwed_again = 0; |
1
|
1867 |
1826
|
1868 // Quote marks strings intially. |
|
1869 quote_is_transpose = 0; |
|
1870 } |
|
1871 |
2167
|
1872 int |
|
1873 whitespace_in_literal_matrix (void) |
|
1874 { |
|
1875 int pref = 0; |
|
1876 string val = builtin_string_variable ("whitespace_in_literal_matrix"); |
|
1877 if (! val.empty ()) |
|
1878 { |
|
1879 if (val.compare ("ignore", 0, 6) == 0) |
|
1880 pref = 2; |
|
1881 else if (val.compare ("traditional", 0, 11) == 0) |
|
1882 pref = 1; |
|
1883 } |
|
1884 Vwhitespace_in_literal_matrix = pref; |
|
1885 return 0; |
|
1886 } |
|
1887 |
|
1888 void |
|
1889 symbols_of_lex (void) |
|
1890 { |
|
1891 DEFVAR (whitespace_in_literal_matrix, "", 0, whitespace_in_literal_matrix, |
|
1892 "control auto-insertion of commas and semicolons in literal matrices"); |
|
1893 } |
|
1894 |
1826
|
1895 // Maybe someday... |
|
1896 // |
|
1897 // "+=" return ADD_EQ; |
|
1898 // "-=" return SUB_EQ; |
|
1899 // "*=" return MUL_EQ; |
|
1900 // "/=" return DIV_EQ; |
|
1901 // "\\=" return LEFTDIV_EQ; |
|
1902 // ".+=" return ADD_EQ; |
|
1903 // ".-=" return SUB_EQ; |
|
1904 // ".*=" return EMUL_EQ; |
|
1905 // "./=" return EDIV_EQ; |
|
1906 // ".\\=" return ELEFTDIV_EQ; |
1994
|
1907 |
|
1908 /* |
|
1909 ;;; Local Variables: *** |
|
1910 ;;; mode: C++ *** |
|
1911 ;;; End: *** |
|
1912 */ |