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