changeset 13:d179b0bb85e4

make lexer a member function in parser class and eliminate some more global variables
author John W. Eaton <jwe@octave.org>
date Thu, 23 May 2019 18:26:35 -0400
parents 894be158b32d
children 1e5a1e15fa56
files parser.h parser.yy
diffstat 2 files changed, 18 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/parser.h	Thu May 23 17:57:20 2019 -0400
+++ b/parser.h	Thu May 23 18:26:35 2019 -0400
@@ -29,11 +29,18 @@
 
   private:
 
+    int lexer (double& token_value);
+
     interpreter& m_interpreter;
 
     void *m_parser_state;
 
     bool m_beg_of_stmt;
+
+    // Auxiliary variables for lexing.
+    size_t m_bufptr = 0;
+    size_t m_chunk_size = 0;
+    const char *m_buf;
   };
 }
 
--- a/parser.yy	Thu May 23 17:57:20 2019 -0400
+++ b/parser.yy	Thu May 23 18:26:35 2019 -0400
@@ -39,13 +39,6 @@
   {
     m_interpreter.emit_result (value);
   }
-
-  // For communication between the lexer and parser.
-  size_t bufptr = 0;
-  size_t chunk_size = 0;
-  const char *buf;
-
-  static int yylex (YYSTYPE& token_value);
 }
 
 static void yyerror (calc::parser&, char const *);
@@ -135,24 +128,24 @@
   // if not a number.  It skips all blanks and tabs, and returns -1 for
   // end-of-input.
 
-  static int yylex (YYSTYPE& token_value)
+  int parser::lexer (double& token_value)
   {
     int c;
 
-    if (bufptr >= chunk_size)
+    if (m_bufptr >= m_chunk_size)
       return -1;
 
     // Skip white space.
-    while ((c = buf[bufptr++]) == ' ' || c == '\t' || c == '\n')
+    while ((c = m_buf[m_bufptr++]) == ' ' || c == '\t' || c == '\n')
       ;
 
     // Process numbers.
     if (c == '.' || isdigit (c))
       {
         int chars_read = 0;
-        bufptr--;
-        sscanf (&buf[bufptr], "%lf%n", &token_value, &chars_read);
-        bufptr += chars_read;
+        m_bufptr--;
+        sscanf (&m_buf[m_bufptr], "%lf%n", &token_value, &chars_read);
+        m_bufptr += chars_read;
         return NUM;
       }
 
@@ -162,16 +155,16 @@
 
   int parser::parse_and_execute (const std::string& line)
   {
-    bufptr = 0;
-    chunk_size = line.length ();
-    buf = line.c_str ();
+    m_bufptr = 0;
+    m_chunk_size = line.length ();
+    m_buf = line.c_str ();
 
     int status;
 
     do
       {
-        YYSTYPE token_value;
-        int input_char = yylex (token_value);
+        double token_value;
+        int input_char = lexer (token_value);
 
         if (input_char < 0)
           return -1;