From e2303f5540e09fd42077c4afd4ecb793f965b988 Mon Sep 17 00:00:00 2001 From: "Mike D. Lowis" Date: Fri, 22 Jun 2012 13:51:10 -0400 Subject: [PATCH] Created imarker class for use in buffered streams ind btparser --- source/marker/imarker.cpp | 41 +++++++++++++++++++++++++++++ source/marker/imarker.h | 21 +++++++++++++++ source/parser/btparser/btparser.cpp | 35 +++++------------------- source/parser/btparser/btparser.h | 11 +++----- 4 files changed, 71 insertions(+), 37 deletions(-) create mode 100644 source/marker/imarker.cpp create mode 100644 source/marker/imarker.h diff --git a/source/marker/imarker.cpp b/source/marker/imarker.cpp new file mode 100644 index 0000000..694406b --- /dev/null +++ b/source/marker/imarker.cpp @@ -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 index 0000000..2dab5f6 --- /dev/null +++ b/source/marker/imarker.h @@ -0,0 +1,21 @@ +#ifndef ISTREAM_H +#define ISTREAM_H + +#include + +class IMarker +{ + protected: + unsigned int cur_location; + std::vector 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 diff --git a/source/parser/btparser/btparser.cpp b/source/parser/btparser/btparser.cpp index 4e3a8d5..a914bb8 100644 --- a/source/parser/btparser/btparser.cpp +++ b/source/parser/btparser/btparser.cpp @@ -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); -} - diff --git a/source/parser/btparser/btparser.h b/source/parser/btparser/btparser.h index e116f69..e5540b0 100644 --- a/source/parser/btparser/btparser.h +++ b/source/parser/btparser/btparser.h @@ -3,17 +3,16 @@ #include #include +#include "imarker.h" #include "iparser.h" -class BTParser : public IParser +class BTParser : public IMarker, public IParser { protected: - unsigned int current; - std::vector markers; std::vector 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 -- 2.52.0