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