diff libinterp/parse-tree/lex.ll @ 18520:932aca9a7c57

Allow multi-level classdef package. * lex.h (octave_base_lexer::fq_identifier_contains_keyword, octave_base_lexer::handle_fq_identifier, octave_base_lexer::enable_fq_identifier): New methods. * lex.ll (octave_base_lexer::fq_identifier_contains_keyword, octave_base_lexer::handle_fq_identifier, octave_base_lexer::enable_fq_identifier): Likewise. (FQ_IDENT_START, FQIDENT): New exclusive start condition and regex to handle fully-qualified identifier. (superclass reference rule, metaclass query rule): Use FQIDENT. (octave_base_lexer::handle_superclass_identifier, octave_base_lexer::handle_meta_identifier): Don't split package name. * parse.h (octave_base_parser::make_superclass_ref, octave_base_parser::make_meta_class_query): Remove package_nm argument. * pt-classdef.h (tree_classdef_superclass::id, tree_classdef_superclass::pkg): Removed members, replaced by cls_name. (tree_classdef_superclass::~tree_classdef_superclass): Remove deletion of removed members. (tree_classdef_superclass::cls_name): New member. (tree_classdef_superclass::tree_classdef_superclass): Initialize it. (tree_classdef_superclass::ident, tree_classdef_superclass::package): Removed methods. (tree_classdef_superclass::class_name): New method. * token.h (token::meta_name_token): Remove enum value. (token(int, std::string, std::string, std::string, int, int)): Remove constructor. (token::superclass_package_name, token::meta_package_name, token::meta_class_name): Remove methods. (token::sc::package_nm, token::mc): Remove union members. * token.cc (token(int, std::string, std::string, std::string, int, int)): Remove constructor. (token::~token): Remove case of meta_name_token. (token::superclass_package_name, token::meta_package_name, token::meta_class_name): Remove methods. * oct-parse.in.yy (FQ_IDENT): New terminal. (superclass_identifier): Adapt to changes in class token and class octave_base_parser. (meta_identifier): Likewise. (superclass_list): Add mid-rule to enable fully-qualified identifier. (superclass): Use FQ_IDENT. (octave_base_parser::make_superclass_ref, octave_base_parser::make_meta_class_query): Remove package_nm argument. * ov-classdef.cc (octave_classdef_superclass_ref::do_multi_index_op): Adapt to removal of package_nm argument. (F__meta_class_query__): Likewise. (cdef_class::make_meta_class): Adapt to changes in class tree_classdef_superclass.
author Michael Goffioul <michael.goffioul@gmail.com>
date Sat, 22 Feb 2014 19:56:17 -0500
parents 4e0d72145c5a
children f958e8cd6348
line wrap: on
line diff
--- a/libinterp/parse-tree/lex.ll	Sat Feb 22 08:23:26 2014 -0800
+++ b/libinterp/parse-tree/lex.ll	Sat Feb 22 19:56:17 2014 -0500
@@ -54,6 +54,8 @@
 %x DQ_STRING_START
 %x SQ_STRING_START
 
+%x FQ_IDENT_START
+
 %{
 
 #include <cctype>
@@ -311,6 +313,7 @@
 Im      [iIjJ]
 CCHAR   [#%]
 IDENT   ([_$a-zA-Z][_$a-zA-Z0-9]*)
+FQIDENT ({IDENT}(\.{IDENT})*)
 EXPON   ([DdEe][+-]?{D}+)
 NUMBER  (({D}+\.?{D}*{EXPON}?)|(\.{D}+{EXPON}?)|(0[xX][0-9a-fA-F]+))
 
@@ -1031,6 +1034,35 @@
   }
 
 %{
+// Fully-qualified identifiers (used for classdef).
+%}
+
+<FQ_IDENT_START>{FQIDENT} {
+    curr_lexer->lexer_debug ("<FQ_IDENT_START>{FQIDENT}");
+    curr_lexer->pop_start_state ();
+
+    int id_tok = curr_lexer->handle_fq_identifier ();
+
+    if (id_tok >= 0)
+      {
+        curr_lexer->looking_for_object_index = true;
+
+        return curr_lexer->count_token_internal (id_tok);
+      }
+  }
+
+<FQ_IDENT_START>{S}+ {
+    curr_lexer->current_input_column += yyleng;
+
+    curr_lexer->mark_previous_token_trailing_space ();
+  }
+
+<FQ_IDENT_START>. {
+    yyless (0);
+    curr_lexer->pop_start_state ();
+  }
+
+%{
 // Imaginary numbers.
 %}
 
@@ -1168,9 +1200,8 @@
 // Superclass method identifiers.
 %}
 
-{IDENT}@{IDENT} |
-{IDENT}@{IDENT}\.{IDENT} {
-    curr_lexer->lexer_debug ("{IDENT}@{IDENT}|{IDENT}@{IDENT}\\.{IDENT}");
+{IDENT}@{FQIDENT} {
+    curr_lexer->lexer_debug ("{IDENT}@{FQIDENT}");
 
     if (curr_lexer->previous_token_may_be_command ())
       {
@@ -1194,9 +1225,8 @@
 // Metaclass query
 %}
 
-\?{IDENT} |
-\?{IDENT}\.{IDENT} {
-    curr_lexer->lexer_debug ("\\?{IDENT}|\\?{IDENT}\\.{IDENT}");
+\?{FQIDENT} {
+    curr_lexer->lexer_debug ("\\?{FQIDENT}");
 
     if (curr_lexer->previous_token_may_be_command ()
         &&  curr_lexer->space_follows_previous_token ())
@@ -2560,6 +2590,34 @@
 }
 
 bool
+octave_base_lexer::fq_identifier_contains_keyword (const std::string& s)
+{
+  size_t p1 = 0;
+  size_t p2;
+
+  std::string s_part;
+
+  do
+    {
+      p2 = s.find ('.', p1);
+
+      if (p2 != std::string::npos)
+        {
+          s_part = s.substr (p1, p2 - p1);
+          p1 = p2 + 1;
+        }
+      else
+        s_part = s.substr (p1);
+
+      if (is_keyword_token (s_part))
+        return true;
+    }
+  while (p2 != std::string::npos);
+
+  return false;
+}
+
+bool
 octave_base_lexer::whitespace_is_significant (void)
 {
   return (nesting_level.is_bracket ()
@@ -2737,23 +2795,16 @@
   std::string cls = meth.substr (pos + 1);
   meth = meth.substr (0, pos);
 
-  std::string pkg;
-  pos = cls.find (".");
-  if (pos != std::string::npos)
-    {
-      pkg = cls.substr (0, pos);
-      cls = cls.substr (pos + 1);
-    }
-
-  int kw_token = (is_keyword_token (meth) || is_keyword_token (cls)
-                  || is_keyword_token (pkg));
+  bool kw_token = (is_keyword_token (meth)
+                   || fq_identifier_contains_keyword (cls));
+
   if (kw_token)
     {
       error ("method, class, and package names may not be keywords");
       return LEXICAL_ERROR;
     }
 
-  push_token (new token (SUPERCLASSREF, meth, pkg, cls,
+  push_token (new token (SUPERCLASSREF, meth, cls,
                          input_line_number, current_input_column));
 
   current_input_column += flex_yyleng ();
@@ -2766,22 +2817,13 @@
 {
   std::string cls = std::string(flex_yytext ()).substr (1);
 
-  std::string pkg;
-  size_t pos = cls.find (".");
-  if (pos != std::string::npos)
-    {
-      pkg = cls.substr (0, pos);
-      cls = cls.substr (pos + 1);
-    }
-
-  int kw_token = is_keyword_token (cls) || is_keyword_token (pkg);
-  if (kw_token)
+  if (fq_identifier_contains_keyword (cls))
     {
       error ("class and package names may not be keywords");
       return LEXICAL_ERROR;
     }
 
-  push_token (new token (METAQUERY, pkg, cls, input_line_number,
+  push_token (new token (METAQUERY, cls, input_line_number,
                          current_input_column));
 
   current_input_column += flex_yyleng ();
@@ -2789,6 +2831,25 @@
   return METAQUERY;
 }
 
+int
+octave_base_lexer::handle_fq_identifier (void)
+{
+  std::string tok = flex_yytext ();
+
+  if (fq_identifier_contains_keyword (tok))
+    {
+      error ("function, method, class, and package names may not be keywords");
+      return LEXICAL_ERROR;
+    }
+
+  push_token (new token (FQ_IDENT, tok, input_line_number,
+                         current_input_column));
+
+  current_input_column += flex_yyleng ();
+
+  return FQ_IDENT;
+}
+
 // Figure out exactly what kind of token to return when we have seen
 // an identifier.  Handles keywords.  Return -1 if the identifier
 // should be ignored.
@@ -3346,6 +3407,12 @@
   return tok;
 }
 
+void
+octave_base_lexer::enable_fq_identifier (void)
+{
+  push_start_state (FQ_IDENT_START);
+}
+
 int
 octave_lexer::fill_flex_buffer (char *buf, unsigned max_size)
 {