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