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