diff libinterp/parse-tree/lex.ll @ 16360:11115c237231

recognize variables when parsing (bug #38576) * lex.h, lex.ll (lexical_feedback::mark_as_variable, lexical_feedback::maybe_mark_previous_token_as_variable, lexical_feedback::mark_as_variables, octave_lexer::is_variable): New fucntions. ("="): Call maybe_mark_previous_toke_as_variable. (handle_identifier): Don't tag variables as possible commands. (param_list): Mark names in list as variables. (octave_base_parser::validate_matrix_for_assignment): Likewise. * pt-arg-list.h, pt-arg-list.cc (tree_argument_list::variable_names): New function. * pt-misc.h, pt-misc.cc (tree_parameter_list::variable_names): New function. * token.h, token.cc (token::symbol_name, token::is_symbol): New functions. * test/bug-38576.tst: New file. * test/Makefile.am (FCN_FILES): Add bug-38576.tst to the list.
author John W. Eaton <jwe@octave.org>
date Sat, 23 Mar 2013 15:02:29 -0400
parents 6bfd8dbd7d3c
children e1dcd834751f
line wrap: on
line diff
--- a/libinterp/parse-tree/lex.ll	Sat Mar 23 00:09:25 2013 +0100
+++ b/libinterp/parse-tree/lex.ll	Sat Mar 23 15:02:29 2013 -0400
@@ -1056,7 +1056,12 @@
 // = and op= operators.
 %}
 
-"="    { return curr_lexer->handle_op ("=", '='); }
+"=" {
+    curr_lexer->maybe_mark_previous_token_as_variable ();
+
+    return curr_lexer->handle_op ("=", '=');
+  }
+
 "+="   { return curr_lexer->handle_incompatible_op ("+=", ADD_EQ); }
 "-="   { return curr_lexer->handle_incompatible_op ("-=", SUB_EQ); }
 "*="   { return curr_lexer->handle_incompatible_op ("*=", MUL_EQ); }
@@ -1551,6 +1556,24 @@
   return tok ? tok->may_be_command () : false;
 }
 
+void
+lexical_feedback::maybe_mark_previous_token_as_variable (void)
+{
+  token *tok = tokens.front ();
+  if (tok->is_symbol ())
+    pending_local_variables.insert (tok->symbol_name ());
+}
+
+void
+lexical_feedback::mark_as_variables (const std::list<std::string>& lst)
+{
+  for (std::list<std::string>::const_iterator p = lst.begin ();
+       p != lst.end (); p++)
+    {
+      pending_local_variables.insert (*p);
+    }
+}
+
 static bool
 looks_like_copyright (const std::string& s)
 {
@@ -1784,6 +1807,14 @@
   return retval;
 }
 
+bool
+octave_base_lexer::is_variable (const std::string& name)
+{
+  return (symbol_table::is_variable (name)
+          || (pending_local_variables.find (name)
+              != pending_local_variables.end ()));
+}
+
 // Handle keywords.  Return -1 if the keyword should be ignored.
 
 int
@@ -2551,7 +2582,8 @@
                               input_line_number, current_input_column);
 
   if (at_beginning_of_statement
-      && (! (tok == "e"
+      && (! (is_variable (tok)
+             || tok == "e"
              || tok == "I" || tok == "i"
              || tok == "J" || tok == "j"
              || tok == "Inf" || tok == "inf"