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