1994
|
1 /* |
1
|
2 |
2847
|
3 Copyright (C) 1996, 1997 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 |
3503
|
34 #include <strstream> |
1823
|
35 #include <string> |
|
36 |
1
|
37 #include "SLStack.h" |
|
38 |
2926
|
39 #include "cmd-edit.h" |
|
40 |
1497
|
41 // These would be alphabetical, but y.tab.h must be included before |
|
42 // oct-gperf.h and y.tab.h must be included after token.h and the tree |
|
43 // class declarations. We can't include y.tab.h in oct-gperf.h |
|
44 // because it may not be protected to allow it to be included multiple |
|
45 // times. |
|
46 |
3665
|
47 #include "comment-list.h" |
2181
|
48 #include "defun.h" |
1355
|
49 #include "error.h" |
1351
|
50 #include "input.h" |
1355
|
51 #include "lex.h" |
2891
|
52 #include "ov.h" |
1355
|
53 #include "parse.h" |
2987
|
54 #include "pt-all.h" |
2891
|
55 #include "symtab.h" |
|
56 #include "token.h" |
|
57 #include "toplev.h" |
1355
|
58 #include "utils.h" |
|
59 #include "variables.h" |
2492
|
60 #include <y.tab.h> |
|
61 #include <oct-gperf.h> |
1
|
62 |
2716
|
63 #if ! (defined (FLEX_SCANNER) \ |
|
64 && defined (YY_FLEX_MAJOR_VERSION) && YY_FLEX_MAJOR_VERSION >= 2 \ |
|
65 && defined (YY_FLEX_MINOR_VERSION) && YY_FLEX_MINOR_VERSION >= 5) |
|
66 #error lex.l requires flex version 2.5.4 or later |
|
67 #endif |
|
68 |
3883
|
69 // TRUE means that we have encountered EOF on the input stream. |
|
70 bool parser_end_of_input = false; |
|
71 |
1826
|
72 // Flags that need to be shared between the lexer and parser. |
|
73 lexical_feedback lexer_flags; |
|
74 |
1351
|
75 // Stack to hold tokens so that we can delete them when the parser is |
|
76 // reset and avoid growing forever just because we are stashing some |
|
77 // information. This has to appear before lex.h is included, because |
|
78 // one of the macros defined there uses token_stack. |
2614
|
79 // |
|
80 // XXX FIXME XXX -- this should really be static, but that causes |
|
81 // problems on some systems. |
|
82 SLStack <token*> token_stack; |
1351
|
83 |
1826
|
84 // Did eat_whitespace() eat a space or tab, or a newline, or both? |
1
|
85 |
1826
|
86 typedef int yum_yum; |
1
|
87 |
1826
|
88 const yum_yum ATE_NOTHING = 0; |
|
89 const yum_yum ATE_SPACE_OR_TAB = 1; |
|
90 const yum_yum ATE_NEWLINE = 2; |
1088
|
91 |
3351
|
92 // Is the closest nesting level a square bracket, squiggly brace or a paren? |
1826
|
93 |
3351
|
94 class bracket_brace_paren_nesting_level : public SLStack <int> |
1826
|
95 { |
|
96 public: |
|
97 |
3351
|
98 bracket_brace_paren_nesting_level (void) : SLStack<int> () { } |
1826
|
99 |
3351
|
100 ~bracket_brace_paren_nesting_level (void) { } |
|
101 |
|
102 void bracket (void) { push (BRACKET); } |
|
103 bool is_bracket (void) { return ! empty () && top () == BRACKET; } |
1826
|
104 |
|
105 void brace (void) { push (BRACE); } |
|
106 bool is_brace (void) { return ! empty () && top () == BRACE; } |
|
107 |
|
108 void paren (void) { push (PAREN); } |
|
109 bool is_paren (void) { return ! empty () && top () == PAREN; } |
985
|
110 |
1826
|
111 bool none (void) { return empty (); } |
|
112 |
|
113 void remove (void) { if (! empty ()) SLStack<int>::pop (); } |
|
114 |
|
115 private: |
|
116 |
3351
|
117 enum { BRACKET = 1, BRACE = 2, PAREN = 3 }; |
1826
|
118 |
3351
|
119 bracket_brace_paren_nesting_level (const bracket_brace_paren_nesting_level&); |
1826
|
120 |
3351
|
121 bracket_brace_paren_nesting_level& |
|
122 operator = (const bracket_brace_paren_nesting_level&); |
1826
|
123 }; |
|
124 |
3351
|
125 static bracket_brace_paren_nesting_level nesting_level; |
1
|
126 |
2167
|
127 // Should whitespace in a literal matrix list be automatically |
|
128 // converted to commas and semicolons? |
|
129 // |
|
130 // user specifies value of var |
|
131 // -------------- ------------ |
|
132 // "ignore" 2 |
|
133 // "traditional" 1 |
|
134 // anything else 0 |
|
135 // |
|
136 // Octave will never insert a comma in a literal matrix list if the |
|
137 // user specifies "ignore". For example, the statement [1 2] will |
|
138 // result in an error instead of being treated the same as [1, 2], and |
|
139 // the statement |
|
140 // |
|
141 // [ 1, 2, |
|
142 // 3, 4 ] |
|
143 // |
|
144 // will result in the vector [1 2 3 4] instead of a matrix. |
|
145 // |
|
146 // Traditional behavior makes Octave convert spaces to a comma between |
|
147 // identifiers and `('. For example, the statement |
|
148 // |
|
149 // [eye (2)] |
|
150 // |
|
151 // will be parsed as |
|
152 // |
|
153 // [eye, (2)] |
|
154 // |
|
155 // and will result in an error since the `eye' function will be |
|
156 // called with no arguments. To get around this, you would have to |
|
157 // omit the space between `eye' and the `('. |
|
158 // |
|
159 // The default value is 0, which results in behavior that is the same |
|
160 // as traditional, except that Octave does not convert spaces to a |
|
161 // comma between identifiers and `('. For example, the statement |
|
162 // |
|
163 // [eye (2)] |
|
164 // |
|
165 // will result in a call to `eye' with the argument `2'. |
|
166 |
|
167 static int Vwhitespace_in_literal_matrix; |
|
168 |
3388
|
169 static bool Vwarn_separator_insert = false; |
|
170 |
3400
|
171 static bool Vwarn_single_quote_string = false; |
|
172 |
146
|
173 // Forward declarations for functions defined at the bottom of this |
|
174 // file. |
|
175 |
1
|
176 static void fixup_column_count (char *s); |
146
|
177 static void do_comma_insert_check (void); |
3523
|
178 static int is_plot_keyword (const std::string& s); |
|
179 static int is_keyword (const std::string& s); |
|
180 static std::string plot_style_token (const std::string& s); |
|
181 static symbol_record *lookup_identifier (const std::string& s); |
1
|
182 static void grab_help_text (void); |
2857
|
183 static bool match_any (char c, const char *s); |
3263
|
184 static bool next_token_is_sep_op (void); |
3246
|
185 static bool next_token_is_bin_op (bool spc_prev); |
|
186 static bool next_token_is_postfix_unary_op (bool spc_prev); |
3523
|
187 static std::string strip_trailing_whitespace (char *s); |
3246
|
188 static void handle_number (void); |
975
|
189 static int handle_string (char delim, int text_style = 0); |
3351
|
190 static int handle_close_bracket (int spc_gobbled); |
3523
|
191 static int handle_identifier (const std::string& tok, int spc_gobbled); |
3096
|
192 static bool have_continuation (bool trailing_comments_ok = true); |
|
193 static bool have_ellipsis_continuation (bool trailing_comments_ok = true); |
3665
|
194 static void scan_for_comments (const char *); |
1826
|
195 static yum_yum eat_whitespace (void); |
|
196 static yum_yum eat_continuation (void); |
3388
|
197 static void maybe_warn_separator_insert (char sep); |
3400
|
198 static void gripe_single_quote_string (void); |
1
|
199 |
|
200 %} |
|
201 |
|
202 D [0-9] |
|
203 S [ \t] |
2042
|
204 NL ((\n)|(\r\n)) |
|
205 SNL ({S}|{NL}) |
1
|
206 EL (\.\.\.) |
967
|
207 BS (\\) |
|
208 CONT ({EL}|{BS}) |
1
|
209 Im [iIjJ] |
967
|
210 CCHAR [#%] |
|
211 COMMENT ({CCHAR}.*{NL}) |
|
212 SNLCMT ({SNL}|{COMMENT}) |
|
213 NOTEQ ((~=)|(!=)|(<>)) |
|
214 POW ((\*\*)|(\^)) |
|
215 EPOW (\.{POW}) |
|
216 NOT ((\~)|(\!)) |
1
|
217 IDENT ([_a-zA-Z][_a-zA-Z0-9]*) |
|
218 EXPON ([DdEe][+-]?{D}+) |
3220
|
219 NUMBER (({D}+\.?{D}*{EXPON}?)|(\.{D}+{EXPON}?)|(0[xX][0-9a-fA-F]+)) |
1
|
220 %% |
|
221 |
968
|
222 %{ |
|
223 // Help and other text-style functions are a pain in the ass. This |
|
224 // stuff needs to be simplified. May require some changes in the |
|
225 // parser too. |
|
226 %} |
|
227 |
967
|
228 <TEXT_FCN>{NL} { |
|
229 BEGIN 0; |
|
230 current_input_column = 1; |
2857
|
231 lexer_flags.quote_is_transpose = false; |
|
232 lexer_flags.cant_be_identifier = false; |
|
233 lexer_flags.convert_spaces_to_comma = true; |
967
|
234 return '\n'; |
|
235 } |
1
|
236 |
967
|
237 <TEXT_FCN>[\;\,] { |
1826
|
238 if (lexer_flags.doing_set && strcmp (yytext, ",") == 0) |
967
|
239 { |
1060
|
240 TOK_PUSH_AND_RETURN (yytext, TEXT); |
967
|
241 } |
|
242 else |
|
243 { |
|
244 BEGIN 0; |
|
245 if (strcmp (yytext, ",") == 0) |
|
246 TOK_RETURN (','); |
|
247 else |
|
248 TOK_RETURN (';'); |
|
249 } |
|
250 } |
1
|
251 |
975
|
252 <TEXT_FCN>[\"\'] { |
|
253 current_input_column++; |
3246
|
254 return handle_string (yytext[0], true); |
975
|
255 } |
|
256 |
3079
|
257 <TEXT_FCN>[^ \t\n\;\,\"\'][^ \t\n\;\,]*{S}* { |
3523
|
258 std::string tok = strip_trailing_whitespace (yytext); |
1060
|
259 TOK_PUSH_AND_RETURN (tok, TEXT); |
967
|
260 } |
1
|
261 |
968
|
262 %{ |
1
|
263 // For this and the next two rules, we're looking at ']', and we |
971
|
264 // need to know if the next token is `=' or `=='. |
1
|
265 // |
|
266 // It would have been so much easier if the delimiters were simply |
|
267 // different for the expression on the left hand side of the equals |
|
268 // operator. |
971
|
269 // |
|
270 // It's also a pain in the ass to decide whether to insert a comma |
|
271 // after seeing a ']' character... |
968
|
272 %} |
|
273 |
2119
|
274 <MATRIX>{SNLCMT}*\]{S}* { |
3665
|
275 scan_for_comments (yytext); |
1001
|
276 fixup_column_count (yytext); |
|
277 int c = yytext[yyleng-1]; |
|
278 int cont_is_spc = eat_continuation (); |
|
279 int spc_gobbled = (cont_is_spc || c == ' ' || c == '\t'); |
3351
|
280 return handle_close_bracket (spc_gobbled); |
967
|
281 } |
1
|
282 |
968
|
283 %{ |
1088
|
284 // Commas are element separators in matrix constants. If we don't |
|
285 // check for continuations here we can end up inserting too many |
|
286 // commas. |
968
|
287 %} |
|
288 |
967
|
289 <MATRIX>{S}*\,{S}* { |
1088
|
290 current_input_column += yyleng; |
3388
|
291 |
1088
|
292 int tmp = eat_continuation (); |
3388
|
293 |
2857
|
294 lexer_flags.quote_is_transpose = false; |
|
295 lexer_flags.cant_be_identifier = false; |
|
296 lexer_flags.convert_spaces_to_comma = true; |
3388
|
297 |
|
298 if ((tmp & ATE_NEWLINE) == ATE_NEWLINE) |
|
299 { |
|
300 maybe_warn_separator_insert (';'); |
|
301 |
|
302 if (Vwhitespace_in_literal_matrix != 2) |
|
303 unput (';'); |
|
304 } |
|
305 |
1088
|
306 return (','); |
967
|
307 } |
1
|
308 |
968
|
309 %{ |
|
310 // In some cases, spaces in matrix constants can turn into commas. |
|
311 // If commas are required, spaces are not important in matrix |
1088
|
312 // constants so we just eat them. If we don't check for continuations |
|
313 // here we can end up inserting too many commas. |
968
|
314 %} |
430
|
315 |
968
|
316 <MATRIX>{S}+ { |
1088
|
317 current_input_column += yyleng; |
3388
|
318 |
|
319 int tmp = eat_continuation (); |
|
320 int bin_op = next_token_is_bin_op (true); |
|
321 int postfix_un_op = next_token_is_postfix_unary_op (true); |
|
322 |
|
323 if (! (postfix_un_op || bin_op) |
|
324 && nesting_level.is_bracket () |
|
325 && lexer_flags.convert_spaces_to_comma) |
967
|
326 { |
3388
|
327 if ((tmp & ATE_NEWLINE) == ATE_NEWLINE) |
|
328 { |
|
329 maybe_warn_separator_insert (';'); |
967
|
330 |
3388
|
331 if (Vwhitespace_in_literal_matrix != 2) |
|
332 unput (';'); |
|
333 } |
|
334 |
|
335 if (Vwhitespace_in_literal_matrix != 2) |
1088
|
336 { |
2857
|
337 lexer_flags.quote_is_transpose = false; |
|
338 lexer_flags.cant_be_identifier = false; |
|
339 lexer_flags.convert_spaces_to_comma = true; |
3388
|
340 |
|
341 maybe_warn_separator_insert (','); |
|
342 |
1088
|
343 return (','); |
|
344 } |
967
|
345 } |
|
346 } |
430
|
347 |
968
|
348 %{ |
1088
|
349 // Semicolons are handled as row seprators in matrix constants. If we |
|
350 // don't eat whitespace here we can end up inserting too many |
|
351 // semicolons. |
968
|
352 %} |
|
353 |
985
|
354 <MATRIX>{SNLCMT}*;{SNLCMT}* { |
3665
|
355 scan_for_comments (yytext); |
967
|
356 fixup_column_count (yytext); |
1001
|
357 eat_whitespace (); |
2857
|
358 lexer_flags.quote_is_transpose = false; |
|
359 lexer_flags.cant_be_identifier = false; |
|
360 lexer_flags.convert_spaces_to_comma = true; |
967
|
361 return ';'; |
|
362 } |
|
363 |
968
|
364 %{ |
1088
|
365 // In some cases, new lines can also become row separators. If we |
|
366 // don't eat whitespace here we can end up inserting too many |
|
367 // semicolons. |
985
|
368 %} |
|
369 |
3180
|
370 <MATRIX>{S}*{COMMENT}{SNLCMT}* | |
|
371 <MATRIX>{S}*{NL}{SNLCMT}* { |
3665
|
372 scan_for_comments (yytext); |
1082
|
373 fixup_column_count (yytext); |
1088
|
374 eat_whitespace (); |
3388
|
375 |
2167
|
376 if (Vwhitespace_in_literal_matrix != 2) |
985
|
377 { |
2857
|
378 lexer_flags.quote_is_transpose = false; |
|
379 lexer_flags.cant_be_identifier = false; |
|
380 lexer_flags.convert_spaces_to_comma = true; |
985
|
381 |
1826
|
382 if (nesting_level.none ()) |
985
|
383 return LEXICAL_ERROR; |
3388
|
384 } |
985
|
385 |
3388
|
386 if (nesting_level.is_bracket ()) |
|
387 { |
|
388 maybe_warn_separator_insert (';'); |
|
389 |
|
390 if (Vwhitespace_in_literal_matrix != 2) |
985
|
391 return ';'; |
|
392 } |
|
393 } |
|
394 |
|
395 %{ |
3351
|
396 // Open and close bracket are handled differently if we are in the range |
968
|
397 // part of a plot command. |
975
|
398 // |
968
|
399 %} |
1
|
400 |
967
|
401 \[{S}* { |
3351
|
402 nesting_level.bracket (); |
975
|
403 |
1082
|
404 current_input_column += yyleng; |
2857
|
405 lexer_flags.quote_is_transpose = false; |
|
406 lexer_flags.cant_be_identifier = false; |
|
407 lexer_flags.convert_spaces_to_comma = true; |
975
|
408 |
|
409 promptflag--; |
|
410 eat_whitespace (); |
|
411 |
1826
|
412 if (lexer_flags.plotting && ! lexer_flags.past_plot_range) |
967
|
413 { |
2857
|
414 lexer_flags.in_plot_range = true; |
1082
|
415 return OPEN_BRACE; |
967
|
416 } |
|
417 else |
|
418 { |
3351
|
419 lexer_flags.bracketflag++; |
975
|
420 BEGIN MATRIX; |
1082
|
421 return '['; |
967
|
422 } |
|
423 } |
1
|
424 |
968
|
425 \] { |
1826
|
426 nesting_level.remove (); |
968
|
427 |
1826
|
428 if (lexer_flags.plotting && ! lexer_flags.past_plot_range) |
968
|
429 { |
2857
|
430 lexer_flags.in_plot_range = false; |
968
|
431 TOK_RETURN (CLOSE_BRACE); |
|
432 } |
|
433 else |
|
434 TOK_RETURN (']'); |
|
435 } |
|
436 |
|
437 %{ |
|
438 // Imaginary numbers. |
|
439 %} |
|
440 |
|
441 {NUMBER}{Im} { |
3246
|
442 handle_number (); |
968
|
443 return IMAG_NUM; |
|
444 } |
|
445 |
|
446 %{ |
|
447 // Real numbers. Don't grab the `.' part of a dot operator as part of |
|
448 // the constant. |
|
449 %} |
|
450 |
|
451 {D}+/\.[\*/\\^'] | |
|
452 {NUMBER} { |
3246
|
453 handle_number (); |
968
|
454 return NUM; |
|
455 } |
|
456 |
|
457 %{ |
|
458 // Eat whitespace. Whitespace inside matrix constants is handled by |
|
459 // the <MATRIX> start state code above. |
|
460 %} |
|
461 |
967
|
462 {S}* { |
|
463 current_input_column += yyleng; |
|
464 } |
|
465 |
968
|
466 %{ |
|
467 // Continuation lines. Allow comments after continuations. |
|
468 %} |
|
469 |
967
|
470 {CONT}{S}*{NL} | |
|
471 {CONT}{S}*{COMMENT} { |
3665
|
472 scan_for_comments (yytext); |
967
|
473 promptflag--; |
|
474 current_input_column = 1; |
|
475 } |
1
|
476 |
968
|
477 %{ |
|
478 // An ellipsis not at the end of a line is not a continuation, but |
|
479 // does have another meaning. |
|
480 %} |
|
481 |
967
|
482 {EL} { |
|
483 return ELLIPSIS; |
|
484 } |
1
|
485 |
968
|
486 %{ |
|
487 // End of file. |
|
488 %} |
|
489 |
967
|
490 <<EOF>> { |
|
491 TOK_RETURN (END_OF_INPUT); |
|
492 } |
1
|
493 |
968
|
494 %{ |
970
|
495 // Identifiers. Truncate the token at the first space or tab but |
|
496 // don't write directly on yytext. |
968
|
497 %} |
|
498 |
967
|
499 {IDENT}{S}* { |
3523
|
500 std::string tok = strip_trailing_whitespace (yytext); |
1001
|
501 int c = yytext[yyleng-1]; |
|
502 int cont_is_spc = eat_continuation (); |
|
503 int spc_gobbled = (cont_is_spc || c == ' ' || c == '\t'); |
|
504 return handle_identifier (tok, spc_gobbled); |
967
|
505 } |
1
|
506 |
968
|
507 %{ |
|
508 // A new line character. New line characters inside matrix constants |
985
|
509 // are handled by the <MATRIX> start state code above. If closest |
|
510 // nesting is inside parentheses, don't return a row separator. |
968
|
511 %} |
|
512 |
967
|
513 {NL} { |
|
514 current_input_column = 1; |
2857
|
515 lexer_flags.quote_is_transpose = false; |
|
516 lexer_flags.cant_be_identifier = false; |
|
517 lexer_flags.convert_spaces_to_comma = true; |
985
|
518 |
1826
|
519 if (nesting_level.none ()) |
985
|
520 return '\n'; |
|
521 |
3351
|
522 if (nesting_level.is_bracket ()) |
985
|
523 return LEXICAL_ERROR; |
967
|
524 } |
1
|
525 |
968
|
526 %{ |
|
527 // Single quote can either be the beginning of a string or a transpose |
|
528 // operator. |
|
529 %} |
|
530 |
967
|
531 "'" { |
|
532 current_input_column++; |
2857
|
533 lexer_flags.convert_spaces_to_comma = true; |
1
|
534 |
1826
|
535 if (lexer_flags.quote_is_transpose) |
967
|
536 { |
|
537 do_comma_insert_check (); |
|
538 return QUOTE; |
|
539 } |
|
540 else |
973
|
541 return handle_string ('\''); |
967
|
542 } |
1
|
543 |
968
|
544 %{ |
971
|
545 // Double quotes always begin strings. |
|
546 %} |
|
547 |
973
|
548 \" { |
|
549 current_input_column++; |
|
550 return handle_string ('"'); |
|
551 } |
971
|
552 |
|
553 %{ |
|
554 // The colon operator is handled differently if we are in the range |
|
555 // part of a plot command. |
968
|
556 %} |
|
557 |
967
|
558 ":" { |
1826
|
559 if (lexer_flags.plotting |
|
560 && (lexer_flags.in_plot_range || lexer_flags.in_plot_using)) |
2857
|
561 BIN_OP_RETURN (COLON, true); |
967
|
562 else |
2857
|
563 BIN_OP_RETURN (':', false); |
967
|
564 } |
1
|
565 |
968
|
566 %{ |
985
|
567 // Gobble comments. If closest nesting is inside parentheses, don't |
|
568 // return a new line. |
|
569 %} |
968
|
570 |
967
|
571 {CCHAR} { |
1826
|
572 if (help_buf.empty () |
|
573 && lexer_flags.beginning_of_function |
|
574 && nesting_level.none ()) |
967
|
575 { |
3665
|
576 lexer_flags.beginning_of_function = false; |
|
577 |
967
|
578 grab_help_text (); |
3665
|
579 |
|
580 octave_comment_buffer::append (help_buf); |
967
|
581 } |
|
582 else |
|
583 { |
3665
|
584 std::string buf; |
|
585 |
|
586 bool begin_comment = true; |
|
587 |
967
|
588 int c; |
|
589 while ((c = yyinput ()) != EOF && c != '\n') |
3665
|
590 { |
|
591 if (begin_comment && (c == '#' || c == '%')) |
|
592 ; /* Skip leading comment characters. */ |
|
593 else |
3802
|
594 buf += static_cast<char> (c); |
3665
|
595 } |
|
596 |
|
597 octave_comment_buffer::append (buf); |
967
|
598 } |
440
|
599 |
967
|
600 current_input_column = 1; |
2857
|
601 lexer_flags.quote_is_transpose = false; |
|
602 lexer_flags.cant_be_identifier = false; |
|
603 lexer_flags.convert_spaces_to_comma = true; |
985
|
604 |
1826
|
605 if (nesting_level.none ()) |
985
|
606 return '\n'; |
3351
|
607 else if (nesting_level.is_bracket ()) |
1566
|
608 return ';'; |
967
|
609 } |
440
|
610 |
968
|
611 %{ |
|
612 // Other operators. |
|
613 %} |
|
614 |
2857
|
615 ".+" { BIN_OP_RETURN (EPLUS, false); } |
|
616 ".-" { BIN_OP_RETURN (EMINUS, false); } |
|
617 ".*" { BIN_OP_RETURN (EMUL, false); } |
|
618 "./" { BIN_OP_RETURN (EDIV, false); } |
|
619 ".\\" { BIN_OP_RETURN (ELEFTDIV, false); } |
|
620 {EPOW} { BIN_OP_RETURN (EPOW, false); } |
|
621 ".'" { do_comma_insert_check (); BIN_OP_RETURN (TRANSPOSE, true); } |
|
622 "++" { do_comma_insert_check (); BIN_OP_RETURN (PLUS_PLUS, true); } |
|
623 "--" { do_comma_insert_check (); BIN_OP_RETURN (MINUS_MINUS, true); } |
|
624 "<=" { BIN_OP_RETURN (EXPR_LE, false); } |
|
625 "==" { BIN_OP_RETURN (EXPR_EQ, false); } |
|
626 {NOTEQ} { BIN_OP_RETURN (EXPR_NE, false); } |
|
627 ">=" { BIN_OP_RETURN (EXPR_GE, false); } |
2877
|
628 "&" { BIN_OP_RETURN (EXPR_AND, false); } |
2857
|
629 "|" { BIN_OP_RETURN (EXPR_OR, false); } |
|
630 "<" { BIN_OP_RETURN (EXPR_LT, false); } |
|
631 ">" { BIN_OP_RETURN (EXPR_GT, false); } |
|
632 "*" { BIN_OP_RETURN ('*', false); } |
|
633 "/" { BIN_OP_RETURN ('/', false); } |
|
634 "\\" { BIN_OP_RETURN (LEFTDIV, false); } |
|
635 ";" { BIN_OP_RETURN (';', true); } |
|
636 "," { BIN_OP_RETURN (',', true); } |
|
637 {POW} { BIN_OP_RETURN (POW, false); } |
|
638 "=" { BIN_OP_RETURN ('=', true); } |
2877
|
639 "&&" { BIN_OP_RETURN (EXPR_AND_AND, false); } |
2857
|
640 "||" { BIN_OP_RETURN (EXPR_OR_OR, false); } |
2900
|
641 "<<" { BIN_OP_RETURN (LSHIFT, false); } |
|
642 ">>" { BIN_OP_RETURN (RSHIFT, false); } |
967
|
643 |
|
644 {NOT} { |
1826
|
645 if (lexer_flags.plotting && ! lexer_flags.in_plot_range) |
2857
|
646 lexer_flags.past_plot_range = true; |
|
647 BIN_OP_RETURN (EXPR_NOT, false); |
967
|
648 } |
1
|
649 |
1276
|
650 "+" { |
1826
|
651 if (lexer_flags.plotting && ! lexer_flags.in_plot_range) |
2857
|
652 lexer_flags.past_plot_range = true; |
|
653 BIN_OP_RETURN ('+', false); |
967
|
654 } |
|
655 |
1276
|
656 "-" { |
1826
|
657 if (lexer_flags.plotting && ! lexer_flags.in_plot_range) |
2857
|
658 lexer_flags.past_plot_range = true; |
|
659 BIN_OP_RETURN ('-', false); |
967
|
660 } |
|
661 |
|
662 "(" { |
1826
|
663 if (lexer_flags.plotting && ! lexer_flags.in_plot_range) |
2857
|
664 lexer_flags.past_plot_range = true; |
1826
|
665 nesting_level.paren (); |
985
|
666 promptflag--; |
967
|
667 TOK_RETURN ('('); |
|
668 } |
|
669 |
|
670 ")" { |
1826
|
671 nesting_level.remove (); |
1001
|
672 |
967
|
673 current_input_column++; |
2857
|
674 lexer_flags.cant_be_identifier = true; |
|
675 lexer_flags.quote_is_transpose = true; |
3351
|
676 lexer_flags.convert_spaces_to_comma = nesting_level.is_bracket (); |
1001
|
677 do_comma_insert_check (); |
967
|
678 return ')'; |
|
679 } |
|
680 |
2066
|
681 "." { |
|
682 TOK_RETURN ('.'); |
|
683 } |
|
684 |
2877
|
685 "+=" { BIN_OP_RETURN (ADD_EQ, false); } |
|
686 "-=" { BIN_OP_RETURN (SUB_EQ, false); } |
|
687 "*=" { BIN_OP_RETURN (MUL_EQ, false); } |
|
688 "/=" { BIN_OP_RETURN (DIV_EQ, false); } |
3204
|
689 "\\=" { BIN_OP_RETURN (LEFTDIV_EQ, false); } |
2877
|
690 ".+=" { BIN_OP_RETURN (ADD_EQ, false); } |
|
691 ".-=" { BIN_OP_RETURN (SUB_EQ, false); } |
|
692 ".*=" { BIN_OP_RETURN (EMUL_EQ, false); } |
|
693 "./=" { BIN_OP_RETURN (EDIV_EQ, false); } |
3204
|
694 ".\\=" { BIN_OP_RETURN (ELEFTDIV_EQ, false); } |
2877
|
695 "&=" { BIN_OP_RETURN (AND_EQ, false); } |
|
696 "|=" { BIN_OP_RETURN (OR_EQ, false); } |
2900
|
697 "<<=" { BIN_OP_RETURN (LSHIFT_EQ, false); } |
|
698 ">>=" { BIN_OP_RETURN (RSHIFT_EQ, false); } |
2877
|
699 |
3351
|
700 "{" { |
|
701 nesting_level.brace (); |
|
702 promptflag--; |
|
703 TOK_RETURN ('{'); |
|
704 } |
|
705 |
|
706 "}" { |
|
707 nesting_level.remove (); |
|
708 |
|
709 current_input_column++; |
|
710 lexer_flags.cant_be_identifier = true; |
|
711 lexer_flags.quote_is_transpose = true; |
|
712 lexer_flags.convert_spaces_to_comma = nesting_level.is_bracket (); |
|
713 do_comma_insert_check (); // Is this really necessary? |
|
714 |
|
715 return '}'; |
|
716 } |
|
717 |
968
|
718 %{ |
2066
|
719 // Unrecognized input is a lexical error. |
968
|
720 %} |
1
|
721 |
2042
|
722 . { |
2066
|
723 current_input_column++; |
|
724 |
3867
|
725 // EOF can't happen here (we catch it above). |
|
726 |
|
727 error ("invalid character `%s' (ASCII %d) near line %d, column %d", |
|
728 undo_string_escape (yytext[0]), static_cast<int> (yytext[0]), |
|
729 input_line_number, current_input_column); |
2066
|
730 |
|
731 return LEXICAL_ERROR; |
|
732 } |
1
|
733 |
|
734 %% |
|
735 |
767
|
736 // GAG. |
|
737 // |
|
738 // If we're reading a matrix and the next character is '[', make sure |
|
739 // that we insert a comma ahead of it. |
|
740 |
146
|
741 void |
1
|
742 do_comma_insert_check (void) |
|
743 { |
1001
|
744 int spc_gobbled = eat_continuation (); |
2970
|
745 |
1
|
746 int c = yyinput (); |
2970
|
747 |
3246
|
748 unput (c); |
2970
|
749 |
1001
|
750 if (spc_gobbled) |
3246
|
751 unput (' '); |
2970
|
752 |
3351
|
753 lexer_flags.do_comma_insert = (lexer_flags.bracketflag && c == '['); |
1
|
754 } |
|
755 |
767
|
756 // Fix things up for errors or interrupts. The parser is never called |
|
757 // recursively, so it is always safe to reinitialize its state before |
|
758 // doing any parsing. |
|
759 |
1
|
760 void |
|
761 reset_parser (void) |
|
762 { |
1826
|
763 // Start off on the right foot. |
1
|
764 BEGIN 0; |
166
|
765 error_state = 0; |
3489
|
766 warning_state = 0; |
3883
|
767 parser_end_of_input = false; |
287
|
768 |
1826
|
769 // We do want a prompt by default. |
1
|
770 promptflag = 1; |
287
|
771 |
3351
|
772 // Error may have occurred inside some brackets, braces, or parentheses. |
985
|
773 nesting_level.clear (); |
287
|
774 |
1826
|
775 // Clear out the stack of token info used to track line and column |
|
776 // numbers. |
143
|
777 while (! token_stack.empty ()) |
|
778 delete token_stack.pop (); |
287
|
779 |
1826
|
780 // Can be reset by defining a function. |
985
|
781 if (! (reading_script_file || reading_fcn_file)) |
|
782 { |
|
783 current_input_column = 1; |
2926
|
784 input_line_number = command_editor::current_command_number () - 1; |
985
|
785 } |
287
|
786 |
1826
|
787 // Only ask for input from stdin if we are expecting interactive |
|
788 // input. |
3174
|
789 if ((interactive || forced_interactive) |
3880
|
790 && ! (reading_fcn_file |
|
791 || reading_script_file |
|
792 || get_input_from_eval_string |
3174
|
793 || input_from_startup_file)) |
287
|
794 yyrestart (stdin); |
991
|
795 |
1826
|
796 // Clear the buffer for help text. |
|
797 help_buf.resize (0); |
1755
|
798 |
1826
|
799 // Reset other flags. |
|
800 lexer_flags.init (); |
1
|
801 } |
|
802 |
767
|
803 // If we read some newlines, we need figure out what column we're |
|
804 // really looking at. |
|
805 |
1
|
806 static void |
|
807 fixup_column_count (char *s) |
|
808 { |
|
809 char c; |
|
810 while ((c = *s++) != '\0') |
|
811 { |
|
812 if (c == '\n') |
143
|
813 current_input_column = 1; |
1
|
814 else |
|
815 current_input_column++; |
|
816 } |
|
817 } |
|
818 |
767
|
819 // Include these so that we don't have to link to libfl.a. |
246
|
820 |
1
|
821 #ifdef yywrap |
|
822 #undef yywrap |
|
823 #endif |
3332
|
824 int |
1
|
825 yywrap (void) |
|
826 { |
287
|
827 return 1; |
1
|
828 } |
|
829 |
767
|
830 // Tell us all what the current buffer is. |
|
831 |
1
|
832 YY_BUFFER_STATE |
|
833 current_buffer (void) |
|
834 { |
|
835 return YY_CURRENT_BUFFER; |
|
836 } |
|
837 |
767
|
838 // Create a new buffer. |
|
839 |
1
|
840 YY_BUFFER_STATE |
|
841 create_buffer (FILE *f) |
|
842 { |
|
843 return yy_create_buffer (f, YY_BUF_SIZE); |
|
844 } |
|
845 |
767
|
846 // Start reading a new buffer. |
|
847 |
1
|
848 void |
|
849 switch_to_buffer (YY_BUFFER_STATE buf) |
|
850 { |
|
851 yy_switch_to_buffer (buf); |
|
852 } |
|
853 |
767
|
854 // Delete a buffer. |
|
855 |
1
|
856 void |
|
857 delete_buffer (YY_BUFFER_STATE buf) |
|
858 { |
|
859 yy_delete_buffer (buf); |
|
860 } |
|
861 |
767
|
862 // Restore a buffer (for unwind-prot). |
|
863 |
1
|
864 void |
|
865 restore_input_buffer (void *buf) |
|
866 { |
2861
|
867 switch_to_buffer (static_cast<YY_BUFFER_STATE> (buf)); |
1
|
868 } |
|
869 |
767
|
870 // Delete a buffer (for unwind-prot). |
|
871 |
1
|
872 void |
|
873 delete_input_buffer (void *buf) |
|
874 { |
2861
|
875 delete_buffer (static_cast<YY_BUFFER_STATE> (buf)); |
1
|
876 } |
|
877 |
767
|
878 // Check to see if a character string matches any of the possible line |
|
879 // styles for plots. |
|
880 |
3536
|
881 static std::string |
3523
|
882 plot_style_token (const std::string& s) |
1
|
883 { |
3523
|
884 std::string retval; |
1823
|
885 |
3575
|
886 // XXX FIXME XXX -- specify minimum match length for these. |
2804
|
887 static const char *plot_styles[] = |
146
|
888 { |
925
|
889 "boxes", |
924
|
890 "boxerrorbars", |
2542
|
891 "boxxyerrorbars", |
|
892 "candlesticks", |
146
|
893 "dots", |
|
894 "errorbars", |
2542
|
895 "financebars", |
|
896 "fsteps", |
|
897 "histeps", |
146
|
898 "impulses", |
|
899 "lines", |
|
900 "linespoints", |
|
901 "points", |
926
|
902 "steps", |
2542
|
903 "vector", |
|
904 "xerrorbars", |
|
905 "xyerrorbars", |
|
906 "yerrorbars", |
522
|
907 0, |
146
|
908 }; |
|
909 |
2804
|
910 const char * const *tmp = plot_styles; |
522
|
911 while (*tmp) |
1
|
912 { |
1823
|
913 if (almost_match (*tmp, s.c_str ())) |
|
914 { |
|
915 retval = *tmp; |
|
916 break; |
|
917 } |
1
|
918 |
|
919 tmp++; |
|
920 } |
|
921 |
1823
|
922 return retval; |
1
|
923 } |
|
924 |
3165
|
925 // Check to see if a character string matches any of the possible axes |
|
926 // tags for plots. |
|
927 |
3536
|
928 static std::string |
3523
|
929 plot_axes_token (const std::string& s) |
3165
|
930 { |
3523
|
931 std::string retval; |
3165
|
932 |
3575
|
933 // XXX FIXME XXX -- specify minimum match length for these. |
3561
|
934 static const char *plot_axes[] = |
3165
|
935 { |
|
936 "x1y1", |
|
937 "x1y2", |
|
938 "x2y1", |
|
939 "x2y2", |
|
940 0, |
|
941 }; |
|
942 |
3561
|
943 const char **tmp = plot_axes; |
3165
|
944 while (*tmp) |
|
945 { |
|
946 if (almost_match (*tmp, s.c_str ())) |
|
947 { |
|
948 retval = *tmp; |
|
949 break; |
|
950 } |
|
951 |
|
952 tmp++; |
|
953 } |
|
954 |
|
955 return retval; |
|
956 } |
|
957 |
767
|
958 // Check to see if a character string matches any one of the plot |
941
|
959 // option keywords. Don't match abbreviations for clear, since that's |
|
960 // not a gnuplot keyword (users will probably only expect to be able |
|
961 // to abbreviate actual gnuplot keywords). |
767
|
962 |
1
|
963 static int |
3523
|
964 is_plot_keyword (const std::string& s) |
1
|
965 { |
1823
|
966 const char *t = s.c_str (); |
3575
|
967 if (almost_match ("title", t, 1)) |
146
|
968 { |
|
969 return TITLE; |
|
970 } |
3575
|
971 else if (almost_match ("using", t, 1)) |
146
|
972 { |
2857
|
973 lexer_flags.in_plot_using = true; |
146
|
974 return USING; |
|
975 } |
3575
|
976 else if (almost_match ("with", t, 1)) |
146
|
977 { |
2857
|
978 lexer_flags.in_plot_style = true; |
146
|
979 return WITH; |
|
980 } |
3575
|
981 else if (almost_match ("axes", t, 2) || almost_match ("axis", t, 2)) |
3165
|
982 { |
|
983 lexer_flags.in_plot_axes = true; |
|
984 return AXES; |
|
985 } |
1823
|
986 else if (strcmp ("clear", t) == 0) |
883
|
987 { |
|
988 return CLEAR; |
|
989 } |
1
|
990 else |
146
|
991 { |
|
992 return 0; |
|
993 } |
1
|
994 } |
|
995 |
2877
|
996 // Handle keywords. |
767
|
997 |
1
|
998 static int |
3523
|
999 is_keyword (const std::string& s) |
1
|
1000 { |
3805
|
1001 int l = input_line_number; |
|
1002 int c = current_input_column; |
|
1003 |
3165
|
1004 if (lexer_flags.plotting) |
1
|
1005 { |
3165
|
1006 if (lexer_flags.in_plot_style) |
1
|
1007 { |
3523
|
1008 std::string sty = plot_style_token (s); |
3165
|
1009 |
|
1010 if (! sty.empty ()) |
|
1011 { |
|
1012 lexer_flags.in_plot_style = false; |
3805
|
1013 yylval.tok_val = new token (sty, l, c); |
3165
|
1014 token_stack.push (yylval.tok_val); |
|
1015 return STYLE; |
|
1016 } |
1
|
1017 } |
3165
|
1018 else if (lexer_flags.in_plot_axes) |
|
1019 { |
3523
|
1020 std::string axes = plot_axes_token (s); |
3165
|
1021 |
|
1022 if (! axes.empty ()) |
|
1023 { |
|
1024 lexer_flags.in_plot_axes = false; |
3805
|
1025 yylval.tok_val = new token (axes, l, c); |
3165
|
1026 token_stack.push (yylval.tok_val); |
|
1027 return AXES_TAG; |
|
1028 } |
|
1029 } |
1
|
1030 } |
|
1031 |
1823
|
1032 int len = s.length (); |
922
|
1033 |
1823
|
1034 const octave_kw *kw = octave_kw_lookup (s.c_str (), len); |
191
|
1035 |
1497
|
1036 if (kw) |
143
|
1037 { |
1497
|
1038 yylval.tok_val = 0; |
|
1039 |
|
1040 switch (kw->kw_id) |
|
1041 { |
|
1042 case all_va_args_kw: |
|
1043 case break_kw: |
2764
|
1044 case case_kw: |
1497
|
1045 case catch_kw: |
|
1046 case continue_kw: |
|
1047 case else_kw: |
|
1048 case elseif_kw: |
|
1049 case global_kw: |
2764
|
1050 case otherwise_kw: |
1497
|
1051 case return_kw: |
2846
|
1052 case static_kw: |
3484
|
1053 case until_kw: |
1497
|
1054 case unwind_protect_cleanup_kw: |
|
1055 break; |
|
1056 |
|
1057 case end_kw: |
|
1058 yylval.tok_val = new token (token::simple_end, l, c); |
|
1059 break; |
|
1060 |
|
1061 case end_try_catch_kw: |
|
1062 yylval.tok_val = new token (token::try_catch_end, l, c); |
|
1063 break; |
|
1064 |
|
1065 case end_unwind_protect_kw: |
|
1066 yylval.tok_val = new token (token::unwind_protect_end, l, c); |
|
1067 break; |
|
1068 |
|
1069 case endfor_kw: |
|
1070 yylval.tok_val = new token (token::for_end, l, c); |
|
1071 break; |
|
1072 |
|
1073 case endfunction_kw: |
|
1074 yylval.tok_val = new token (token::function_end, l, c); |
|
1075 break; |
|
1076 |
|
1077 case endif_kw: |
|
1078 yylval.tok_val = new token (token::if_end, l, c); |
|
1079 break; |
|
1080 |
2764
|
1081 case endswitch_kw: |
|
1082 yylval.tok_val = new token (token::switch_end, l, c); |
|
1083 break; |
|
1084 |
1497
|
1085 case endwhile_kw: |
|
1086 yylval.tok_val = new token (token::while_end, l, c); |
|
1087 break; |
|
1088 |
3484
|
1089 case do_kw: |
1497
|
1090 case for_kw: |
|
1091 case while_kw: |
|
1092 promptflag--; |
1826
|
1093 lexer_flags.looping++; |
1497
|
1094 break; |
|
1095 |
|
1096 case if_kw: |
|
1097 case try_kw: |
2764
|
1098 case switch_kw: |
1497
|
1099 case unwind_protect_kw: |
|
1100 promptflag--; |
|
1101 break; |
|
1102 |
|
1103 case gplot_kw: |
2857
|
1104 lexer_flags.plotting = true; |
1497
|
1105 yylval.tok_val = new token (token::two_dee, l, c); |
|
1106 break; |
|
1107 |
|
1108 case gsplot_kw: |
2857
|
1109 lexer_flags.plotting = true; |
1497
|
1110 yylval.tok_val = new token (token::three_dee, l, c); |
|
1111 break; |
|
1112 |
|
1113 case replot_kw: |
2857
|
1114 lexer_flags.plotting = true; |
1497
|
1115 yylval.tok_val = new token (token::replot, l, c); |
|
1116 break; |
|
1117 |
|
1118 case function_kw: |
1826
|
1119 if (lexer_flags.defining_func) |
1497
|
1120 { |
|
1121 error ("function keyword invalid within a function body"); |
|
1122 |
|
1123 if ((reading_fcn_file || reading_script_file) |
1755
|
1124 && ! curr_fcn_file_name.empty ()) |
1497
|
1125 error ("defining new function near line %d of file `%s.m'", |
1755
|
1126 input_line_number, curr_fcn_file_name.c_str ()); |
1497
|
1127 else |
1755
|
1128 error ("defining new function near line %d", |
|
1129 input_line_number); |
1497
|
1130 |
|
1131 return LEXICAL_ERROR; |
|
1132 } |
|
1133 else |
|
1134 { |
2877
|
1135 // Prepare for local symbols. |
|
1136 |
1497
|
1137 tmp_local_sym_tab = new symbol_table (); |
2877
|
1138 |
|
1139 promptflag--; |
|
1140 |
2857
|
1141 lexer_flags.defining_func = true; |
2877
|
1142 lexer_flags.parsed_function_name = false; |
2857
|
1143 lexer_flags.beginning_of_function = true; |
2877
|
1144 |
1497
|
1145 if (! (reading_fcn_file || reading_script_file)) |
|
1146 input_line_number = 1; |
|
1147 } |
|
1148 break; |
|
1149 |
3174
|
1150 case magic_file_kw: |
|
1151 { |
|
1152 if ((reading_fcn_file || reading_script_file) |
|
1153 && ! curr_fcn_file_full_name.empty ()) |
|
1154 yylval.tok_val = new token (curr_fcn_file_full_name, l, c); |
|
1155 else |
|
1156 yylval.tok_val = new token ("stdin", l, c); |
|
1157 } |
|
1158 break; |
|
1159 |
|
1160 case magic_line_kw: |
|
1161 yylval.tok_val = new token (static_cast<double> (l), "", l, c); |
|
1162 break; |
|
1163 |
1497
|
1164 default: |
|
1165 panic_impossible (); |
|
1166 } |
|
1167 |
|
1168 if (! yylval.tok_val) |
|
1169 yylval.tok_val = new token (l, c); |
|
1170 |
476
|
1171 token_stack.push (yylval.tok_val); |
1497
|
1172 |
|
1173 return kw->tok; |
143
|
1174 } |
1
|
1175 |
|
1176 return 0; |
|
1177 } |
|
1178 |
767
|
1179 // Try to find an identifier. All binding to global or builtin |
|
1180 // variables occurs when expressions are evaluated. |
|
1181 |
1
|
1182 static symbol_record * |
3523
|
1183 lookup_identifier (const std::string& name) |
1
|
1184 { |
2856
|
1185 return curr_sym_tab->lookup (name, true); |
1
|
1186 } |
|
1187 |
2702
|
1188 static bool |
3523
|
1189 is_variable (const std::string& name) |
2702
|
1190 { |
2856
|
1191 symbol_record *sr = curr_sym_tab->lookup (name); |
2702
|
1192 |
|
1193 return sr && sr->is_variable (); |
|
1194 } |
|
1195 |
|
1196 static void |
3523
|
1197 force_local_variable (const std::string& name) |
2702
|
1198 { |
2856
|
1199 symbol_record *sr = curr_sym_tab->lookup (name, true); |
2702
|
1200 |
|
1201 if (sr) |
|
1202 sr->define (octave_value ()); |
|
1203 } |
|
1204 |
1019
|
1205 // Grab the help text from an function file. Always overwrites the |
|
1206 // current contents of help_buf. |
767
|
1207 |
3427
|
1208 // XXX FIXME XXX -- gobble_leading_white_space() in parse.y |
2300
|
1209 // duplicates some of this code! |
|
1210 |
1
|
1211 static void |
|
1212 grab_help_text (void) |
|
1213 { |
1755
|
1214 help_buf.resize (0); |
1019
|
1215 |
2300
|
1216 bool begin_comment = true; |
|
1217 bool in_comment = true; |
3427
|
1218 bool discard_space = true; |
3665
|
1219 |
1019
|
1220 int c = 0; |
1
|
1221 |
1019
|
1222 while ((c = yyinput ()) != EOF) |
|
1223 { |
2300
|
1224 if (begin_comment) |
|
1225 { |
|
1226 if (c == '%' || c == '#') |
|
1227 continue; |
3427
|
1228 else if (discard_space && c == ' ') |
|
1229 { |
|
1230 discard_space = false; |
|
1231 continue; |
|
1232 } |
2300
|
1233 else |
|
1234 begin_comment = false; |
|
1235 } |
|
1236 |
1019
|
1237 if (in_comment) |
1
|
1238 { |
3802
|
1239 help_buf += static_cast<char> (c); |
1755
|
1240 |
1019
|
1241 if (c == '\n') |
3427
|
1242 { |
|
1243 in_comment = false; |
|
1244 discard_space = true; |
|
1245 } |
1019
|
1246 } |
|
1247 else |
|
1248 { |
|
1249 switch (c) |
991
|
1250 { |
1019
|
1251 case '%': |
|
1252 case '#': |
2300
|
1253 in_comment = true; |
|
1254 begin_comment = true; |
1019
|
1255 break; |
777
|
1256 |
1019
|
1257 case ' ': |
|
1258 case '\t': |
|
1259 break; |
777
|
1260 |
1019
|
1261 default: |
|
1262 goto done; |
1
|
1263 } |
|
1264 } |
1019
|
1265 } |
991
|
1266 |
1019
|
1267 done: |
991
|
1268 |
1019
|
1269 if (c) |
3246
|
1270 unput (c); |
1
|
1271 } |
|
1272 |
767
|
1273 // Return 1 if the given character matches any character in the given |
|
1274 // string. |
|
1275 |
2857
|
1276 static bool |
2804
|
1277 match_any (char c, const char *s) |
1
|
1278 { |
|
1279 char tmp; |
|
1280 while ((tmp = *s++) != '\0') |
|
1281 { |
|
1282 if (c == tmp) |
2857
|
1283 return true; |
1
|
1284 } |
2857
|
1285 return false; |
1
|
1286 } |
|
1287 |
767
|
1288 // Given information about the spacing surrounding an operator, |
|
1289 // return 1 if it looks like it should be treated as a binary |
|
1290 // operator. For example, |
|
1291 // |
3774
|
1292 // [ 1 + 2 ] or [ 1+ 2] or [ 1+2 ] ==> binary |
|
1293 // |
|
1294 // [ 1 +2 ] ==> unary |
767
|
1295 |
2857
|
1296 static bool |
3246
|
1297 looks_like_bin_op (bool spc_prev, int next_char) |
1
|
1298 { |
3246
|
1299 bool spc_next = (next_char == ' ' || next_char == '\t'); |
|
1300 |
608
|
1301 return ((spc_prev && spc_next) || ! spc_prev); |
1
|
1302 } |
|
1303 |
3263
|
1304 // Recognize separators. If the separator is a CRLF pair, it is |
|
1305 // replaced by a single LF. |
|
1306 |
|
1307 static bool |
|
1308 next_token_is_sep_op (void) |
|
1309 { |
|
1310 bool retval = false; |
|
1311 |
|
1312 int c1 = yyinput (); |
|
1313 |
|
1314 if (c1 == '\r') |
|
1315 { |
|
1316 int c2 = yyinput (); |
|
1317 |
|
1318 if (c2 == '\n') |
|
1319 { |
|
1320 c1 = '\n'; |
|
1321 |
|
1322 retval = true; |
|
1323 } |
|
1324 else |
|
1325 unput (c2); |
|
1326 } |
|
1327 else |
|
1328 retval = match_any (c1, ",;\n]"); |
|
1329 |
|
1330 unput (c1); |
|
1331 |
|
1332 return retval; |
|
1333 } |
|
1334 |
767
|
1335 // Try to determine if the next token should be treated as a postfix |
|
1336 // unary operator. This is ugly, but it seems to do the right thing. |
|
1337 |
2857
|
1338 static bool |
3246
|
1339 next_token_is_postfix_unary_op (bool spc_prev) |
1
|
1340 { |
2857
|
1341 bool un_op = false; |
1
|
1342 |
|
1343 int c0 = yyinput (); |
|
1344 |
3246
|
1345 if (c0 == '\'' && ! spc_prev) |
|
1346 { |
|
1347 un_op = true; |
|
1348 } |
|
1349 else if (c0 == '.') |
|
1350 { |
|
1351 int c1 = yyinput (); |
|
1352 un_op = (c1 == '\''); |
|
1353 unput (c1); |
|
1354 } |
1
|
1355 |
3246
|
1356 unput (c0); |
1
|
1357 |
|
1358 return un_op; |
|
1359 } |
|
1360 |
767
|
1361 // Try to determine if the next token should be treated as a binary |
3246
|
1362 // operator. |
1521
|
1363 // |
3246
|
1364 // This kluge exists because whitespace is not always ignored inside |
3774
|
1365 // the square brackets that are used to create matrix objects (though |
|
1366 // spacing only really matters in the cases that can be interpreted |
|
1367 // either as binary ops or prefix unary ops: currently just +, -). |
|
1368 // |
3779
|
1369 // Note that a line continuation directly following a + or - operator |
|
1370 // (e.g., the characters '[' 'a' ' ' '+' '\' LFD 'b' ']') will be |
|
1371 // parsed as a binary operator. |
767
|
1372 |
2857
|
1373 static bool |
3246
|
1374 next_token_is_bin_op (bool spc_prev) |
1
|
1375 { |
2857
|
1376 bool bin_op = false; |
1
|
1377 |
|
1378 int c0 = yyinput (); |
|
1379 |
|
1380 switch (c0) |
|
1381 { |
777
|
1382 case '+': |
|
1383 case '-': |
3774
|
1384 { |
|
1385 int c1 = yyinput (); |
|
1386 |
|
1387 switch (c1) |
|
1388 { |
|
1389 case '+': |
|
1390 case '-': |
|
1391 // Unary ops, spacing doesn't matter. |
|
1392 break; |
|
1393 |
|
1394 case '=': |
|
1395 // Binary ops, spacing doesn't matter. |
|
1396 bin_op = true; |
|
1397 break; |
|
1398 |
|
1399 default: |
|
1400 // Could be either, spacing matters. |
|
1401 bin_op = looks_like_bin_op (spc_prev, c1); |
|
1402 break; |
|
1403 } |
|
1404 |
|
1405 unput (c1); |
|
1406 } |
|
1407 break; |
|
1408 |
|
1409 case ':': |
3246
|
1410 case '/': |
|
1411 case '\\': |
|
1412 case '^': |
3774
|
1413 // Always a binary op (may also include /=, \=, and ^=). |
|
1414 bin_op = true; |
1276
|
1415 break; |
|
1416 |
3246
|
1417 // .+ .- ./ .\ .^ .* .** |
1554
|
1418 case '.': |
|
1419 { |
|
1420 int c1 = yyinput (); |
3246
|
1421 |
3774
|
1422 if (match_any (c1, "+-/\\^*")) |
|
1423 // Always a binary op (may also include .+=, .-=, ./=, ...). |
|
1424 bin_op = true; |
3698
|
1425 else if (! isdigit (c1) && c1 != ' ' && c1 != '\t' && c1 != '.') |
3774
|
1426 // A structure element reference is a binary op. |
|
1427 bin_op = true; |
3246
|
1428 |
|
1429 unput (c1); |
1554
|
1430 } |
|
1431 break; |
|
1432 |
3246
|
1433 // = == & && | || * ** |
|
1434 case '=': |
1
|
1435 case '&': |
3246
|
1436 case '|': |
1
|
1437 case '*': |
3774
|
1438 // Always a binary op (may also include ==, &&, ||, **). |
|
1439 bin_op = true; |
3246
|
1440 break; |
|
1441 |
3774
|
1442 // < <= <> > >= |
1
|
1443 case '<': |
|
1444 case '>': |
3774
|
1445 // Always a binary op (may also include <=, <>, >=). |
|
1446 bin_op = true; |
|
1447 break; |
|
1448 |
|
1449 // ~= != |
777
|
1450 case '~': |
|
1451 case '!': |
3246
|
1452 { |
|
1453 int c1 = yyinput (); |
|
1454 |
3774
|
1455 // ~ and ! can be unary ops, so require following =. |
|
1456 if (c1 == '=') |
|
1457 bin_op = true; |
3246
|
1458 |
|
1459 unput (c1); |
|
1460 } |
1
|
1461 break; |
|
1462 |
|
1463 default: |
1276
|
1464 break; |
1
|
1465 } |
|
1466 |
3246
|
1467 unput (c0); |
1
|
1468 |
|
1469 return bin_op; |
|
1470 } |
|
1471 |
767
|
1472 // Used to delete trailing white space from tokens. |
|
1473 |
3536
|
1474 static std::string |
1
|
1475 strip_trailing_whitespace (char *s) |
|
1476 { |
3523
|
1477 std::string retval = s; |
1
|
1478 |
1823
|
1479 size_t pos = retval.find_first_of (" \t"); |
1
|
1480 |
1823
|
1481 if (pos != NPOS) |
|
1482 retval.resize (pos); |
1
|
1483 |
|
1484 return retval; |
|
1485 } |
|
1486 |
3665
|
1487 static void |
|
1488 scan_for_comments (const char *text) |
|
1489 { |
|
1490 std::string comment_buf; |
|
1491 |
|
1492 bool in_comment = false; |
|
1493 bool beginning_of_comment = false; |
|
1494 |
|
1495 int len = strlen (text); |
|
1496 int i = 0; |
|
1497 |
|
1498 while (i < len) |
|
1499 { |
|
1500 char c = text[i++]; |
|
1501 |
|
1502 switch (c) |
|
1503 { |
|
1504 case '%': |
|
1505 case '#': |
|
1506 if (in_comment) |
|
1507 { |
|
1508 if (! beginning_of_comment) |
3802
|
1509 comment_buf += static_cast<char> (c); |
3665
|
1510 } |
|
1511 else |
|
1512 { |
|
1513 in_comment = true; |
|
1514 beginning_of_comment = true; |
|
1515 } |
|
1516 break; |
|
1517 |
|
1518 case '\n': |
|
1519 if (in_comment) |
|
1520 { |
3802
|
1521 comment_buf += static_cast<char> (c); |
3665
|
1522 octave_comment_buffer::append (comment_buf); |
|
1523 comment_buf.resize (0); |
|
1524 in_comment = false; |
|
1525 beginning_of_comment = false; |
|
1526 } |
|
1527 break; |
|
1528 |
|
1529 case '\r': |
|
1530 if (in_comment) |
3802
|
1531 comment_buf += static_cast<char> (c); |
3665
|
1532 if (i < len) |
|
1533 { |
|
1534 c = text[i++]; |
|
1535 |
|
1536 if (c == '\n') |
|
1537 { |
|
1538 if (in_comment) |
|
1539 { |
3802
|
1540 comment_buf += static_cast<char> (c); |
3665
|
1541 octave_comment_buffer::append (comment_buf); |
|
1542 in_comment = false; |
|
1543 beginning_of_comment = false; |
|
1544 } |
|
1545 } |
|
1546 } |
|
1547 |
|
1548 default: |
|
1549 if (in_comment) |
|
1550 { |
3802
|
1551 comment_buf += static_cast<char> (c); |
3665
|
1552 beginning_of_comment = false; |
|
1553 } |
|
1554 break; |
|
1555 } |
|
1556 } |
|
1557 |
|
1558 if (! comment_buf.empty ()) |
|
1559 octave_comment_buffer::append (comment_buf); |
|
1560 } |
|
1561 |
1001
|
1562 // Discard whitespace, including comments and continuations. |
1088
|
1563 // |
|
1564 // Return value is logical OR of the following values: |
|
1565 // |
1826
|
1566 // ATE_NOTHING : no spaces to eat |
1088
|
1567 // ATE_SPACE_OR_TAB : space or tab in input |
|
1568 // ATE_NEWLINE : bare new line in input |
1001
|
1569 |
1826
|
1570 static yum_yum |
975
|
1571 eat_whitespace (void) |
|
1572 { |
1826
|
1573 yum_yum retval = ATE_NOTHING; |
3665
|
1574 |
|
1575 std::string comment_buf; |
|
1576 |
2857
|
1577 bool in_comment = false; |
3665
|
1578 bool beginning_of_comment = false; |
|
1579 |
|
1580 int c = 0; |
|
1581 |
975
|
1582 while ((c = yyinput ()) != EOF) |
|
1583 { |
|
1584 current_input_column++; |
|
1585 |
|
1586 switch (c) |
|
1587 { |
|
1588 case ' ': |
|
1589 case '\t': |
3665
|
1590 if (in_comment) |
|
1591 { |
3802
|
1592 comment_buf += static_cast<char> (c); |
3665
|
1593 beginning_of_comment = false; |
|
1594 } |
1088
|
1595 retval |= ATE_SPACE_OR_TAB; |
975
|
1596 break; |
|
1597 |
|
1598 case '\n': |
1088
|
1599 retval |= ATE_NEWLINE; |
3665
|
1600 if (in_comment) |
|
1601 { |
3802
|
1602 comment_buf += static_cast<char> (c); |
3665
|
1603 octave_comment_buffer::append (comment_buf); |
|
1604 comment_buf.resize (0); |
|
1605 in_comment = false; |
|
1606 beginning_of_comment = false; |
|
1607 } |
975
|
1608 current_input_column = 0; |
|
1609 break; |
|
1610 |
|
1611 case '#': |
|
1612 case '%': |
3665
|
1613 if (in_comment) |
|
1614 { |
|
1615 if (! beginning_of_comment) |
3802
|
1616 comment_buf += static_cast<char> (c); |
3665
|
1617 } |
|
1618 else |
|
1619 { |
|
1620 in_comment = true; |
|
1621 beginning_of_comment = true; |
|
1622 } |
975
|
1623 break; |
|
1624 |
1001
|
1625 case '.': |
|
1626 if (in_comment) |
3665
|
1627 { |
3802
|
1628 comment_buf += static_cast<char> (c); |
3665
|
1629 beginning_of_comment = false; |
|
1630 break; |
|
1631 } |
1001
|
1632 else |
|
1633 { |
|
1634 if (have_ellipsis_continuation ()) |
|
1635 break; |
|
1636 else |
|
1637 goto done; |
|
1638 } |
|
1639 |
|
1640 case '\\': |
|
1641 if (in_comment) |
3665
|
1642 { |
3802
|
1643 comment_buf += static_cast<char> (c); |
3665
|
1644 beginning_of_comment = false; |
|
1645 break; |
|
1646 } |
1001
|
1647 else |
|
1648 { |
3105
|
1649 if (have_continuation ()) |
1001
|
1650 break; |
|
1651 else |
|
1652 goto done; |
|
1653 } |
|
1654 |
975
|
1655 default: |
|
1656 if (in_comment) |
3665
|
1657 { |
3802
|
1658 comment_buf += static_cast<char> (c); |
3665
|
1659 beginning_of_comment = false; |
|
1660 break; |
|
1661 } |
975
|
1662 else |
|
1663 goto done; |
|
1664 } |
|
1665 } |
|
1666 |
3665
|
1667 if (! comment_buf.empty ()) |
|
1668 octave_comment_buffer::append (comment_buf); |
|
1669 |
975
|
1670 done: |
3246
|
1671 unput (c); |
1082
|
1672 current_input_column--; |
1001
|
1673 return retval; |
975
|
1674 } |
|
1675 |
3220
|
1676 static inline bool |
|
1677 looks_like_hex (const char *s, int len) |
|
1678 { |
|
1679 return (len > 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X')); |
|
1680 } |
|
1681 |
975
|
1682 static void |
3246
|
1683 handle_number (void) |
972
|
1684 { |
3220
|
1685 double value = 0.0; |
|
1686 int nread = 0; |
|
1687 |
3598
|
1688 if (looks_like_hex (yytext, strlen (yytext))) |
3220
|
1689 { |
|
1690 unsigned long ival; |
3598
|
1691 |
|
1692 nread = sscanf (yytext, "%lx", &ival); |
|
1693 |
3220
|
1694 value = static_cast<double> (ival); |
|
1695 } |
|
1696 else |
3598
|
1697 { |
|
1698 char *tmp = strsave (yytext); |
|
1699 |
|
1700 char *idx = strpbrk (tmp, "Dd"); |
2621
|
1701 |
3598
|
1702 if (idx) |
|
1703 *idx = 'e'; |
|
1704 |
|
1705 nread = sscanf (tmp, "%lf", &value); |
|
1706 |
|
1707 delete [] tmp; |
|
1708 } |
972
|
1709 |
1826
|
1710 // If yytext doesn't contain a valid number, we are in deep doo doo. |
985
|
1711 |
972
|
1712 assert (nread == 1); |
|
1713 |
1826
|
1714 lexer_flags.quote_is_transpose = 1; |
|
1715 lexer_flags.cant_be_identifier = 1; |
|
1716 lexer_flags.convert_spaces_to_comma = 1; |
972
|
1717 |
1826
|
1718 if (lexer_flags.plotting && ! lexer_flags.in_plot_range) |
3575
|
1719 lexer_flags.past_plot_range = true; |
972
|
1720 |
|
1721 yylval.tok_val = new token (value, yytext, input_line_number, |
|
1722 current_input_column); |
|
1723 |
|
1724 token_stack.push (yylval.tok_val); |
|
1725 |
|
1726 current_input_column += yyleng; |
|
1727 |
|
1728 do_comma_insert_check (); |
|
1729 } |
|
1730 |
1001
|
1731 // We have seen a backslash and need to find out if it should be |
|
1732 // treated as a continuation character. If so, this eats it, up to |
|
1733 // and including the new line character. |
|
1734 // |
973
|
1735 // Match whitespace only, followed by a comment character or newline. |
|
1736 // Once a comment character is found, discard all input until newline. |
|
1737 // If non-whitespace characters are found before comment |
|
1738 // characters, return 0. Otherwise, return 1. |
|
1739 |
2857
|
1740 static bool |
3096
|
1741 have_continuation (bool trailing_comments_ok) |
973
|
1742 { |
3523
|
1743 std::ostrstream buf; |
973
|
1744 |
3665
|
1745 std::string comment_buf; |
|
1746 |
2857
|
1747 bool in_comment = false; |
3665
|
1748 bool beginning_of_comment = false; |
|
1749 |
|
1750 int c = 0; |
|
1751 |
973
|
1752 while ((c = yyinput ()) != EOF) |
|
1753 { |
3802
|
1754 buf << static_cast<char> (c); |
973
|
1755 |
|
1756 switch (c) |
|
1757 { |
|
1758 case ' ': |
|
1759 case '\t': |
3665
|
1760 if (in_comment) |
|
1761 { |
3802
|
1762 comment_buf += static_cast<char> (c); |
3665
|
1763 beginning_of_comment = false; |
|
1764 } |
973
|
1765 break; |
|
1766 |
|
1767 case '%': |
|
1768 case '#': |
1091
|
1769 if (trailing_comments_ok) |
3665
|
1770 { |
|
1771 if (in_comment) |
|
1772 { |
|
1773 if (! beginning_of_comment) |
3802
|
1774 comment_buf += static_cast<char> (c); |
3665
|
1775 } |
|
1776 else |
|
1777 { |
|
1778 in_comment = true; |
|
1779 beginning_of_comment = true; |
|
1780 } |
|
1781 } |
1091
|
1782 else |
|
1783 goto cleanup; |
973
|
1784 break; |
|
1785 |
|
1786 case '\n': |
3665
|
1787 if (in_comment) |
|
1788 { |
3802
|
1789 comment_buf += static_cast<char> (c); |
3665
|
1790 octave_comment_buffer::append (comment_buf); |
|
1791 } |
975
|
1792 current_input_column = 0; |
1001
|
1793 promptflag--; |
2857
|
1794 return true; |
973
|
1795 |
3263
|
1796 case '\r': |
3665
|
1797 if (in_comment) |
3802
|
1798 comment_buf += static_cast<char> (c); |
3263
|
1799 c = yyinput (); |
|
1800 if (c == EOF) |
|
1801 break; |
|
1802 else if (c == '\n') |
|
1803 { |
3665
|
1804 if (in_comment) |
|
1805 { |
3802
|
1806 comment_buf += static_cast<char> (c); |
3665
|
1807 octave_comment_buffer::append (comment_buf); |
|
1808 } |
3263
|
1809 current_input_column = 0; |
|
1810 promptflag--; |
|
1811 return true; |
3665
|
1812 } |
3263
|
1813 |
3802
|
1814 // Fall through... |
|
1815 |
973
|
1816 default: |
3665
|
1817 if (in_comment) |
|
1818 { |
3802
|
1819 comment_buf += static_cast<char> (c); |
3665
|
1820 beginning_of_comment = false; |
|
1821 } |
|
1822 else |
1091
|
1823 goto cleanup; |
|
1824 break; |
973
|
1825 } |
|
1826 } |
|
1827 |
3246
|
1828 unput (c); |
2857
|
1829 return false; |
973
|
1830 |
3096
|
1831 cleanup: |
3538
|
1832 buf << std::ends; |
1091
|
1833 char *s = buf.str (); |
|
1834 if (s) |
|
1835 { |
|
1836 int len = strlen (s); |
|
1837 while (len--) |
3246
|
1838 unput (s[len]); |
1091
|
1839 } |
|
1840 delete [] s; |
3096
|
1841 |
2857
|
1842 return false; |
973
|
1843 } |
|
1844 |
1001
|
1845 // We have seen a `.' and need to see if it is the start of a |
|
1846 // continuation. If so, this eats it, up to and including the new |
|
1847 // line character. |
|
1848 |
2857
|
1849 static bool |
3096
|
1850 have_ellipsis_continuation (bool trailing_comments_ok) |
973
|
1851 { |
|
1852 char c1 = yyinput (); |
|
1853 if (c1 == '.') |
|
1854 { |
|
1855 char c2 = yyinput (); |
1091
|
1856 if (c2 == '.' && have_continuation (trailing_comments_ok)) |
2857
|
1857 return true; |
973
|
1858 else |
|
1859 { |
3246
|
1860 unput (c2); |
|
1861 unput (c1); |
973
|
1862 } |
|
1863 } |
|
1864 else |
3246
|
1865 unput (c1); |
973
|
1866 |
2857
|
1867 return false; |
973
|
1868 } |
|
1869 |
1001
|
1870 // See if we have a continuation line. If so, eat it and the leading |
|
1871 // whitespace on the next line. |
1088
|
1872 // |
|
1873 // Return value is the same as described for eat_whitespace(). |
1001
|
1874 |
1826
|
1875 static yum_yum |
1001
|
1876 eat_continuation (void) |
|
1877 { |
1826
|
1878 int retval = ATE_NOTHING; |
3665
|
1879 |
1001
|
1880 int c = yyinput (); |
3665
|
1881 |
1001
|
1882 if ((c == '.' && have_ellipsis_continuation ()) |
3105
|
1883 || (c == '\\' && have_continuation ())) |
1001
|
1884 retval = eat_whitespace (); |
|
1885 else |
3246
|
1886 unput (c); |
1001
|
1887 |
|
1888 return retval; |
|
1889 } |
|
1890 |
973
|
1891 static int |
975
|
1892 handle_string (char delim, int text_style) |
973
|
1893 { |
3523
|
1894 std::ostrstream buf; |
973
|
1895 |
3805
|
1896 int bos_line = input_line_number; |
|
1897 int bos_col = current_input_column; |
|
1898 |
973
|
1899 int c; |
1031
|
1900 int escape_pending = 0; |
973
|
1901 |
|
1902 while ((c = yyinput ()) != EOF) |
|
1903 { |
|
1904 current_input_column++; |
|
1905 |
3105
|
1906 if (c == '\\') |
973
|
1907 { |
1053
|
1908 if (escape_pending) |
|
1909 { |
3802
|
1910 buf << static_cast<char> (c); |
1053
|
1911 escape_pending = 0; |
|
1912 } |
|
1913 else |
|
1914 { |
3096
|
1915 if (have_continuation (false)) |
1053
|
1916 escape_pending = 0; |
|
1917 else |
|
1918 { |
3802
|
1919 buf << static_cast<char> (c); |
1053
|
1920 escape_pending = 1; |
|
1921 } |
|
1922 } |
1031
|
1923 continue; |
973
|
1924 } |
|
1925 else if (c == '.') |
|
1926 { |
3096
|
1927 if (! have_ellipsis_continuation (false)) |
3802
|
1928 buf << static_cast<char> (c); |
973
|
1929 } |
|
1930 else if (c == '\n') |
|
1931 { |
1053
|
1932 error ("unterminated string constant"); |
973
|
1933 break; |
|
1934 } |
|
1935 else if (c == delim) |
|
1936 { |
1031
|
1937 if (escape_pending) |
3802
|
1938 buf << static_cast<char> (c); |
973
|
1939 else |
|
1940 { |
|
1941 c = yyinput (); |
|
1942 if (c == delim) |
3802
|
1943 buf << static_cast<char> (c); |
973
|
1944 else |
|
1945 { |
3246
|
1946 unput (c); |
3538
|
1947 buf << std::ends; |
3105
|
1948 char *t = buf.str (); |
3523
|
1949 std::string s = do_string_escapes (t); |
3105
|
1950 delete [] t; |
975
|
1951 |
1826
|
1952 if (text_style && lexer_flags.doing_set) |
975
|
1953 { |
3523
|
1954 s = std::string (1, delim) + s + std::string (1, delim); |
975
|
1955 } |
|
1956 else |
|
1957 { |
2857
|
1958 lexer_flags.quote_is_transpose = true; |
|
1959 lexer_flags.cant_be_identifier = true; |
|
1960 lexer_flags.convert_spaces_to_comma = true; |
975
|
1961 } |
|
1962 |
3805
|
1963 yylval.tok_val = new token (s, bos_line, bos_col); |
973
|
1964 token_stack.push (yylval.tok_val); |
3400
|
1965 |
|
1966 if (delim == '\'') |
|
1967 gripe_single_quote_string (); |
|
1968 |
973
|
1969 return TEXT; |
|
1970 } |
|
1971 } |
|
1972 } |
|
1973 else |
|
1974 { |
3802
|
1975 buf << static_cast<char> (c); |
973
|
1976 } |
|
1977 |
1031
|
1978 escape_pending = 0; |
973
|
1979 } |
|
1980 |
|
1981 return LEXICAL_ERROR; |
|
1982 } |
|
1983 |
3208
|
1984 static bool |
|
1985 next_token_is_assign_op (void) |
|
1986 { |
|
1987 bool retval = false; |
|
1988 |
|
1989 int c0 = yyinput (); |
|
1990 |
|
1991 switch (c0) |
|
1992 { |
|
1993 case '=': |
|
1994 { |
|
1995 int c1 = yyinput (); |
|
1996 unput (c1); |
|
1997 if (c1 != '=') |
|
1998 retval = true; |
|
1999 } |
|
2000 break; |
|
2001 |
|
2002 case '+': |
|
2003 case '-': |
|
2004 case '*': |
|
2005 case '/': |
|
2006 case '\\': |
|
2007 case '&': |
|
2008 case '|': |
|
2009 { |
|
2010 int c1 = yyinput (); |
|
2011 unput (c1); |
|
2012 if (c1 == '=') |
|
2013 retval = true; |
|
2014 } |
|
2015 break; |
|
2016 |
|
2017 case '.': |
|
2018 { |
|
2019 int c1 = yyinput (); |
|
2020 if (match_any (c1, "+-*/\\")) |
|
2021 { |
|
2022 int c2 = yyinput (); |
|
2023 unput (c2); |
|
2024 if (c2 == '=') |
|
2025 retval = true; |
|
2026 } |
|
2027 unput (c1); |
|
2028 } |
|
2029 break; |
|
2030 |
|
2031 case '>': |
|
2032 { |
|
2033 int c1 = yyinput (); |
|
2034 if (c1 == '>') |
|
2035 { |
|
2036 int c2 = yyinput (); |
|
2037 unput (c2); |
|
2038 if (c2 == '=') |
|
2039 retval = true; |
|
2040 } |
|
2041 unput (c1); |
|
2042 } |
|
2043 break; |
|
2044 |
|
2045 case '<': |
|
2046 { |
|
2047 int c1 = yyinput (); |
|
2048 if (c1 == '<') |
|
2049 { |
|
2050 int c2 = yyinput (); |
|
2051 unput (c2); |
|
2052 if (c2 == '=') |
|
2053 retval = true; |
|
2054 } |
|
2055 unput (c1); |
|
2056 } |
|
2057 break; |
|
2058 |
|
2059 default: |
|
2060 break; |
|
2061 } |
|
2062 |
|
2063 unput (c0); |
|
2064 |
|
2065 return retval; |
|
2066 } |
|
2067 |
971
|
2068 static int |
3351
|
2069 handle_close_bracket (int spc_gobbled) |
971
|
2070 { |
3208
|
2071 int retval = ']'; |
|
2072 |
1826
|
2073 if (! nesting_level.none ()) |
971
|
2074 { |
1826
|
2075 nesting_level.remove (); |
3351
|
2076 lexer_flags.bracketflag--; |
971
|
2077 } |
|
2078 |
3351
|
2079 if (lexer_flags.bracketflag == 0) |
1001
|
2080 BEGIN 0; |
|
2081 |
3208
|
2082 if (next_token_is_assign_op () && ! lexer_flags.looking_at_return_list) |
971
|
2083 { |
3208
|
2084 retval = CLOSE_BRACE; |
971
|
2085 } |
|
2086 else |
|
2087 { |
3208
|
2088 int c1 = yyinput (); |
971
|
2089 unput (c1); |
|
2090 |
3388
|
2091 if (lexer_flags.bracketflag) |
971
|
2092 { |
3246
|
2093 int bin_op = next_token_is_bin_op (spc_gobbled); |
3263
|
2094 |
3246
|
2095 int postfix_un_op = next_token_is_postfix_unary_op (spc_gobbled); |
971
|
2096 |
3263
|
2097 int sep_op = next_token_is_sep_op (); |
971
|
2098 |
3263
|
2099 if (! (postfix_un_op || bin_op || sep_op) |
3351
|
2100 && nesting_level.is_bracket () |
1826
|
2101 && lexer_flags.convert_spaces_to_comma) |
971
|
2102 { |
3388
|
2103 maybe_warn_separator_insert (','); |
|
2104 |
|
2105 if (Vwhitespace_in_literal_matrix != 2) |
|
2106 { |
|
2107 unput (','); |
|
2108 return ']'; |
|
2109 } |
971
|
2110 } |
|
2111 } |
|
2112 } |
|
2113 |
2857
|
2114 lexer_flags.quote_is_transpose = true; |
|
2115 lexer_flags.cant_be_identifier = false; |
|
2116 lexer_flags.convert_spaces_to_comma = true; |
3208
|
2117 |
|
2118 return retval; |
971
|
2119 } |
|
2120 |
1072
|
2121 static void |
|
2122 maybe_unput_comma (int spc_gobbled) |
|
2123 { |
3388
|
2124 if (nesting_level.is_bracket ()) |
1072
|
2125 { |
3246
|
2126 int bin_op = next_token_is_bin_op (spc_gobbled); |
1072
|
2127 |
3246
|
2128 int postfix_un_op = next_token_is_postfix_unary_op (spc_gobbled); |
1072
|
2129 |
|
2130 int c1 = yyinput (); |
|
2131 int c2 = yyinput (); |
2970
|
2132 |
1072
|
2133 unput (c2); |
|
2134 unput (c1); |
2970
|
2135 |
3263
|
2136 int sep_op = next_token_is_sep_op (); |
2970
|
2137 |
1072
|
2138 int dot_op = (c1 == '.' |
|
2139 && (isalpha (c2) || isspace (c2) || c2 == '_')); |
2970
|
2140 |
3388
|
2141 if (postfix_un_op || bin_op || sep_op || dot_op) |
|
2142 return; |
|
2143 |
|
2144 int index_op = (c1 == '('); |
|
2145 |
|
2146 if (index_op) |
|
2147 { |
|
2148 // If there is no space before the '(', we don't insert a comma. |
|
2149 if (! spc_gobbled) |
|
2150 return; |
|
2151 |
|
2152 maybe_warn_separator_insert (','); |
1072
|
2153 |
3388
|
2154 // If there is a space, we only insert a comma if we are |
|
2155 // trying to be Matlab-like. |
|
2156 if (Vwhitespace_in_literal_matrix == 1) |
|
2157 unput (','); |
|
2158 } |
|
2159 else |
|
2160 { |
|
2161 maybe_warn_separator_insert (','); |
|
2162 |
|
2163 if (Vwhitespace_in_literal_matrix != 2) |
|
2164 unput (','); |
|
2165 } |
1072
|
2166 } |
|
2167 } |
|
2168 |
767
|
2169 // Figure out exactly what kind of token to return when we have seen |
|
2170 // an identifier. Handles keywords. |
|
2171 |
146
|
2172 static int |
3523
|
2173 handle_identifier (const std::string& tok, int spc_gobbled) |
146
|
2174 { |
1826
|
2175 // It is almost always an error for an identifier to be followed |
|
2176 // directly by another identifier. Special cases are handled |
|
2177 // below. |
883
|
2178 |
2857
|
2179 lexer_flags.cant_be_identifier = true; |
883
|
2180 |
2970
|
2181 // If we are expecting a structure element, avoid recognizing |
|
2182 // keywords and other special names and return STRUCT_ELT, which is |
|
2183 // a string that is also a valid identifier. But first, we have to |
|
2184 // decide whether to insert a comma. |
747
|
2185 |
1826
|
2186 if (lexer_flags.looking_at_indirect_ref) |
1072
|
2187 { |
2970
|
2188 do_comma_insert_check (); |
|
2189 |
1072
|
2190 maybe_unput_comma (spc_gobbled); |
2819
|
2191 |
|
2192 yylval.tok_val = new token (tok, input_line_number, |
|
2193 current_input_column); |
|
2194 |
|
2195 token_stack.push (yylval.tok_val); |
|
2196 |
2857
|
2197 lexer_flags.cant_be_identifier = false; |
|
2198 lexer_flags.quote_is_transpose = true; |
|
2199 lexer_flags.convert_spaces_to_comma = true; |
2819
|
2200 |
|
2201 current_input_column += yyleng; |
|
2202 |
2970
|
2203 return STRUCT_ELT; |
1072
|
2204 } |
747
|
2205 |
1826
|
2206 // If we have a regular keyword, or a plot STYLE, return it. |
|
2207 // Keywords can be followed by identifiers (TOK_RETURN handles |
|
2208 // that). |
146
|
2209 |
|
2210 int kw_token = is_keyword (tok); |
2970
|
2211 |
146
|
2212 if (kw_token) |
|
2213 { |
|
2214 if (kw_token == STYLE) |
|
2215 { |
1060
|
2216 current_input_column += yyleng; |
2857
|
2217 lexer_flags.quote_is_transpose = false; |
|
2218 lexer_flags.convert_spaces_to_comma = true; |
146
|
2219 return kw_token; |
|
2220 } |
|
2221 else |
|
2222 TOK_RETURN (kw_token); |
|
2223 } |
|
2224 |
1826
|
2225 // See if we have a plot keyword (title, using, with, or clear). |
146
|
2226 |
1826
|
2227 if (lexer_flags.plotting) |
941
|
2228 { |
1826
|
2229 // Yes, we really do need both of these plot_range variables. |
|
2230 // One is used to mark when we are past all possiblity of a plot |
|
2231 // range, the other is used to mark when we are actually between |
|
2232 // the square brackets that surround the range. |
146
|
2233 |
1826
|
2234 if (! lexer_flags.in_plot_range) |
2857
|
2235 lexer_flags.past_plot_range = true; |
941
|
2236 |
3351
|
2237 // Option keywords can't appear in brackets, braces, or parentheses. |
1273
|
2238 |
|
2239 int plot_option_kw = 0; |
2970
|
2240 |
1826
|
2241 if (nesting_level.none ()) |
1273
|
2242 plot_option_kw = is_plot_keyword (tok); |
3575
|
2243 |
1826
|
2244 if (lexer_flags.cant_be_identifier && plot_option_kw) |
941
|
2245 TOK_RETURN (plot_option_kw); |
|
2246 } |
146
|
2247 |
3480
|
2248 int c1 = yyinput (); |
|
2249 |
|
2250 bool next_tok_is_dot = (c1 == '.'); |
|
2251 bool next_tok_is_paren = (c1 == '('); |
|
2252 |
|
2253 bool next_tok_is_eq = false; |
|
2254 if (c1 == '=') |
|
2255 { |
|
2256 int c2 = yyinput (); |
|
2257 unput (c2); |
|
2258 |
|
2259 if (c2 != '=') |
|
2260 next_tok_is_eq = true; |
|
2261 } |
|
2262 |
|
2263 unput (c1); |
1001
|
2264 |
1826
|
2265 // Make sure we put the return values of a function in the symbol |
|
2266 // table that is local to the function. |
146
|
2267 |
2877
|
2268 // If we are defining a function and we have not seen the function |
|
2269 // name yet and the next token is `=', then this identifier must be |
|
2270 // the only return value for the function and it belongs in the |
|
2271 // local symbol table. |
|
2272 |
1826
|
2273 if (next_tok_is_eq |
2877
|
2274 && lexer_flags.defining_func |
|
2275 && ! lexer_flags.parsed_function_name) |
146
|
2276 curr_sym_tab = tmp_local_sym_tab; |
|
2277 |
2702
|
2278 // Kluge alert. |
|
2279 // |
|
2280 // If we are looking at a text style function, set up to gobble its |
2745
|
2281 // arguments. |
|
2282 // |
|
2283 // If the following token is `=', or if we are parsing a function |
3189
|
2284 // return list or function parameter list, or if we are looking at |
|
2285 // something like [ab,cd] = foo (), force the symbol to be inserted |
|
2286 // as a variable in the current symbol table. |
2702
|
2287 |
|
2288 if (is_text_function_name (tok) && ! is_variable (tok)) |
|
2289 { |
2745
|
2290 if (next_tok_is_eq |
|
2291 || lexer_flags.looking_at_return_list |
3189
|
2292 || lexer_flags.looking_at_parameter_list |
3246
|
2293 || lexer_flags.looking_at_matrix_or_assign_lhs |
|
2294 || (next_tok_is_dot && next_token_is_bin_op (spc_gobbled))) |
2745
|
2295 { |
|
2296 force_local_variable (tok); |
|
2297 } |
2702
|
2298 else if (! next_tok_is_paren) |
|
2299 { |
|
2300 if (tok == "gset") |
2857
|
2301 lexer_flags.doing_set = true; |
2702
|
2302 |
|
2303 BEGIN TEXT_FCN; |
|
2304 } |
|
2305 } |
|
2306 |
1826
|
2307 // Find the token in the symbol table. |
146
|
2308 |
|
2309 yylval.tok_val = new token (lookup_identifier (tok), |
|
2310 input_line_number, |
|
2311 current_input_column); |
|
2312 |
|
2313 token_stack.push (yylval.tok_val); |
|
2314 |
1826
|
2315 // After seeing an identifer, it is ok to convert spaces to a comma |
|
2316 // (if needed). |
146
|
2317 |
2857
|
2318 lexer_flags.convert_spaces_to_comma = true; |
146
|
2319 |
2877
|
2320 if (! next_tok_is_eq) |
|
2321 { |
|
2322 lexer_flags.quote_is_transpose = true; |
146
|
2323 |
2877
|
2324 do_comma_insert_check (); |
|
2325 |
|
2326 maybe_unput_comma (spc_gobbled); |
146
|
2327 } |
|
2328 |
2877
|
2329 current_input_column += yyleng; |
146
|
2330 |
|
2331 return NAME; |
|
2332 } |
|
2333 |
767
|
2334 // Print a warning if a function file that defines a function has |
|
2335 // anything other than comments and whitespace following the END token |
|
2336 // that matches the FUNCTION statement. |
|
2337 |
1
|
2338 void |
|
2339 check_for_garbage_after_fcn_def (void) |
|
2340 { |
1826
|
2341 // By making a newline be the next character to be read, we will |
|
2342 // force the parser to return after reading the function. Calling |
3246
|
2343 // unput with EOF does not work. |
1
|
2344 |
3665
|
2345 std::string comment_buf; |
|
2346 |
2857
|
2347 bool in_comment = false; |
3665
|
2348 bool beginning_of_comment = true; |
|
2349 |
1
|
2350 int lineno = input_line_number; |
3665
|
2351 |
|
2352 int c = 0; |
|
2353 |
1
|
2354 while ((c = yyinput ()) != EOF) |
|
2355 { |
|
2356 switch (c) |
|
2357 { |
|
2358 case ' ': |
|
2359 case '\t': |
|
2360 case ';': |
|
2361 case ',': |
3665
|
2362 if (in_comment) |
|
2363 { |
3802
|
2364 comment_buf += static_cast<char> (c); |
3665
|
2365 beginning_of_comment = false; |
|
2366 } |
1
|
2367 break; |
777
|
2368 |
1
|
2369 case '%': |
|
2370 case '#': |
3665
|
2371 if (in_comment) |
|
2372 { |
|
2373 if (! beginning_of_comment) |
3802
|
2374 comment_buf += static_cast<char> (c); |
3665
|
2375 } |
|
2376 else |
|
2377 { |
|
2378 in_comment = true; |
|
2379 beginning_of_comment = true; |
|
2380 } |
1
|
2381 break; |
777
|
2382 |
3802
|
2383 case '\n': |
|
2384 if (in_comment) |
|
2385 { |
|
2386 comment_buf += static_cast<char> (c); |
|
2387 octave_comment_buffer::append (comment_buf); |
|
2388 comment_buf.resize (0); |
|
2389 in_comment = false; |
|
2390 beginning_of_comment = false; |
|
2391 } |
|
2392 break; |
|
2393 |
|
2394 case '\r': |
|
2395 if (in_comment) |
|
2396 comment_buf += static_cast<char> (c); |
|
2397 c = yyinput (); |
|
2398 if (c == EOF) |
|
2399 break; |
|
2400 else if (c == '\n') |
|
2401 { |
|
2402 if (in_comment) |
|
2403 { |
|
2404 comment_buf += static_cast<char> (c); |
|
2405 octave_comment_buffer::append (comment_buf); |
|
2406 comment_buf.resize (0); |
|
2407 in_comment = false; |
|
2408 beginning_of_comment = false; |
|
2409 } |
|
2410 break; |
|
2411 } |
|
2412 |
|
2413 // Fall through... |
|
2414 |
1
|
2415 default: |
|
2416 if (in_comment) |
3665
|
2417 { |
3802
|
2418 comment_buf += static_cast<char> (c); |
3665
|
2419 beginning_of_comment = false; |
|
2420 break; |
|
2421 } |
1
|
2422 else |
|
2423 { |
|
2424 warning ("ignoring trailing garbage after end of function\n\ |
1755
|
2425 near line %d of file `%s.m'", lineno, curr_fcn_file_name.c_str ()); |
1
|
2426 |
3246
|
2427 unput ('\n'); |
1
|
2428 return; |
|
2429 } |
|
2430 } |
|
2431 } |
3665
|
2432 |
|
2433 if (! comment_buf.empty ()) |
|
2434 octave_comment_buffer::append (comment_buf); |
|
2435 |
3246
|
2436 unput ('\n'); |
1
|
2437 } |
|
2438 |
1826
|
2439 void |
|
2440 lexical_feedback::init (void) |
|
2441 { |
|
2442 // Not initially defining a matrix list. |
3351
|
2443 bracketflag = 0; |
1826
|
2444 |
|
2445 // Not initially inside a loop or if statement. |
|
2446 looping = 0; |
|
2447 |
2857
|
2448 // Not initially defining a function. |
|
2449 beginning_of_function = false; |
|
2450 defining_func = false; |
2877
|
2451 parsed_function_name = false; |
2857
|
2452 |
|
2453 // Not parsing a function return or parameter list. |
|
2454 looking_at_return_list = false; |
|
2455 looking_at_parameter_list = false; |
|
2456 |
3796
|
2457 // Not parsing a matrix or the left hand side of multi-value |
|
2458 // assignment statement. |
|
2459 looking_at_matrix_or_assign_lhs = false; |
|
2460 |
2857
|
2461 // Next token can be identifier. |
|
2462 cant_be_identifier = false; |
|
2463 |
|
2464 // No need to do comma insert or convert spaces to comma at |
|
2465 // beginning of input. |
|
2466 convert_spaces_to_comma = true; |
|
2467 do_comma_insert = false; |
|
2468 |
|
2469 // Not initially doing any plotting or setting of plot attributes. |
|
2470 doing_set = false; |
|
2471 in_plot_range = false; |
|
2472 in_plot_style = false; |
3165
|
2473 in_plot_axes = false; |
2857
|
2474 in_plot_using = false; |
|
2475 past_plot_range = false; |
|
2476 plotting = false; |
|
2477 |
1826
|
2478 // Not initially looking at indirect references. |
2857
|
2479 looking_at_indirect_ref = false; |
1826
|
2480 |
|
2481 // Quote marks strings intially. |
2857
|
2482 quote_is_transpose = false; |
1826
|
2483 } |
|
2484 |
3388
|
2485 static void |
|
2486 maybe_warn_separator_insert (char sep) |
|
2487 { |
3523
|
2488 std::string nm = curr_fcn_file_full_name; |
3388
|
2489 |
|
2490 if (Vwarn_separator_insert) |
|
2491 { |
|
2492 if (nm.empty ()) |
|
2493 warning ("potential auto-insertion of `%c' near line %d", |
|
2494 sep, input_line_number); |
|
2495 else |
|
2496 warning ("potential auto-insertion of `%c' near line %d of file %s", |
|
2497 sep, input_line_number, nm.c_str ()); |
|
2498 } |
|
2499 } |
|
2500 |
3400
|
2501 static void |
|
2502 gripe_single_quote_string (void) |
|
2503 { |
3523
|
2504 std::string nm = curr_fcn_file_full_name; |
3400
|
2505 |
|
2506 if (Vwarn_single_quote_string) |
|
2507 { |
|
2508 if (nm.empty ()) |
|
2509 warning ("single quote delimited string near line %d", |
|
2510 input_line_number); |
|
2511 else |
|
2512 warning ("single quote delimited string near line %d of file %s", |
|
2513 input_line_number, nm.c_str ()); |
|
2514 } |
|
2515 } |
|
2516 |
3388
|
2517 static int |
|
2518 warn_separator_insert (void) |
|
2519 { |
|
2520 Vwarn_separator_insert = check_preference ("warn_separator_insert"); |
|
2521 |
|
2522 return 0; |
|
2523 } |
|
2524 |
3246
|
2525 static int |
3400
|
2526 warn_single_quote_string (void) |
|
2527 { |
|
2528 Vwarn_single_quote_string = check_preference ("warn_single_quote_string"); |
|
2529 |
|
2530 return 0; |
|
2531 } |
|
2532 |
|
2533 static int |
2167
|
2534 whitespace_in_literal_matrix (void) |
|
2535 { |
|
2536 int pref = 0; |
3009
|
2537 |
3523
|
2538 std::string val = builtin_string_variable ("whitespace_in_literal_matrix"); |
3009
|
2539 |
2167
|
2540 if (! val.empty ()) |
|
2541 { |
3565
|
2542 if (val == "ignore") |
2167
|
2543 pref = 2; |
3565
|
2544 else if (val == "traditional") |
2167
|
2545 pref = 1; |
|
2546 } |
3009
|
2547 |
2167
|
2548 Vwhitespace_in_literal_matrix = pref; |
3009
|
2549 |
2167
|
2550 return 0; |
|
2551 } |
|
2552 |
|
2553 void |
|
2554 symbols_of_lex (void) |
|
2555 { |
3388
|
2556 DEFVAR (warn_separator_insert, 0.0, warn_separator_insert, |
3428
|
2557 "-*- texinfo -*-\n\ |
|
2558 @defvr {Built-in Variable} warn_separator_insert\n\ |
3567
|
2559 Print warning if commas or semicolons might be inserted\n\ |
3428
|
2560 automatically in literal matrices.\n\ |
|
2561 @end defvr"); |
3388
|
2562 |
3400
|
2563 DEFVAR (warn_single_quote_string, 0.0, warn_single_quote_string, |
3428
|
2564 "-*- texinfo -*-\n\ |
|
2565 @defvr {Built-in Variable} warn_single_quote_string\n\ |
|
2566 Print warning if a signle quote character is used to introduce a\n\ |
|
2567 string constant.\n\ |
|
2568 @end defvr"); |
3400
|
2569 |
3258
|
2570 DEFVAR (whitespace_in_literal_matrix, "", whitespace_in_literal_matrix, |
3428
|
2571 "-*- texinfo -*-\n\ |
|
2572 @defvr {Built-in Variable} whitespace_in_literal_matrix\n\ |
|
2573 Control auto-insertion of commas and semicolons in literal matrices.\n\ |
|
2574 @end defvr"); |
|
2575 |
2167
|
2576 } |
|
2577 |
1994
|
2578 /* |
|
2579 ;;; Local Variables: *** |
|
2580 ;;; mode: C++ *** |
|
2581 ;;; End: *** |
|
2582 */ |