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