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