changeset 22856:d5b58e234c37

lex.h: Remove wrapper class "token_cache" for std::deque. * libinterp/parse-tree/lex.h: Remove wrapper class "token_cache". It contains lots of never used functions and it all boild down to the basic usage of std::deque in combination with checks for emptyness. The size restriction is now managed by the STL container (as it would happen anyway, because std::deque was wrapped). * libinterp/parse-tree/lex.h: Update the only usages of that class and clarify the code mostly to single lines.
author Kai T. Ohlhus <k.ohlhus@gmail.com>
date Fri, 02 Dec 2016 14:52:09 +0100
parents f9fdd2f66514
children d90e0c79aa45
files libinterp/parse-tree/lex.h libinterp/parse-tree/lex.ll
diffstat 2 files changed, 16 insertions(+), 102 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/parse-tree/lex.h	Fri Dec 02 02:44:29 2016 -0800
+++ b/libinterp/parse-tree/lex.h	Fri Dec 02 14:52:09 2016 +0100
@@ -177,88 +177,6 @@
       std::stack<int> context;
     };
 
-    class token_cache
-    {
-    public:
-
-      // Store an "unlimited" number of tokens.
-      token_cache (size_t sz_arg = std::numeric_limits<size_t>::max ())
-        : buffer (), sz (sz_arg)
-      { }
-
-      void push (token *tok)
-      {
-        if (buffer.size () == sz)
-          pop ();
-
-        buffer.push_front (tok);
-      }
-
-      void pop (void)
-      {
-        if (! empty ())
-          {
-            delete buffer.back ();
-            buffer.pop_back ();
-          }
-      }
-
-      // Direct access.
-      token *at (size_t n)
-      {
-        return empty () ? 0 : buffer.at (n);
-      }
-
-      const token *at (size_t n) const
-      {
-        return empty () ? 0 : buffer.at (n);
-      }
-
-      // Most recently pushed.
-      token *front (void)
-      {
-        return empty () ? 0 : buffer.front ();
-      }
-
-      const token *front (void) const
-      {
-        return empty () ? 0 : buffer.front ();
-      }
-
-      token *back (void)
-      {
-        return empty () ? 0 : buffer.back ();
-      }
-
-      const token *back (void) const
-      {
-        return empty () ? 0 : buffer.back ();
-      }
-
-      // Number of elements currently in the buffer, max of sz.
-      size_t size (void) const { return buffer.size (); }
-
-      bool empty (void) const { return buffer.empty (); }
-
-      void clear (void)
-      {
-        while (! empty ())
-          pop ();
-      }
-
-    private:
-
-      std::deque<token *> buffer;
-
-      size_t sz;
-
-      // No copying!
-
-      token_cache (const token_cache&);
-
-      token_cache& operator = (const token_cache&);
-    };
-
     lexical_feedback (void)
       : end_of_input (false), at_beginning_of_statement (true),
         looking_at_anon_fcn_args (false), looking_at_return_list (false),
@@ -452,7 +370,7 @@
     bbp_nesting_level nesting_level;
 
     // Tokens generated by the lexer.
-    token_cache tokens;
+    std::deque<token *> tokens;
 
   private:
 
@@ -852,4 +770,3 @@
 }
 
 #endif
-
--- a/libinterp/parse-tree/lex.ll	Fri Dec 02 02:44:29 2016 -0800
+++ b/libinterp/parse-tree/lex.ll	Fri Dec 02 14:52:09 2016 +0100
@@ -2145,30 +2145,26 @@
   int
   lexical_feedback::previous_token_value (void) const
   {
-    const token *tok = tokens.front ();
-    return tok ? tok->token_value () : 0;
+    return tokens.empty () ? 0 : tokens.front ()->token_value ();
   }
 
   bool
   lexical_feedback::previous_token_value_is (int tok_val) const
   {
-    const token *tok = tokens.front ();
-    return tok ? tok->token_value_is (tok_val) : false;
+    return tokens.empty () ? false : tokens.front ()->token_value_is (tok_val);
   }
 
   void
   lexical_feedback::mark_previous_token_trailing_space (void)
   {
-    token *tok = tokens.front ();
-    if (tok && ! previous_token_value_is ('\n'))
-      tok->mark_trailing_space ();
+    if (! tokens.empty () && ! previous_token_value_is ('\n'))
+      tokens.front ()->mark_trailing_space ();
   }
 
   bool
   lexical_feedback::space_follows_previous_token (void) const
   {
-    const token *tok = tokens.front ();
-    return tok ? tok->space_follows_token () : false;
+    return tokens.empty () ? false : tokens.front ()->space_follows_token ();
   }
 
   bool
@@ -2194,24 +2190,25 @@
   bool
   lexical_feedback::previous_token_is_keyword (void) const
   {
-    const token *tok = tokens.front ();
-    return tok ? tok->is_keyword () : false;
+    return tokens.empty () ? false : tokens.front ()->is_keyword ();
   }
 
   bool
   lexical_feedback::previous_token_may_be_command (void) const
   {
-    const token *tok = tokens.front ();
-    return tok ? tok->may_be_command () : false;
+    return tokens.empty () ? false : tokens.front ()->may_be_command ();
   }
 
   void
   lexical_feedback::maybe_mark_previous_token_as_variable (void)
   {
-    token *tok = tokens.front ();
-
-    if (tok && tok->is_symbol ())
-      pending_local_variables.insert (tok->symbol_name ());
+    if (! tokens.empty ())
+    {
+      token *tok = tokens.front ();
+
+      if (tok->is_symbol ())
+        pending_local_variables.insert (tok->symbol_name ());
+    }
   }
 
   void
@@ -3203,7 +3200,7 @@
   {
     YYSTYPE *lval = yyget_lval (scanner);
     lval->tok_val = tok;
-    tokens.push (tok);
+    tokens.push_front (tok);
   }
 
   token *