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