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