changeset 16193:d7392bf42fd1

use inheritance to simplify initialization in octave_lexer constructor * lex.h (lexical_feedback): New class containing feedback data for the lexer that was previously contained directly in octave_lexer. (octave_lexer): Inherit from lexical_feedback.
author John W. Eaton <jwe@octave.org>
date Tue, 05 Mar 2013 10:19:38 -0500
parents 741dbca67d80
children b7ca669af528
files libinterp/parse-tree/lex.h libinterp/parse-tree/lex.ll
diffstat 2 files changed, 167 insertions(+), 133 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/parse-tree/lex.h
+++ b/libinterp/parse-tree/lex.h
@@ -56,7 +56,7 @@
 // For communication between the lexer and parser.
 
 class
-octave_lexer
+lexical_feedback
 {
 public:
 
@@ -77,7 +77,6 @@
       NEWLINE = 4
     };
 
-
   // Track nesting of square brackets, curly braces, and parentheses.
 
   class bbp_nesting_level
@@ -152,6 +151,151 @@
 
     std::stack<int> context;
   };
+  
+  lexical_feedback (void)
+    : end_of_input (false), convert_spaces_to_comma (true),
+      do_comma_insert (false), at_beginning_of_statement (true),
+      looking_at_anon_fcn_args (false), looking_at_return_list (false),
+      looking_at_parameter_list (false), looking_at_decl_list (false),
+      looking_at_initializer_expression (false),
+      looking_at_matrix_or_assign_lhs (false),
+      looking_for_object_index (false), 
+      looking_at_indirect_ref (false), parsing_class_method (false),
+      maybe_classdef_get_set_method (false), parsing_classdef (false),
+      quote_is_transpose (false),
+      input_line_number (1), current_input_column (1),
+      bracketflag (0), braceflag (0),
+      looping (0), defining_func (0), looking_at_function_handle (0),
+      block_comment_nesting_level (0),
+      looking_at_object_index (), parsed_function_name (),
+      pending_local_variables (), nesting_level (), token_stack ()
+  {
+    init ();
+  }
+
+  ~lexical_feedback (void);
+
+  void init (void);
+  
+  // true means that we have encountered eof on the input stream.
+  bool end_of_input;
+
+  // true means that we should convert spaces to a comma inside a
+  // matrix definition.
+  bool convert_spaces_to_comma;
+
+  // gag.  stupid kludge so that [[1,2][3,4]] will work.
+  bool do_comma_insert;
+
+  // true means we are at the beginning of a statement, where a
+  // command name is possible.
+  bool at_beginning_of_statement;
+
+  // true means we are parsing an anonymous function argument list.
+  bool looking_at_anon_fcn_args;
+
+  // true means we're parsing the return list for a function.
+  bool looking_at_return_list;
+
+  // true means we're parsing the parameter list for a function.
+  bool looking_at_parameter_list;
+
+  // true means we're parsing a declaration list (global or
+  // persistent).
+  bool looking_at_decl_list;
+
+  // true means we are looking at the initializer expression for a
+  // parameter list element.
+  bool looking_at_initializer_expression;
+
+  // true means we're parsing a matrix or the left hand side of
+  // multi-value assignment statement.
+  bool looking_at_matrix_or_assign_lhs;
+
+  // object index not possible until we've seen something.
+  bool looking_for_object_index;
+
+  // true means we're looking at an indirect reference to a
+  // structure element.
+  bool looking_at_indirect_ref;
+
+  // true means we are parsing a class method in function or classdef file.
+  bool parsing_class_method;
+
+  // true means we are parsing a class method declaration line in a
+  // classdef file and can accept a property get or set method name.
+  // for example, "get.propertyname" is recognized as a function name.
+  bool maybe_classdef_get_set_method;
+
+  // true means we are parsing a classdef file
+  bool parsing_classdef;
+
+  // return transpose or start a string?
+  bool quote_is_transpose;
+
+  // the current input line number.
+  int input_line_number;
+
+  // the column of the current token.
+  int current_input_column;
+
+  // square bracket level count.
+  int bracketflag;
+
+  // curly brace level count.
+  int braceflag;
+
+  // true means we're in the middle of defining a loop.
+  int looping;
+
+  // nonzero means we're in the middle of defining a function.
+  int defining_func;
+
+  // nonzero means we are parsing a function handle.
+  int looking_at_function_handle;
+
+  // nestng level for blcok comments.
+  int block_comment_nesting_level;
+
+  // if the front of the list is true, the closest paren, brace, or
+  // bracket nesting is an index for an object.
+  std::list<bool> looking_at_object_index;
+
+  // if the top of the stack is true, then we've already seen the name
+  // of the current function.  should only matter if
+  // current_function_level > 0
+  std::stack<bool> parsed_function_name;
+
+  // set of identifiers that might be local variable names.
+  std::set<std::string> pending_local_variables;
+
+  // is the closest nesting level a square bracket, squiggly brace or
+  // a paren?
+  bbp_nesting_level nesting_level;
+
+  // Stack to hold tokens so that we can delete them when the parser is
+  // reset and avoid growing forever just because we are stashing some
+  // information.
+  std::stack <token*> token_stack;
+
+private:
+
+  // No copying!
+
+  lexical_feedback (const lexical_feedback&);
+
+  lexical_feedback& operator = (const lexical_feedback&);
+};
+
+// octave_lexer inherits from lexical_feedback because we will
+// eventually have several different constructors and it is easier to
+// intialize if everything is grouped in a parent class rather than
+// listing all the members in the octave_lexer class.
+
+class
+octave_lexer : public lexical_feedback
+{
+public:
 
   // Handle buffering of input for lexer.
 
@@ -182,23 +326,7 @@
   };
 
   octave_lexer (void)
-    : scanner (0), end_of_input (false), convert_spaces_to_comma (true),
-      do_comma_insert (false), at_beginning_of_statement (true),
-      looking_at_anon_fcn_args (false), looking_at_return_list (false),
-      looking_at_parameter_list (false), looking_at_decl_list (false),
-      looking_at_initializer_expression (false),
-      looking_at_matrix_or_assign_lhs (false),
-      looking_for_object_index (false), 
-      looking_at_indirect_ref (false), parsing_class_method (false),
-      maybe_classdef_get_set_method (false), parsing_classdef (false),
-      quote_is_transpose (false),
-      input_line_number (1), current_input_column (1),
-      bracketflag (0), braceflag (0),
-      looping (0), defining_func (0), looking_at_function_handle (0),
-      block_comment_nesting_level (0),
-      looking_at_object_index (), parsed_function_name (),
-      pending_local_variables (), nesting_level (), input_buf (),
-      token_stack ()
+    : lexical_feedback (), scanner (0), input_buf ()
   {
     init ();
   }
@@ -307,102 +435,6 @@
   // Internal state of the flex-generated lexer.
   void *scanner;
 
-  // TRUE means that we have encountered EOF on the input stream.
-  bool end_of_input;
-
-  // TRUE means that we should convert spaces to a comma inside a
-  // matrix definition.
-  bool convert_spaces_to_comma;
-
-  // GAG.  Stupid kludge so that [[1,2][3,4]] will work.
-  bool do_comma_insert;
-
-  // TRUE means we are at the beginning of a statement, where a
-  // command name is possible.
-  bool at_beginning_of_statement;
-
-  // TRUE means we are parsing an anonymous function argument list.
-  bool looking_at_anon_fcn_args;
-
-  // TRUE means we're parsing the return list for a function.
-  bool looking_at_return_list;
-
-  // TRUE means we're parsing the parameter list for a function.
-  bool looking_at_parameter_list;
-
-  // TRUE means we're parsing a declaration list (global or
-  // persistent).
-  bool looking_at_decl_list;
-
-  // TRUE means we are looking at the initializer expression for a
-  // parameter list element.
-  bool looking_at_initializer_expression;
-
-  // TRUE means we're parsing a matrix or the left hand side of
-  // multi-value assignment statement.
-  bool looking_at_matrix_or_assign_lhs;
-
-  // Object index not possible until we've seen something.
-  bool looking_for_object_index;
-
-  // TRUE means we're looking at an indirect reference to a
-  // structure element.
-  bool looking_at_indirect_ref;
-
-  // TRUE means we are parsing a class method in function or classdef file.
-  bool parsing_class_method;
-
-  // TRUE means we are parsing a class method declaration line in a
-  // classdef file and can accept a property get or set method name.
-  // For example, "get.PropertyName" is recognized as a function name.
-  bool maybe_classdef_get_set_method;
-
-  // TRUE means we are parsing a classdef file
-  bool parsing_classdef;
-
-  // Return transpose or start a string?
-  bool quote_is_transpose;
-
-  // The current input line number.
-  int input_line_number;
-
-  // The column of the current token.
-  int current_input_column;
-
-  // Square bracket level count.
-  int bracketflag;
-
-  // Curly brace level count.
-  int braceflag;
-
-  // TRUE means we're in the middle of defining a loop.
-  int looping;
-
-  // Nonzero means we're in the middle of defining a function.
-  int defining_func;
-
-  // Nonzero means we are parsing a function handle.
-  int looking_at_function_handle;
-
-  // Nestng level for blcok comments.
-  int block_comment_nesting_level;
-
-  // If the front of the list is TRUE, the closest paren, brace, or
-  // bracket nesting is an index for an object.
-  std::list<bool> looking_at_object_index;
-
-  // If the top of the stack is TRUE, then we've already seen the name
-  // of the current function.  Should only matter if
-  // current_function_level > 0
-  std::stack<bool> parsed_function_name;
-
-  // Set of identifiers that might be local variable names.
-  std::set<std::string> pending_local_variables;
-
-  // Is the closest nesting level a square bracket, squiggly brace or
-  // a paren?
-  bbp_nesting_level nesting_level;
-
   // Object that reads and buffers input.
   input_buffer input_buf;
 
@@ -411,11 +443,6 @@
 
 private:
 
-  // Stack to hold tokens so that we can delete them when the parser is
-  // reset and avoid growing forever just because we are stashing some
-  // information.
-  std::stack <token*> token_stack;
-
   // No copying!
 
   octave_lexer (const octave_lexer&);
--- a/libinterp/parse-tree/lex.ll
+++ b/libinterp/parse-tree/lex.ll
@@ -1292,6 +1292,26 @@
   char *buf;
 };
 
+lexical_feedback::~lexical_feedback (void)
+{
+  // Clear out the stack of token info used to track line and
+  // column numbers.
+
+  while (! token_stack.empty ())
+    {
+      delete token_stack.top ();
+      token_stack.pop ();
+    }
+}
+
+void
+lexical_feedback::init (void)
+{
+  // The closest paren, brace, or bracket nesting is not an object
+  // index.
+  looking_at_object_index.push_front (false);
+}
+
 void
 octave_lexer::input_buffer::read (void)
 {
@@ -1337,25 +1357,12 @@
 
 octave_lexer::~octave_lexer (void)
 {
-  // Clear out the stack of token info used to track line and
-  // column numbers.
-
-  while (! token_stack.empty ())
-    {
-      delete token_stack.top ();
-      token_stack.pop ();
-    }
-
   yylex_destroy (scanner);
 }
 
 void
 octave_lexer::init (void)
 {
-  // The closest paren, brace, or bracket nesting is not an object
-  // index.
-  looking_at_object_index.push_front (false);
-
   yylex_init (&scanner);
 
   // Make octave_lexer object available through yyextra in