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