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