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