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 |
3665
|
57 #include "comment-list.h" |
2181
|
58 #include "defun.h" |
1355
|
59 #include "error.h" |
1351
|
60 #include "input.h" |
1355
|
61 #include "lex.h" |
2891
|
62 #include "ov.h" |
1355
|
63 #include "parse.h" |
2987
|
64 #include "pt-all.h" |
2891
|
65 #include "symtab.h" |
|
66 #include "token.h" |
|
67 #include "toplev.h" |
1355
|
68 #include "utils.h" |
|
69 #include "variables.h" |
2492
|
70 #include <y.tab.h> |
|
71 #include <oct-gperf.h> |
1
|
72 |
2716
|
73 #if ! (defined (FLEX_SCANNER) \ |
|
74 && defined (YY_FLEX_MAJOR_VERSION) && YY_FLEX_MAJOR_VERSION >= 2 \ |
|
75 && defined (YY_FLEX_MINOR_VERSION) && YY_FLEX_MINOR_VERSION >= 5) |
|
76 #error lex.l requires flex version 2.5.4 or later |
|
77 #endif |
|
78 |
3883
|
79 // TRUE means that we have encountered EOF on the input stream. |
|
80 bool parser_end_of_input = false; |
|
81 |
1826
|
82 // Flags that need to be shared between the lexer and parser. |
|
83 lexical_feedback lexer_flags; |
|
84 |
1351
|
85 // Stack to hold tokens so that we can delete them when the parser is |
|
86 // reset and avoid growing forever just because we are stashing some |
|
87 // information. This has to appear before lex.h is included, because |
|
88 // one of the macros defined there uses token_stack. |
2614
|
89 // |
|
90 // XXX FIXME XXX -- this should really be static, but that causes |
|
91 // problems on some systems. |
4214
|
92 std::stack <token*> token_stack; |
1351
|
93 |
1826
|
94 // Did eat_whitespace() eat a space or tab, or a newline, or both? |
1
|
95 |
1826
|
96 typedef int yum_yum; |
1
|
97 |
1826
|
98 const yum_yum ATE_NOTHING = 0; |
|
99 const yum_yum ATE_SPACE_OR_TAB = 1; |
|
100 const yum_yum ATE_NEWLINE = 2; |
1088
|
101 |
3351
|
102 // Is the closest nesting level a square bracket, squiggly brace or a paren? |
1826
|
103 |
4214
|
104 class bracket_brace_paren_nesting_level |
1826
|
105 { |
|
106 public: |
|
107 |
4214
|
108 bracket_brace_paren_nesting_level (void) : context () { } |
1826
|
109 |
3351
|
110 ~bracket_brace_paren_nesting_level (void) { } |
|
111 |
4214
|
112 void bracket (void) { context.push (BRACKET); } |
|
113 bool is_bracket (void) |
|
114 { return ! context.empty () && context.top () == BRACKET; } |
|
115 |
|
116 void brace (void) { context.push (BRACE); } |
|
117 bool is_brace (void) |
|
118 { return ! context.empty () && context.top () == BRACE; } |
|
119 |
|
120 void paren (void) { context.push (PAREN); } |
|
121 bool is_paren (void) |
|
122 { return ! context.empty () && context.top () == PAREN; } |
|
123 |
|
124 bool none (void) { return context.empty (); } |
|
125 |
|
126 void remove (void) { if (! context.empty ()) context.pop (); } |
|
127 |
|
128 void clear (void) { while (! context.empty ()) context.pop (); } |
1826
|
129 |
|
130 private: |
|
131 |
4214
|
132 std::stack<int> context; |
|
133 |
3351
|
134 enum { BRACKET = 1, BRACE = 2, PAREN = 3 }; |
1826
|
135 |
3351
|
136 bracket_brace_paren_nesting_level (const bracket_brace_paren_nesting_level&); |
1826
|
137 |
3351
|
138 bracket_brace_paren_nesting_level& |
|
139 operator = (const bracket_brace_paren_nesting_level&); |
1826
|
140 }; |
|
141 |
3351
|
142 static bracket_brace_paren_nesting_level nesting_level; |
1
|
143 |
2167
|
144 // Should whitespace in a literal matrix list be automatically |
|
145 // converted to commas and semicolons? |
|
146 // |
|
147 // user specifies value of var |
|
148 // -------------- ------------ |
|
149 // "ignore" 2 |
|
150 // "traditional" 1 |
|
151 // anything else 0 |
|
152 // |
|
153 // Octave will never insert a comma in a literal matrix list if the |
|
154 // user specifies "ignore". For example, the statement [1 2] will |
|
155 // result in an error instead of being treated the same as [1, 2], and |
|
156 // the statement |
|
157 // |
|
158 // [ 1, 2, |
|
159 // 3, 4 ] |
|
160 // |
|
161 // will result in the vector [1 2 3 4] instead of a matrix. |
|
162 // |
|
163 // Traditional behavior makes Octave convert spaces to a comma between |
|
164 // identifiers and `('. For example, the statement |
|
165 // |
|
166 // [eye (2)] |
|
167 // |
|
168 // will be parsed as |
|
169 // |
|
170 // [eye, (2)] |
|
171 // |
|
172 // and will result in an error since the `eye' function will be |
|
173 // called with no arguments. To get around this, you would have to |
|
174 // omit the space between `eye' and the `('. |
|
175 // |
|
176 // The default value is 0, which results in behavior that is the same |
|
177 // as traditional, except that Octave does not convert spaces to a |
|
178 // comma between identifiers and `('. For example, the statement |
|
179 // |
|
180 // [eye (2)] |
|
181 // |
|
182 // will result in a call to `eye' with the argument `2'. |
|
183 |
|
184 static int Vwhitespace_in_literal_matrix; |
|
185 |
4037
|
186 static bool Vwarn_matlab_incompatible = false; |
|
187 |
3388
|
188 static bool Vwarn_separator_insert = false; |
|
189 |
3400
|
190 static bool Vwarn_single_quote_string = false; |
|
191 |
146
|
192 // Forward declarations for functions defined at the bottom of this |
|
193 // file. |
|
194 |
1
|
195 static void fixup_column_count (char *s); |
146
|
196 static void do_comma_insert_check (void); |
3523
|
197 static int is_plot_keyword (const std::string& s); |
|
198 static int is_keyword (const std::string& s); |
4238
|
199 static void prep_for_function (void); |
|
200 static void prep_for_nested_function (void); |
3523
|
201 static std::string plot_style_token (const std::string& s); |
|
202 static symbol_record *lookup_identifier (const std::string& s); |
1
|
203 static void grab_help_text (void); |
2857
|
204 static bool match_any (char c, const char *s); |
3263
|
205 static bool next_token_is_sep_op (void); |
3246
|
206 static bool next_token_is_bin_op (bool spc_prev); |
|
207 static bool next_token_is_postfix_unary_op (bool spc_prev); |
3523
|
208 static std::string strip_trailing_whitespace (char *s); |
3246
|
209 static void handle_number (void); |
975
|
210 static int handle_string (char delim, int text_style = 0); |
3351
|
211 static int handle_close_bracket (int spc_gobbled); |
3974
|
212 static int handle_identifier (void); |
3096
|
213 static bool have_continuation (bool trailing_comments_ok = true); |
|
214 static bool have_ellipsis_continuation (bool trailing_comments_ok = true); |
3665
|
215 static void scan_for_comments (const char *); |
1826
|
216 static yum_yum eat_whitespace (void); |
|
217 static yum_yum eat_continuation (void); |
3388
|
218 static void maybe_warn_separator_insert (char sep); |
3400
|
219 static void gripe_single_quote_string (void); |
4037
|
220 static void gripe_matlab_incompatible (const std::string& msg); |
|
221 static void maybe_gripe_matlab_incompatible_comment (char c); |
|
222 static void gripe_matlab_incompatible_continuation (void); |
|
223 static void gripe_matlab_incompatible_operator (const std::string& op); |
1
|
224 |
|
225 %} |
|
226 |
|
227 D [0-9] |
|
228 S [ \t] |
2042
|
229 NL ((\n)|(\r\n)) |
|
230 SNL ({S}|{NL}) |
1
|
231 EL (\.\.\.) |
967
|
232 BS (\\) |
|
233 CONT ({EL}|{BS}) |
1
|
234 Im [iIjJ] |
967
|
235 CCHAR [#%] |
|
236 COMMENT ({CCHAR}.*{NL}) |
|
237 SNLCMT ({SNL}|{COMMENT}) |
|
238 NOT ((\~)|(\!)) |
4037
|
239 POW ((\*\*)|(\^)) |
|
240 EPOW (\.{POW}) |
1
|
241 IDENT ([_a-zA-Z][_a-zA-Z0-9]*) |
|
242 EXPON ([DdEe][+-]?{D}+) |
3220
|
243 NUMBER (({D}+\.?{D}*{EXPON}?)|(\.{D}+{EXPON}?)|(0[xX][0-9a-fA-F]+)) |
1
|
244 %% |
|
245 |
4240
|
246 <NESTED_FUNCTION_END>. { |
|
247 BEGIN NESTED_FUNCTION_BEGIN; |
|
248 unput (yytext[0]); |
|
249 return ';'; |
|
250 } |
|
251 |
|
252 <NESTED_FUNCTION_BEGIN>. { |
4238
|
253 BEGIN 0; |
4240
|
254 unput (yytext[0]); |
4238
|
255 prep_for_nested_function (); |
|
256 return FCN; |
|
257 } |
|
258 |
968
|
259 %{ |
4208
|
260 // Help and other command-style functions are a pain in the ass. This |
968
|
261 // stuff needs to be simplified. May require some changes in the |
|
262 // parser too. |
|
263 %} |
|
264 |
4208
|
265 <COMMAND_START>{NL} { |
967
|
266 BEGIN 0; |
|
267 current_input_column = 1; |
2857
|
268 lexer_flags.quote_is_transpose = false; |
|
269 lexer_flags.cant_be_identifier = false; |
|
270 lexer_flags.convert_spaces_to_comma = true; |
967
|
271 return '\n'; |
|
272 } |
1
|
273 |
4208
|
274 <COMMAND_START>[\;\,] { |
1826
|
275 if (lexer_flags.doing_set && strcmp (yytext, ",") == 0) |
967
|
276 { |
4064
|
277 TOK_PUSH_AND_RETURN (yytext, STRING); |
967
|
278 } |
|
279 else |
|
280 { |
|
281 BEGIN 0; |
|
282 if (strcmp (yytext, ",") == 0) |
|
283 TOK_RETURN (','); |
|
284 else |
|
285 TOK_RETURN (';'); |
|
286 } |
|
287 } |
1
|
288 |
4208
|
289 <COMMAND_START>[\"\'] { |
975
|
290 current_input_column++; |
3246
|
291 return handle_string (yytext[0], true); |
975
|
292 } |
|
293 |
4208
|
294 <COMMAND_START>[^#% \t\n\;\,\"\'][^ \t\n\;\,]*{S}* { |
3523
|
295 std::string tok = strip_trailing_whitespace (yytext); |
4064
|
296 TOK_PUSH_AND_RETURN (tok, STRING); |
967
|
297 } |
1
|
298 |
968
|
299 %{ |
1
|
300 // For this and the next two rules, we're looking at ']', and we |
971
|
301 // need to know if the next token is `=' or `=='. |
1
|
302 // |
|
303 // It would have been so much easier if the delimiters were simply |
|
304 // different for the expression on the left hand side of the equals |
|
305 // operator. |
971
|
306 // |
|
307 // It's also a pain in the ass to decide whether to insert a comma |
|
308 // after seeing a ']' character... |
968
|
309 %} |
|
310 |
4208
|
311 <MATRIX_START>{SNLCMT}*\]{S}* { |
3665
|
312 scan_for_comments (yytext); |
1001
|
313 fixup_column_count (yytext); |
|
314 int c = yytext[yyleng-1]; |
|
315 int cont_is_spc = eat_continuation (); |
|
316 int spc_gobbled = (cont_is_spc || c == ' ' || c == '\t'); |
3351
|
317 return handle_close_bracket (spc_gobbled); |
967
|
318 } |
1
|
319 |
968
|
320 %{ |
1088
|
321 // Commas are element separators in matrix constants. If we don't |
|
322 // check for continuations here we can end up inserting too many |
|
323 // commas. |
968
|
324 %} |
|
325 |
4208
|
326 <MATRIX_START>{S}*\,{S}* { |
1088
|
327 current_input_column += yyleng; |
3388
|
328 |
1088
|
329 int tmp = eat_continuation (); |
3388
|
330 |
2857
|
331 lexer_flags.quote_is_transpose = false; |
|
332 lexer_flags.cant_be_identifier = false; |
|
333 lexer_flags.convert_spaces_to_comma = true; |
3388
|
334 |
|
335 if ((tmp & ATE_NEWLINE) == ATE_NEWLINE) |
|
336 { |
|
337 maybe_warn_separator_insert (';'); |
|
338 |
|
339 if (Vwhitespace_in_literal_matrix != 2) |
|
340 unput (';'); |
|
341 } |
|
342 |
1088
|
343 return (','); |
967
|
344 } |
1
|
345 |
968
|
346 %{ |
|
347 // In some cases, spaces in matrix constants can turn into commas. |
|
348 // If commas are required, spaces are not important in matrix |
1088
|
349 // constants so we just eat them. If we don't check for continuations |
|
350 // here we can end up inserting too many commas. |
968
|
351 %} |
430
|
352 |
4208
|
353 <MATRIX_START>{S}+ { |
1088
|
354 current_input_column += yyleng; |
3388
|
355 |
|
356 int tmp = eat_continuation (); |
|
357 int bin_op = next_token_is_bin_op (true); |
|
358 int postfix_un_op = next_token_is_postfix_unary_op (true); |
|
359 |
|
360 if (! (postfix_un_op || bin_op) |
|
361 && nesting_level.is_bracket () |
|
362 && lexer_flags.convert_spaces_to_comma) |
967
|
363 { |
3388
|
364 if ((tmp & ATE_NEWLINE) == ATE_NEWLINE) |
|
365 { |
|
366 maybe_warn_separator_insert (';'); |
967
|
367 |
3388
|
368 if (Vwhitespace_in_literal_matrix != 2) |
|
369 unput (';'); |
|
370 } |
|
371 |
|
372 if (Vwhitespace_in_literal_matrix != 2) |
1088
|
373 { |
2857
|
374 lexer_flags.quote_is_transpose = false; |
|
375 lexer_flags.cant_be_identifier = false; |
|
376 lexer_flags.convert_spaces_to_comma = true; |
3388
|
377 |
|
378 maybe_warn_separator_insert (','); |
|
379 |
1088
|
380 return (','); |
|
381 } |
967
|
382 } |
|
383 } |
430
|
384 |
968
|
385 %{ |
1088
|
386 // Semicolons are handled as row seprators in matrix constants. If we |
|
387 // don't eat whitespace here we can end up inserting too many |
|
388 // semicolons. |
968
|
389 %} |
|
390 |
4208
|
391 <MATRIX_START>{SNLCMT}*;{SNLCMT}* { |
3665
|
392 scan_for_comments (yytext); |
967
|
393 fixup_column_count (yytext); |
1001
|
394 eat_whitespace (); |
2857
|
395 lexer_flags.quote_is_transpose = false; |
|
396 lexer_flags.cant_be_identifier = false; |
|
397 lexer_flags.convert_spaces_to_comma = true; |
967
|
398 return ';'; |
|
399 } |
|
400 |
968
|
401 %{ |
1088
|
402 // In some cases, new lines can also become row separators. If we |
|
403 // don't eat whitespace here we can end up inserting too many |
|
404 // semicolons. |
985
|
405 %} |
|
406 |
4208
|
407 <MATRIX_START>{S}*{COMMENT}{SNLCMT}* | |
|
408 <MATRIX_START>{S}*{NL}{SNLCMT}* { |
3665
|
409 scan_for_comments (yytext); |
1082
|
410 fixup_column_count (yytext); |
1088
|
411 eat_whitespace (); |
3388
|
412 |
2167
|
413 if (Vwhitespace_in_literal_matrix != 2) |
985
|
414 { |
2857
|
415 lexer_flags.quote_is_transpose = false; |
|
416 lexer_flags.cant_be_identifier = false; |
|
417 lexer_flags.convert_spaces_to_comma = true; |
985
|
418 |
1826
|
419 if (nesting_level.none ()) |
985
|
420 return LEXICAL_ERROR; |
3388
|
421 } |
985
|
422 |
3388
|
423 if (nesting_level.is_bracket ()) |
|
424 { |
|
425 maybe_warn_separator_insert (';'); |
|
426 |
|
427 if (Vwhitespace_in_literal_matrix != 2) |
985
|
428 return ';'; |
|
429 } |
|
430 } |
|
431 |
|
432 %{ |
3351
|
433 // Open and close bracket are handled differently if we are in the range |
968
|
434 // part of a plot command. |
975
|
435 // |
968
|
436 %} |
1
|
437 |
967
|
438 \[{S}* { |
3351
|
439 nesting_level.bracket (); |
975
|
440 |
1082
|
441 current_input_column += yyleng; |
2857
|
442 lexer_flags.quote_is_transpose = false; |
|
443 lexer_flags.cant_be_identifier = false; |
|
444 lexer_flags.convert_spaces_to_comma = true; |
975
|
445 |
|
446 promptflag--; |
|
447 eat_whitespace (); |
|
448 |
1826
|
449 if (lexer_flags.plotting && ! lexer_flags.past_plot_range) |
967
|
450 { |
2857
|
451 lexer_flags.in_plot_range = true; |
1082
|
452 return OPEN_BRACE; |
967
|
453 } |
|
454 else |
|
455 { |
3351
|
456 lexer_flags.bracketflag++; |
4208
|
457 BEGIN MATRIX_START; |
1082
|
458 return '['; |
967
|
459 } |
|
460 } |
1
|
461 |
968
|
462 \] { |
1826
|
463 nesting_level.remove (); |
968
|
464 |
1826
|
465 if (lexer_flags.plotting && ! lexer_flags.past_plot_range) |
968
|
466 { |
2857
|
467 lexer_flags.in_plot_range = false; |
968
|
468 TOK_RETURN (CLOSE_BRACE); |
|
469 } |
|
470 else |
|
471 TOK_RETURN (']'); |
|
472 } |
|
473 |
|
474 %{ |
|
475 // Imaginary numbers. |
|
476 %} |
|
477 |
|
478 {NUMBER}{Im} { |
3246
|
479 handle_number (); |
968
|
480 return IMAG_NUM; |
|
481 } |
|
482 |
|
483 %{ |
|
484 // Real numbers. Don't grab the `.' part of a dot operator as part of |
|
485 // the constant. |
|
486 %} |
|
487 |
|
488 {D}+/\.[\*/\\^'] | |
|
489 {NUMBER} { |
3246
|
490 handle_number (); |
968
|
491 return NUM; |
|
492 } |
|
493 |
|
494 %{ |
|
495 // Eat whitespace. Whitespace inside matrix constants is handled by |
4208
|
496 // the <MATRIX_START> start state code above. |
968
|
497 %} |
|
498 |
967
|
499 {S}* { |
|
500 current_input_column += yyleng; |
|
501 } |
|
502 |
968
|
503 %{ |
|
504 // Continuation lines. Allow comments after continuations. |
|
505 %} |
|
506 |
967
|
507 {CONT}{S}*{NL} | |
|
508 {CONT}{S}*{COMMENT} { |
4037
|
509 if (yytext[0] == '\\') |
|
510 gripe_matlab_incompatible_continuation (); |
3665
|
511 scan_for_comments (yytext); |
967
|
512 promptflag--; |
|
513 current_input_column = 1; |
|
514 } |
1
|
515 |
968
|
516 %{ |
|
517 // An ellipsis not at the end of a line is not a continuation, but |
|
518 // does have another meaning. |
|
519 %} |
|
520 |
967
|
521 {EL} { |
3973
|
522 if (lexer_flags.looking_at_parameter_list) |
3974
|
523 { |
|
524 warning ("`...' is deprecated; use varargin instead"); |
|
525 return VARARGIN; |
|
526 } |
3973
|
527 else if (lexer_flags.looking_at_return_list) |
3974
|
528 { |
|
529 warning ("`...' is deprecated; use varargout instead"); |
|
530 return VARARGOUT; |
|
531 } |
3973
|
532 else |
|
533 return LEXICAL_ERROR; |
967
|
534 } |
1
|
535 |
968
|
536 %{ |
|
537 // End of file. |
|
538 %} |
|
539 |
967
|
540 <<EOF>> { |
|
541 TOK_RETURN (END_OF_INPUT); |
|
542 } |
1
|
543 |
968
|
544 %{ |
970
|
545 // Identifiers. Truncate the token at the first space or tab but |
|
546 // don't write directly on yytext. |
968
|
547 %} |
|
548 |
967
|
549 {IDENT}{S}* { |
4238
|
550 int id_tok = handle_identifier (); |
|
551 |
|
552 if (id_tok >= 0) |
|
553 return id_tok; |
967
|
554 } |
1
|
555 |
968
|
556 %{ |
|
557 // A new line character. New line characters inside matrix constants |
4208
|
558 // are handled by the <MATRIX_START> start state code above. If closest |
985
|
559 // nesting is inside parentheses, don't return a row separator. |
968
|
560 %} |
|
561 |
967
|
562 {NL} { |
|
563 current_input_column = 1; |
2857
|
564 lexer_flags.quote_is_transpose = false; |
|
565 lexer_flags.cant_be_identifier = false; |
|
566 lexer_flags.convert_spaces_to_comma = true; |
985
|
567 |
1826
|
568 if (nesting_level.none ()) |
985
|
569 return '\n'; |
4037
|
570 else if (nesting_level.is_paren ()) |
|
571 gripe_matlab_incompatible ("bare newline inside parentheses"); |
|
572 else if (nesting_level.is_bracket ()) |
985
|
573 return LEXICAL_ERROR; |
967
|
574 } |
1
|
575 |
968
|
576 %{ |
|
577 // Single quote can either be the beginning of a string or a transpose |
|
578 // operator. |
|
579 %} |
|
580 |
967
|
581 "'" { |
|
582 current_input_column++; |
2857
|
583 lexer_flags.convert_spaces_to_comma = true; |
1
|
584 |
1826
|
585 if (lexer_flags.quote_is_transpose) |
967
|
586 { |
|
587 do_comma_insert_check (); |
|
588 return QUOTE; |
|
589 } |
|
590 else |
973
|
591 return handle_string ('\''); |
967
|
592 } |
1
|
593 |
968
|
594 %{ |
971
|
595 // Double quotes always begin strings. |
|
596 %} |
|
597 |
973
|
598 \" { |
|
599 current_input_column++; |
|
600 return handle_string ('"'); |
|
601 } |
971
|
602 |
|
603 %{ |
|
604 // The colon operator is handled differently if we are in the range |
|
605 // part of a plot command. |
968
|
606 %} |
|
607 |
967
|
608 ":" { |
1826
|
609 if (lexer_flags.plotting |
|
610 && (lexer_flags.in_plot_range || lexer_flags.in_plot_using)) |
2857
|
611 BIN_OP_RETURN (COLON, true); |
967
|
612 else |
2857
|
613 BIN_OP_RETURN (':', false); |
967
|
614 } |
1
|
615 |
968
|
616 %{ |
985
|
617 // Gobble comments. If closest nesting is inside parentheses, don't |
|
618 // return a new line. |
|
619 %} |
968
|
620 |
967
|
621 {CCHAR} { |
1826
|
622 if (help_buf.empty () |
|
623 && lexer_flags.beginning_of_function |
|
624 && nesting_level.none ()) |
967
|
625 { |
3665
|
626 lexer_flags.beginning_of_function = false; |
|
627 |
967
|
628 grab_help_text (); |
3665
|
629 |
|
630 octave_comment_buffer::append (help_buf); |
967
|
631 } |
|
632 else |
|
633 { |
3665
|
634 std::string buf; |
|
635 |
|
636 bool begin_comment = true; |
|
637 |
967
|
638 int c; |
|
639 while ((c = yyinput ()) != EOF && c != '\n') |
3665
|
640 { |
|
641 if (begin_comment && (c == '#' || c == '%')) |
|
642 ; /* Skip leading comment characters. */ |
|
643 else |
3802
|
644 buf += static_cast<char> (c); |
3665
|
645 } |
|
646 |
|
647 octave_comment_buffer::append (buf); |
967
|
648 } |
440
|
649 |
967
|
650 current_input_column = 1; |
2857
|
651 lexer_flags.quote_is_transpose = false; |
|
652 lexer_flags.cant_be_identifier = false; |
|
653 lexer_flags.convert_spaces_to_comma = true; |
985
|
654 |
4037
|
655 maybe_gripe_matlab_incompatible_comment (yytext[0]); |
|
656 |
1826
|
657 if (nesting_level.none ()) |
985
|
658 return '\n'; |
3351
|
659 else if (nesting_level.is_bracket ()) |
1566
|
660 return ';'; |
967
|
661 } |
440
|
662 |
968
|
663 %{ |
|
664 // Other operators. |
|
665 %} |
|
666 |
4037
|
667 ".+" { XBIN_OP_RETURN (EPLUS, false); } |
|
668 ".-" { XBIN_OP_RETURN (EMINUS, false); } |
2857
|
669 ".*" { BIN_OP_RETURN (EMUL, false); } |
|
670 "./" { BIN_OP_RETURN (EDIV, false); } |
|
671 ".\\" { BIN_OP_RETURN (ELEFTDIV, false); } |
4037
|
672 ".^" { BIN_OP_RETURN (EPOW, false); } |
|
673 ".**" { XBIN_OP_RETURN (EPOW, false); } |
2857
|
674 ".'" { do_comma_insert_check (); BIN_OP_RETURN (TRANSPOSE, true); } |
4037
|
675 "++" { do_comma_insert_check (); XBIN_OP_RETURN (PLUS_PLUS, true); } |
|
676 "--" { do_comma_insert_check (); XBIN_OP_RETURN (MINUS_MINUS, true); } |
2857
|
677 "<=" { BIN_OP_RETURN (EXPR_LE, false); } |
|
678 "==" { BIN_OP_RETURN (EXPR_EQ, false); } |
4037
|
679 "~=" { BIN_OP_RETURN (EXPR_NE, false); } |
|
680 "!=" { XBIN_OP_RETURN (EXPR_NE, false); } |
2857
|
681 ">=" { BIN_OP_RETURN (EXPR_GE, false); } |
2877
|
682 "&" { BIN_OP_RETURN (EXPR_AND, false); } |
2857
|
683 "|" { BIN_OP_RETURN (EXPR_OR, false); } |
|
684 "<" { BIN_OP_RETURN (EXPR_LT, false); } |
|
685 ">" { BIN_OP_RETURN (EXPR_GT, false); } |
|
686 "*" { BIN_OP_RETURN ('*', false); } |
|
687 "/" { BIN_OP_RETURN ('/', false); } |
|
688 "\\" { BIN_OP_RETURN (LEFTDIV, false); } |
|
689 ";" { BIN_OP_RETURN (';', true); } |
|
690 "," { BIN_OP_RETURN (',', true); } |
4037
|
691 "^" { BIN_OP_RETURN (POW, false); } |
|
692 "**" { XBIN_OP_RETURN (POW, false); } |
2857
|
693 "=" { BIN_OP_RETURN ('=', true); } |
2877
|
694 "&&" { BIN_OP_RETURN (EXPR_AND_AND, false); } |
2857
|
695 "||" { BIN_OP_RETURN (EXPR_OR_OR, false); } |
4037
|
696 "<<" { XBIN_OP_RETURN (LSHIFT, false); } |
|
697 ">>" { XBIN_OP_RETURN (RSHIFT, false); } |
967
|
698 |
|
699 {NOT} { |
1826
|
700 if (lexer_flags.plotting && ! lexer_flags.in_plot_range) |
2857
|
701 lexer_flags.past_plot_range = true; |
4037
|
702 |
|
703 if (yytext[0] == '~') |
|
704 BIN_OP_RETURN (EXPR_NOT, false); |
|
705 else |
|
706 XBIN_OP_RETURN (EXPR_NOT, false); |
967
|
707 } |
1
|
708 |
1276
|
709 "+" { |
1826
|
710 if (lexer_flags.plotting && ! lexer_flags.in_plot_range) |
2857
|
711 lexer_flags.past_plot_range = true; |
|
712 BIN_OP_RETURN ('+', false); |
967
|
713 } |
|
714 |
1276
|
715 "-" { |
1826
|
716 if (lexer_flags.plotting && ! lexer_flags.in_plot_range) |
2857
|
717 lexer_flags.past_plot_range = true; |
|
718 BIN_OP_RETURN ('-', false); |
967
|
719 } |
|
720 |
|
721 "(" { |
4131
|
722 lexer_flags.looking_at_indirect_ref = false; |
1826
|
723 if (lexer_flags.plotting && ! lexer_flags.in_plot_range) |
2857
|
724 lexer_flags.past_plot_range = true; |
1826
|
725 nesting_level.paren (); |
985
|
726 promptflag--; |
967
|
727 TOK_RETURN ('('); |
|
728 } |
|
729 |
|
730 ")" { |
1826
|
731 nesting_level.remove (); |
1001
|
732 |
967
|
733 current_input_column++; |
2857
|
734 lexer_flags.cant_be_identifier = true; |
|
735 lexer_flags.quote_is_transpose = true; |
3351
|
736 lexer_flags.convert_spaces_to_comma = nesting_level.is_bracket (); |
1001
|
737 do_comma_insert_check (); |
967
|
738 return ')'; |
|
739 } |
|
740 |
2066
|
741 "." { |
|
742 TOK_RETURN ('.'); |
|
743 } |
|
744 |
3988
|
745 "@" { |
|
746 current_input_column++; |
|
747 lexer_flags.cant_be_identifier = false; |
|
748 lexer_flags.quote_is_transpose = false; |
|
749 lexer_flags.convert_spaces_to_comma = false; |
|
750 return '@'; |
|
751 } |
|
752 |
4037
|
753 "+=" { XBIN_OP_RETURN (ADD_EQ, false); } |
|
754 "-=" { XBIN_OP_RETURN (SUB_EQ, false); } |
|
755 "*=" { XBIN_OP_RETURN (MUL_EQ, false); } |
|
756 "/=" { XBIN_OP_RETURN (DIV_EQ, false); } |
|
757 "\\=" { XBIN_OP_RETURN (LEFTDIV_EQ, false); } |
|
758 ".+=" { XBIN_OP_RETURN (ADD_EQ, false); } |
|
759 ".-=" { XBIN_OP_RETURN (SUB_EQ, false); } |
|
760 ".*=" { XBIN_OP_RETURN (EMUL_EQ, false); } |
|
761 "./=" { XBIN_OP_RETURN (EDIV_EQ, false); } |
|
762 ".\\=" { XBIN_OP_RETURN (ELEFTDIV_EQ, false); } |
|
763 {POW}= { XBIN_OP_RETURN (POW_EQ, false); } |
|
764 {EPOW}= { XBIN_OP_RETURN (EPOW_EQ, false); } |
|
765 "&=" { XBIN_OP_RETURN (AND_EQ, false); } |
|
766 "|=" { XBIN_OP_RETURN (OR_EQ, false); } |
|
767 "<<=" { XBIN_OP_RETURN (LSHIFT_EQ, false); } |
|
768 ">>=" { XBIN_OP_RETURN (RSHIFT_EQ, false); } |
2877
|
769 |
3351
|
770 "{" { |
|
771 nesting_level.brace (); |
|
772 promptflag--; |
|
773 TOK_RETURN ('{'); |
|
774 } |
|
775 |
|
776 "}" { |
|
777 nesting_level.remove (); |
|
778 |
|
779 current_input_column++; |
|
780 lexer_flags.cant_be_identifier = true; |
|
781 lexer_flags.quote_is_transpose = true; |
|
782 lexer_flags.convert_spaces_to_comma = nesting_level.is_bracket (); |
|
783 do_comma_insert_check (); // Is this really necessary? |
|
784 |
|
785 return '}'; |
|
786 } |
|
787 |
968
|
788 %{ |
2066
|
789 // Unrecognized input is a lexical error. |
968
|
790 %} |
1
|
791 |
2042
|
792 . { |
4240
|
793 // EOF happens here if we are parsing nested functions. |
|
794 |
4248
|
795 unput (yytext[0]); |
|
796 |
|
797 int c = yyinput (); |
|
798 |
|
799 if (c != EOF) |
4240
|
800 { |
|
801 current_input_column++; |
|
802 |
|
803 error ("invalid character `%s' (ASCII %d) near line %d, column %d", |
4248
|
804 undo_string_escape (static_cast<char> (c)), c, |
4240
|
805 input_line_number, current_input_column); |
|
806 |
|
807 return LEXICAL_ERROR; |
|
808 } |
|
809 else |
|
810 TOK_RETURN (END_OF_INPUT); |
2066
|
811 } |
1
|
812 |
|
813 %% |
|
814 |
767
|
815 // GAG. |
|
816 // |
|
817 // If we're reading a matrix and the next character is '[', make sure |
|
818 // that we insert a comma ahead of it. |
|
819 |
146
|
820 void |
1
|
821 do_comma_insert_check (void) |
|
822 { |
1001
|
823 int spc_gobbled = eat_continuation (); |
2970
|
824 |
1
|
825 int c = yyinput (); |
2970
|
826 |
3246
|
827 unput (c); |
2970
|
828 |
1001
|
829 if (spc_gobbled) |
3246
|
830 unput (' '); |
2970
|
831 |
3351
|
832 lexer_flags.do_comma_insert = (lexer_flags.bracketflag && c == '['); |
1
|
833 } |
|
834 |
767
|
835 // Fix things up for errors or interrupts. The parser is never called |
|
836 // recursively, so it is always safe to reinitialize its state before |
|
837 // doing any parsing. |
|
838 |
1
|
839 void |
|
840 reset_parser (void) |
|
841 { |
1826
|
842 // Start off on the right foot. |
1
|
843 BEGIN 0; |
166
|
844 error_state = 0; |
3489
|
845 warning_state = 0; |
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 |
3388
|
2577 static void |
|
2578 maybe_warn_separator_insert (char sep) |
|
2579 { |
3523
|
2580 std::string nm = curr_fcn_file_full_name; |
3388
|
2581 |
|
2582 if (Vwarn_separator_insert) |
|
2583 { |
|
2584 if (nm.empty ()) |
|
2585 warning ("potential auto-insertion of `%c' near line %d", |
|
2586 sep, input_line_number); |
|
2587 else |
|
2588 warning ("potential auto-insertion of `%c' near line %d of file %s", |
|
2589 sep, input_line_number, nm.c_str ()); |
|
2590 } |
|
2591 } |
|
2592 |
3400
|
2593 static void |
|
2594 gripe_single_quote_string (void) |
|
2595 { |
3523
|
2596 std::string nm = curr_fcn_file_full_name; |
3400
|
2597 |
|
2598 if (Vwarn_single_quote_string) |
|
2599 { |
|
2600 if (nm.empty ()) |
|
2601 warning ("single quote delimited string near line %d", |
|
2602 input_line_number); |
|
2603 else |
|
2604 warning ("single quote delimited string near line %d of file %s", |
|
2605 input_line_number, nm.c_str ()); |
|
2606 } |
|
2607 } |
|
2608 |
4037
|
2609 static void |
|
2610 gripe_matlab_incompatible (const std::string& msg) |
|
2611 { |
|
2612 if (Vwarn_matlab_incompatible) |
|
2613 warning ("potential Matlab compatibility problem: %s", msg.c_str ()); |
|
2614 } |
|
2615 |
|
2616 static void |
|
2617 maybe_gripe_matlab_incompatible_comment (char c) |
|
2618 { |
|
2619 if (c == '#') |
|
2620 gripe_matlab_incompatible ("# used as comment character"); |
|
2621 } |
|
2622 |
|
2623 static void |
|
2624 gripe_matlab_incompatible_continuation (void) |
|
2625 { |
|
2626 gripe_matlab_incompatible ("\\ used as line continuation marker"); |
|
2627 } |
|
2628 |
|
2629 static void |
|
2630 gripe_matlab_incompatible_operator (const std::string& op) |
|
2631 { |
|
2632 std::string t = op; |
|
2633 int n = t.length (); |
|
2634 if (t[n-1] == '\n') |
|
2635 t.resize (n-1); |
|
2636 gripe_matlab_incompatible (t + " used as operator"); |
|
2637 } |
|
2638 |
|
2639 static int |
|
2640 warn_matlab_incompatible (void) |
|
2641 { |
|
2642 Vwarn_matlab_incompatible = check_preference ("warn_matlab_incompatible"); |
|
2643 |
|
2644 return 0; |
|
2645 } |
|
2646 |
3388
|
2647 static int |
|
2648 warn_separator_insert (void) |
|
2649 { |
|
2650 Vwarn_separator_insert = check_preference ("warn_separator_insert"); |
|
2651 |
|
2652 return 0; |
|
2653 } |
|
2654 |
3246
|
2655 static int |
3400
|
2656 warn_single_quote_string (void) |
|
2657 { |
|
2658 Vwarn_single_quote_string = check_preference ("warn_single_quote_string"); |
|
2659 |
|
2660 return 0; |
|
2661 } |
|
2662 |
|
2663 static int |
2167
|
2664 whitespace_in_literal_matrix (void) |
|
2665 { |
|
2666 int pref = 0; |
3009
|
2667 |
3523
|
2668 std::string val = builtin_string_variable ("whitespace_in_literal_matrix"); |
3009
|
2669 |
2167
|
2670 if (! val.empty ()) |
|
2671 { |
3565
|
2672 if (val == "ignore") |
2167
|
2673 pref = 2; |
3565
|
2674 else if (val == "traditional") |
2167
|
2675 pref = 1; |
|
2676 } |
3009
|
2677 |
2167
|
2678 Vwhitespace_in_literal_matrix = pref; |
3009
|
2679 |
2167
|
2680 return 0; |
|
2681 } |
|
2682 |
|
2683 void |
|
2684 symbols_of_lex (void) |
|
2685 { |
4233
|
2686 DEFVAR (warn_matlab_incompatible, false, warn_matlab_incompatible, |
4037
|
2687 "-*- texinfo -*-\n\ |
|
2688 @defvr {Built-in Variable} warn_matlab_incompatible\n\ |
|
2689 Print warnings for Octave language features that may cause\n\ |
|
2690 compatibility problems with Matlab.\n\ |
|
2691 @end defvr"); |
|
2692 |
4233
|
2693 DEFVAR (warn_separator_insert, false, warn_separator_insert, |
3428
|
2694 "-*- texinfo -*-\n\ |
|
2695 @defvr {Built-in Variable} warn_separator_insert\n\ |
3567
|
2696 Print warning if commas or semicolons might be inserted\n\ |
3428
|
2697 automatically in literal matrices.\n\ |
|
2698 @end defvr"); |
3388
|
2699 |
4233
|
2700 DEFVAR (warn_single_quote_string, false, warn_single_quote_string, |
3428
|
2701 "-*- texinfo -*-\n\ |
|
2702 @defvr {Built-in Variable} warn_single_quote_string\n\ |
|
2703 Print warning if a signle quote character is used to introduce a\n\ |
|
2704 string constant.\n\ |
|
2705 @end defvr"); |
3400
|
2706 |
3258
|
2707 DEFVAR (whitespace_in_literal_matrix, "", whitespace_in_literal_matrix, |
3428
|
2708 "-*- texinfo -*-\n\ |
|
2709 @defvr {Built-in Variable} whitespace_in_literal_matrix\n\ |
|
2710 Control auto-insertion of commas and semicolons in literal matrices.\n\ |
|
2711 @end defvr"); |
|
2712 |
2167
|
2713 } |
|
2714 |
1994
|
2715 /* |
|
2716 ;;; Local Variables: *** |
|
2717 ;;; mode: C++ *** |
|
2718 ;;; End: *** |
|
2719 */ |