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