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