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