]> git.mdlowis.com Git - archive/parse-utils.git/commitdiff
Made consume and match functionality of Ilexer abstract and add LLNLexer class for...
authorMike D. Lowis <mike@mdlowis.com>
Mon, 2 Apr 2012 17:42:59 +0000 (13:42 -0400)
committerMike D. Lowis <mike@mdlowis.com>
Mon, 2 Apr 2012 17:42:59 +0000 (13:42 -0400)
source/lexer/ilexer.cpp
source/lexer/ilexer.h
source/lexer/llnlexer/llnlexer.cpp [new file with mode: 0644]
source/lexer/llnlexer/llnlexer.h [new file with mode: 0644]

index bd6c3331005a008a93925e5b06d81a3258d6629c..66e8645f9658f5467cad88ae1bda4fe495260de8 100644 (file)
@@ -3,7 +3,7 @@
 
 using namespace std;
 
-ILexer::ILexer(istream& in) : line(-1), column(-1), in_stream(in), current(in_stream.get())
+ILexer::ILexer(istream& in) : line(-1), column(-1), in_stream(in)
 {
 }
 
@@ -16,38 +16,3 @@ bool ILexer::eof(void)
     return in_stream.eof();
 }
 
-void ILexer::consume(void)
-{
-    if(in_stream.eof())
-    {
-        current = EOF;
-    }
-    else
-    {
-        current = in_stream.get();
-        if(current == '\n')
-        {
-            line++;
-            column = 0;
-        }
-        else
-        {
-            column++;
-        }
-    }
-}
-
-void ILexer::match(char x) {
-    if ( current == x)
-    {
-        consume();
-    }
-    else
-    {
-        Exception ex(line,column);
-        ex << "Unexpected character. Expected " << x << ", received " << current << ".";
-        throw ex;
-    }
-}
-
-
index 034e8da36368222ed6d518ad89c0a75f979f9316..accf67dbb7d783285c51eaf8597bb6f254c5de7d 100644 (file)
@@ -12,14 +12,13 @@ class ILexer
         int line;
         int column;
         std::istream& in_stream;
-        char current;
 
     public:
         ILexer(std::istream& in);
         virtual ~ILexer();
-        void consume(void);
-        void match(char x);
-        bool eof(void);
+        virtual void consume(void) = 0;
+        virtual void match(char x) = 0;
+        virtual bool eof(void);
         virtual Token next(void) = 0;
 };
 
diff --git a/source/lexer/llnlexer/llnlexer.cpp b/source/lexer/llnlexer/llnlexer.cpp
new file mode 100644 (file)
index 0000000..553f303
--- /dev/null
@@ -0,0 +1,63 @@
+#include "llnlexer.h"
+#include "exception.h"
+
+LLNLexer::LLNLexer(std::istream& in) : ILexer(in), cur_idx(0)
+{
+}
+
+LLNLexer::~LLNLexer()
+{
+}
+
+void LLNLexer::consume(void)
+{
+    cur_idx++;
+    if(cur_idx == la_buffer.size())
+    {
+        cur_idx = 0;
+        la_buffer.clear();
+    }
+    sync(1);
+}
+
+void LLNLexer::match(char match)
+{
+    if( lookahead(1) == match )
+    {
+        consume();
+    }
+    else
+    {
+        Exception ex(line,column);
+        ex << "Unexpected character. Expected '" << match << "', received '" << lookahead(1) << "'.";
+        throw ex;
+    }
+}
+
+void LLNLexer::sync(unsigned int i)
+{
+    unsigned int next_index = cur_idx + i - 1;
+    unsigned int max_index = (la_buffer.size() == 0) ? 0 : (la_buffer.size() - 1);
+    if( next_index >= max_index )
+    {
+        fill( next_index - max_index);
+    }
+}
+
+void LLNLexer::fill(unsigned int n)
+{
+    unsigned int i = 0;
+    for (i = 0; i <= n; i++)
+    {
+        la_buffer.push_back( in_stream.get() );
+    }
+}
+
+char LLNLexer::lookahead(unsigned int i)
+{
+    sync(i);
+    return la_buffer.at( cur_idx + i - 1 );
+}
+
+
+
diff --git a/source/lexer/llnlexer/llnlexer.h b/source/lexer/llnlexer/llnlexer.h
new file mode 100644 (file)
index 0000000..d7f0dbf
--- /dev/null
@@ -0,0 +1,26 @@
+#ifndef LLNLEXER_H
+#define LLNLEXER_H
+
+#include <vector>
+#include "ilexer.h"
+
+class LLNLexer : public ILexer
+{
+    protected:
+        unsigned int cur_idx;
+        std::vector<char> la_buffer;
+
+    public:
+        LLNLexer(std::istream& in);
+        virtual ~LLNLexer();
+
+        void consume(void);
+        void match(char type);
+        void sync(unsigned int i);
+        void fill(unsigned int n);
+        char lookahead(unsigned int i);
+
+        Token next(void) = 0;
+};
+
+#endif