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