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