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