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