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