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