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