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