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