Mercurial > octave-nkf
annotate src/lex.l @ 7720:4e2eafef689c
unify comment and help text processing in lex.l and parse.y
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 17 Apr 2008 16:44:49 -0400 |
parents | 5b4d278ec828 |
children | c3bb0b7a4261 |
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); | |
7720
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
247 static int process_comment (bool& eof); |
2857 | 248 static bool match_any (char c, const char *s); |
3263 | 249 static bool next_token_is_sep_op (void); |
3246 | 250 static bool next_token_is_bin_op (bool spc_prev); |
251 static bool next_token_is_postfix_unary_op (bool spc_prev); | |
3523 | 252 static std::string strip_trailing_whitespace (char *s); |
3246 | 253 static void handle_number (void); |
975 | 254 static int handle_string (char delim, int text_style = 0); |
4612 | 255 static int handle_close_bracket (bool spc_gobbled, int bracket_type); |
3974 | 256 static int handle_identifier (void); |
3096 | 257 static bool have_continuation (bool trailing_comments_ok = true); |
258 static bool have_ellipsis_continuation (bool trailing_comments_ok = true); | |
3665 | 259 static void scan_for_comments (const char *); |
1826 | 260 static yum_yum eat_whitespace (void); |
261 static yum_yum eat_continuation (void); | |
3388 | 262 static void maybe_warn_separator_insert (char sep); |
3400 | 263 static void gripe_single_quote_string (void); |
4037 | 264 static void gripe_matlab_incompatible (const std::string& msg); |
265 static void maybe_gripe_matlab_incompatible_comment (char c); | |
266 static void gripe_matlab_incompatible_continuation (void); | |
267 static void gripe_matlab_incompatible_operator (const std::string& op); | |
1 | 268 |
269 %} | |
270 | |
271 D [0-9] | |
272 S [ \t] | |
5570 | 273 NL ((\n)|(\r)|(\r\n)) |
2042 | 274 SNL ({S}|{NL}) |
1 | 275 EL (\.\.\.) |
967 | 276 BS (\\) |
277 CONT ({EL}|{BS}) | |
1 | 278 Im [iIjJ] |
967 | 279 CCHAR [#%] |
280 COMMENT ({CCHAR}.*{NL}) | |
281 SNLCMT ({SNL}|{COMMENT}) | |
282 NOT ((\~)|(\!)) | |
4037 | 283 POW ((\*\*)|(\^)) |
284 EPOW (\.{POW}) | |
5290 | 285 IDENT ([_$a-zA-Z][_$a-zA-Z0-9]*) |
1 | 286 EXPON ([DdEe][+-]?{D}+) |
3220 | 287 NUMBER (({D}+\.?{D}*{EXPON}?)|(\.{D}+{EXPON}?)|(0[xX][0-9a-fA-F]+)) |
1 | 288 %% |
289 | |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
290 <SCRIPT_FILE_BEGIN>. { |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
291 BEGIN (INITIAL); |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
292 yyunput (yytext[0], yytext); |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
293 COUNT_TOK_AND_RETURN (SCRIPT); |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
294 } |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
295 |
4240 | 296 <NESTED_FUNCTION_END>. { |
4323 | 297 BEGIN (NESTED_FUNCTION_BEGIN); |
4410 | 298 yyunput (yytext[0], yytext); |
4910 | 299 COUNT_TOK_AND_RETURN (';'); |
4240 | 300 } |
301 | |
302 <NESTED_FUNCTION_BEGIN>. { | |
4323 | 303 BEGIN (INITIAL); |
4410 | 304 yyunput (yytext[0], yytext); |
4238 | 305 prep_for_nested_function (); |
4910 | 306 COUNT_TOK_AND_RETURN (FCN); |
4238 | 307 } |
308 | |
968 | 309 %{ |
4208 | 310 // Help and other command-style functions are a pain in the ass. This |
968 | 311 // stuff needs to be simplified. May require some changes in the |
312 // parser too. | |
313 %} | |
314 | |
4208 | 315 <COMMAND_START>{NL} { |
4323 | 316 BEGIN (INITIAL); |
967 | 317 current_input_column = 1; |
2857 | 318 lexer_flags.quote_is_transpose = false; |
319 lexer_flags.convert_spaces_to_comma = true; | |
5212 | 320 lexer_flags.doing_rawcommand = false; |
4910 | 321 COUNT_TOK_AND_RETURN ('\n'); |
967 | 322 } |
1 | 323 |
4208 | 324 <COMMAND_START>[\;\,] { |
5102 | 325 if (lexer_flags.doing_rawcommand) |
5279 | 326 TOK_PUSH_AND_RETURN (yytext, SQ_STRING); |
5102 | 327 |
328 BEGIN (INITIAL); | |
329 | |
330 if (strcmp (yytext, ",") == 0) | |
331 TOK_RETURN (','); | |
967 | 332 else |
5102 | 333 TOK_RETURN (';'); |
967 | 334 } |
1 | 335 |
4208 | 336 <COMMAND_START>[\"\'] { |
975 | 337 current_input_column++; |
4910 | 338 COUNT_TOK_AND_RETURN (handle_string (yytext[0], true)); |
975 | 339 } |
340 | |
4923 | 341 <COMMAND_START>[^#% \t\r\n\;\,\"\'][^ \t\r\n\;\,]*{S}* { |
3523 | 342 std::string tok = strip_trailing_whitespace (yytext); |
5279 | 343 TOK_PUSH_AND_RETURN (tok, SQ_STRING); |
967 | 344 } |
1 | 345 |
968 | 346 %{ |
1 | 347 // For this and the next two rules, we're looking at ']', and we |
971 | 348 // need to know if the next token is `=' or `=='. |
1 | 349 // |
350 // It would have been so much easier if the delimiters were simply | |
351 // different for the expression on the left hand side of the equals | |
352 // operator. | |
971 | 353 // |
354 // It's also a pain in the ass to decide whether to insert a comma | |
355 // after seeing a ']' character... | |
968 | 356 %} |
357 | |
4208 | 358 <MATRIX_START>{SNLCMT}*\]{S}* { |
3665 | 359 scan_for_comments (yytext); |
1001 | 360 fixup_column_count (yytext); |
361 int c = yytext[yyleng-1]; | |
362 int cont_is_spc = eat_continuation (); | |
4608 | 363 bool spc_gobbled = (cont_is_spc || c == ' ' || c == '\t'); |
5345 | 364 int tok_to_return = handle_close_bracket (spc_gobbled, ']'); |
365 if (spc_gobbled) | |
366 yyunput (' ', yytext); | |
367 COUNT_TOK_AND_RETURN (tok_to_return); | |
4608 | 368 } |
369 | |
370 <MATRIX_START>{SNLCMT}*\}{S}* { | |
371 scan_for_comments (yytext); | |
372 fixup_column_count (yytext); | |
373 int c = yytext[yyleng-1]; | |
374 int cont_is_spc = eat_continuation (); | |
375 bool spc_gobbled = (cont_is_spc || c == ' ' || c == '\t'); | |
5345 | 376 int tok_to_return = handle_close_bracket (spc_gobbled, '}'); |
377 if (spc_gobbled) | |
378 yyunput (' ', yytext); | |
379 COUNT_TOK_AND_RETURN (tok_to_return); | |
967 | 380 } |
1 | 381 |
968 | 382 %{ |
1088 | 383 // Commas are element separators in matrix constants. If we don't |
384 // check for continuations here we can end up inserting too many | |
385 // commas. | |
968 | 386 %} |
387 | |
4208 | 388 <MATRIX_START>{S}*\,{S}* { |
1088 | 389 current_input_column += yyleng; |
3388 | 390 |
1088 | 391 int tmp = eat_continuation (); |
3388 | 392 |
2857 | 393 lexer_flags.quote_is_transpose = false; |
394 lexer_flags.convert_spaces_to_comma = true; | |
3388 | 395 |
396 if ((tmp & ATE_NEWLINE) == ATE_NEWLINE) | |
397 { | |
398 maybe_warn_separator_insert (';'); | |
399 | |
4476 | 400 yyunput (';', yytext); |
3388 | 401 } |
402 | |
4910 | 403 COUNT_TOK_AND_RETURN (','); |
967 | 404 } |
1 | 405 |
968 | 406 %{ |
407 // In some cases, spaces in matrix constants can turn into commas. | |
408 // If commas are required, spaces are not important in matrix | |
1088 | 409 // constants so we just eat them. If we don't check for continuations |
410 // here we can end up inserting too many commas. | |
968 | 411 %} |
430 | 412 |
4208 | 413 <MATRIX_START>{S}+ { |
1088 | 414 current_input_column += yyleng; |
3388 | 415 |
416 int tmp = eat_continuation (); | |
417 int bin_op = next_token_is_bin_op (true); | |
418 int postfix_un_op = next_token_is_postfix_unary_op (true); | |
419 | |
420 if (! (postfix_un_op || bin_op) | |
4608 | 421 && nesting_level.is_bracket_or_brace () |
3388 | 422 && lexer_flags.convert_spaces_to_comma) |
967 | 423 { |
3388 | 424 if ((tmp & ATE_NEWLINE) == ATE_NEWLINE) |
425 { | |
426 maybe_warn_separator_insert (';'); | |
967 | 427 |
4476 | 428 yyunput (';', yytext); |
3388 | 429 } |
430 | |
4476 | 431 lexer_flags.quote_is_transpose = false; |
432 lexer_flags.convert_spaces_to_comma = true; | |
433 | |
434 maybe_warn_separator_insert (','); | |
435 | |
4910 | 436 COUNT_TOK_AND_RETURN (','); |
967 | 437 } |
438 } | |
430 | 439 |
968 | 440 %{ |
1088 | 441 // Semicolons are handled as row seprators in matrix constants. If we |
442 // don't eat whitespace here we can end up inserting too many | |
443 // semicolons. | |
968 | 444 %} |
445 | |
4208 | 446 <MATRIX_START>{SNLCMT}*;{SNLCMT}* { |
3665 | 447 scan_for_comments (yytext); |
967 | 448 fixup_column_count (yytext); |
1001 | 449 eat_whitespace (); |
2857 | 450 lexer_flags.quote_is_transpose = false; |
451 lexer_flags.convert_spaces_to_comma = true; | |
4910 | 452 COUNT_TOK_AND_RETURN (';'); |
967 | 453 } |
454 | |
968 | 455 %{ |
1088 | 456 // In some cases, new lines can also become row separators. If we |
457 // don't eat whitespace here we can end up inserting too many | |
458 // semicolons. | |
985 | 459 %} |
460 | |
4208 | 461 <MATRIX_START>{S}*{COMMENT}{SNLCMT}* | |
462 <MATRIX_START>{S}*{NL}{SNLCMT}* { | |
3665 | 463 scan_for_comments (yytext); |
1082 | 464 fixup_column_count (yytext); |
1088 | 465 eat_whitespace (); |
3388 | 466 |
4476 | 467 lexer_flags.quote_is_transpose = false; |
468 lexer_flags.convert_spaces_to_comma = true; | |
469 | |
470 if (nesting_level.none ()) | |
471 return LEXICAL_ERROR; | |
985 | 472 |
4608 | 473 if (nesting_level.is_bracket_or_brace ()) |
3388 | 474 { |
475 maybe_warn_separator_insert (';'); | |
476 | |
4910 | 477 COUNT_TOK_AND_RETURN (';'); |
985 | 478 } |
479 } | |
480 | |
967 | 481 \[{S}* { |
3351 | 482 nesting_level.bracket (); |
975 | 483 |
1082 | 484 current_input_column += yyleng; |
2857 | 485 lexer_flags.quote_is_transpose = false; |
486 lexer_flags.convert_spaces_to_comma = true; | |
975 | 487 |
5615 | 488 if (lexer_flags.defining_func && ! lexer_flags.parsed_function_name) |
489 lexer_flags.looking_at_return_list = true; | |
490 else | |
491 lexer_flags.looking_at_matrix_or_assign_lhs = true; | |
492 | |
975 | 493 promptflag--; |
494 eat_whitespace (); | |
495 | |
5102 | 496 lexer_flags.bracketflag++; |
497 BEGIN (MATRIX_START); | |
498 COUNT_TOK_AND_RETURN ('['); | |
967 | 499 } |
1 | 500 |
968 | 501 \] { |
1826 | 502 nesting_level.remove (); |
968 | 503 |
5102 | 504 TOK_RETURN (']'); |
968 | 505 } |
506 | |
507 %{ | |
508 // Imaginary numbers. | |
509 %} | |
510 | |
511 {NUMBER}{Im} { | |
3246 | 512 handle_number (); |
4910 | 513 COUNT_TOK_AND_RETURN (IMAG_NUM); |
968 | 514 } |
515 | |
516 %{ | |
517 // Real numbers. Don't grab the `.' part of a dot operator as part of | |
518 // the constant. | |
519 %} | |
520 | |
521 {D}+/\.[\*/\\^'] | | |
522 {NUMBER} { | |
3246 | 523 handle_number (); |
4910 | 524 COUNT_TOK_AND_RETURN (NUM); |
968 | 525 } |
526 | |
527 %{ | |
528 // Eat whitespace. Whitespace inside matrix constants is handled by | |
4208 | 529 // the <MATRIX_START> start state code above. |
968 | 530 %} |
531 | |
967 | 532 {S}* { |
533 current_input_column += yyleng; | |
534 } | |
535 | |
968 | 536 %{ |
537 // Continuation lines. Allow comments after continuations. | |
538 %} | |
539 | |
967 | 540 {CONT}{S}*{NL} | |
541 {CONT}{S}*{COMMENT} { | |
4037 | 542 if (yytext[0] == '\\') |
543 gripe_matlab_incompatible_continuation (); | |
3665 | 544 scan_for_comments (yytext); |
967 | 545 promptflag--; |
546 current_input_column = 1; | |
547 } | |
1 | 548 |
968 | 549 %{ |
550 // End of file. | |
551 %} | |
552 | |
967 | 553 <<EOF>> { |
554 TOK_RETURN (END_OF_INPUT); | |
555 } | |
1 | 556 |
968 | 557 %{ |
970 | 558 // Identifiers. Truncate the token at the first space or tab but |
559 // don't write directly on yytext. | |
968 | 560 %} |
561 | |
967 | 562 {IDENT}{S}* { |
4238 | 563 int id_tok = handle_identifier (); |
564 | |
565 if (id_tok >= 0) | |
4910 | 566 COUNT_TOK_AND_RETURN (id_tok); |
967 | 567 } |
1 | 568 |
968 | 569 %{ |
4342 | 570 // Function handles. |
571 %} | |
572 | |
4930 | 573 "@" { |
574 current_input_column++; | |
575 lexer_flags.quote_is_transpose = false; | |
576 lexer_flags.convert_spaces_to_comma = false; | |
577 lexer_flags.looking_at_function_handle++; | |
578 COUNT_TOK_AND_RETURN ('@'); | |
4342 | 579 } |
580 | |
581 %{ | |
968 | 582 // A new line character. New line characters inside matrix constants |
4208 | 583 // are handled by the <MATRIX_START> start state code above. If closest |
985 | 584 // nesting is inside parentheses, don't return a row separator. |
968 | 585 %} |
586 | |
967 | 587 {NL} { |
588 current_input_column = 1; | |
2857 | 589 lexer_flags.quote_is_transpose = false; |
590 lexer_flags.convert_spaces_to_comma = true; | |
1826 | 591 if (nesting_level.none ()) |
4910 | 592 COUNT_TOK_AND_RETURN ('\n'); |
4037 | 593 else if (nesting_level.is_paren ()) |
594 gripe_matlab_incompatible ("bare newline inside parentheses"); | |
4608 | 595 else if (nesting_level.is_bracket_or_brace ()) |
985 | 596 return LEXICAL_ERROR; |
967 | 597 } |
1 | 598 |
968 | 599 %{ |
600 // Single quote can either be the beginning of a string or a transpose | |
601 // operator. | |
602 %} | |
603 | |
967 | 604 "'" { |
605 current_input_column++; | |
2857 | 606 lexer_flags.convert_spaces_to_comma = true; |
1 | 607 |
1826 | 608 if (lexer_flags.quote_is_transpose) |
967 | 609 { |
610 do_comma_insert_check (); | |
4910 | 611 COUNT_TOK_AND_RETURN (QUOTE); |
967 | 612 } |
613 else | |
4910 | 614 COUNT_TOK_AND_RETURN (handle_string ('\'')); |
967 | 615 } |
1 | 616 |
968 | 617 %{ |
971 | 618 // Double quotes always begin strings. |
619 %} | |
620 | |
973 | 621 \" { |
622 current_input_column++; | |
4910 | 623 COUNT_TOK_AND_RETURN (handle_string ('"')); |
973 | 624 } |
971 | 625 |
626 %{ | |
985 | 627 // Gobble comments. If closest nesting is inside parentheses, don't |
628 // return a new line. | |
629 %} | |
968 | 630 |
967 | 631 {CCHAR} { |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
632 bool eof = false; |
7720
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
633 yyunput (yytext[0], yytext); |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
634 int tok = process_comment (eof); |
7715
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 | |
7720
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1137 std::string |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1138 grab_comment_block (stream_reader& reader, bool& eof) |
1 | 1139 { |
4426 | 1140 std::string buf; |
1019 | 1141 |
7720
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1142 // TRUE means we are at the beginning of a comment block. |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1143 bool begin_comment = false; |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1144 |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1145 // TRUE means we are currently reading a comment block. |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1146 bool in_comment = false; |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1147 |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1148 bool warned_incompatible = false; |
3665 | 1149 |
1019 | 1150 int c = 0; |
1 | 1151 |
7720
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1152 while ((c = reader.getc ()) != EOF) |
1019 | 1153 { |
7720
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1154 current_input_column++; |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1155 |
2300 | 1156 if (begin_comment) |
1157 { | |
1158 if (c == '%' || c == '#') | |
1159 continue; | |
1160 else | |
1161 begin_comment = false; | |
1162 } | |
1163 | |
1019 | 1164 if (in_comment) |
1 | 1165 { |
4426 | 1166 buf += static_cast<char> (c); |
1755 | 1167 |
1019 | 1168 if (c == '\n') |
3427 | 1169 { |
7720
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1170 input_line_number++; |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1171 current_input_column = 0; |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1172 |
3427 | 1173 in_comment = false; |
1174 } | |
1019 | 1175 } |
1176 else | |
1177 { | |
1178 switch (c) | |
991 | 1179 { |
7720
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1180 case ' ': |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1181 case '\t': |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1182 break; |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1183 |
4037 | 1184 case '#': |
7720
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1185 if (! warned_incompatible) |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1186 { |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1187 warned_incompatible = true; |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1188 maybe_gripe_matlab_incompatible_comment (c); |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1189 } |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1190 // fall through... |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1191 |
1019 | 1192 case '%': |
2300 | 1193 in_comment = true; |
1194 begin_comment = true; | |
1019 | 1195 break; |
777 | 1196 |
1019 | 1197 default: |
7720
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1198 current_input_column--; |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1199 reader.ungetc (c); |
1019 | 1200 goto done; |
1 | 1201 } |
1202 } | |
1019 | 1203 } |
991 | 1204 |
1019 | 1205 done: |
991 | 1206 |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1207 if (c == EOF) |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1208 eof = true; |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1209 |
4426 | 1210 return buf; |
1 | 1211 } |
1212 | |
7720
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1213 class |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1214 flex_stream_reader : public stream_reader |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1215 { |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1216 public: |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1217 flex_stream_reader (char *buf_arg) : stream_reader (), buf (buf_arg) { } |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1218 |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1219 int getc (void) { return ::yyinput (); } |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1220 int ungetc (int c) { ::yyunput (c, buf); return 0; } |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1221 |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1222 private: |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1223 char *buf; |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1224 }; |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1225 |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1226 static int |
7720
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1227 process_comment (bool& eof) |
7715
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 eof = false; |
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 std::string help_txt; |
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 if (! help_buf.empty ()) |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1234 help_txt = help_buf.top (); |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1235 |
7720
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1236 flex_stream_reader flex_reader (yytext); |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1237 |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1238 std::string txt = grab_comment_block (flex_reader, eof); |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1239 |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1240 if (help_txt.empty () && nesting_level.none ()) |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1241 { |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1242 if (! help_buf.empty ()) |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1243 help_buf.pop (); |
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 help_buf.push (txt); |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1246 } |
7720
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1247 |
4e2eafef689c
unify comment and help text processing in lex.l and parse.y
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
1248 octave_comment_buffer::append (txt); |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1249 |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1250 current_input_column = 1; |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1251 lexer_flags.quote_is_transpose = false; |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1252 lexer_flags.convert_spaces_to_comma = true; |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1253 |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1254 if (YY_START == COMMAND_START) |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1255 BEGIN (INITIAL); |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1256 |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1257 if (nesting_level.none ()) |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1258 { |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1259 lexer_flags.doing_rawcommand = false; |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1260 return '\n'; |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1261 } |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1262 else if (nesting_level.is_bracket_or_brace ()) |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1263 return ';'; |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1264 else |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1265 return 0; |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1266 } |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1267 |
767 | 1268 // Return 1 if the given character matches any character in the given |
1269 // string. | |
1270 | |
2857 | 1271 static bool |
2804 | 1272 match_any (char c, const char *s) |
1 | 1273 { |
1274 char tmp; | |
1275 while ((tmp = *s++) != '\0') | |
1276 { | |
1277 if (c == tmp) | |
2857 | 1278 return true; |
1 | 1279 } |
2857 | 1280 return false; |
1 | 1281 } |
1282 | |
767 | 1283 // Given information about the spacing surrounding an operator, |
1284 // return 1 if it looks like it should be treated as a binary | |
1285 // operator. For example, | |
1286 // | |
3774 | 1287 // [ 1 + 2 ] or [ 1+ 2] or [ 1+2 ] ==> binary |
1288 // | |
1289 // [ 1 +2 ] ==> unary | |
767 | 1290 |
2857 | 1291 static bool |
3246 | 1292 looks_like_bin_op (bool spc_prev, int next_char) |
1 | 1293 { |
3246 | 1294 bool spc_next = (next_char == ' ' || next_char == '\t'); |
1295 | |
608 | 1296 return ((spc_prev && spc_next) || ! spc_prev); |
1 | 1297 } |
1298 | |
3263 | 1299 // Recognize separators. If the separator is a CRLF pair, it is |
1300 // replaced by a single LF. | |
1301 | |
1302 static bool | |
1303 next_token_is_sep_op (void) | |
1304 { | |
1305 bool retval = false; | |
1306 | |
1307 int c1 = yyinput (); | |
1308 | |
1309 if (c1 == '\r') | |
1310 { | |
1311 int c2 = yyinput (); | |
1312 | |
1313 if (c2 == '\n') | |
1314 { | |
1315 c1 = '\n'; | |
1316 | |
1317 retval = true; | |
1318 } | |
1319 else | |
4410 | 1320 yyunput (c2, yytext); |
3263 | 1321 } |
1322 else | |
1323 retval = match_any (c1, ",;\n]"); | |
1324 | |
4410 | 1325 yyunput (c1, yytext); |
3263 | 1326 |
1327 return retval; | |
1328 } | |
1329 | |
767 | 1330 // Try to determine if the next token should be treated as a postfix |
1331 // unary operator. This is ugly, but it seems to do the right thing. | |
1332 | |
2857 | 1333 static bool |
3246 | 1334 next_token_is_postfix_unary_op (bool spc_prev) |
1 | 1335 { |
2857 | 1336 bool un_op = false; |
1 | 1337 |
1338 int c0 = yyinput (); | |
1339 | |
3246 | 1340 if (c0 == '\'' && ! spc_prev) |
1341 { | |
1342 un_op = true; | |
1343 } | |
1344 else if (c0 == '.') | |
1345 { | |
1346 int c1 = yyinput (); | |
1347 un_op = (c1 == '\''); | |
4410 | 1348 yyunput (c1, yytext); |
3246 | 1349 } |
4613 | 1350 else if (c0 == '+') |
1351 { | |
1352 int c1 = yyinput (); | |
1353 un_op = (c1 == '+'); | |
1354 yyunput (c1, yytext); | |
1355 } | |
1356 else if (c0 == '-') | |
1357 { | |
1358 int c1 = yyinput (); | |
1359 un_op = (c1 == '-'); | |
1360 yyunput (c1, yytext); | |
1361 } | |
1 | 1362 |
4410 | 1363 yyunput (c0, yytext); |
1 | 1364 |
1365 return un_op; | |
1366 } | |
1367 | |
767 | 1368 // Try to determine if the next token should be treated as a binary |
3246 | 1369 // operator. |
1521 | 1370 // |
3246 | 1371 // This kluge exists because whitespace is not always ignored inside |
3774 | 1372 // the square brackets that are used to create matrix objects (though |
1373 // spacing only really matters in the cases that can be interpreted | |
1374 // either as binary ops or prefix unary ops: currently just +, -). | |
1375 // | |
3779 | 1376 // Note that a line continuation directly following a + or - operator |
1377 // (e.g., the characters '[' 'a' ' ' '+' '\' LFD 'b' ']') will be | |
1378 // parsed as a binary operator. | |
767 | 1379 |
2857 | 1380 static bool |
3246 | 1381 next_token_is_bin_op (bool spc_prev) |
1 | 1382 { |
2857 | 1383 bool bin_op = false; |
1 | 1384 |
1385 int c0 = yyinput (); | |
1386 | |
1387 switch (c0) | |
1388 { | |
777 | 1389 case '+': |
1390 case '-': | |
3774 | 1391 { |
1392 int c1 = yyinput (); | |
1393 | |
1394 switch (c1) | |
1395 { | |
1396 case '+': | |
1397 case '-': | |
1398 // Unary ops, spacing doesn't matter. | |
1399 break; | |
1400 | |
1401 case '=': | |
1402 // Binary ops, spacing doesn't matter. | |
1403 bin_op = true; | |
1404 break; | |
1405 | |
1406 default: | |
1407 // Could be either, spacing matters. | |
1408 bin_op = looks_like_bin_op (spc_prev, c1); | |
1409 break; | |
1410 } | |
1411 | |
4410 | 1412 yyunput (c1, yytext); |
3774 | 1413 } |
1414 break; | |
1415 | |
1416 case ':': | |
3246 | 1417 case '/': |
1418 case '\\': | |
1419 case '^': | |
3774 | 1420 // Always a binary op (may also include /=, \=, and ^=). |
1421 bin_op = true; | |
1276 | 1422 break; |
1423 | |
3246 | 1424 // .+ .- ./ .\ .^ .* .** |
1554 | 1425 case '.': |
1426 { | |
1427 int c1 = yyinput (); | |
3246 | 1428 |
3774 | 1429 if (match_any (c1, "+-/\\^*")) |
1430 // Always a binary op (may also include .+=, .-=, ./=, ...). | |
1431 bin_op = true; | |
3698 | 1432 else if (! isdigit (c1) && c1 != ' ' && c1 != '\t' && c1 != '.') |
3774 | 1433 // A structure element reference is a binary op. |
1434 bin_op = true; | |
3246 | 1435 |
4410 | 1436 yyunput (c1, yytext); |
1554 | 1437 } |
1438 break; | |
1439 | |
3246 | 1440 // = == & && | || * ** |
1441 case '=': | |
1 | 1442 case '&': |
3246 | 1443 case '|': |
1 | 1444 case '*': |
3774 | 1445 // Always a binary op (may also include ==, &&, ||, **). |
1446 bin_op = true; | |
3246 | 1447 break; |
1448 | |
3774 | 1449 // < <= <> > >= |
1 | 1450 case '<': |
1451 case '>': | |
3774 | 1452 // Always a binary op (may also include <=, <>, >=). |
1453 bin_op = true; | |
1454 break; | |
1455 | |
1456 // ~= != | |
777 | 1457 case '~': |
1458 case '!': | |
3246 | 1459 { |
1460 int c1 = yyinput (); | |
1461 | |
3774 | 1462 // ~ and ! can be unary ops, so require following =. |
1463 if (c1 == '=') | |
1464 bin_op = true; | |
3246 | 1465 |
4410 | 1466 yyunput (c1, yytext); |
3246 | 1467 } |
1 | 1468 break; |
1469 | |
1470 default: | |
1276 | 1471 break; |
1 | 1472 } |
1473 | |
4410 | 1474 yyunput (c0, yytext); |
1 | 1475 |
1476 return bin_op; | |
1477 } | |
1478 | |
767 | 1479 // Used to delete trailing white space from tokens. |
1480 | |
3536 | 1481 static std::string |
1 | 1482 strip_trailing_whitespace (char *s) |
1483 { | |
3523 | 1484 std::string retval = s; |
1 | 1485 |
1823 | 1486 size_t pos = retval.find_first_of (" \t"); |
1 | 1487 |
1823 | 1488 if (pos != NPOS) |
1489 retval.resize (pos); | |
1 | 1490 |
1491 return retval; | |
1492 } | |
1493 | |
3665 | 1494 static void |
1495 scan_for_comments (const char *text) | |
1496 { | |
1497 std::string comment_buf; | |
1498 | |
1499 bool in_comment = false; | |
1500 bool beginning_of_comment = false; | |
1501 | |
1502 int len = strlen (text); | |
1503 int i = 0; | |
1504 | |
1505 while (i < len) | |
1506 { | |
1507 char c = text[i++]; | |
1508 | |
1509 switch (c) | |
1510 { | |
1511 case '%': | |
1512 case '#': | |
1513 if (in_comment) | |
1514 { | |
1515 if (! beginning_of_comment) | |
3802 | 1516 comment_buf += static_cast<char> (c); |
3665 | 1517 } |
1518 else | |
1519 { | |
4037 | 1520 maybe_gripe_matlab_incompatible_comment (c); |
3665 | 1521 in_comment = true; |
1522 beginning_of_comment = true; | |
1523 } | |
1524 break; | |
1525 | |
1526 case '\n': | |
1527 if (in_comment) | |
1528 { | |
3802 | 1529 comment_buf += static_cast<char> (c); |
3665 | 1530 octave_comment_buffer::append (comment_buf); |
1531 comment_buf.resize (0); | |
1532 in_comment = false; | |
1533 beginning_of_comment = false; | |
1534 } | |
1535 break; | |
1536 | |
1537 case '\r': | |
1538 if (in_comment) | |
3802 | 1539 comment_buf += static_cast<char> (c); |
3665 | 1540 if (i < len) |
1541 { | |
1542 c = text[i++]; | |
1543 | |
1544 if (c == '\n') | |
1545 { | |
1546 if (in_comment) | |
1547 { | |
3802 | 1548 comment_buf += static_cast<char> (c); |
3665 | 1549 octave_comment_buffer::append (comment_buf); |
1550 in_comment = false; | |
1551 beginning_of_comment = false; | |
1552 } | |
1553 } | |
1554 } | |
1555 | |
1556 default: | |
1557 if (in_comment) | |
1558 { | |
3802 | 1559 comment_buf += static_cast<char> (c); |
3665 | 1560 beginning_of_comment = false; |
1561 } | |
1562 break; | |
1563 } | |
1564 } | |
1565 | |
1566 if (! comment_buf.empty ()) | |
1567 octave_comment_buffer::append (comment_buf); | |
1568 } | |
1569 | |
1001 | 1570 // Discard whitespace, including comments and continuations. |
1088 | 1571 // |
1572 // Return value is logical OR of the following values: | |
1573 // | |
1826 | 1574 // ATE_NOTHING : no spaces to eat |
1088 | 1575 // ATE_SPACE_OR_TAB : space or tab in input |
1576 // ATE_NEWLINE : bare new line in input | |
1001 | 1577 |
1826 | 1578 static yum_yum |
975 | 1579 eat_whitespace (void) |
1580 { | |
1826 | 1581 yum_yum retval = ATE_NOTHING; |
3665 | 1582 |
1583 std::string comment_buf; | |
1584 | |
2857 | 1585 bool in_comment = false; |
3665 | 1586 bool beginning_of_comment = false; |
1587 | |
1588 int c = 0; | |
1589 | |
975 | 1590 while ((c = yyinput ()) != EOF) |
1591 { | |
1592 current_input_column++; | |
1593 | |
1594 switch (c) | |
1595 { | |
1596 case ' ': | |
1597 case '\t': | |
3665 | 1598 if (in_comment) |
1599 { | |
3802 | 1600 comment_buf += static_cast<char> (c); |
3665 | 1601 beginning_of_comment = false; |
1602 } | |
1088 | 1603 retval |= ATE_SPACE_OR_TAB; |
975 | 1604 break; |
1605 | |
1606 case '\n': | |
1088 | 1607 retval |= ATE_NEWLINE; |
3665 | 1608 if (in_comment) |
1609 { | |
3802 | 1610 comment_buf += static_cast<char> (c); |
3665 | 1611 octave_comment_buffer::append (comment_buf); |
1612 comment_buf.resize (0); | |
1613 in_comment = false; | |
1614 beginning_of_comment = false; | |
1615 } | |
975 | 1616 current_input_column = 0; |
1617 break; | |
1618 | |
1619 case '#': | |
1620 case '%': | |
3665 | 1621 if (in_comment) |
1622 { | |
1623 if (! beginning_of_comment) | |
3802 | 1624 comment_buf += static_cast<char> (c); |
3665 | 1625 } |
1626 else | |
1627 { | |
4037 | 1628 maybe_gripe_matlab_incompatible_comment (c); |
3665 | 1629 in_comment = true; |
1630 beginning_of_comment = true; | |
1631 } | |
975 | 1632 break; |
1633 | |
1001 | 1634 case '.': |
1635 if (in_comment) | |
3665 | 1636 { |
3802 | 1637 comment_buf += static_cast<char> (c); |
3665 | 1638 beginning_of_comment = false; |
1639 break; | |
1640 } | |
1001 | 1641 else |
1642 { | |
1643 if (have_ellipsis_continuation ()) | |
1644 break; | |
1645 else | |
1646 goto done; | |
1647 } | |
1648 | |
1649 case '\\': | |
1650 if (in_comment) | |
3665 | 1651 { |
3802 | 1652 comment_buf += static_cast<char> (c); |
3665 | 1653 beginning_of_comment = false; |
1654 break; | |
1655 } | |
1001 | 1656 else |
1657 { | |
3105 | 1658 if (have_continuation ()) |
1001 | 1659 break; |
1660 else | |
1661 goto done; | |
1662 } | |
1663 | |
6187 | 1664 case '\r': |
1665 if (in_comment) | |
1666 comment_buf += static_cast<char> (c); | |
1667 c = yyinput (); | |
1668 if (c == EOF) | |
1669 break; | |
1670 else if (c == '\n') | |
1671 { | |
1672 retval |= ATE_NEWLINE; | |
1673 if (in_comment) | |
1674 { | |
1675 comment_buf += static_cast<char> (c); | |
1676 octave_comment_buffer::append (comment_buf); | |
1677 in_comment = false; | |
1678 beginning_of_comment = false; | |
1679 } | |
1680 current_input_column = 0; | |
1681 break; | |
1682 } | |
1683 | |
1684 // Fall through... | |
1685 | |
975 | 1686 default: |
1687 if (in_comment) | |
3665 | 1688 { |
3802 | 1689 comment_buf += static_cast<char> (c); |
3665 | 1690 beginning_of_comment = false; |
1691 break; | |
1692 } | |
975 | 1693 else |
1694 goto done; | |
1695 } | |
1696 } | |
1697 | |
3665 | 1698 if (! comment_buf.empty ()) |
1699 octave_comment_buffer::append (comment_buf); | |
1700 | |
975 | 1701 done: |
4410 | 1702 yyunput (c, yytext); |
1082 | 1703 current_input_column--; |
1001 | 1704 return retval; |
975 | 1705 } |
1706 | |
3220 | 1707 static inline bool |
1708 looks_like_hex (const char *s, int len) | |
1709 { | |
1710 return (len > 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X')); | |
1711 } | |
1712 | |
975 | 1713 static void |
3246 | 1714 handle_number (void) |
972 | 1715 { |
3220 | 1716 double value = 0.0; |
1717 int nread = 0; | |
1718 | |
3598 | 1719 if (looks_like_hex (yytext, strlen (yytext))) |
3220 | 1720 { |
1721 unsigned long ival; | |
3598 | 1722 |
1723 nread = sscanf (yytext, "%lx", &ival); | |
1724 | |
3220 | 1725 value = static_cast<double> (ival); |
1726 } | |
1727 else | |
3598 | 1728 { |
1729 char *tmp = strsave (yytext); | |
1730 | |
1731 char *idx = strpbrk (tmp, "Dd"); | |
2621 | 1732 |
3598 | 1733 if (idx) |
1734 *idx = 'e'; | |
1735 | |
1736 nread = sscanf (tmp, "%lf", &value); | |
1737 | |
1738 delete [] tmp; | |
1739 } | |
972 | 1740 |
1826 | 1741 // If yytext doesn't contain a valid number, we are in deep doo doo. |
985 | 1742 |
972 | 1743 assert (nread == 1); |
1744 | |
3988 | 1745 lexer_flags.quote_is_transpose = true; |
1746 lexer_flags.convert_spaces_to_comma = true; | |
972 | 1747 |
1748 yylval.tok_val = new token (value, yytext, input_line_number, | |
1749 current_input_column); | |
1750 | |
1751 token_stack.push (yylval.tok_val); | |
1752 | |
1753 current_input_column += yyleng; | |
1754 | |
1755 do_comma_insert_check (); | |
1756 } | |
1757 | |
1001 | 1758 // We have seen a backslash and need to find out if it should be |
1759 // treated as a continuation character. If so, this eats it, up to | |
1760 // and including the new line character. | |
1761 // | |
973 | 1762 // Match whitespace only, followed by a comment character or newline. |
1763 // Once a comment character is found, discard all input until newline. | |
1764 // If non-whitespace characters are found before comment | |
1765 // characters, return 0. Otherwise, return 1. | |
1766 | |
2857 | 1767 static bool |
3096 | 1768 have_continuation (bool trailing_comments_ok) |
973 | 1769 { |
5765 | 1770 std::ostringstream buf; |
973 | 1771 |
3665 | 1772 std::string comment_buf; |
1773 | |
2857 | 1774 bool in_comment = false; |
3665 | 1775 bool beginning_of_comment = false; |
1776 | |
1777 int c = 0; | |
1778 | |
973 | 1779 while ((c = yyinput ()) != EOF) |
1780 { | |
3802 | 1781 buf << static_cast<char> (c); |
973 | 1782 |
1783 switch (c) | |
1784 { | |
1785 case ' ': | |
1786 case '\t': | |
3665 | 1787 if (in_comment) |
1788 { | |
3802 | 1789 comment_buf += static_cast<char> (c); |
3665 | 1790 beginning_of_comment = false; |
1791 } | |
973 | 1792 break; |
1793 | |
1794 case '%': | |
1795 case '#': | |
1091 | 1796 if (trailing_comments_ok) |
3665 | 1797 { |
1798 if (in_comment) | |
1799 { | |
1800 if (! beginning_of_comment) | |
3802 | 1801 comment_buf += static_cast<char> (c); |
3665 | 1802 } |
1803 else | |
1804 { | |
4037 | 1805 maybe_gripe_matlab_incompatible_comment (c); |
3665 | 1806 in_comment = true; |
1807 beginning_of_comment = true; | |
1808 } | |
1809 } | |
1091 | 1810 else |
1811 goto cleanup; | |
973 | 1812 break; |
1813 | |
1814 case '\n': | |
3665 | 1815 if (in_comment) |
1816 { | |
3802 | 1817 comment_buf += static_cast<char> (c); |
3665 | 1818 octave_comment_buffer::append (comment_buf); |
1819 } | |
975 | 1820 current_input_column = 0; |
1001 | 1821 promptflag--; |
4037 | 1822 gripe_matlab_incompatible_continuation (); |
2857 | 1823 return true; |
973 | 1824 |
3263 | 1825 case '\r': |
3665 | 1826 if (in_comment) |
3802 | 1827 comment_buf += static_cast<char> (c); |
3263 | 1828 c = yyinput (); |
1829 if (c == EOF) | |
1830 break; | |
1831 else if (c == '\n') | |
1832 { | |
3665 | 1833 if (in_comment) |
1834 { | |
3802 | 1835 comment_buf += static_cast<char> (c); |
3665 | 1836 octave_comment_buffer::append (comment_buf); |
1837 } | |
3263 | 1838 current_input_column = 0; |
1839 promptflag--; | |
4037 | 1840 gripe_matlab_incompatible_continuation (); |
3263 | 1841 return true; |
3665 | 1842 } |
3263 | 1843 |
3802 | 1844 // Fall through... |
1845 | |
973 | 1846 default: |
3665 | 1847 if (in_comment) |
1848 { | |
3802 | 1849 comment_buf += static_cast<char> (c); |
3665 | 1850 beginning_of_comment = false; |
1851 } | |
1852 else | |
1091 | 1853 goto cleanup; |
1854 break; | |
973 | 1855 } |
1856 } | |
1857 | |
4410 | 1858 yyunput (c, yytext); |
2857 | 1859 return false; |
973 | 1860 |
3096 | 1861 cleanup: |
4051 | 1862 |
5765 | 1863 std::string s = buf.str (); |
4051 | 1864 |
1865 int len = s.length (); | |
1866 while (len--) | |
4410 | 1867 yyunput (s[len], yytext); |
3096 | 1868 |
2857 | 1869 return false; |
973 | 1870 } |
1871 | |
1001 | 1872 // We have seen a `.' and need to see if it is the start of a |
1873 // continuation. If so, this eats it, up to and including the new | |
1874 // line character. | |
1875 | |
2857 | 1876 static bool |
3096 | 1877 have_ellipsis_continuation (bool trailing_comments_ok) |
973 | 1878 { |
1879 char c1 = yyinput (); | |
1880 if (c1 == '.') | |
1881 { | |
1882 char c2 = yyinput (); | |
1091 | 1883 if (c2 == '.' && have_continuation (trailing_comments_ok)) |
2857 | 1884 return true; |
973 | 1885 else |
1886 { | |
4410 | 1887 yyunput (c2, yytext); |
1888 yyunput (c1, yytext); | |
973 | 1889 } |
1890 } | |
1891 else | |
4410 | 1892 yyunput (c1, yytext); |
973 | 1893 |
2857 | 1894 return false; |
973 | 1895 } |
1896 | |
1001 | 1897 // See if we have a continuation line. If so, eat it and the leading |
1898 // whitespace on the next line. | |
1088 | 1899 // |
1900 // Return value is the same as described for eat_whitespace(). | |
1001 | 1901 |
1826 | 1902 static yum_yum |
1001 | 1903 eat_continuation (void) |
1904 { | |
1826 | 1905 int retval = ATE_NOTHING; |
3665 | 1906 |
1001 | 1907 int c = yyinput (); |
3665 | 1908 |
1001 | 1909 if ((c == '.' && have_ellipsis_continuation ()) |
3105 | 1910 || (c == '\\' && have_continuation ())) |
1001 | 1911 retval = eat_whitespace (); |
1912 else | |
4410 | 1913 yyunput (c, yytext); |
1001 | 1914 |
1915 return retval; | |
1916 } | |
1917 | |
973 | 1918 static int |
975 | 1919 handle_string (char delim, int text_style) |
973 | 1920 { |
5765 | 1921 std::ostringstream buf; |
973 | 1922 |
3805 | 1923 int bos_line = input_line_number; |
1924 int bos_col = current_input_column; | |
1925 | |
973 | 1926 int c; |
1031 | 1927 int escape_pending = 0; |
973 | 1928 |
1929 while ((c = yyinput ()) != EOF) | |
1930 { | |
1931 current_input_column++; | |
1932 | |
3105 | 1933 if (c == '\\') |
973 | 1934 { |
5359 | 1935 if (delim == '\'' || escape_pending) |
1053 | 1936 { |
3802 | 1937 buf << static_cast<char> (c); |
1053 | 1938 escape_pending = 0; |
1939 } | |
1940 else | |
1941 { | |
3096 | 1942 if (have_continuation (false)) |
1053 | 1943 escape_pending = 0; |
1944 else | |
1945 { | |
3802 | 1946 buf << static_cast<char> (c); |
1053 | 1947 escape_pending = 1; |
1948 } | |
1949 } | |
1031 | 1950 continue; |
973 | 1951 } |
1952 else if (c == '.') | |
1953 { | |
5359 | 1954 if (delim == '\'' || ! have_ellipsis_continuation (false)) |
3802 | 1955 buf << static_cast<char> (c); |
973 | 1956 } |
1957 else if (c == '\n') | |
1958 { | |
1053 | 1959 error ("unterminated string constant"); |
973 | 1960 break; |
1961 } | |
1962 else if (c == delim) | |
1963 { | |
1031 | 1964 if (escape_pending) |
3802 | 1965 buf << static_cast<char> (c); |
973 | 1966 else |
1967 { | |
1968 c = yyinput (); | |
1969 if (c == delim) | |
5102 | 1970 { |
1971 buf << static_cast<char> (c); | |
1972 if (lexer_flags.doing_rawcommand) | |
1973 buf << static_cast<char> (c); | |
1974 } | |
973 | 1975 else |
1976 { | |
5102 | 1977 std::string s; |
4410 | 1978 yyunput (c, yytext); |
5765 | 1979 |
5279 | 1980 if (lexer_flags.doing_rawcommand || delim == '\'') |
5765 | 1981 s = buf.str (); |
5102 | 1982 else |
5765 | 1983 s = do_string_escapes (buf.str ()); |
975 | 1984 |
5102 | 1985 if (text_style && lexer_flags.doing_rawcommand) |
1986 s = std::string (1, delim) + s + std::string (1, delim); | |
975 | 1987 else |
1988 { | |
2857 | 1989 lexer_flags.quote_is_transpose = true; |
1990 lexer_flags.convert_spaces_to_comma = true; | |
975 | 1991 } |
1992 | |
3805 | 1993 yylval.tok_val = new token (s, bos_line, bos_col); |
973 | 1994 token_stack.push (yylval.tok_val); |
3400 | 1995 |
4037 | 1996 if (delim == '"') |
1997 gripe_matlab_incompatible ("\" used as string delimiter"); | |
1998 else if (delim == '\'') | |
3400 | 1999 gripe_single_quote_string (); |
2000 | |
5279 | 2001 return delim == '"' ? DQ_STRING : SQ_STRING; |
973 | 2002 } |
2003 } | |
2004 } | |
2005 else | |
2006 { | |
3802 | 2007 buf << static_cast<char> (c); |
973 | 2008 } |
2009 | |
1031 | 2010 escape_pending = 0; |
973 | 2011 } |
2012 | |
2013 return LEXICAL_ERROR; | |
2014 } | |
2015 | |
3208 | 2016 static bool |
2017 next_token_is_assign_op (void) | |
2018 { | |
2019 bool retval = false; | |
2020 | |
2021 int c0 = yyinput (); | |
2022 | |
2023 switch (c0) | |
2024 { | |
2025 case '=': | |
2026 { | |
2027 int c1 = yyinput (); | |
4410 | 2028 yyunput (c1, yytext); |
3208 | 2029 if (c1 != '=') |
2030 retval = true; | |
2031 } | |
2032 break; | |
2033 | |
2034 case '+': | |
2035 case '-': | |
2036 case '*': | |
2037 case '/': | |
2038 case '\\': | |
2039 case '&': | |
2040 case '|': | |
2041 { | |
2042 int c1 = yyinput (); | |
4410 | 2043 yyunput (c1, yytext); |
3208 | 2044 if (c1 == '=') |
2045 retval = true; | |
2046 } | |
2047 break; | |
2048 | |
2049 case '.': | |
2050 { | |
2051 int c1 = yyinput (); | |
2052 if (match_any (c1, "+-*/\\")) | |
2053 { | |
2054 int c2 = yyinput (); | |
4410 | 2055 yyunput (c2, yytext); |
3208 | 2056 if (c2 == '=') |
2057 retval = true; | |
2058 } | |
4410 | 2059 yyunput (c1, yytext); |
3208 | 2060 } |
2061 break; | |
2062 | |
2063 case '>': | |
2064 { | |
2065 int c1 = yyinput (); | |
2066 if (c1 == '>') | |
2067 { | |
2068 int c2 = yyinput (); | |
4410 | 2069 yyunput (c2, yytext); |
3208 | 2070 if (c2 == '=') |
2071 retval = true; | |
2072 } | |
4410 | 2073 yyunput (c1, yytext); |
3208 | 2074 } |
2075 break; | |
2076 | |
2077 case '<': | |
2078 { | |
2079 int c1 = yyinput (); | |
2080 if (c1 == '<') | |
2081 { | |
2082 int c2 = yyinput (); | |
4410 | 2083 yyunput (c2, yytext); |
3208 | 2084 if (c2 == '=') |
2085 retval = true; | |
2086 } | |
4410 | 2087 yyunput (c1, yytext); |
3208 | 2088 } |
2089 break; | |
2090 | |
2091 default: | |
2092 break; | |
2093 } | |
2094 | |
4410 | 2095 yyunput (c0, yytext); |
3208 | 2096 |
2097 return retval; | |
2098 } | |
2099 | |
4633 | 2100 static bool |
2101 next_token_is_index_op (void) | |
2102 { | |
2103 int c = yyinput (); | |
2104 yyunput (c, yytext); | |
2105 return c == '(' || c == '{'; | |
2106 } | |
2107 | |
4612 | 2108 static int |
2109 handle_close_bracket (bool spc_gobbled, int bracket_type) | |
971 | 2110 { |
4612 | 2111 int retval = bracket_type; |
3208 | 2112 |
1826 | 2113 if (! nesting_level.none ()) |
971 | 2114 { |
1826 | 2115 nesting_level.remove (); |
4613 | 2116 |
2117 if (bracket_type == ']') | |
2118 lexer_flags.bracketflag--; | |
2119 else if (bracket_type == '}') | |
2120 lexer_flags.braceflag--; | |
2121 else | |
2122 panic_impossible (); | |
971 | 2123 } |
2124 | |
4613 | 2125 if (lexer_flags.bracketflag == 0 && lexer_flags.braceflag == 0) |
4323 | 2126 BEGIN (INITIAL); |
1001 | 2127 |
4608 | 2128 if (bracket_type == ']' |
2129 && next_token_is_assign_op () | |
2130 && ! lexer_flags.looking_at_return_list) | |
971 | 2131 { |
3208 | 2132 retval = CLOSE_BRACE; |
971 | 2133 } |
4613 | 2134 else if ((lexer_flags.bracketflag || lexer_flags.braceflag) |
2135 && lexer_flags.convert_spaces_to_comma | |
2136 && (nesting_level.is_bracket () | |
2137 || (nesting_level.is_brace () | |
2138 && ! lexer_flags.looking_at_object_index))) | |
971 | 2139 { |
4633 | 2140 bool index_op = next_token_is_index_op (); |
2141 | |
2142 // Don't insert comma if we are looking at something like | |
2143 // | |
2144 // [x{i}{j}] or [x{i}(j)] | |
2145 // | |
2146 // but do if we are looking at | |
2147 // | |
2148 // [x{i} {j}] or [x{i} (j)] | |
2149 | |
2150 if (spc_gobbled || ! (bracket_type == '}' && index_op)) | |
971 | 2151 { |
4633 | 2152 bool bin_op = next_token_is_bin_op (spc_gobbled); |
2153 | |
2154 bool postfix_un_op = next_token_is_postfix_unary_op (spc_gobbled); | |
2155 | |
2156 bool sep_op = next_token_is_sep_op (); | |
2157 | |
2158 if (! (postfix_un_op || bin_op || sep_op)) | |
2159 { | |
2160 maybe_warn_separator_insert (','); | |
2161 | |
2162 yyunput (',', yytext); | |
2163 return retval; | |
2164 } | |
971 | 2165 } |
2166 } | |
2167 | |
2857 | 2168 lexer_flags.quote_is_transpose = true; |
2169 lexer_flags.convert_spaces_to_comma = true; | |
3208 | 2170 |
2171 return retval; | |
971 | 2172 } |
2173 | |
1072 | 2174 static void |
2175 maybe_unput_comma (int spc_gobbled) | |
2176 { | |
4613 | 2177 if (nesting_level.is_bracket () |
2178 || (nesting_level.is_brace () | |
2179 && ! lexer_flags.looking_at_object_index)) | |
1072 | 2180 { |
3246 | 2181 int bin_op = next_token_is_bin_op (spc_gobbled); |
1072 | 2182 |
3246 | 2183 int postfix_un_op = next_token_is_postfix_unary_op (spc_gobbled); |
1072 | 2184 |
2185 int c1 = yyinput (); | |
2186 int c2 = yyinput (); | |
2970 | 2187 |
4410 | 2188 yyunput (c2, yytext); |
2189 yyunput (c1, yytext); | |
2970 | 2190 |
3263 | 2191 int sep_op = next_token_is_sep_op (); |
2970 | 2192 |
1072 | 2193 int dot_op = (c1 == '.' |
2194 && (isalpha (c2) || isspace (c2) || c2 == '_')); | |
2970 | 2195 |
3388 | 2196 if (postfix_un_op || bin_op || sep_op || dot_op) |
2197 return; | |
2198 | |
3985 | 2199 int index_op = (c1 == '(' || c1 == '{'); |
3388 | 2200 |
4476 | 2201 // If there is no space before the indexing op, we don't insert |
2202 // a comma. | |
2203 | |
2204 if (index_op && ! spc_gobbled) | |
2205 return; | |
2206 | |
2207 maybe_warn_separator_insert (','); | |
2208 | |
2209 yyunput (',', yytext); | |
1072 | 2210 } |
2211 } | |
2212 | |
767 | 2213 // Figure out exactly what kind of token to return when we have seen |
4238 | 2214 // an identifier. Handles keywords. Return -1 if the identifier |
2215 // should be ignored. | |
767 | 2216 |
146 | 2217 static int |
3974 | 2218 handle_identifier (void) |
146 | 2219 { |
3974 | 2220 std::string tok = strip_trailing_whitespace (yytext); |
2221 | |
2222 int c = yytext[yyleng-1]; | |
2223 | |
2224 int cont_is_spc = eat_continuation (); | |
2225 | |
2226 int spc_gobbled = (cont_is_spc || c == ' ' || c == '\t'); | |
2227 | |
2970 | 2228 // If we are expecting a structure element, avoid recognizing |
2229 // keywords and other special names and return STRUCT_ELT, which is | |
2230 // a string that is also a valid identifier. But first, we have to | |
2231 // decide whether to insert a comma. | |
747 | 2232 |
1826 | 2233 if (lexer_flags.looking_at_indirect_ref) |
1072 | 2234 { |
2970 | 2235 do_comma_insert_check (); |
2236 | |
1072 | 2237 maybe_unput_comma (spc_gobbled); |
2819 | 2238 |
2239 yylval.tok_val = new token (tok, input_line_number, | |
2240 current_input_column); | |
2241 | |
2242 token_stack.push (yylval.tok_val); | |
2243 | |
2857 | 2244 lexer_flags.quote_is_transpose = true; |
2245 lexer_flags.convert_spaces_to_comma = true; | |
2819 | 2246 |
2247 current_input_column += yyleng; | |
2248 | |
2970 | 2249 return STRUCT_ELT; |
1072 | 2250 } |
747 | 2251 |
4930 | 2252 int kw_token = is_keyword_token (tok); |
2253 | |
2254 if (lexer_flags.looking_at_function_handle) | |
2255 { | |
2256 if (kw_token) | |
2257 { | |
2258 error ("function handles may not refer to keywords"); | |
2259 | |
2260 return LEXICAL_ERROR; | |
2261 } | |
2262 else | |
2263 TOK_PUSH_AND_RETURN (tok, FCN_HANDLE); | |
2264 } | |
2265 | |
5102 | 2266 // If we have a regular keyword, return it. |
1826 | 2267 // Keywords can be followed by identifiers (TOK_RETURN handles |
2268 // that). | |
146 | 2269 |
2270 if (kw_token) | |
2271 { | |
4238 | 2272 if (kw_token < 0) |
2273 return kw_token; | |
146 | 2274 else |
2275 TOK_RETURN (kw_token); | |
2276 } | |
2277 | |
1826 | 2278 // See if we have a plot keyword (title, using, with, or clear). |
146 | 2279 |
3480 | 2280 int c1 = yyinput (); |
2281 | |
2282 bool next_tok_is_paren = (c1 == '('); | |
2283 | |
2284 bool next_tok_is_eq = false; | |
2285 if (c1 == '=') | |
2286 { | |
2287 int c2 = yyinput (); | |
4410 | 2288 yyunput (c2, yytext); |
3480 | 2289 |
2290 if (c2 != '=') | |
2291 next_tok_is_eq = true; | |
2292 } | |
2293 | |
4410 | 2294 yyunput (c1, yytext); |
1001 | 2295 |
2702 | 2296 // Kluge alert. |
2297 // | |
2298 // If we are looking at a text style function, set up to gobble its | |
2745 | 2299 // arguments. |
2300 // | |
2301 // If the following token is `=', or if we are parsing a function | |
3189 | 2302 // return list or function parameter list, or if we are looking at |
2303 // something like [ab,cd] = foo (), force the symbol to be inserted | |
2304 // as a variable in the current symbol table. | |
2702 | 2305 |
4208 | 2306 if (is_command_name (tok) && ! is_variable (tok)) |
2702 | 2307 { |
2745 | 2308 if (next_tok_is_eq |
2309 || lexer_flags.looking_at_return_list | |
7634
ae90e05ad299
fix parameter list initializer bug
John W. Eaton <jwe@octave.org>
parents:
7587
diff
changeset
|
2310 || (lexer_flags.looking_at_parameter_list |
ae90e05ad299
fix parameter list initializer bug
John W. Eaton <jwe@octave.org>
parents:
7587
diff
changeset
|
2311 && ! lexer_flags.looking_at_initializer_expression) |
4101 | 2312 || lexer_flags.looking_at_matrix_or_assign_lhs) |
2745 | 2313 { |
2314 force_local_variable (tok); | |
2315 } | |
6067 | 2316 else if (! (next_tok_is_paren || lexer_flags.looking_at_object_index)) |
2702 | 2317 { |
5102 | 2318 BEGIN (COMMAND_START); |
2319 } | |
2320 | |
6067 | 2321 if (is_rawcommand_name (tok) && ! lexer_flags.looking_at_object_index) |
5102 | 2322 { |
2323 lexer_flags.doing_rawcommand = true; | |
4323 | 2324 BEGIN (COMMAND_START); |
2702 | 2325 } |
2326 } | |
2327 | |
4234 | 2328 // Find the token in the symbol table. Beware the magic |
2329 // transformation of the end keyword... | |
2330 | |
2331 if (tok == "end") | |
2332 tok = "__end__"; | |
146 | 2333 |
7336 | 2334 yylval.tok_val = new token (&(symbol_table::insert (tok)), |
2335 input_line_number, current_input_column); | |
2336 | |
146 | 2337 token_stack.push (yylval.tok_val); |
2338 | |
1826 | 2339 // After seeing an identifer, it is ok to convert spaces to a comma |
2340 // (if needed). | |
146 | 2341 |
2857 | 2342 lexer_flags.convert_spaces_to_comma = true; |
146 | 2343 |
2877 | 2344 if (! next_tok_is_eq) |
2345 { | |
2346 lexer_flags.quote_is_transpose = true; | |
146 | 2347 |
2877 | 2348 do_comma_insert_check (); |
2349 | |
2350 maybe_unput_comma (spc_gobbled); | |
146 | 2351 } |
2352 | |
2877 | 2353 current_input_column += yyleng; |
146 | 2354 |
2355 return NAME; | |
2356 } | |
2357 | |
1826 | 2358 void |
2359 lexical_feedback::init (void) | |
2360 { | |
2361 // Not initially defining a matrix list. | |
3351 | 2362 bracketflag = 0; |
1826 | 2363 |
4613 | 2364 // Not initially defining a cell array list. |
2365 braceflag = 0; | |
2366 | |
1826 | 2367 // Not initially inside a loop or if statement. |
2368 looping = 0; | |
2369 | |
2857 | 2370 // Not initially defining a function. |
2371 defining_func = false; | |
2877 | 2372 parsed_function_name = false; |
4240 | 2373 parsing_nested_function = 0; |
7336 | 2374 parsing_class_method = false; |
2857 | 2375 |
4930 | 2376 // Not initiallly looking at a function handle. |
2377 looking_at_function_handle = 0; | |
2378 | |
2857 | 2379 // Not parsing a function return or parameter list. |
2380 looking_at_return_list = false; | |
2381 looking_at_parameter_list = false; | |
2382 | |
7634
ae90e05ad299
fix parameter list initializer bug
John W. Eaton <jwe@octave.org>
parents:
7587
diff
changeset
|
2383 // Not looking at an argument list initializer expression. |
ae90e05ad299
fix parameter list initializer bug
John W. Eaton <jwe@octave.org>
parents:
7587
diff
changeset
|
2384 looking_at_initializer_expression = false; |
ae90e05ad299
fix parameter list initializer bug
John W. Eaton <jwe@octave.org>
parents:
7587
diff
changeset
|
2385 |
3796 | 2386 // Not parsing a matrix or the left hand side of multi-value |
2387 // assignment statement. | |
2388 looking_at_matrix_or_assign_lhs = false; | |
2389 | |
4234 | 2390 // Not parsing an object index. |
4237 | 2391 looking_at_object_index = 0; |
4234 | 2392 |
2857 | 2393 // No need to do comma insert or convert spaces to comma at |
2394 // beginning of input. | |
2395 convert_spaces_to_comma = true; | |
2396 do_comma_insert = false; | |
2397 | |
2398 // Not initially doing any plotting or setting of plot attributes. | |
5102 | 2399 doing_rawcommand = false; |
2857 | 2400 |
1826 | 2401 // Not initially looking at indirect references. |
2857 | 2402 looking_at_indirect_ref = false; |
1826 | 2403 |
2404 // Quote marks strings intially. | |
2857 | 2405 quote_is_transpose = false; |
1826 | 2406 } |
2407 | |
4867 | 2408 bool |
2409 is_keyword (const std::string& s) | |
2410 { | |
5088 | 2411 return octave_kw_hash::in_word_set (s.c_str (), s.length ()) != 0; |
4867 | 2412 } |
2413 | |
4264 | 2414 DEFCMD (iskeyword, args, , |
2415 "-*- texinfo -*-\n\ | |
2416 @deftypefn {Built-in Function} {} iskeyword (@var{name})\n\ | |
2417 Return true if @var{name} is an Octave keyword. If @var{name}\n\ | |
2418 is omitted, return a list of keywords.\n\ | |
2419 @end deftypefn") | |
2420 { | |
2421 octave_value retval; | |
2422 | |
2423 int argc = args.length () + 1; | |
2424 | |
4867 | 2425 string_vector argv = args.make_argv ("iskeyword"); |
4264 | 2426 |
2427 if (error_state) | |
2428 return retval; | |
2429 | |
2430 if (argc == 1) | |
2431 { | |
2432 string_vector lst (TOTAL_KEYWORDS); | |
2433 | |
2434 for (int i = 0; i < TOTAL_KEYWORDS; i++) | |
2435 lst[i] = wordlist[i].name; | |
2436 | |
2437 retval = Cell (lst.qsort ()); | |
2438 } | |
2439 else if (argc == 2) | |
2440 { | |
4867 | 2441 retval = is_keyword (argv[1]); |
4264 | 2442 } |
2443 else | |
5823 | 2444 print_usage (); |
4264 | 2445 |
2446 return retval; | |
2447 } | |
2448 | |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
2449 void |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
2450 prep_lexer_for_script (void) |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
2451 { |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
2452 BEGIN (SCRIPT_FILE_BEGIN); |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
2453 } |
4264 | 2454 |
3388 | 2455 static void |
2456 maybe_warn_separator_insert (char sep) | |
2457 { | |
3523 | 2458 std::string nm = curr_fcn_file_full_name; |
3388 | 2459 |
5794 | 2460 if (nm.empty ()) |
2461 warning_with_id ("Octave:separator-insert", | |
2462 "potential auto-insertion of `%c' near line %d", | |
2463 sep, input_line_number); | |
2464 else | |
2465 warning_with_id ("Octave:separator-insert", | |
2466 "potential auto-insertion of `%c' near line %d of file %s", | |
2467 sep, input_line_number, nm.c_str ()); | |
3388 | 2468 } |
2469 | |
3400 | 2470 static void |
2471 gripe_single_quote_string (void) | |
2472 { | |
3523 | 2473 std::string nm = curr_fcn_file_full_name; |
3400 | 2474 |
5794 | 2475 if (nm.empty ()) |
2476 warning_with_id ("Octave:single-quote-string", | |
2477 "single quote delimited string near line %d", | |
2478 input_line_number); | |
2479 else | |
2480 warning_with_id ("Octave:single-quote-string", | |
2481 "single quote delimited string near line %d of file %s", | |
2482 input_line_number, nm.c_str ()); | |
3400 | 2483 } |
2484 | |
4037 | 2485 static void |
2486 gripe_matlab_incompatible (const std::string& msg) | |
2487 { | |
5794 | 2488 warning_with_id ("Octave:matlab-incompatible", |
2489 "potential Matlab compatibility problem: %s", | |
2490 msg.c_str ()); | |
4037 | 2491 } |
2492 | |
2493 static void | |
2494 maybe_gripe_matlab_incompatible_comment (char c) | |
2495 { | |
2496 if (c == '#') | |
2497 gripe_matlab_incompatible ("# used as comment character"); | |
2498 } | |
2499 | |
2500 static void | |
2501 gripe_matlab_incompatible_continuation (void) | |
2502 { | |
2503 gripe_matlab_incompatible ("\\ used as line continuation marker"); | |
2504 } | |
2505 | |
2506 static void | |
2507 gripe_matlab_incompatible_operator (const std::string& op) | |
2508 { | |
2509 std::string t = op; | |
2510 int n = t.length (); | |
2511 if (t[n-1] == '\n') | |
2512 t.resize (n-1); | |
2513 gripe_matlab_incompatible (t + " used as operator"); | |
2514 } | |
2515 | |
4910 | 2516 DEFUN (__token_count__, , , |
2517 "-*- texinfo -*-\n\ | |
2518 @deftypefn {Built-in Function} {} __token_count__\n\ | |
2519 Number of language tokens processed since Octave startup.\n\ | |
2520 @end deftypefn") | |
2521 { | |
2522 return octave_value (Vtoken_count); | |
2523 } | |
2524 | |
1994 | 2525 /* |
2526 ;;; Local Variables: *** | |
2527 ;;; mode: C++ *** | |
2528 ;;; End: *** | |
2529 */ |