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