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