]> git.mdlowis.com Git - archive/parse-utils.git/commitdiff
Created imarker class for use in buffered streams ind btparser
authorMike D. Lowis <mike@mdlowis.com>
Fri, 22 Jun 2012 17:51:10 +0000 (13:51 -0400)
committerMike D. Lowis <mike@mdlowis.com>
Fri, 22 Jun 2012 17:51:10 +0000 (13:51 -0400)
source/marker/imarker.cpp [new file with mode: 0644]
source/marker/imarker.h [new file with mode: 0644]
source/parser/btparser/btparser.cpp
source/parser/btparser/btparser.h

diff --git a/source/marker/imarker.cpp b/source/marker/imarker.cpp
new file mode 100644 (file)
index 0000000..694406b
--- /dev/null
@@ -0,0 +1,41 @@
+#include "imarker.h"
+
+void IMarker::advance(void)
+{
+    cur_location++;
+}
+
+unsigned int IMarker::location(void)
+{
+    return cur_location;
+}
+
+void IMarker::location(unsigned int index)
+{
+    cur_location = index;
+}
+
+unsigned int IMarker::mark(void)
+{
+    unsigned int index = location();
+    markers.push_back( index );
+    return index;
+}
+
+void IMarker::release(void)
+{
+    unsigned int marker = markers.back();
+    markers.pop_back();
+    seek(marker);
+}
+
+void IMarker::seek(unsigned int index)
+{
+    location( index );
+}
+
+bool IMarker::isMarked(void)
+{
+    return (markers.size() > 0);
+}
+
diff --git a/source/marker/imarker.h b/source/marker/imarker.h
new file mode 100644 (file)
index 0000000..2dab5f6
--- /dev/null
@@ -0,0 +1,21 @@
+#ifndef ISTREAM_H
+#define ISTREAM_H
+
+#include <vector>
+
+class IMarker
+{
+    protected:
+        unsigned int cur_location;
+        std::vector<unsigned int> markers;
+    public:
+        void advance(void);
+        unsigned int location(void);
+        void location(unsigned int index);
+        unsigned int mark(void);
+        void release(void);
+        void seek(unsigned int index);
+        bool isMarked(void);
+};
+
+#endif
index 4e3a8d5c2db3e5345764e4676466fe06c4d3b551..a914bb8764d29aaf9ca0f1cbcb0b34e657118ae9 100644 (file)
@@ -3,7 +3,7 @@
 
 using namespace std;
 
-BTParser::BTParser() : current(0)
+BTParser::BTParser()
 {
 }
 
@@ -13,10 +13,10 @@ BTParser::~BTParser()
 
 void BTParser::consume(void)
 {
-    current++;
-    if((current == lookahead.size()) && !isSpeculating())
+    advance();
+    if((location() == lookahead.size()) && !isMarked())
     {
-        current = 0;
+        seek(0);
         lookahead.clear();
     }
     sync(1);
@@ -24,7 +24,7 @@ void BTParser::consume(void)
 
 void BTParser::sync(unsigned int i)
 {
-    unsigned int next_index = current + i - 1;
+    unsigned int next_index = location() + i - 1;
     unsigned int max_index = (lookahead.size() - 1);
 
     if( lookahead.size() == 0 )
@@ -63,7 +63,7 @@ void BTParser::match(TokenType_T type)
 Token& BTParser::lookaheadToken(unsigned int i)
 {
     sync(i);
-    return lookahead.at( current + i - 1 );
+    return lookahead.at( location() + i - 1 );
 }
 
 TokenType_T BTParser::lookaheadType(unsigned int i)
@@ -71,26 +71,3 @@ TokenType_T BTParser::lookaheadType(unsigned int i)
     return lookaheadToken(i).type();
 }
 
-unsigned int BTParser::mark(void)
-{
-    markers.push_back(current);
-    return current;
-}
-
-void BTParser::release(void)
-{
-    unsigned int marker = markers.back();
-    markers.pop_back();
-    seek(marker);
-}
-
-void BTParser::seek(unsigned int index)
-{
-    current = index;
-}
-
-bool BTParser::isSpeculating(void)
-{
-    return (markers.size() > 0);
-}
-
index e116f69ebc1fde3662ccd3df74060acbcf7943e6..e5540b0f45f909452921544addd7c89f7fdab41e 100644 (file)
@@ -3,17 +3,16 @@
 
 #include <exception>
 #include <vector>
+#include "imarker.h"
 #include "iparser.h"
 
-class BTParser : public IParser
+class BTParser : public IMarker, public IParser
 {
     protected:
-        unsigned int current;
-        std::vector<unsigned int> markers;
         std::vector<Token> lookahead;
     public:
         BTParser();
-        ~BTParser();
+        virtual ~BTParser();
 
         void consume(void);
         void sync(unsigned int i);
@@ -21,10 +20,6 @@ class BTParser : public IParser
         void match(TokenType_T type);
         Token& lookaheadToken(unsigned int i);
         TokenType_T lookaheadType(unsigned int i);
-        unsigned int mark(void);
-        void release(void);
-        void seek(unsigned int index);
-        bool isSpeculating(void);
 };
 
 #endif