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 |
1
|
958 #ifdef yywrap |
|
959 #undef yywrap |
|
960 #endif |
3332
|
961 int |
1
|
962 yywrap (void) |
|
963 { |
287
|
964 return 1; |
1
|
965 } |
|
966 |
767
|
967 // Tell us all what the current buffer is. |
|
968 |
1
|
969 YY_BUFFER_STATE |
|
970 current_buffer (void) |
|
971 { |
|
972 return YY_CURRENT_BUFFER; |
|
973 } |
|
974 |
767
|
975 // Create a new buffer. |
|
976 |
1
|
977 YY_BUFFER_STATE |
|
978 create_buffer (FILE *f) |
|
979 { |
|
980 return yy_create_buffer (f, YY_BUF_SIZE); |
|
981 } |
|
982 |
767
|
983 // Start reading a new buffer. |
|
984 |
1
|
985 void |
|
986 switch_to_buffer (YY_BUFFER_STATE buf) |
|
987 { |
|
988 yy_switch_to_buffer (buf); |
|
989 } |
|
990 |
767
|
991 // Delete a buffer. |
|
992 |
1
|
993 void |
|
994 delete_buffer (YY_BUFFER_STATE buf) |
|
995 { |
|
996 yy_delete_buffer (buf); |
|
997 } |
|
998 |
767
|
999 // Restore a buffer (for unwind-prot). |
|
1000 |
1
|
1001 void |
|
1002 restore_input_buffer (void *buf) |
|
1003 { |
2861
|
1004 switch_to_buffer (static_cast<YY_BUFFER_STATE> (buf)); |
1
|
1005 } |
|
1006 |
767
|
1007 // Delete a buffer (for unwind-prot). |
|
1008 |
1
|
1009 void |
|
1010 delete_input_buffer (void *buf) |
|
1011 { |
2861
|
1012 delete_buffer (static_cast<YY_BUFFER_STATE> (buf)); |
1
|
1013 } |
|
1014 |
767
|
1015 // Check to see if a character string matches any of the possible line |
|
1016 // styles for plots. |
|
1017 |
3536
|
1018 static std::string |
3523
|
1019 plot_style_token (const std::string& s) |
1
|
1020 { |
3523
|
1021 std::string retval; |
1823
|
1022 |
3575
|
1023 // XXX FIXME XXX -- specify minimum match length for these. |
2804
|
1024 static const char *plot_styles[] = |
146
|
1025 { |
925
|
1026 "boxes", |
924
|
1027 "boxerrorbars", |
2542
|
1028 "boxxyerrorbars", |
|
1029 "candlesticks", |
146
|
1030 "dots", |
|
1031 "errorbars", |
2542
|
1032 "financebars", |
|
1033 "fsteps", |
|
1034 "histeps", |
146
|
1035 "impulses", |
|
1036 "lines", |
|
1037 "linespoints", |
|
1038 "points", |
926
|
1039 "steps", |
2542
|
1040 "vector", |
|
1041 "xerrorbars", |
|
1042 "xyerrorbars", |
|
1043 "yerrorbars", |
522
|
1044 0, |
146
|
1045 }; |
|
1046 |
2804
|
1047 const char * const *tmp = plot_styles; |
522
|
1048 while (*tmp) |
1
|
1049 { |
1823
|
1050 if (almost_match (*tmp, s.c_str ())) |
|
1051 { |
|
1052 retval = *tmp; |
|
1053 break; |
|
1054 } |
1
|
1055 |
|
1056 tmp++; |
|
1057 } |
|
1058 |
1823
|
1059 return retval; |
1
|
1060 } |
|
1061 |
3165
|
1062 // Check to see if a character string matches any of the possible axes |
|
1063 // tags for plots. |
|
1064 |
3536
|
1065 static std::string |
3523
|
1066 plot_axes_token (const std::string& s) |
3165
|
1067 { |
3523
|
1068 std::string retval; |
3165
|
1069 |
3575
|
1070 // XXX FIXME XXX -- specify minimum match length for these. |
3561
|
1071 static const char *plot_axes[] = |
3165
|
1072 { |
|
1073 "x1y1", |
|
1074 "x1y2", |
|
1075 "x2y1", |
|
1076 "x2y2", |
|
1077 0, |
|
1078 }; |
|
1079 |
3561
|
1080 const char **tmp = plot_axes; |
3165
|
1081 while (*tmp) |
|
1082 { |
|
1083 if (almost_match (*tmp, s.c_str ())) |
|
1084 { |
|
1085 retval = *tmp; |
|
1086 break; |
|
1087 } |
|
1088 |
|
1089 tmp++; |
|
1090 } |
|
1091 |
|
1092 return retval; |
|
1093 } |
|
1094 |
767
|
1095 // Check to see if a character string matches any one of the plot |
941
|
1096 // option keywords. Don't match abbreviations for clear, since that's |
|
1097 // not a gnuplot keyword (users will probably only expect to be able |
|
1098 // to abbreviate actual gnuplot keywords). |
767
|
1099 |
1
|
1100 static int |
3523
|
1101 is_plot_keyword (const std::string& s) |
1
|
1102 { |
1823
|
1103 const char *t = s.c_str (); |
3575
|
1104 if (almost_match ("title", t, 1)) |
146
|
1105 { |
|
1106 return TITLE; |
|
1107 } |
3575
|
1108 else if (almost_match ("using", t, 1)) |
146
|
1109 { |
2857
|
1110 lexer_flags.in_plot_using = true; |
146
|
1111 return USING; |
|
1112 } |
3575
|
1113 else if (almost_match ("with", t, 1)) |
146
|
1114 { |
2857
|
1115 lexer_flags.in_plot_style = true; |
146
|
1116 return WITH; |
|
1117 } |
3575
|
1118 else if (almost_match ("axes", t, 2) || almost_match ("axis", t, 2)) |
3165
|
1119 { |
|
1120 lexer_flags.in_plot_axes = true; |
|
1121 return AXES; |
|
1122 } |
1823
|
1123 else if (strcmp ("clear", t) == 0) |
883
|
1124 { |
|
1125 return CLEAR; |
|
1126 } |
1
|
1127 else |
146
|
1128 { |
|
1129 return 0; |
|
1130 } |
1
|
1131 } |
|
1132 |
4238
|
1133 static void |
|
1134 prep_for_function (void) |
|
1135 { |
|
1136 end_tokens_expected++; |
|
1137 |
|
1138 // Prepare for local symbols. |
|
1139 |
|
1140 tmp_local_sym_tab = new symbol_table (); |
|
1141 |
|
1142 promptflag--; |
|
1143 |
|
1144 lexer_flags.defining_func = true; |
|
1145 lexer_flags.parsed_function_name = false; |
|
1146 lexer_flags.beginning_of_function = true; |
|
1147 |
|
1148 if (! (reading_fcn_file || reading_script_file)) |
|
1149 input_line_number = 1; |
|
1150 } |
|
1151 |
|
1152 static void |
|
1153 prep_for_nested_function (void) |
|
1154 { |
4240
|
1155 lexer_flags.parsing_nested_function = 1; |
4426
|
1156 help_buf.push (std::string ()); |
4238
|
1157 prep_for_function (); |
4240
|
1158 // We're still only expecting one end token for this set of functions. |
|
1159 end_tokens_expected--; |
4238
|
1160 yylval.tok_val = new token (input_line_number, current_input_column); |
|
1161 token_stack.push (yylval.tok_val); |
|
1162 } |
|
1163 |
|
1164 // Handle keywords. Return -1 if the keyword should be ignored. |
767
|
1165 |
1
|
1166 static int |
3523
|
1167 is_keyword (const std::string& s) |
1
|
1168 { |
3805
|
1169 int l = input_line_number; |
|
1170 int c = current_input_column; |
|
1171 |
3165
|
1172 if (lexer_flags.plotting) |
1
|
1173 { |
3165
|
1174 if (lexer_flags.in_plot_style) |
1
|
1175 { |
3523
|
1176 std::string sty = plot_style_token (s); |
3165
|
1177 |
|
1178 if (! sty.empty ()) |
|
1179 { |
|
1180 lexer_flags.in_plot_style = false; |
3805
|
1181 yylval.tok_val = new token (sty, l, c); |
3165
|
1182 token_stack.push (yylval.tok_val); |
|
1183 return STYLE; |
|
1184 } |
1
|
1185 } |
3165
|
1186 else if (lexer_flags.in_plot_axes) |
|
1187 { |
3523
|
1188 std::string axes = plot_axes_token (s); |
3165
|
1189 |
|
1190 if (! axes.empty ()) |
|
1191 { |
|
1192 lexer_flags.in_plot_axes = false; |
3805
|
1193 yylval.tok_val = new token (axes, l, c); |
3165
|
1194 token_stack.push (yylval.tok_val); |
|
1195 return AXES_TAG; |
|
1196 } |
|
1197 } |
1
|
1198 } |
|
1199 |
1823
|
1200 int len = s.length (); |
922
|
1201 |
1823
|
1202 const octave_kw *kw = octave_kw_lookup (s.c_str (), len); |
191
|
1203 |
1497
|
1204 if (kw) |
143
|
1205 { |
1497
|
1206 yylval.tok_val = 0; |
|
1207 |
|
1208 switch (kw->kw_id) |
|
1209 { |
|
1210 case all_va_args_kw: |
|
1211 case break_kw: |
2764
|
1212 case case_kw: |
1497
|
1213 case catch_kw: |
|
1214 case continue_kw: |
|
1215 case else_kw: |
|
1216 case elseif_kw: |
|
1217 case global_kw: |
2764
|
1218 case otherwise_kw: |
1497
|
1219 case return_kw: |
2846
|
1220 case static_kw: |
3484
|
1221 case until_kw: |
1497
|
1222 case unwind_protect_cleanup_kw: |
|
1223 break; |
|
1224 |
|
1225 case end_kw: |
4234
|
1226 if (lexer_flags.looking_at_object_index) |
|
1227 return 0; |
|
1228 else |
4238
|
1229 { |
|
1230 if (reading_fcn_file && end_tokens_expected == 1) |
|
1231 return -1; |
|
1232 else |
|
1233 { |
|
1234 yylval.tok_val = new token (token::simple_end, l, c); |
|
1235 end_tokens_expected--; |
|
1236 } |
|
1237 } |
1497
|
1238 break; |
|
1239 |
|
1240 case end_try_catch_kw: |
4238
|
1241 end_tokens_expected--; |
1497
|
1242 yylval.tok_val = new token (token::try_catch_end, l, c); |
|
1243 break; |
|
1244 |
|
1245 case end_unwind_protect_kw: |
4238
|
1246 end_tokens_expected--; |
1497
|
1247 yylval.tok_val = new token (token::unwind_protect_end, l, c); |
|
1248 break; |
|
1249 |
|
1250 case endfor_kw: |
4238
|
1251 end_tokens_expected--; |
1497
|
1252 yylval.tok_val = new token (token::for_end, l, c); |
|
1253 break; |
|
1254 |
|
1255 case endfunction_kw: |
4238
|
1256 { |
|
1257 if (reading_fcn_file && end_tokens_expected == 1) |
|
1258 return -1; |
|
1259 else |
|
1260 { |
|
1261 yylval.tok_val = new token (token::function_end, l, c); |
|
1262 end_tokens_expected--; |
|
1263 } |
|
1264 } |
1497
|
1265 break; |
|
1266 |
|
1267 case endif_kw: |
4238
|
1268 end_tokens_expected--; |
1497
|
1269 yylval.tok_val = new token (token::if_end, l, c); |
|
1270 break; |
|
1271 |
2764
|
1272 case endswitch_kw: |
4238
|
1273 end_tokens_expected--; |
2764
|
1274 yylval.tok_val = new token (token::switch_end, l, c); |
|
1275 break; |
|
1276 |
1497
|
1277 case endwhile_kw: |
4238
|
1278 end_tokens_expected--; |
1497
|
1279 yylval.tok_val = new token (token::while_end, l, c); |
|
1280 break; |
|
1281 |
|
1282 case for_kw: |
|
1283 case while_kw: |
4238
|
1284 end_tokens_expected++; |
|
1285 // Fall through... |
|
1286 |
|
1287 case do_kw: |
1497
|
1288 promptflag--; |
1826
|
1289 lexer_flags.looping++; |
1497
|
1290 break; |
|
1291 |
|
1292 case if_kw: |
|
1293 case try_kw: |
2764
|
1294 case switch_kw: |
1497
|
1295 case unwind_protect_kw: |
4238
|
1296 end_tokens_expected++; |
1497
|
1297 promptflag--; |
|
1298 break; |
|
1299 |
|
1300 case gplot_kw: |
2857
|
1301 lexer_flags.plotting = true; |
1497
|
1302 yylval.tok_val = new token (token::two_dee, l, c); |
|
1303 break; |
|
1304 |
|
1305 case gsplot_kw: |
2857
|
1306 lexer_flags.plotting = true; |
1497
|
1307 yylval.tok_val = new token (token::three_dee, l, c); |
|
1308 break; |
|
1309 |
|
1310 case replot_kw: |
2857
|
1311 lexer_flags.plotting = true; |
1497
|
1312 yylval.tok_val = new token (token::replot, l, c); |
|
1313 break; |
|
1314 |
|
1315 case function_kw: |
4238
|
1316 { |
|
1317 if (lexer_flags.defining_func) |
|
1318 { |
|
1319 if (reading_fcn_file) |
|
1320 { |
|
1321 if (lexer_flags.parsing_nested_function) |
|
1322 { |
4323
|
1323 BEGIN (NESTED_FUNCTION_END); |
4240
|
1324 |
4238
|
1325 yylval.tok_val = new token (token::function_end, l, c); |
4240
|
1326 token_stack.push (yylval.tok_val); |
|
1327 |
|
1328 return END; |
4238
|
1329 } |
|
1330 else |
|
1331 { |
|
1332 prep_for_nested_function (); |
4240
|
1333 |
4238
|
1334 return FCN; |
|
1335 } |
|
1336 } |
|
1337 else |
|
1338 { |
|
1339 error ("nested functions not implemented in this context"); |
|
1340 |
|
1341 if ((reading_fcn_file || reading_script_file) |
|
1342 && ! curr_fcn_file_name.empty ()) |
|
1343 error ("near line %d of file `%s.m'", |
|
1344 input_line_number, curr_fcn_file_name.c_str ()); |
|
1345 else |
|
1346 error ("near line %d", input_line_number); |
|
1347 |
|
1348 return LEXICAL_ERROR; |
|
1349 } |
|
1350 } |
|
1351 else |
|
1352 prep_for_function (); |
|
1353 } |
1497
|
1354 break; |
|
1355 |
3174
|
1356 case magic_file_kw: |
|
1357 { |
|
1358 if ((reading_fcn_file || reading_script_file) |
|
1359 && ! curr_fcn_file_full_name.empty ()) |
|
1360 yylval.tok_val = new token (curr_fcn_file_full_name, l, c); |
|
1361 else |
|
1362 yylval.tok_val = new token ("stdin", l, c); |
|
1363 } |
|
1364 break; |
|
1365 |
|
1366 case magic_line_kw: |
|
1367 yylval.tok_val = new token (static_cast<double> (l), "", l, c); |
|
1368 break; |
|
1369 |
3974
|
1370 case varargin_kw: |
|
1371 if (! lexer_flags.looking_at_parameter_list) |
|
1372 return 0; |
|
1373 break; |
|
1374 |
|
1375 case varargout_kw: |
4678
|
1376 if (! (lexer_flags.looking_at_return_list |
|
1377 || (lexer_flags.defining_func |
|
1378 && ! lexer_flags.parsed_function_name))) |
3974
|
1379 return 0; |
|
1380 break; |
|
1381 |
1497
|
1382 default: |
|
1383 panic_impossible (); |
|
1384 } |
|
1385 |
|
1386 if (! yylval.tok_val) |
|
1387 yylval.tok_val = new token (l, c); |
|
1388 |
476
|
1389 token_stack.push (yylval.tok_val); |
1497
|
1390 |
|
1391 return kw->tok; |
143
|
1392 } |
1
|
1393 |
|
1394 return 0; |
|
1395 } |
|
1396 |
767
|
1397 // Try to find an identifier. All binding to global or builtin |
|
1398 // variables occurs when expressions are evaluated. |
|
1399 |
1
|
1400 static symbol_record * |
3523
|
1401 lookup_identifier (const std::string& name) |
1
|
1402 { |
4238
|
1403 std::string sym_name = name; |
|
1404 |
|
1405 if (curr_sym_tab == fbi_sym_tab |
|
1406 && lexer_flags.parsing_nested_function) |
|
1407 sym_name = parent_function_name + ":" + sym_name; |
|
1408 |
|
1409 return curr_sym_tab->lookup (sym_name, true); |
1
|
1410 } |
|
1411 |
2702
|
1412 static bool |
3523
|
1413 is_variable (const std::string& name) |
2702
|
1414 { |
2856
|
1415 symbol_record *sr = curr_sym_tab->lookup (name); |
2702
|
1416 |
|
1417 return sr && sr->is_variable (); |
|
1418 } |
|
1419 |
|
1420 static void |
3523
|
1421 force_local_variable (const std::string& name) |
2702
|
1422 { |
4101
|
1423 if (! is_variable (name)) |
|
1424 curr_sym_tab->clear (name); |
|
1425 |
2856
|
1426 symbol_record *sr = curr_sym_tab->lookup (name, true); |
2702
|
1427 |
|
1428 if (sr) |
|
1429 sr->define (octave_value ()); |
|
1430 } |
|
1431 |
4426
|
1432 // Grab the help text from an function file. |
767
|
1433 |
3427
|
1434 // XXX FIXME XXX -- gobble_leading_white_space() in parse.y |
2300
|
1435 // duplicates some of this code! |
|
1436 |
4426
|
1437 static std::string |
1
|
1438 grab_help_text (void) |
|
1439 { |
4426
|
1440 std::string buf; |
1019
|
1441 |
2300
|
1442 bool begin_comment = true; |
|
1443 bool in_comment = true; |
3427
|
1444 bool discard_space = true; |
3665
|
1445 |
1019
|
1446 int c = 0; |
1
|
1447 |
1019
|
1448 while ((c = yyinput ()) != EOF) |
|
1449 { |
2300
|
1450 if (begin_comment) |
|
1451 { |
|
1452 if (c == '%' || c == '#') |
|
1453 continue; |
3427
|
1454 else if (discard_space && c == ' ') |
|
1455 { |
|
1456 discard_space = false; |
|
1457 continue; |
|
1458 } |
2300
|
1459 else |
|
1460 begin_comment = false; |
|
1461 } |
|
1462 |
1019
|
1463 if (in_comment) |
1
|
1464 { |
4426
|
1465 buf += static_cast<char> (c); |
1755
|
1466 |
1019
|
1467 if (c == '\n') |
3427
|
1468 { |
|
1469 in_comment = false; |
|
1470 discard_space = true; |
|
1471 } |
1019
|
1472 } |
|
1473 else |
|
1474 { |
|
1475 switch (c) |
991
|
1476 { |
4037
|
1477 case '#': |
1019
|
1478 case '%': |
4037
|
1479 maybe_gripe_matlab_incompatible_comment (yytext[0]); |
2300
|
1480 in_comment = true; |
|
1481 begin_comment = true; |
1019
|
1482 break; |
777
|
1483 |
1019
|
1484 case ' ': |
|
1485 case '\t': |
|
1486 break; |
777
|
1487 |
1019
|
1488 default: |
|
1489 goto done; |
1
|
1490 } |
|
1491 } |
1019
|
1492 } |
991
|
1493 |
1019
|
1494 done: |
991
|
1495 |
1019
|
1496 if (c) |
4410
|
1497 yyunput (c, yytext); |
4426
|
1498 |
|
1499 return buf; |
1
|
1500 } |
|
1501 |
767
|
1502 // Return 1 if the given character matches any character in the given |
|
1503 // string. |
|
1504 |
2857
|
1505 static bool |
2804
|
1506 match_any (char c, const char *s) |
1
|
1507 { |
|
1508 char tmp; |
|
1509 while ((tmp = *s++) != '\0') |
|
1510 { |
|
1511 if (c == tmp) |
2857
|
1512 return true; |
1
|
1513 } |
2857
|
1514 return false; |
1
|
1515 } |
|
1516 |
767
|
1517 // Given information about the spacing surrounding an operator, |
|
1518 // return 1 if it looks like it should be treated as a binary |
|
1519 // operator. For example, |
|
1520 // |
3774
|
1521 // [ 1 + 2 ] or [ 1+ 2] or [ 1+2 ] ==> binary |
|
1522 // |
|
1523 // [ 1 +2 ] ==> unary |
767
|
1524 |
2857
|
1525 static bool |
3246
|
1526 looks_like_bin_op (bool spc_prev, int next_char) |
1
|
1527 { |
3246
|
1528 bool spc_next = (next_char == ' ' || next_char == '\t'); |
|
1529 |
608
|
1530 return ((spc_prev && spc_next) || ! spc_prev); |
1
|
1531 } |
|
1532 |
3263
|
1533 // Recognize separators. If the separator is a CRLF pair, it is |
|
1534 // replaced by a single LF. |
|
1535 |
|
1536 static bool |
|
1537 next_token_is_sep_op (void) |
|
1538 { |
|
1539 bool retval = false; |
|
1540 |
|
1541 int c1 = yyinput (); |
|
1542 |
|
1543 if (c1 == '\r') |
|
1544 { |
|
1545 int c2 = yyinput (); |
|
1546 |
|
1547 if (c2 == '\n') |
|
1548 { |
|
1549 c1 = '\n'; |
|
1550 |
|
1551 retval = true; |
|
1552 } |
|
1553 else |
4410
|
1554 yyunput (c2, yytext); |
3263
|
1555 } |
|
1556 else |
|
1557 retval = match_any (c1, ",;\n]"); |
|
1558 |
4410
|
1559 yyunput (c1, yytext); |
3263
|
1560 |
|
1561 return retval; |
|
1562 } |
|
1563 |
767
|
1564 // Try to determine if the next token should be treated as a postfix |
|
1565 // unary operator. This is ugly, but it seems to do the right thing. |
|
1566 |
2857
|
1567 static bool |
3246
|
1568 next_token_is_postfix_unary_op (bool spc_prev) |
1
|
1569 { |
2857
|
1570 bool un_op = false; |
1
|
1571 |
|
1572 int c0 = yyinput (); |
|
1573 |
3246
|
1574 if (c0 == '\'' && ! spc_prev) |
|
1575 { |
|
1576 un_op = true; |
|
1577 } |
|
1578 else if (c0 == '.') |
|
1579 { |
|
1580 int c1 = yyinput (); |
|
1581 un_op = (c1 == '\''); |
4410
|
1582 yyunput (c1, yytext); |
3246
|
1583 } |
4613
|
1584 else if (c0 == '+') |
|
1585 { |
|
1586 int c1 = yyinput (); |
|
1587 un_op = (c1 == '+'); |
|
1588 yyunput (c1, yytext); |
|
1589 } |
|
1590 else if (c0 == '-') |
|
1591 { |
|
1592 int c1 = yyinput (); |
|
1593 un_op = (c1 == '-'); |
|
1594 yyunput (c1, yytext); |
|
1595 } |
1
|
1596 |
4410
|
1597 yyunput (c0, yytext); |
1
|
1598 |
|
1599 return un_op; |
|
1600 } |
|
1601 |
767
|
1602 // Try to determine if the next token should be treated as a binary |
3246
|
1603 // operator. |
1521
|
1604 // |
3246
|
1605 // This kluge exists because whitespace is not always ignored inside |
3774
|
1606 // the square brackets that are used to create matrix objects (though |
|
1607 // spacing only really matters in the cases that can be interpreted |
|
1608 // either as binary ops or prefix unary ops: currently just +, -). |
|
1609 // |
3779
|
1610 // Note that a line continuation directly following a + or - operator |
|
1611 // (e.g., the characters '[' 'a' ' ' '+' '\' LFD 'b' ']') will be |
|
1612 // parsed as a binary operator. |
767
|
1613 |
2857
|
1614 static bool |
3246
|
1615 next_token_is_bin_op (bool spc_prev) |
1
|
1616 { |
2857
|
1617 bool bin_op = false; |
1
|
1618 |
|
1619 int c0 = yyinput (); |
|
1620 |
|
1621 switch (c0) |
|
1622 { |
777
|
1623 case '+': |
|
1624 case '-': |
3774
|
1625 { |
|
1626 int c1 = yyinput (); |
|
1627 |
|
1628 switch (c1) |
|
1629 { |
|
1630 case '+': |
|
1631 case '-': |
|
1632 // Unary ops, spacing doesn't matter. |
|
1633 break; |
|
1634 |
|
1635 case '=': |
|
1636 // Binary ops, spacing doesn't matter. |
|
1637 bin_op = true; |
|
1638 break; |
|
1639 |
|
1640 default: |
|
1641 // Could be either, spacing matters. |
|
1642 bin_op = looks_like_bin_op (spc_prev, c1); |
|
1643 break; |
|
1644 } |
|
1645 |
4410
|
1646 yyunput (c1, yytext); |
3774
|
1647 } |
|
1648 break; |
|
1649 |
|
1650 case ':': |
3246
|
1651 case '/': |
|
1652 case '\\': |
|
1653 case '^': |
3774
|
1654 // Always a binary op (may also include /=, \=, and ^=). |
|
1655 bin_op = true; |
1276
|
1656 break; |
|
1657 |
3246
|
1658 // .+ .- ./ .\ .^ .* .** |
1554
|
1659 case '.': |
|
1660 { |
|
1661 int c1 = yyinput (); |
3246
|
1662 |
3774
|
1663 if (match_any (c1, "+-/\\^*")) |
|
1664 // Always a binary op (may also include .+=, .-=, ./=, ...). |
|
1665 bin_op = true; |
3698
|
1666 else if (! isdigit (c1) && c1 != ' ' && c1 != '\t' && c1 != '.') |
3774
|
1667 // A structure element reference is a binary op. |
|
1668 bin_op = true; |
3246
|
1669 |
4410
|
1670 yyunput (c1, yytext); |
1554
|
1671 } |
|
1672 break; |
|
1673 |
3246
|
1674 // = == & && | || * ** |
|
1675 case '=': |
1
|
1676 case '&': |
3246
|
1677 case '|': |
1
|
1678 case '*': |
3774
|
1679 // Always a binary op (may also include ==, &&, ||, **). |
|
1680 bin_op = true; |
3246
|
1681 break; |
|
1682 |
3774
|
1683 // < <= <> > >= |
1
|
1684 case '<': |
|
1685 case '>': |
3774
|
1686 // Always a binary op (may also include <=, <>, >=). |
|
1687 bin_op = true; |
|
1688 break; |
|
1689 |
|
1690 // ~= != |
777
|
1691 case '~': |
|
1692 case '!': |
3246
|
1693 { |
|
1694 int c1 = yyinput (); |
|
1695 |
3774
|
1696 // ~ and ! can be unary ops, so require following =. |
|
1697 if (c1 == '=') |
|
1698 bin_op = true; |
3246
|
1699 |
4410
|
1700 yyunput (c1, yytext); |
3246
|
1701 } |
1
|
1702 break; |
|
1703 |
|
1704 default: |
1276
|
1705 break; |
1
|
1706 } |
|
1707 |
4410
|
1708 yyunput (c0, yytext); |
1
|
1709 |
|
1710 return bin_op; |
|
1711 } |
|
1712 |
767
|
1713 // Used to delete trailing white space from tokens. |
|
1714 |
3536
|
1715 static std::string |
1
|
1716 strip_trailing_whitespace (char *s) |
|
1717 { |
3523
|
1718 std::string retval = s; |
1
|
1719 |
1823
|
1720 size_t pos = retval.find_first_of (" \t"); |
1
|
1721 |
1823
|
1722 if (pos != NPOS) |
|
1723 retval.resize (pos); |
1
|
1724 |
|
1725 return retval; |
|
1726 } |
|
1727 |
3665
|
1728 static void |
|
1729 scan_for_comments (const char *text) |
|
1730 { |
|
1731 std::string comment_buf; |
|
1732 |
|
1733 bool in_comment = false; |
|
1734 bool beginning_of_comment = false; |
|
1735 |
|
1736 int len = strlen (text); |
|
1737 int i = 0; |
|
1738 |
|
1739 while (i < len) |
|
1740 { |
|
1741 char c = text[i++]; |
|
1742 |
|
1743 switch (c) |
|
1744 { |
|
1745 case '%': |
|
1746 case '#': |
|
1747 if (in_comment) |
|
1748 { |
|
1749 if (! beginning_of_comment) |
3802
|
1750 comment_buf += static_cast<char> (c); |
3665
|
1751 } |
|
1752 else |
|
1753 { |
4037
|
1754 maybe_gripe_matlab_incompatible_comment (c); |
3665
|
1755 in_comment = true; |
|
1756 beginning_of_comment = true; |
|
1757 } |
|
1758 break; |
|
1759 |
|
1760 case '\n': |
|
1761 if (in_comment) |
|
1762 { |
3802
|
1763 comment_buf += static_cast<char> (c); |
3665
|
1764 octave_comment_buffer::append (comment_buf); |
|
1765 comment_buf.resize (0); |
|
1766 in_comment = false; |
|
1767 beginning_of_comment = false; |
|
1768 } |
|
1769 break; |
|
1770 |
|
1771 case '\r': |
|
1772 if (in_comment) |
3802
|
1773 comment_buf += static_cast<char> (c); |
3665
|
1774 if (i < len) |
|
1775 { |
|
1776 c = text[i++]; |
|
1777 |
|
1778 if (c == '\n') |
|
1779 { |
|
1780 if (in_comment) |
|
1781 { |
3802
|
1782 comment_buf += static_cast<char> (c); |
3665
|
1783 octave_comment_buffer::append (comment_buf); |
|
1784 in_comment = false; |
|
1785 beginning_of_comment = false; |
|
1786 } |
|
1787 } |
|
1788 } |
|
1789 |
|
1790 default: |
|
1791 if (in_comment) |
|
1792 { |
3802
|
1793 comment_buf += static_cast<char> (c); |
3665
|
1794 beginning_of_comment = false; |
|
1795 } |
|
1796 break; |
|
1797 } |
|
1798 } |
|
1799 |
|
1800 if (! comment_buf.empty ()) |
|
1801 octave_comment_buffer::append (comment_buf); |
|
1802 } |
|
1803 |
1001
|
1804 // Discard whitespace, including comments and continuations. |
1088
|
1805 // |
|
1806 // Return value is logical OR of the following values: |
|
1807 // |
1826
|
1808 // ATE_NOTHING : no spaces to eat |
1088
|
1809 // ATE_SPACE_OR_TAB : space or tab in input |
|
1810 // ATE_NEWLINE : bare new line in input |
1001
|
1811 |
1826
|
1812 static yum_yum |
975
|
1813 eat_whitespace (void) |
|
1814 { |
1826
|
1815 yum_yum retval = ATE_NOTHING; |
3665
|
1816 |
|
1817 std::string comment_buf; |
|
1818 |
2857
|
1819 bool in_comment = false; |
3665
|
1820 bool beginning_of_comment = false; |
|
1821 |
|
1822 int c = 0; |
|
1823 |
975
|
1824 while ((c = yyinput ()) != EOF) |
|
1825 { |
|
1826 current_input_column++; |
|
1827 |
|
1828 switch (c) |
|
1829 { |
|
1830 case ' ': |
|
1831 case '\t': |
3665
|
1832 if (in_comment) |
|
1833 { |
3802
|
1834 comment_buf += static_cast<char> (c); |
3665
|
1835 beginning_of_comment = false; |
|
1836 } |
1088
|
1837 retval |= ATE_SPACE_OR_TAB; |
975
|
1838 break; |
|
1839 |
|
1840 case '\n': |
1088
|
1841 retval |= ATE_NEWLINE; |
3665
|
1842 if (in_comment) |
|
1843 { |
3802
|
1844 comment_buf += static_cast<char> (c); |
3665
|
1845 octave_comment_buffer::append (comment_buf); |
|
1846 comment_buf.resize (0); |
|
1847 in_comment = false; |
|
1848 beginning_of_comment = false; |
|
1849 } |
975
|
1850 current_input_column = 0; |
|
1851 break; |
|
1852 |
|
1853 case '#': |
|
1854 case '%': |
3665
|
1855 if (in_comment) |
|
1856 { |
|
1857 if (! beginning_of_comment) |
3802
|
1858 comment_buf += static_cast<char> (c); |
3665
|
1859 } |
|
1860 else |
|
1861 { |
4037
|
1862 maybe_gripe_matlab_incompatible_comment (c); |
3665
|
1863 in_comment = true; |
|
1864 beginning_of_comment = true; |
|
1865 } |
975
|
1866 break; |
|
1867 |
1001
|
1868 case '.': |
|
1869 if (in_comment) |
3665
|
1870 { |
3802
|
1871 comment_buf += static_cast<char> (c); |
3665
|
1872 beginning_of_comment = false; |
|
1873 break; |
|
1874 } |
1001
|
1875 else |
|
1876 { |
|
1877 if (have_ellipsis_continuation ()) |
|
1878 break; |
|
1879 else |
|
1880 goto done; |
|
1881 } |
|
1882 |
|
1883 case '\\': |
|
1884 if (in_comment) |
3665
|
1885 { |
3802
|
1886 comment_buf += static_cast<char> (c); |
3665
|
1887 beginning_of_comment = false; |
|
1888 break; |
|
1889 } |
1001
|
1890 else |
|
1891 { |
3105
|
1892 if (have_continuation ()) |
1001
|
1893 break; |
|
1894 else |
|
1895 goto done; |
|
1896 } |
|
1897 |
975
|
1898 default: |
|
1899 if (in_comment) |
3665
|
1900 { |
3802
|
1901 comment_buf += static_cast<char> (c); |
3665
|
1902 beginning_of_comment = false; |
|
1903 break; |
|
1904 } |
975
|
1905 else |
|
1906 goto done; |
|
1907 } |
|
1908 } |
|
1909 |
3665
|
1910 if (! comment_buf.empty ()) |
|
1911 octave_comment_buffer::append (comment_buf); |
|
1912 |
975
|
1913 done: |
4410
|
1914 yyunput (c, yytext); |
1082
|
1915 current_input_column--; |
1001
|
1916 return retval; |
975
|
1917 } |
|
1918 |
3220
|
1919 static inline bool |
|
1920 looks_like_hex (const char *s, int len) |
|
1921 { |
|
1922 return (len > 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X')); |
|
1923 } |
|
1924 |
975
|
1925 static void |
3246
|
1926 handle_number (void) |
972
|
1927 { |
3220
|
1928 double value = 0.0; |
|
1929 int nread = 0; |
|
1930 |
3598
|
1931 if (looks_like_hex (yytext, strlen (yytext))) |
3220
|
1932 { |
|
1933 unsigned long ival; |
3598
|
1934 |
|
1935 nread = sscanf (yytext, "%lx", &ival); |
|
1936 |
3220
|
1937 value = static_cast<double> (ival); |
|
1938 } |
|
1939 else |
3598
|
1940 { |
|
1941 char *tmp = strsave (yytext); |
|
1942 |
|
1943 char *idx = strpbrk (tmp, "Dd"); |
2621
|
1944 |
3598
|
1945 if (idx) |
|
1946 *idx = 'e'; |
|
1947 |
|
1948 nread = sscanf (tmp, "%lf", &value); |
|
1949 |
|
1950 delete [] tmp; |
|
1951 } |
972
|
1952 |
1826
|
1953 // If yytext doesn't contain a valid number, we are in deep doo doo. |
985
|
1954 |
972
|
1955 assert (nread == 1); |
|
1956 |
3988
|
1957 lexer_flags.quote_is_transpose = true; |
|
1958 lexer_flags.cant_be_identifier = true; |
|
1959 lexer_flags.convert_spaces_to_comma = true; |
972
|
1960 |
1826
|
1961 if (lexer_flags.plotting && ! lexer_flags.in_plot_range) |
3575
|
1962 lexer_flags.past_plot_range = true; |
972
|
1963 |
|
1964 yylval.tok_val = new token (value, yytext, input_line_number, |
|
1965 current_input_column); |
|
1966 |
|
1967 token_stack.push (yylval.tok_val); |
|
1968 |
|
1969 current_input_column += yyleng; |
|
1970 |
|
1971 do_comma_insert_check (); |
|
1972 } |
|
1973 |
1001
|
1974 // We have seen a backslash and need to find out if it should be |
|
1975 // treated as a continuation character. If so, this eats it, up to |
|
1976 // and including the new line character. |
|
1977 // |
973
|
1978 // Match whitespace only, followed by a comment character or newline. |
|
1979 // Once a comment character is found, discard all input until newline. |
|
1980 // If non-whitespace characters are found before comment |
|
1981 // characters, return 0. Otherwise, return 1. |
|
1982 |
2857
|
1983 static bool |
3096
|
1984 have_continuation (bool trailing_comments_ok) |
973
|
1985 { |
4051
|
1986 OSSTREAM buf; |
973
|
1987 |
3665
|
1988 std::string comment_buf; |
|
1989 |
2857
|
1990 bool in_comment = false; |
3665
|
1991 bool beginning_of_comment = false; |
|
1992 |
|
1993 int c = 0; |
|
1994 |
973
|
1995 while ((c = yyinput ()) != EOF) |
|
1996 { |
3802
|
1997 buf << static_cast<char> (c); |
973
|
1998 |
|
1999 switch (c) |
|
2000 { |
|
2001 case ' ': |
|
2002 case '\t': |
3665
|
2003 if (in_comment) |
|
2004 { |
3802
|
2005 comment_buf += static_cast<char> (c); |
3665
|
2006 beginning_of_comment = false; |
|
2007 } |
973
|
2008 break; |
|
2009 |
|
2010 case '%': |
|
2011 case '#': |
1091
|
2012 if (trailing_comments_ok) |
3665
|
2013 { |
|
2014 if (in_comment) |
|
2015 { |
|
2016 if (! beginning_of_comment) |
3802
|
2017 comment_buf += static_cast<char> (c); |
3665
|
2018 } |
|
2019 else |
|
2020 { |
4037
|
2021 maybe_gripe_matlab_incompatible_comment (c); |
3665
|
2022 in_comment = true; |
|
2023 beginning_of_comment = true; |
|
2024 } |
|
2025 } |
1091
|
2026 else |
|
2027 goto cleanup; |
973
|
2028 break; |
|
2029 |
|
2030 case '\n': |
3665
|
2031 if (in_comment) |
|
2032 { |
3802
|
2033 comment_buf += static_cast<char> (c); |
3665
|
2034 octave_comment_buffer::append (comment_buf); |
|
2035 } |
975
|
2036 current_input_column = 0; |
1001
|
2037 promptflag--; |
4037
|
2038 gripe_matlab_incompatible_continuation (); |
2857
|
2039 return true; |
973
|
2040 |
3263
|
2041 case '\r': |
3665
|
2042 if (in_comment) |
3802
|
2043 comment_buf += static_cast<char> (c); |
3263
|
2044 c = yyinput (); |
|
2045 if (c == EOF) |
|
2046 break; |
|
2047 else if (c == '\n') |
|
2048 { |
3665
|
2049 if (in_comment) |
|
2050 { |
3802
|
2051 comment_buf += static_cast<char> (c); |
3665
|
2052 octave_comment_buffer::append (comment_buf); |
|
2053 } |
3263
|
2054 current_input_column = 0; |
|
2055 promptflag--; |
4037
|
2056 gripe_matlab_incompatible_continuation (); |
3263
|
2057 return true; |
3665
|
2058 } |
3263
|
2059 |
3802
|
2060 // Fall through... |
|
2061 |
973
|
2062 default: |
3665
|
2063 if (in_comment) |
|
2064 { |
3802
|
2065 comment_buf += static_cast<char> (c); |
3665
|
2066 beginning_of_comment = false; |
|
2067 } |
|
2068 else |
1091
|
2069 goto cleanup; |
|
2070 break; |
973
|
2071 } |
|
2072 } |
|
2073 |
4410
|
2074 yyunput (c, yytext); |
2857
|
2075 return false; |
973
|
2076 |
3096
|
2077 cleanup: |
4051
|
2078 |
|
2079 buf << OSSTREAM_ENDS; |
|
2080 std::string s = OSSTREAM_STR (buf); |
|
2081 OSSTREAM_FREEZE (buf); |
|
2082 |
|
2083 int len = s.length (); |
|
2084 while (len--) |
4410
|
2085 yyunput (s[len], yytext); |
3096
|
2086 |
2857
|
2087 return false; |
973
|
2088 } |
|
2089 |
1001
|
2090 // We have seen a `.' and need to see if it is the start of a |
|
2091 // continuation. If so, this eats it, up to and including the new |
|
2092 // line character. |
|
2093 |
2857
|
2094 static bool |
3096
|
2095 have_ellipsis_continuation (bool trailing_comments_ok) |
973
|
2096 { |
|
2097 char c1 = yyinput (); |
|
2098 if (c1 == '.') |
|
2099 { |
|
2100 char c2 = yyinput (); |
1091
|
2101 if (c2 == '.' && have_continuation (trailing_comments_ok)) |
2857
|
2102 return true; |
973
|
2103 else |
|
2104 { |
4410
|
2105 yyunput (c2, yytext); |
|
2106 yyunput (c1, yytext); |
973
|
2107 } |
|
2108 } |
|
2109 else |
4410
|
2110 yyunput (c1, yytext); |
973
|
2111 |
2857
|
2112 return false; |
973
|
2113 } |
|
2114 |
1001
|
2115 // See if we have a continuation line. If so, eat it and the leading |
|
2116 // whitespace on the next line. |
1088
|
2117 // |
|
2118 // Return value is the same as described for eat_whitespace(). |
1001
|
2119 |
1826
|
2120 static yum_yum |
1001
|
2121 eat_continuation (void) |
|
2122 { |
1826
|
2123 int retval = ATE_NOTHING; |
3665
|
2124 |
1001
|
2125 int c = yyinput (); |
3665
|
2126 |
1001
|
2127 if ((c == '.' && have_ellipsis_continuation ()) |
3105
|
2128 || (c == '\\' && have_continuation ())) |
1001
|
2129 retval = eat_whitespace (); |
|
2130 else |
4410
|
2131 yyunput (c, yytext); |
1001
|
2132 |
|
2133 return retval; |
|
2134 } |
|
2135 |
973
|
2136 static int |
975
|
2137 handle_string (char delim, int text_style) |
973
|
2138 { |
4051
|
2139 OSSTREAM buf; |
973
|
2140 |
3805
|
2141 int bos_line = input_line_number; |
|
2142 int bos_col = current_input_column; |
|
2143 |
973
|
2144 int c; |
1031
|
2145 int escape_pending = 0; |
973
|
2146 |
|
2147 while ((c = yyinput ()) != EOF) |
|
2148 { |
|
2149 current_input_column++; |
|
2150 |
3105
|
2151 if (c == '\\') |
973
|
2152 { |
1053
|
2153 if (escape_pending) |
|
2154 { |
3802
|
2155 buf << static_cast<char> (c); |
1053
|
2156 escape_pending = 0; |
|
2157 } |
|
2158 else |
|
2159 { |
3096
|
2160 if (have_continuation (false)) |
1053
|
2161 escape_pending = 0; |
|
2162 else |
|
2163 { |
3802
|
2164 buf << static_cast<char> (c); |
1053
|
2165 escape_pending = 1; |
|
2166 } |
|
2167 } |
1031
|
2168 continue; |
973
|
2169 } |
|
2170 else if (c == '.') |
|
2171 { |
3096
|
2172 if (! have_ellipsis_continuation (false)) |
3802
|
2173 buf << static_cast<char> (c); |
973
|
2174 } |
|
2175 else if (c == '\n') |
|
2176 { |
1053
|
2177 error ("unterminated string constant"); |
973
|
2178 break; |
|
2179 } |
|
2180 else if (c == delim) |
|
2181 { |
1031
|
2182 if (escape_pending) |
3802
|
2183 buf << static_cast<char> (c); |
973
|
2184 else |
|
2185 { |
|
2186 c = yyinput (); |
|
2187 if (c == delim) |
3802
|
2188 buf << static_cast<char> (c); |
973
|
2189 else |
|
2190 { |
4410
|
2191 yyunput (c, yytext); |
4051
|
2192 buf << OSSTREAM_ENDS; |
|
2193 std::string s = do_string_escapes (OSSTREAM_STR (buf)); |
|
2194 OSSTREAM_FREEZE (buf); |
975
|
2195 |
1826
|
2196 if (text_style && lexer_flags.doing_set) |
975
|
2197 { |
3523
|
2198 s = std::string (1, delim) + s + std::string (1, delim); |
975
|
2199 } |
|
2200 else |
|
2201 { |
2857
|
2202 lexer_flags.quote_is_transpose = true; |
|
2203 lexer_flags.cant_be_identifier = true; |
|
2204 lexer_flags.convert_spaces_to_comma = true; |
975
|
2205 } |
|
2206 |
3805
|
2207 yylval.tok_val = new token (s, bos_line, bos_col); |
973
|
2208 token_stack.push (yylval.tok_val); |
3400
|
2209 |
4037
|
2210 if (delim == '"') |
|
2211 gripe_matlab_incompatible ("\" used as string delimiter"); |
|
2212 else if (delim == '\'') |
3400
|
2213 gripe_single_quote_string (); |
|
2214 |
4064
|
2215 return STRING; |
973
|
2216 } |
|
2217 } |
|
2218 } |
|
2219 else |
|
2220 { |
3802
|
2221 buf << static_cast<char> (c); |
973
|
2222 } |
|
2223 |
1031
|
2224 escape_pending = 0; |
973
|
2225 } |
|
2226 |
|
2227 return LEXICAL_ERROR; |
|
2228 } |
|
2229 |
3208
|
2230 static bool |
|
2231 next_token_is_assign_op (void) |
|
2232 { |
|
2233 bool retval = false; |
|
2234 |
|
2235 int c0 = yyinput (); |
|
2236 |
|
2237 switch (c0) |
|
2238 { |
|
2239 case '=': |
|
2240 { |
|
2241 int c1 = yyinput (); |
4410
|
2242 yyunput (c1, yytext); |
3208
|
2243 if (c1 != '=') |
|
2244 retval = true; |
|
2245 } |
|
2246 break; |
|
2247 |
|
2248 case '+': |
|
2249 case '-': |
|
2250 case '*': |
|
2251 case '/': |
|
2252 case '\\': |
|
2253 case '&': |
|
2254 case '|': |
|
2255 { |
|
2256 int c1 = yyinput (); |
4410
|
2257 yyunput (c1, yytext); |
3208
|
2258 if (c1 == '=') |
|
2259 retval = true; |
|
2260 } |
|
2261 break; |
|
2262 |
|
2263 case '.': |
|
2264 { |
|
2265 int c1 = yyinput (); |
|
2266 if (match_any (c1, "+-*/\\")) |
|
2267 { |
|
2268 int c2 = yyinput (); |
4410
|
2269 yyunput (c2, yytext); |
3208
|
2270 if (c2 == '=') |
|
2271 retval = true; |
|
2272 } |
4410
|
2273 yyunput (c1, yytext); |
3208
|
2274 } |
|
2275 break; |
|
2276 |
|
2277 case '>': |
|
2278 { |
|
2279 int c1 = yyinput (); |
|
2280 if (c1 == '>') |
|
2281 { |
|
2282 int c2 = yyinput (); |
4410
|
2283 yyunput (c2, yytext); |
3208
|
2284 if (c2 == '=') |
|
2285 retval = true; |
|
2286 } |
4410
|
2287 yyunput (c1, yytext); |
3208
|
2288 } |
|
2289 break; |
|
2290 |
|
2291 case '<': |
|
2292 { |
|
2293 int c1 = yyinput (); |
|
2294 if (c1 == '<') |
|
2295 { |
|
2296 int c2 = yyinput (); |
4410
|
2297 yyunput (c2, yytext); |
3208
|
2298 if (c2 == '=') |
|
2299 retval = true; |
|
2300 } |
4410
|
2301 yyunput (c1, yytext); |
3208
|
2302 } |
|
2303 break; |
|
2304 |
|
2305 default: |
|
2306 break; |
|
2307 } |
|
2308 |
4410
|
2309 yyunput (c0, yytext); |
3208
|
2310 |
|
2311 return retval; |
|
2312 } |
|
2313 |
4633
|
2314 static bool |
|
2315 next_token_is_index_op (void) |
|
2316 { |
|
2317 int c = yyinput (); |
|
2318 yyunput (c, yytext); |
|
2319 return c == '(' || c == '{'; |
|
2320 } |
|
2321 |
4612
|
2322 static int |
|
2323 handle_close_bracket (bool spc_gobbled, int bracket_type) |
971
|
2324 { |
4612
|
2325 int retval = bracket_type; |
3208
|
2326 |
1826
|
2327 if (! nesting_level.none ()) |
971
|
2328 { |
1826
|
2329 nesting_level.remove (); |
4613
|
2330 |
|
2331 if (bracket_type == ']') |
|
2332 lexer_flags.bracketflag--; |
|
2333 else if (bracket_type == '}') |
|
2334 lexer_flags.braceflag--; |
|
2335 else |
|
2336 panic_impossible (); |
971
|
2337 } |
|
2338 |
4613
|
2339 if (lexer_flags.bracketflag == 0 && lexer_flags.braceflag == 0) |
4323
|
2340 BEGIN (INITIAL); |
1001
|
2341 |
4608
|
2342 if (bracket_type == ']' |
|
2343 && next_token_is_assign_op () |
|
2344 && ! lexer_flags.looking_at_return_list) |
971
|
2345 { |
3208
|
2346 retval = CLOSE_BRACE; |
971
|
2347 } |
4613
|
2348 else if ((lexer_flags.bracketflag || lexer_flags.braceflag) |
|
2349 && lexer_flags.convert_spaces_to_comma |
|
2350 && (nesting_level.is_bracket () |
|
2351 || (nesting_level.is_brace () |
|
2352 && ! lexer_flags.looking_at_object_index))) |
971
|
2353 { |
4633
|
2354 bool index_op = next_token_is_index_op (); |
|
2355 |
|
2356 // Don't insert comma if we are looking at something like |
|
2357 // |
|
2358 // [x{i}{j}] or [x{i}(j)] |
|
2359 // |
|
2360 // but do if we are looking at |
|
2361 // |
|
2362 // [x{i} {j}] or [x{i} (j)] |
|
2363 |
|
2364 if (spc_gobbled || ! (bracket_type == '}' && index_op)) |
971
|
2365 { |
4633
|
2366 bool bin_op = next_token_is_bin_op (spc_gobbled); |
|
2367 |
|
2368 bool postfix_un_op = next_token_is_postfix_unary_op (spc_gobbled); |
|
2369 |
|
2370 bool sep_op = next_token_is_sep_op (); |
|
2371 |
|
2372 if (! (postfix_un_op || bin_op || sep_op)) |
|
2373 { |
|
2374 maybe_warn_separator_insert (','); |
|
2375 |
|
2376 yyunput (',', yytext); |
|
2377 return retval; |
|
2378 } |
971
|
2379 } |
|
2380 } |
|
2381 |
2857
|
2382 lexer_flags.quote_is_transpose = true; |
|
2383 lexer_flags.cant_be_identifier = false; |
|
2384 lexer_flags.convert_spaces_to_comma = true; |
3208
|
2385 |
|
2386 return retval; |
971
|
2387 } |
|
2388 |
1072
|
2389 static void |
|
2390 maybe_unput_comma (int spc_gobbled) |
|
2391 { |
4613
|
2392 if (nesting_level.is_bracket () |
|
2393 || (nesting_level.is_brace () |
|
2394 && ! lexer_flags.looking_at_object_index)) |
1072
|
2395 { |
3246
|
2396 int bin_op = next_token_is_bin_op (spc_gobbled); |
1072
|
2397 |
3246
|
2398 int postfix_un_op = next_token_is_postfix_unary_op (spc_gobbled); |
1072
|
2399 |
|
2400 int c1 = yyinput (); |
|
2401 int c2 = yyinput (); |
2970
|
2402 |
4410
|
2403 yyunput (c2, yytext); |
|
2404 yyunput (c1, yytext); |
2970
|
2405 |
3263
|
2406 int sep_op = next_token_is_sep_op (); |
2970
|
2407 |
1072
|
2408 int dot_op = (c1 == '.' |
|
2409 && (isalpha (c2) || isspace (c2) || c2 == '_')); |
2970
|
2410 |
3388
|
2411 if (postfix_un_op || bin_op || sep_op || dot_op) |
|
2412 return; |
|
2413 |
3985
|
2414 int index_op = (c1 == '(' || c1 == '{'); |
3388
|
2415 |
4476
|
2416 // If there is no space before the indexing op, we don't insert |
|
2417 // a comma. |
|
2418 |
|
2419 if (index_op && ! spc_gobbled) |
|
2420 return; |
|
2421 |
|
2422 maybe_warn_separator_insert (','); |
|
2423 |
|
2424 yyunput (',', yytext); |
1072
|
2425 } |
|
2426 } |
|
2427 |
767
|
2428 // Figure out exactly what kind of token to return when we have seen |
4238
|
2429 // an identifier. Handles keywords. Return -1 if the identifier |
|
2430 // should be ignored. |
767
|
2431 |
146
|
2432 static int |
3974
|
2433 handle_identifier (void) |
146
|
2434 { |
3974
|
2435 std::string tok = strip_trailing_whitespace (yytext); |
|
2436 |
|
2437 int c = yytext[yyleng-1]; |
|
2438 |
|
2439 int cont_is_spc = eat_continuation (); |
|
2440 |
|
2441 int spc_gobbled = (cont_is_spc || c == ' ' || c == '\t'); |
|
2442 |
1826
|
2443 // It is almost always an error for an identifier to be followed |
|
2444 // directly by another identifier. Special cases are handled |
|
2445 // below. |
883
|
2446 |
2857
|
2447 lexer_flags.cant_be_identifier = true; |
883
|
2448 |
2970
|
2449 // If we are expecting a structure element, avoid recognizing |
|
2450 // keywords and other special names and return STRUCT_ELT, which is |
|
2451 // a string that is also a valid identifier. But first, we have to |
|
2452 // decide whether to insert a comma. |
747
|
2453 |
1826
|
2454 if (lexer_flags.looking_at_indirect_ref) |
1072
|
2455 { |
2970
|
2456 do_comma_insert_check (); |
|
2457 |
1072
|
2458 maybe_unput_comma (spc_gobbled); |
2819
|
2459 |
|
2460 yylval.tok_val = new token (tok, input_line_number, |
|
2461 current_input_column); |
|
2462 |
|
2463 token_stack.push (yylval.tok_val); |
|
2464 |
2857
|
2465 lexer_flags.cant_be_identifier = false; |
|
2466 lexer_flags.quote_is_transpose = true; |
|
2467 lexer_flags.convert_spaces_to_comma = true; |
2819
|
2468 |
|
2469 current_input_column += yyleng; |
|
2470 |
2970
|
2471 return STRUCT_ELT; |
1072
|
2472 } |
747
|
2473 |
1826
|
2474 // If we have a regular keyword, or a plot STYLE, return it. |
|
2475 // Keywords can be followed by identifiers (TOK_RETURN handles |
|
2476 // that). |
146
|
2477 |
|
2478 int kw_token = is_keyword (tok); |
2970
|
2479 |
146
|
2480 if (kw_token) |
|
2481 { |
4238
|
2482 if (kw_token < 0) |
|
2483 return kw_token; |
|
2484 else if (kw_token == STYLE) |
146
|
2485 { |
1060
|
2486 current_input_column += yyleng; |
2857
|
2487 lexer_flags.quote_is_transpose = false; |
|
2488 lexer_flags.convert_spaces_to_comma = true; |
146
|
2489 return kw_token; |
|
2490 } |
|
2491 else |
|
2492 TOK_RETURN (kw_token); |
|
2493 } |
|
2494 |
1826
|
2495 // See if we have a plot keyword (title, using, with, or clear). |
146
|
2496 |
1826
|
2497 if (lexer_flags.plotting) |
941
|
2498 { |
1826
|
2499 // Yes, we really do need both of these plot_range variables. |
|
2500 // One is used to mark when we are past all possiblity of a plot |
|
2501 // range, the other is used to mark when we are actually between |
|
2502 // the square brackets that surround the range. |
146
|
2503 |
1826
|
2504 if (! lexer_flags.in_plot_range) |
2857
|
2505 lexer_flags.past_plot_range = true; |
941
|
2506 |
3351
|
2507 // Option keywords can't appear in brackets, braces, or parentheses. |
1273
|
2508 |
|
2509 int plot_option_kw = 0; |
2970
|
2510 |
1826
|
2511 if (nesting_level.none ()) |
1273
|
2512 plot_option_kw = is_plot_keyword (tok); |
3575
|
2513 |
1826
|
2514 if (lexer_flags.cant_be_identifier && plot_option_kw) |
941
|
2515 TOK_RETURN (plot_option_kw); |
|
2516 } |
146
|
2517 |
3480
|
2518 int c1 = yyinput (); |
|
2519 |
|
2520 bool next_tok_is_paren = (c1 == '('); |
|
2521 |
|
2522 bool next_tok_is_eq = false; |
|
2523 if (c1 == '=') |
|
2524 { |
|
2525 int c2 = yyinput (); |
4410
|
2526 yyunput (c2, yytext); |
3480
|
2527 |
|
2528 if (c2 != '=') |
|
2529 next_tok_is_eq = true; |
|
2530 } |
|
2531 |
4410
|
2532 yyunput (c1, yytext); |
1001
|
2533 |
1826
|
2534 // Make sure we put the return values of a function in the symbol |
|
2535 // table that is local to the function. |
146
|
2536 |
2877
|
2537 // If we are defining a function and we have not seen the function |
|
2538 // name yet and the next token is `=', then this identifier must be |
|
2539 // the only return value for the function and it belongs in the |
|
2540 // local symbol table. |
|
2541 |
1826
|
2542 if (next_tok_is_eq |
2877
|
2543 && lexer_flags.defining_func |
|
2544 && ! lexer_flags.parsed_function_name) |
146
|
2545 curr_sym_tab = tmp_local_sym_tab; |
|
2546 |
2702
|
2547 // Kluge alert. |
|
2548 // |
|
2549 // If we are looking at a text style function, set up to gobble its |
2745
|
2550 // arguments. |
|
2551 // |
|
2552 // If the following token is `=', or if we are parsing a function |
3189
|
2553 // return list or function parameter list, or if we are looking at |
|
2554 // something like [ab,cd] = foo (), force the symbol to be inserted |
|
2555 // as a variable in the current symbol table. |
2702
|
2556 |
4208
|
2557 if (is_command_name (tok) && ! is_variable (tok)) |
2702
|
2558 { |
2745
|
2559 if (next_tok_is_eq |
|
2560 || lexer_flags.looking_at_return_list |
3189
|
2561 || lexer_flags.looking_at_parameter_list |
4101
|
2562 || lexer_flags.looking_at_matrix_or_assign_lhs) |
2745
|
2563 { |
|
2564 force_local_variable (tok); |
|
2565 } |
2702
|
2566 else if (! next_tok_is_paren) |
|
2567 { |
|
2568 if (tok == "gset") |
2857
|
2569 lexer_flags.doing_set = true; |
2702
|
2570 |
4323
|
2571 BEGIN (COMMAND_START); |
2702
|
2572 } |
|
2573 } |
|
2574 |
4234
|
2575 // Find the token in the symbol table. Beware the magic |
|
2576 // transformation of the end keyword... |
|
2577 |
|
2578 if (tok == "end") |
|
2579 tok = "__end__"; |
146
|
2580 |
|
2581 yylval.tok_val = new token (lookup_identifier (tok), |
|
2582 input_line_number, |
|
2583 current_input_column); |
|
2584 |
|
2585 token_stack.push (yylval.tok_val); |
|
2586 |
1826
|
2587 // After seeing an identifer, it is ok to convert spaces to a comma |
|
2588 // (if needed). |
146
|
2589 |
2857
|
2590 lexer_flags.convert_spaces_to_comma = true; |
146
|
2591 |
2877
|
2592 if (! next_tok_is_eq) |
|
2593 { |
|
2594 lexer_flags.quote_is_transpose = true; |
146
|
2595 |
2877
|
2596 do_comma_insert_check (); |
|
2597 |
|
2598 maybe_unput_comma (spc_gobbled); |
146
|
2599 } |
|
2600 |
2877
|
2601 current_input_column += yyleng; |
146
|
2602 |
|
2603 return NAME; |
|
2604 } |
|
2605 |
1826
|
2606 void |
|
2607 lexical_feedback::init (void) |
|
2608 { |
|
2609 // Not initially defining a matrix list. |
3351
|
2610 bracketflag = 0; |
1826
|
2611 |
4613
|
2612 // Not initially defining a cell array list. |
|
2613 braceflag = 0; |
|
2614 |
1826
|
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; |
4240
|
2622 parsing_nested_function = 0; |
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 |
4264
|
2659 DEFCMD (iskeyword, args, , |
|
2660 "-*- texinfo -*-\n\ |
|
2661 @deftypefn {Built-in Function} {} iskeyword (@var{name})\n\ |
|
2662 Return true if @var{name} is an Octave keyword. If @var{name}\n\ |
|
2663 is omitted, return a list of keywords.\n\ |
|
2664 @end deftypefn") |
|
2665 { |
|
2666 octave_value retval; |
|
2667 |
|
2668 int argc = args.length () + 1; |
|
2669 |
|
2670 string_vector argv = args.make_argv ("help"); |
|
2671 |
|
2672 if (error_state) |
|
2673 return retval; |
|
2674 |
|
2675 if (argc == 1) |
|
2676 { |
|
2677 string_vector lst (TOTAL_KEYWORDS); |
|
2678 |
|
2679 for (int i = 0; i < TOTAL_KEYWORDS; i++) |
|
2680 lst[i] = wordlist[i].name; |
|
2681 |
|
2682 retval = Cell (lst.qsort ()); |
|
2683 } |
|
2684 else if (argc == 2) |
|
2685 { |
|
2686 std::string s = argv[1]; |
|
2687 |
|
2688 retval = (octave_kw_lookup (s.c_str (), s.length ()) != 0); |
|
2689 } |
|
2690 else |
|
2691 print_usage ("iskeyword"); |
|
2692 |
|
2693 return retval; |
|
2694 } |
|
2695 |
|
2696 |
3388
|
2697 static void |
|
2698 maybe_warn_separator_insert (char sep) |
|
2699 { |
3523
|
2700 std::string nm = curr_fcn_file_full_name; |
3388
|
2701 |
|
2702 if (Vwarn_separator_insert) |
|
2703 { |
|
2704 if (nm.empty ()) |
|
2705 warning ("potential auto-insertion of `%c' near line %d", |
|
2706 sep, input_line_number); |
|
2707 else |
|
2708 warning ("potential auto-insertion of `%c' near line %d of file %s", |
|
2709 sep, input_line_number, nm.c_str ()); |
|
2710 } |
|
2711 } |
|
2712 |
3400
|
2713 static void |
|
2714 gripe_single_quote_string (void) |
|
2715 { |
3523
|
2716 std::string nm = curr_fcn_file_full_name; |
3400
|
2717 |
|
2718 if (Vwarn_single_quote_string) |
|
2719 { |
|
2720 if (nm.empty ()) |
|
2721 warning ("single quote delimited string near line %d", |
|
2722 input_line_number); |
|
2723 else |
|
2724 warning ("single quote delimited string near line %d of file %s", |
|
2725 input_line_number, nm.c_str ()); |
|
2726 } |
|
2727 } |
|
2728 |
4037
|
2729 static void |
|
2730 gripe_matlab_incompatible (const std::string& msg) |
|
2731 { |
|
2732 if (Vwarn_matlab_incompatible) |
|
2733 warning ("potential Matlab compatibility problem: %s", msg.c_str ()); |
|
2734 } |
|
2735 |
|
2736 static void |
|
2737 maybe_gripe_matlab_incompatible_comment (char c) |
|
2738 { |
|
2739 if (c == '#') |
|
2740 gripe_matlab_incompatible ("# used as comment character"); |
|
2741 } |
|
2742 |
|
2743 static void |
|
2744 gripe_matlab_incompatible_continuation (void) |
|
2745 { |
|
2746 gripe_matlab_incompatible ("\\ used as line continuation marker"); |
|
2747 } |
|
2748 |
|
2749 static void |
|
2750 gripe_matlab_incompatible_operator (const std::string& op) |
|
2751 { |
|
2752 std::string t = op; |
|
2753 int n = t.length (); |
|
2754 if (t[n-1] == '\n') |
|
2755 t.resize (n-1); |
|
2756 gripe_matlab_incompatible (t + " used as operator"); |
|
2757 } |
|
2758 |
|
2759 static int |
|
2760 warn_matlab_incompatible (void) |
|
2761 { |
|
2762 Vwarn_matlab_incompatible = check_preference ("warn_matlab_incompatible"); |
|
2763 |
|
2764 return 0; |
|
2765 } |
|
2766 |
3388
|
2767 static int |
|
2768 warn_separator_insert (void) |
|
2769 { |
|
2770 Vwarn_separator_insert = check_preference ("warn_separator_insert"); |
|
2771 |
|
2772 return 0; |
|
2773 } |
|
2774 |
3246
|
2775 static int |
3400
|
2776 warn_single_quote_string (void) |
|
2777 { |
|
2778 Vwarn_single_quote_string = check_preference ("warn_single_quote_string"); |
|
2779 |
|
2780 return 0; |
|
2781 } |
|
2782 |
2167
|
2783 void |
|
2784 symbols_of_lex (void) |
|
2785 { |
4233
|
2786 DEFVAR (warn_matlab_incompatible, false, warn_matlab_incompatible, |
4037
|
2787 "-*- texinfo -*-\n\ |
|
2788 @defvr {Built-in Variable} warn_matlab_incompatible\n\ |
|
2789 Print warnings for Octave language features that may cause\n\ |
|
2790 compatibility problems with Matlab.\n\ |
|
2791 @end defvr"); |
|
2792 |
4233
|
2793 DEFVAR (warn_separator_insert, false, warn_separator_insert, |
3428
|
2794 "-*- texinfo -*-\n\ |
|
2795 @defvr {Built-in Variable} warn_separator_insert\n\ |
3567
|
2796 Print warning if commas or semicolons might be inserted\n\ |
3428
|
2797 automatically in literal matrices.\n\ |
|
2798 @end defvr"); |
3388
|
2799 |
4233
|
2800 DEFVAR (warn_single_quote_string, false, warn_single_quote_string, |
3428
|
2801 "-*- texinfo -*-\n\ |
|
2802 @defvr {Built-in Variable} warn_single_quote_string\n\ |
|
2803 Print warning if a signle quote character is used to introduce a\n\ |
|
2804 string constant.\n\ |
|
2805 @end defvr"); |
2167
|
2806 } |
|
2807 |
1994
|
2808 /* |
|
2809 ;;; Local Variables: *** |
|
2810 ;;; mode: C++ *** |
|
2811 ;;; End: *** |
|
2812 */ |