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