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