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