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++; |
|
612 quote_is_transpose = 1; |
611
|
613 convert_spaces_to_comma = (! in_brace_or_paren.empty () |
|
614 && in_brace_or_paren.top ()); |
1
|
615 return ')'; |
|
616 } |
|
617 |
|
618 . { |
|
619 |
|
620 // We return everything else as single character tokens, which should |
|
621 // eventually result in a parse error. |
|
622 |
143
|
623 TOK_RETURN (yytext[0]); |
1
|
624 } |
|
625 |
|
626 %% |
|
627 |
767
|
628 // GAG. |
|
629 // |
|
630 // If we're reading a matrix and the next character is '[', make sure |
|
631 // that we insert a comma ahead of it. |
|
632 |
146
|
633 void |
1
|
634 do_comma_insert_check (void) |
|
635 { |
|
636 int c = yyinput (); |
146
|
637 yyunput (c, yytext); |
1
|
638 do_comma_insert = (braceflag && c == '['); |
|
639 } |
|
640 |
767
|
641 // Fix things up for errors or interrupts. The parser is never called |
|
642 // recursively, so it is always safe to reinitialize its state before |
|
643 // doing any parsing. |
|
644 |
1
|
645 void |
|
646 reset_parser (void) |
|
647 { |
287
|
648 // Start off on the right foot. |
1
|
649 BEGIN 0; |
166
|
650 error_state = 0; |
287
|
651 |
|
652 // We do want a prompt by default. |
1
|
653 promptflag = 1; |
287
|
654 |
|
655 // Not initially screwed by `function [...] = f (...)' syntax. |
1
|
656 maybe_screwed = 0; |
|
657 maybe_screwed_again = 0; |
287
|
658 |
|
659 // Not initially inside a loop or if statement. |
1
|
660 looping = 0; |
|
661 iffing = 0; |
287
|
662 |
|
663 // Quote marks strings intially. |
|
664 quote_is_transpose = 0; |
|
665 |
|
666 // Next token can be identifier. |
|
667 cant_be_identifier = 0; |
|
668 |
|
669 // No need to do comma insert or convert spaces to comma at beginning |
|
670 // of input. |
|
671 do_comma_insert = 0; |
|
672 convert_spaces_to_comma = 1; |
|
673 |
|
674 // Not initially defining a function. |
|
675 beginning_of_function = 0; |
|
676 defining_func = 0; |
|
677 |
|
678 // Not initially doing any plotting or setting of plot attributes. |
|
679 plotting = 0; |
|
680 in_plot_range = 0; |
|
681 past_plot_range = 0; |
|
682 in_plot_using = 0; |
|
683 in_plot_style = 0; |
|
684 doing_set = 0; |
|
685 |
747
|
686 // Not initially looking at indirect references. |
|
687 looking_at_indirect_ref = 0; |
|
688 |
287
|
689 // Error may have occurred inside some parentheses or braces. |
|
690 in_brace_or_paren.clear (); |
|
691 |
|
692 // Not initially defining a matrix list. |
|
693 braceflag = 0; |
1
|
694 ml.clear (); |
|
695 mlnm.clear (); |
287
|
696 |
|
697 // Clear out the stack of token info used to track line and column |
|
698 // numbers. |
143
|
699 while (! token_stack.empty ()) |
|
700 delete token_stack.pop (); |
287
|
701 |
|
702 // Can be reset by defining a function. |
|
703 current_input_column = 1; |
373
|
704 if (! reading_script_file) |
|
705 input_line_number = current_command_number - 1; |
287
|
706 |
|
707 // Only ask for input from stdin if we are expecting interactive |
|
708 // input. |
338
|
709 if (interactive && ! (reading_fcn_file || get_input_from_eval_string)) |
287
|
710 yyrestart (stdin); |
1
|
711 } |
|
712 |
767
|
713 // Replace backslash escapes in a string with the real values. |
|
714 |
1
|
715 static void |
|
716 do_string_escapes (char *s) |
|
717 { |
|
718 char *p1 = s; |
|
719 char *p2 = s; |
|
720 while (*p2 != '\0') |
|
721 { |
|
722 if (*p2 == '\\' && *(p2+1) != '\0') |
|
723 { |
|
724 switch (*++p2) |
|
725 { |
|
726 case 'a': |
|
727 *p1 = '\a'; |
|
728 break; |
777
|
729 |
1
|
730 case 'b': // backspace |
|
731 *p1 = '\b'; |
|
732 break; |
777
|
733 |
1
|
734 case 'f': // formfeed |
|
735 *p1 = '\f'; |
|
736 break; |
777
|
737 |
1
|
738 case 'n': // newline |
|
739 *p1 = '\n'; |
|
740 break; |
777
|
741 |
1
|
742 case 'r': // carriage return |
|
743 *p1 = '\r'; |
|
744 break; |
777
|
745 |
1
|
746 case 't': // horizontal tab |
|
747 *p1 = '\t'; |
|
748 break; |
777
|
749 |
1
|
750 case 'v': // vertical tab |
|
751 *p1 = '\v'; |
|
752 break; |
777
|
753 |
1
|
754 case '\\': // backslash |
|
755 *p1 = '\\'; |
|
756 break; |
777
|
757 |
1
|
758 case '\'': // quote |
|
759 *p1 = '\''; |
|
760 break; |
777
|
761 |
1
|
762 case '"': // double quote |
|
763 *p1 = '"'; |
|
764 break; |
777
|
765 |
1
|
766 default: |
777
|
767 warning ("unrecognized escape sequence `\\%c' --\ |
|
768 converting to `%c'", *p2, *p2); |
1
|
769 *p1 = *p2; |
|
770 break; |
|
771 } |
|
772 } |
|
773 else if (*p2 == '\'' && *(p2+1) == '\'') |
|
774 { |
|
775 *p1 = '\''; |
|
776 p2++; |
|
777 } |
|
778 else |
|
779 { |
|
780 *p1 = *p2; |
|
781 } |
|
782 |
|
783 p1++; |
|
784 p2++; |
|
785 } |
|
786 |
|
787 *p1 = '\0'; |
|
788 } |
|
789 |
767
|
790 // If we read some newlines, we need figure out what column we're |
|
791 // really looking at. |
|
792 |
1
|
793 static void |
|
794 fixup_column_count (char *s) |
|
795 { |
|
796 char c; |
|
797 while ((c = *s++) != '\0') |
|
798 { |
|
799 if (c == '\n') |
143
|
800 current_input_column = 1; |
1
|
801 else |
|
802 current_input_column++; |
|
803 } |
|
804 } |
|
805 |
767
|
806 // Include these so that we don't have to link to libfl.a. |
246
|
807 |
1
|
808 #ifdef yywrap |
|
809 #undef yywrap |
|
810 #endif |
246
|
811 static int |
1
|
812 yywrap (void) |
|
813 { |
287
|
814 return 1; |
1
|
815 } |
|
816 |
767
|
817 // These are not needed with flex-2.4.6, but may be needed with |
|
818 // earlier 2.4.x versions. |
|
819 |
287
|
820 #if 0 |
246
|
821 static void * |
|
822 yy_flex_alloc (int size) |
|
823 { |
|
824 return (void *) malloc ((unsigned) size); |
|
825 } |
|
826 |
|
827 static void * |
|
828 yy_flex_realloc (void *ptr, int size) |
|
829 { |
|
830 return (void *) realloc (ptr, (unsigned) size); |
|
831 } |
|
832 |
|
833 static void |
|
834 yy_flex_free (void *ptr) |
|
835 { |
|
836 free (ptr); |
|
837 } |
287
|
838 #endif |
246
|
839 |
767
|
840 // Tell us all what the current buffer is. |
|
841 |
1
|
842 YY_BUFFER_STATE |
|
843 current_buffer (void) |
|
844 { |
|
845 return YY_CURRENT_BUFFER; |
|
846 } |
|
847 |
767
|
848 // Create a new buffer. |
|
849 |
1
|
850 YY_BUFFER_STATE |
|
851 create_buffer (FILE *f) |
|
852 { |
|
853 return yy_create_buffer (f, YY_BUF_SIZE); |
|
854 } |
|
855 |
767
|
856 // Start reading a new buffer. |
|
857 |
1
|
858 void |
|
859 switch_to_buffer (YY_BUFFER_STATE buf) |
|
860 { |
|
861 yy_switch_to_buffer (buf); |
|
862 } |
|
863 |
767
|
864 // Delete a buffer. |
|
865 |
1
|
866 void |
|
867 delete_buffer (YY_BUFFER_STATE buf) |
|
868 { |
|
869 yy_delete_buffer (buf); |
|
870 } |
|
871 |
767
|
872 // Restore a buffer (for unwind-prot). |
|
873 |
1
|
874 void |
|
875 restore_input_buffer (void *buf) |
|
876 { |
|
877 switch_to_buffer ((YY_BUFFER_STATE) buf); |
|
878 } |
|
879 |
767
|
880 // Delete a buffer (for unwind-prot). |
|
881 |
1
|
882 void |
|
883 delete_input_buffer (void *buf) |
|
884 { |
|
885 delete_buffer ((YY_BUFFER_STATE) buf); |
|
886 } |
|
887 |
767
|
888 // Check to see if a character string matches any of the possible line |
|
889 // styles for plots. |
|
890 |
1
|
891 static char * |
|
892 plot_style_token (char *s) |
|
893 { |
146
|
894 static char *plot_styles[] = |
|
895 { |
|
896 "dots", |
|
897 "errorbars", |
|
898 "impulses", |
|
899 "lines", |
|
900 "linespoints", |
|
901 "points", |
522
|
902 0, |
146
|
903 }; |
|
904 |
1
|
905 char **tmp = plot_styles; |
522
|
906 while (*tmp) |
1
|
907 { |
|
908 if (almost_match (*tmp, s)) |
|
909 return *tmp; |
|
910 |
|
911 tmp++; |
|
912 } |
|
913 |
522
|
914 return 0; |
1
|
915 } |
|
916 |
767
|
917 // Check to see if a character string matches any one of the plot |
|
918 // option keywords. |
|
919 |
1
|
920 static int |
|
921 is_plot_keyword (char *s) |
|
922 { |
|
923 if (almost_match ("title", s)) |
146
|
924 { |
|
925 return TITLE; |
|
926 } |
1
|
927 else if (almost_match ("using", s)) |
146
|
928 { |
|
929 in_plot_using = 1; |
|
930 past_plot_range = 1; |
|
931 return USING; |
|
932 } |
1
|
933 else if (almost_match ("with", s)) |
146
|
934 { |
|
935 in_plot_style = 1; |
|
936 past_plot_range = 1; |
|
937 return WITH; |
|
938 } |
1
|
939 else |
146
|
940 { |
|
941 return 0; |
|
942 } |
1
|
943 } |
|
944 |
767
|
945 // Handle keywords. Could probably be more efficient... |
|
946 |
1
|
947 static int |
|
948 is_keyword (char *s) |
|
949 { |
|
950 if (plotting && in_plot_style) |
|
951 { |
|
952 char *sty = plot_style_token (s); |
522
|
953 if (sty) |
1
|
954 { |
|
955 in_plot_style = 0; |
143
|
956 yylval.tok_val = new token (sty); |
|
957 token_stack.push (yylval.tok_val); |
1
|
958 return STYLE; |
|
959 } |
|
960 } |
|
961 |
143
|
962 int l = input_line_number; |
|
963 int c = current_input_column; |
|
964 |
1
|
965 int end_found = 0; |
|
966 if (strcmp ("break", s) == 0) |
143
|
967 { |
191
|
968 yylval.tok_val = new token (l, c); |
|
969 token_stack.push (yylval.tok_val); |
143
|
970 return BREAK; |
|
971 } |
1
|
972 else if (strcmp ("continue", s) == 0) |
143
|
973 { |
191
|
974 yylval.tok_val = new token (l, c); |
|
975 token_stack.push (yylval.tok_val); |
143
|
976 return CONTINUE; |
|
977 } |
1
|
978 else if (strcmp ("else", s) == 0) |
143
|
979 { |
191
|
980 yylval.tok_val = new token (l, c); |
|
981 token_stack.push (yylval.tok_val); |
143
|
982 return ELSE; |
|
983 } |
1
|
984 else if (strcmp ("elseif", s) == 0) |
143
|
985 { |
191
|
986 yylval.tok_val = new token (l, c); |
|
987 token_stack.push (yylval.tok_val); |
143
|
988 return ELSEIF; |
|
989 } |
1
|
990 else if (strcmp ("end", s) == 0) |
143
|
991 { |
|
992 end_found = 1; |
|
993 yylval.tok_val = new token (token::simple_end, l, c); |
|
994 token_stack.push (yylval.tok_val); |
|
995 } |
1
|
996 else if (strcmp ("endfor", s) == 0) |
143
|
997 { |
|
998 end_found = 1; |
|
999 yylval.tok_val = new token (token::for_end, l, c); |
|
1000 token_stack.push (yylval.tok_val); |
|
1001 } |
1
|
1002 else if (strcmp ("endfunction", s) == 0) |
143
|
1003 { |
|
1004 end_found = 1; |
|
1005 yylval.tok_val = new token (token::function_end, l, c); |
|
1006 token_stack.push (yylval.tok_val); |
|
1007 } |
1
|
1008 else if (strcmp ("endif", s) == 0) |
143
|
1009 { |
|
1010 end_found = 1; |
|
1011 yylval.tok_val = new token (token::if_end, l, c); |
|
1012 token_stack.push (yylval.tok_val); |
|
1013 } |
1
|
1014 else if (strcmp ("endwhile", s) == 0) |
143
|
1015 { |
|
1016 end_found = 1; |
|
1017 yylval.tok_val = new token (token::while_end, l, c); |
|
1018 token_stack.push (yylval.tok_val); |
|
1019 } |
1
|
1020 else if (strcmp ("for", s) == 0) |
143
|
1021 { |
|
1022 promptflag--; |
|
1023 looping++; |
191
|
1024 yylval.tok_val = new token (l, c); |
|
1025 token_stack.push (yylval.tok_val); |
143
|
1026 return FOR; |
|
1027 } |
1
|
1028 else if (strcmp ("function", s) == 0) |
|
1029 { |
|
1030 if (defining_func) |
|
1031 { |
191
|
1032 error ("function keyword invalid within a function body"); |
|
1033 |
338
|
1034 if ((reading_fcn_file || reading_script_file) |
522
|
1035 && curr_fcn_file_name) |
341
|
1036 error ("defining new function near line %d of file `%s.m'", |
|
1037 input_line_number, curr_fcn_file_name); |
191
|
1038 else |
|
1039 error ("defining new function near line %d", input_line_number); |
|
1040 |
627
|
1041 return LEXICAL_ERROR; |
1
|
1042 } |
|
1043 else |
|
1044 { |
|
1045 tmp_local_sym_tab = new symbol_table (); |
|
1046 curr_sym_tab = tmp_local_sym_tab; |
|
1047 defining_func = 1; |
|
1048 promptflag--; |
|
1049 beginning_of_function = 1; |
|
1050 help_buf[0] = '\0'; |
143
|
1051 input_line_number = 1; |
1
|
1052 return FCN; |
|
1053 } |
|
1054 } |
|
1055 else if (strcmp ("global", s) == 0) |
143
|
1056 { |
578
|
1057 yylval.tok_val = new token (l, c); |
|
1058 token_stack.push (yylval.tok_val); |
143
|
1059 return GLOBAL; |
|
1060 } |
1
|
1061 else if (strcmp ("gplot", s) == 0) |
143
|
1062 { |
|
1063 plotting = 1; |
|
1064 yylval.tok_val = new token (token::two_dee, l, c); |
476
|
1065 token_stack.push (yylval.tok_val); |
143
|
1066 return PLOT; |
|
1067 } |
1
|
1068 else if (strcmp ("gsplot", s) == 0) |
143
|
1069 { |
|
1070 plotting = 1; |
|
1071 yylval.tok_val = new token (token::three_dee, l, c); |
|
1072 token_stack.push (yylval.tok_val); |
|
1073 return PLOT; |
|
1074 } |
476
|
1075 else if (strcmp ("replot", s) == 0) |
|
1076 { |
|
1077 plotting = 1; |
|
1078 yylval.tok_val = new token (token::replot, l, c); |
|
1079 token_stack.push (yylval.tok_val); |
|
1080 return PLOT; |
|
1081 } |
1
|
1082 else if (strcmp ("if", s) == 0) |
143
|
1083 { |
|
1084 iffing++; |
|
1085 promptflag--; |
191
|
1086 yylval.tok_val = new token (l, c); |
|
1087 token_stack.push (yylval.tok_val); |
143
|
1088 return IF; |
|
1089 } |
1
|
1090 else if (strcmp ("return", s) == 0) |
143
|
1091 { |
191
|
1092 yylval.tok_val = new token (l, c); |
|
1093 token_stack.push (yylval.tok_val); |
143
|
1094 return FUNC_RET; |
|
1095 } |
1
|
1096 else if (strcmp ("while", s) == 0) |
143
|
1097 { |
|
1098 promptflag--; |
|
1099 looping++; |
191
|
1100 yylval.tok_val = new token (l, c); |
|
1101 token_stack.push (yylval.tok_val); |
143
|
1102 return WHILE; |
|
1103 } |
1
|
1104 |
|
1105 if (end_found) |
|
1106 { |
143
|
1107 if (! defining_func && ! looping) |
1
|
1108 promptflag++; |
|
1109 return END; |
|
1110 } |
|
1111 |
|
1112 return 0; |
|
1113 } |
|
1114 |
767
|
1115 // Try to find an identifier. All binding to global or builtin |
|
1116 // variables occurs when expressions are evaluated. |
|
1117 |
1
|
1118 static symbol_record * |
|
1119 lookup_identifier (char *name) |
|
1120 { |
|
1121 return curr_sym_tab->lookup (name, 1, 0); |
|
1122 } |
|
1123 |
767
|
1124 // Grab the help text from an function file. |
|
1125 |
1
|
1126 static void |
|
1127 grab_help_text (void) |
|
1128 { |
|
1129 int max_len = HELP_BUF_LENGTH - 1; |
|
1130 |
|
1131 int in_comment = 1; |
|
1132 int len = 0; |
576
|
1133 int c = 0; |
1
|
1134 |
|
1135 while ((c = yyinput ()) != EOF) |
|
1136 { |
|
1137 if (in_comment) |
|
1138 { |
|
1139 help_buf[len++] = c; |
|
1140 if (c == '\n') |
|
1141 in_comment = 0; |
|
1142 } |
|
1143 else |
|
1144 { |
|
1145 switch (c) |
|
1146 { |
|
1147 case '%': |
|
1148 case '#': |
|
1149 in_comment = 1; |
777
|
1150 break; |
|
1151 |
1
|
1152 case ' ': |
|
1153 case '\t': |
446
|
1154 break; |
777
|
1155 |
440
|
1156 default: |
446
|
1157 goto done; |
1
|
1158 } |
|
1159 } |
|
1160 |
|
1161 if (len > max_len) |
|
1162 { |
216
|
1163 warning ("grab_help_text: buffer overflow after caching %d chars", |
1
|
1164 max_len); |
440
|
1165 break; |
1
|
1166 } |
|
1167 } |
|
1168 |
446
|
1169 done: |
|
1170 |
576
|
1171 if (c) |
|
1172 yyunput (c, yytext); |
|
1173 |
1
|
1174 help_buf[len] = '\0'; |
|
1175 } |
|
1176 |
767
|
1177 // Return 1 if the given character matches any character in the given |
|
1178 // string. |
|
1179 |
1
|
1180 static int |
|
1181 match_any (char c, char *s) |
|
1182 { |
|
1183 char tmp; |
|
1184 while ((tmp = *s++) != '\0') |
|
1185 { |
|
1186 if (c == tmp) |
|
1187 return 1; |
|
1188 } |
|
1189 return 0; |
|
1190 } |
|
1191 |
767
|
1192 // Given information about the spacing surrounding an operator, |
|
1193 // return 1 if it looks like it should be treated as a binary |
|
1194 // operator. For example, |
|
1195 // |
|
1196 // [ 1 + 2 ] or [ 1+ 2] or [ 1+2 ] ==> binary |
|
1197 |
1
|
1198 static int |
|
1199 looks_like_bin_op (int spc_prev, int spc_next) |
|
1200 { |
608
|
1201 return ((spc_prev && spc_next) || ! spc_prev); |
1
|
1202 } |
|
1203 |
767
|
1204 // Duh. |
|
1205 |
1
|
1206 static int |
|
1207 next_char_is_space (void) |
|
1208 { |
|
1209 int c = yyinput (); |
|
1210 yyunput (c, yytext); |
|
1211 return (c == ' ' || c == '\t'); |
|
1212 } |
|
1213 |
767
|
1214 // Try to determine if the next token should be treated as a postfix |
|
1215 // unary operator. This is ugly, but it seems to do the right thing. |
|
1216 |
1
|
1217 static int |
|
1218 next_token_is_postfix_unary_op (int spc_prev, char *yytext) |
|
1219 { |
|
1220 int un_op = 0; |
|
1221 |
|
1222 int c0 = yyinput (); |
|
1223 int c1 = yyinput (); |
|
1224 |
|
1225 yyunput (c1, yytext); |
|
1226 yyunput (c0, yytext); |
|
1227 |
|
1228 int transpose = (c0 == '.' && c1 == '\''); |
|
1229 int hermitian = (c0 == '\''); |
|
1230 |
|
1231 un_op = (transpose || (hermitian && ! spc_prev)); |
|
1232 |
|
1233 return un_op; |
|
1234 } |
|
1235 |
767
|
1236 // Try to determine if the next token should be treated as a binary |
|
1237 // operator. This is even uglier, but it also seems to do the right |
|
1238 // thing. |
|
1239 |
1
|
1240 static int |
|
1241 next_token_is_bin_op (int spc_prev, char *yytext) |
|
1242 { |
|
1243 int bin_op = 0; |
|
1244 int spc_next = 0; |
|
1245 |
|
1246 int c0 = yyinput (); |
|
1247 int c1 = yyinput (); |
|
1248 |
|
1249 switch (c0) |
|
1250 { |
777
|
1251 case '+': |
|
1252 case '-': |
|
1253 case '/': |
|
1254 case ':': |
|
1255 case '\\': |
|
1256 case '^': |
1
|
1257 spc_next = (c1 == ' ' || c1 == '\t'); |
|
1258 break; |
|
1259 |
|
1260 case '&': |
|
1261 if (c1 == '&') |
|
1262 spc_next = next_char_is_space (); |
|
1263 else |
|
1264 spc_next = (c1 == ' ' || c1 == '\t'); |
|
1265 break; |
|
1266 |
|
1267 case '*': |
|
1268 if (c1 == '*') |
|
1269 spc_next = next_char_is_space (); |
|
1270 else |
|
1271 spc_next = (c1 == ' ' || c1 == '\t'); |
|
1272 break; |
|
1273 |
|
1274 case '|': |
|
1275 if (c1 == '|') |
|
1276 spc_next = next_char_is_space (); |
|
1277 else |
|
1278 spc_next = (c1 == ' ' || c1 == '\t'); |
|
1279 break; |
|
1280 |
|
1281 case '<': |
|
1282 if (c1 == '=' || c1 == '>') |
|
1283 spc_next = next_char_is_space (); |
|
1284 else |
|
1285 spc_next = (c1 == ' ' || c1 == '\t'); |
|
1286 break; |
|
1287 |
|
1288 case '>': |
|
1289 if (c1 == '=') |
|
1290 spc_next = next_char_is_space (); |
|
1291 else |
|
1292 spc_next = (c1 == ' ' || c1 == '\t'); |
|
1293 break; |
|
1294 |
777
|
1295 case '~': |
|
1296 case '!': |
|
1297 case '=': |
1
|
1298 if (c1 == '=') |
|
1299 spc_next = next_char_is_space (); |
|
1300 else |
|
1301 goto done; |
|
1302 break; |
|
1303 |
|
1304 case '.': |
|
1305 if (c1 == '*') |
|
1306 { |
|
1307 int c2 = yyinput (); |
|
1308 if (c2 == '*') |
|
1309 spc_next = next_char_is_space (); |
|
1310 else |
|
1311 spc_next = (c2 == ' ' || c2 == '\t'); |
|
1312 yyunput (c2, yytext); |
|
1313 } |
|
1314 else if (c1 == '/' || c1 == '\\' || c1 == '^') |
|
1315 spc_next = next_char_is_space (); |
|
1316 else |
|
1317 goto done; |
|
1318 break; |
|
1319 |
|
1320 default: |
|
1321 goto done; |
|
1322 } |
|
1323 |
|
1324 bin_op = looks_like_bin_op (spc_prev, spc_next); |
|
1325 |
|
1326 done: |
|
1327 yyunput (c1, yytext); |
|
1328 yyunput (c0, yytext); |
|
1329 |
|
1330 return bin_op; |
|
1331 } |
|
1332 |
767
|
1333 // Used to delete trailing white space from tokens. |
|
1334 |
146
|
1335 static char * |
1
|
1336 strip_trailing_whitespace (char *s) |
|
1337 { |
|
1338 char *retval = strsave (s); |
|
1339 |
|
1340 char *t = strchr (retval, ' '); |
522
|
1341 if (t) |
1
|
1342 *t = '\0'; |
|
1343 |
|
1344 t = strchr (retval, '\t'); |
522
|
1345 if (t) |
1
|
1346 *t = '\0'; |
|
1347 |
|
1348 return retval; |
|
1349 } |
|
1350 |
767
|
1351 // Figure out exactly what kind of token to return when we have seen |
|
1352 // an identifier. Handles keywords. |
|
1353 |
146
|
1354 static int |
|
1355 handle_identifier (char *tok, int next_tok_is_eq) |
|
1356 { |
747
|
1357 // If we are expecting a structure element, we just want to return |
|
1358 // TEXT_ID, which is a string that is also a valid identifier. |
|
1359 |
|
1360 if (looking_at_indirect_ref) |
|
1361 { |
|
1362 yylval.tok_val = new token (tok); |
|
1363 token_stack.push (yylval.tok_val); |
|
1364 TOK_RETURN (TEXT_ID); |
|
1365 } |
|
1366 |
146
|
1367 // If we have a regular keyword, or a plot STYLE, return it. STYLE is |
|
1368 // special only because it can't be followed by an identifier. |
|
1369 |
|
1370 int kw_token = is_keyword (tok); |
|
1371 if (kw_token) |
|
1372 { |
|
1373 if (kw_token == STYLE) |
|
1374 { |
|
1375 current_input_column += yyleng; |
|
1376 quote_is_transpose = 0; |
|
1377 cant_be_identifier = 1; |
|
1378 convert_spaces_to_comma = 1; |
|
1379 return kw_token; |
|
1380 } |
|
1381 else |
|
1382 TOK_RETURN (kw_token); |
|
1383 } |
|
1384 |
|
1385 // See if we have a plot keyword (title, using, or with). |
|
1386 |
148
|
1387 int plot_option_kw = is_plot_keyword (tok); |
|
1388 if (plotting && cant_be_identifier && plot_option_kw) |
146
|
1389 TOK_RETURN (plot_option_kw); |
|
1390 |
|
1391 // Yes, we really do need both of these plot_range variables. One |
|
1392 // is used to mark when we are past all possiblity of a plot range, |
|
1393 // the other is used to mark when we are actually between the square |
|
1394 // brackets that surround the range. |
|
1395 |
|
1396 if (plotting && ! in_plot_range) |
|
1397 past_plot_range = 1; |
|
1398 |
|
1399 // It is always an error for an identifier to be followed directly by |
|
1400 // another identifier. |
|
1401 |
|
1402 cant_be_identifier = 1; |
|
1403 |
|
1404 // If we are looking at a text style function, set up to gobble its |
|
1405 // arguments. These are also reserved words, but only because it |
|
1406 // would be very difficult to do anything intelligent with them if |
|
1407 // they were not reserved. |
|
1408 |
|
1409 if (is_text_function_name (tok)) |
|
1410 { |
|
1411 BEGIN TEXT_FCN; |
|
1412 |
169
|
1413 if (strcmp (tok, "help") == 0) |
146
|
1414 BEGIN HELP_FCN; |
|
1415 else if (strcmp (tok, "set") == 0) |
|
1416 doing_set = 1; |
|
1417 } |
|
1418 |
|
1419 // Make sure we put the return values of a function in the symbol |
|
1420 // table that is local to the function. |
|
1421 |
|
1422 if (next_tok_is_eq && defining_func && maybe_screwed) |
|
1423 curr_sym_tab = tmp_local_sym_tab; |
|
1424 |
|
1425 // Find the token in the symbol table. |
|
1426 |
|
1427 yylval.tok_val = new token (lookup_identifier (tok), |
|
1428 input_line_number, |
|
1429 current_input_column); |
|
1430 |
|
1431 token_stack.push (yylval.tok_val); |
|
1432 |
|
1433 // After seeing an identifer, it is ok to convert spaces to a comma |
|
1434 // (if needed). |
|
1435 |
|
1436 convert_spaces_to_comma = 1; |
|
1437 current_input_column += yyleng; |
|
1438 |
|
1439 // If we are defining a function and we have not seen the parameter |
|
1440 // list yet and the next token is `=', return a token that represents |
|
1441 // the only return value for the function. For example, |
|
1442 // |
|
1443 // function SCREW = f (args); |
|
1444 // |
|
1445 // The variable maybe_screwed is reset in parse.y. |
|
1446 |
|
1447 if (next_tok_is_eq) |
|
1448 { |
|
1449 if (defining_func && maybe_screwed) |
|
1450 return SCREW; |
|
1451 else |
|
1452 return NAME; |
|
1453 } |
|
1454 |
|
1455 // At this point, we are only dealing with identifiers that are not |
|
1456 // followed by `=' (if the next token is `=', there is no need to |
|
1457 // check to see if we should insert a comma (invalid syntax), or allow |
|
1458 // a following `'' to be treated as a transpose (the next token is |
|
1459 // `=', so it can't be `''. |
|
1460 |
|
1461 quote_is_transpose = 1; |
|
1462 do_comma_insert_check (); |
|
1463 |
|
1464 // Check to see if we should insert a comma. |
|
1465 |
430
|
1466 if (user_pref.commas_in_literal_matrix != 2 |
|
1467 && ! in_brace_or_paren.empty () |
|
1468 && in_brace_or_paren.top ()) |
146
|
1469 { |
|
1470 int c0 = yytext[yyleng-1]; |
|
1471 int spc_prev = (c0 == ' ' || c0 == '\t'); |
|
1472 int bin_op = next_token_is_bin_op (spc_prev, yytext); |
|
1473 |
|
1474 int postfix_un_op = next_token_is_postfix_unary_op (spc_prev, |
|
1475 yytext); |
|
1476 |
|
1477 int c1 = yyinput (); |
|
1478 unput (c1); |
747
|
1479 int other_op = match_any (c1, ".,;\n]"); |
430
|
1480 int index_op = (c1 == '(' |
|
1481 && (user_pref.commas_in_literal_matrix == 0 |
|
1482 || ! spc_prev)); |
412
|
1483 |
|
1484 if (! (postfix_un_op || bin_op || other_op || index_op)) |
430
|
1485 unput (','); |
146
|
1486 } |
|
1487 |
|
1488 return NAME; |
|
1489 } |
|
1490 |
767
|
1491 // Print a warning if a function file that defines a function has |
|
1492 // anything other than comments and whitespace following the END token |
|
1493 // that matches the FUNCTION statement. |
|
1494 |
1
|
1495 void |
|
1496 check_for_garbage_after_fcn_def (void) |
|
1497 { |
|
1498 // By making a newline be the next character to be read, we will force |
|
1499 // the parser to return after reading the function. Calling yyunput |
|
1500 // with EOF seems not to work... |
|
1501 |
|
1502 int in_comment = 0; |
|
1503 int lineno = input_line_number; |
|
1504 int c; |
|
1505 while ((c = yyinput ()) != EOF) |
|
1506 { |
|
1507 switch (c) |
|
1508 { |
|
1509 case ' ': |
|
1510 case '\t': |
|
1511 case ';': |
|
1512 case ',': |
|
1513 break; |
777
|
1514 |
1
|
1515 case '\n': |
|
1516 if (in_comment) |
|
1517 in_comment = 0; |
|
1518 break; |
777
|
1519 |
1
|
1520 case '%': |
|
1521 case '#': |
|
1522 in_comment = 1; |
|
1523 break; |
777
|
1524 |
1
|
1525 default: |
|
1526 if (in_comment) |
|
1527 break; |
|
1528 else |
|
1529 { |
|
1530 warning ("ignoring trailing garbage after end of function\n\ |
341
|
1531 near line %d of file `%s.m'", lineno, curr_fcn_file_name); |
1
|
1532 |
|
1533 yyunput ('\n', yytext); |
|
1534 return; |
|
1535 } |
|
1536 } |
|
1537 } |
|
1538 yyunput ('\n', yytext); |
|
1539 } |
|
1540 |
767
|
1541 /* |
|
1542 |
|
1543 Maybe someday... |
1
|
1544 |
|
1545 "+=" return ADD_EQ; |
|
1546 "-=" return SUB_EQ; |
|
1547 "*=" return MUL_EQ; |
|
1548 "/=" return DIV_EQ; |
|
1549 "\\=" return LEFTDIV_EQ; |
|
1550 ".+=" return ADD_EQ; |
|
1551 ".-=" return SUB_EQ; |
|
1552 ".*=" return EMUL_EQ; |
|
1553 "./=" return EDIV_EQ; |
|
1554 ".\\=" return ELEFTDIV_EQ; |
|
1555 |
|
1556 */ |