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