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