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