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