From c35da35344b23fceb5141e03ccc94b841558f472 Mon Sep 17 00:00:00 2001 From: "Mike D. Lowis" Date: Fri, 9 Mar 2012 17:13:47 -0500 Subject: [PATCH] Refactored to clean up interfaces and better reflect the visitor pattern --- source/parser/btparser/btparser.cpp | 26 +----------- source/parser/btparser/btparser.h | 13 +----- source/parser/iparser.cpp | 29 ++++++++------ source/parser/iparser.h | 16 ++++---- source/parser/llkparser/llkparser.cpp | 36 ----------------- source/parser/llkparser/llkparser.h | 6 --- source/visitor/astprinter/astprinter.cpp | 17 +++----- source/visitor/astprinter/astprinter.h | 5 --- source/visitor/ivisitor.cpp | 50 ++++++++++++++---------- source/visitor/ivisitor.h | 13 ++---- 10 files changed, 68 insertions(+), 143 deletions(-) diff --git a/source/parser/btparser/btparser.cpp b/source/parser/btparser/btparser.cpp index 8213468..0981361 100644 --- a/source/parser/btparser/btparser.cpp +++ b/source/parser/btparser/btparser.cpp @@ -1,36 +1,12 @@ #include "btparser.h" #include "exception.h" -BTParser::BTParser(ILexer* lxer) : lexer(lxer), current(0) +BTParser::BTParser() : current(0) { } BTParser::~BTParser() { - if(lexer != NULL) - { - delete lexer; - // Input stream was deleted with the lexer so null it out - IParser::setInput((istream*)NULL); - } -} - -void BTParser::setInput(char* in) -{ - IParser::setInput(in); - lexer->setInput(in); -} - -void BTParser::setInput(string& in) -{ - IParser::setInput(in); - lexer->setInput(in); -} - -void BTParser::setInput(istream* in) -{ - IParser::setInput(in); - lexer->setInput(in); } void BTParser::consume(void) diff --git a/source/parser/btparser/btparser.h b/source/parser/btparser/btparser.h index 5d94cb3..e116f69 100644 --- a/source/parser/btparser/btparser.h +++ b/source/parser/btparser/btparser.h @@ -4,24 +4,17 @@ #include #include #include "iparser.h" -#include "ilexer.h" -#include "ast.h" class BTParser : public IParser { - private: - ILexer* lexer; + protected: unsigned int current; std::vector markers; std::vector lookahead; public: - BTParser(ILexer* lxer); + BTParser(); ~BTParser(); - void setInput(char* in); - void setInput(string& in); - void setInput(istream* in); - void consume(void); void sync(unsigned int i); void fill(unsigned int n); @@ -32,8 +25,6 @@ class BTParser : public IParser void release(void); void seek(unsigned int index); bool isSpeculating(void); - - virtual AST* parse(void) = 0; }; #endif diff --git a/source/parser/iparser.cpp b/source/parser/iparser.cpp index a6ccb2a..7985d61 100644 --- a/source/parser/iparser.cpp +++ b/source/parser/iparser.cpp @@ -25,31 +25,38 @@ using namespace std; /****************************************************************************** * Public Functions *****************************************************************************/ -IParser::IParser() : input(NULL) +IParser::IParser() : result(NULL), lexer(NULL) +{ +} + +IParser::IParser(ILexer* lxr) : result(NULL), lexer(lxr) { } IParser::~IParser() { - if(input != NULL) + if(lexer != NULL) + { + delete lexer; + } + + if(result != NULL) { - delete input; + delete result; } } -void IParser::setInput(char* in) +void IParser::input(ILexer* lxr) { - input = new istringstream( string( in ) ); + lexer = lxr; } -void IParser::setInput(string& in) +const AST* IParser::ast() const { - input = new istringstream( in ); + return result; } -void IParser::setInput(istream* in) +void IParser::process(IVisitor& visitor) { - input = in; + visitor.visit(result); } - - diff --git a/source/parser/iparser.h b/source/parser/iparser.h index 47b75c1..747778d 100644 --- a/source/parser/iparser.h +++ b/source/parser/iparser.h @@ -20,21 +20,23 @@ #include #include #include "ast.h" +#include "ilexer.h" #include "ivisitor.h" using namespace std; class IParser { - private: - istream* input; + protected: + AST* result; + ILexer* lexer; public: IParser(); + IParser(ILexer* in); virtual ~IParser(); - virtual AST* parse() = 0; - - void setInput(char* in); - void setInput(string& in); - void setInput(istream* in); + virtual void parse() = 0; + virtual void input(ILexer* in); + virtual const AST* ast() const; + virtual void process(IVisitor& visitor); }; #endif diff --git a/source/parser/llkparser/llkparser.cpp b/source/parser/llkparser/llkparser.cpp index d7a2469..8412bc3 100644 --- a/source/parser/llkparser/llkparser.cpp +++ b/source/parser/llkparser/llkparser.cpp @@ -15,48 +15,12 @@ LLKParser::LLKParser(int k_val, ILexer* lxer) : k(k_val), next(0), lexer(lxer) LLKParser::~LLKParser() { - if(lexer != NULL) - { - delete lexer; - // Input stream was deleted with th elexer so null it out - IParser::setInput((istream*)NULL); - } if (lookahead != NULL) { delete[] lookahead; } } -void LLKParser::setInput(char* in) -{ - IParser::setInput(in); - lexer->setInput(in); - for (int i = 0; i < k; i++) - { - consume(); - } -} - -void LLKParser::setInput(string& in) -{ - IParser::setInput(in); - lexer->setInput(in); - for (int i = 0; i < k; i++) - { - consume(); - } -} - -void LLKParser::setInput(istream* in) -{ - IParser::setInput(in); - lexer->setInput(in); - for (int i = 0; i < k; i++) - { - consume(); - } -} - void LLKParser::consume(void) { if ( lookahead != NULL ) diff --git a/source/parser/llkparser/llkparser.h b/source/parser/llkparser/llkparser.h index 8971cfa..63950e6 100644 --- a/source/parser/llkparser/llkparser.h +++ b/source/parser/llkparser/llkparser.h @@ -16,16 +16,10 @@ class LLKParser : public IParser public: LLKParser(int k_val, ILexer* lxer); ~LLKParser(); - - void setInput(char* in); - void setInput(string& in); - void setInput(istream* in); - void consume(void); void match(TokenType_T type); Token& lookaheadToken(int i); TokenType_T lookaheadType(int i); - virtual AST* parse(void) = 0; }; #endif diff --git a/source/visitor/astprinter/astprinter.cpp b/source/visitor/astprinter/astprinter.cpp index fed7d93..11e434e 100644 --- a/source/visitor/astprinter/astprinter.cpp +++ b/source/visitor/astprinter/astprinter.cpp @@ -1,11 +1,6 @@ +#include #include "astprinter.h" -using namespace std; - -string ASTPrinter::str() -{ - return stream.str(); -} void ASTPrinter::beforeVisit(AST* cur, int depth) { @@ -13,25 +8,25 @@ void ASTPrinter::beforeVisit(AST* cur, int depth) void ASTPrinter::afterVisit(AST* cur, int depth) { - stream << endl; + std::cout << endl; } void ASTPrinter::beforeChildren(AST* cur, int depth) { - stream << "(" << cur->type() << " " << cur->text(); + std::cout << "(" << cur->type() << " " << cur->text(); } void ASTPrinter::afterChildren(AST* cur, int depth) { - stream << ")"; + std::cout << ")"; } void ASTPrinter::beforeChild(AST* cur, int depth) { - stream << endl; + std::cout << endl; for(int i = 0; i< depth; i++) { - stream << " "; + std::cout << " "; } } diff --git a/source/visitor/astprinter/astprinter.h b/source/visitor/astprinter/astprinter.h index e7bfda2..42c053e 100644 --- a/source/visitor/astprinter/astprinter.h +++ b/source/visitor/astprinter/astprinter.h @@ -6,11 +6,6 @@ #include class ASTPrinter : public IVisitor { - protected: - ostringstream stream; - public: - ASTPrinter(AST* root) : IVisitor(root) {}; - string str(); private: void beforeVisit(AST* cur, int depth); void afterVisit(AST* cur, int depth); diff --git a/source/visitor/ivisitor.cpp b/source/visitor/ivisitor.cpp index ef4185c..87839b6 100644 --- a/source/visitor/ivisitor.cpp +++ b/source/visitor/ivisitor.cpp @@ -3,33 +3,41 @@ using namespace std; +IVisitor::IVisitor() +{ +} + +IVisitor::~IVisitor() +{ +} + void IVisitor::visit(AST* cur, int depth) { list* children; list::iterator it; - // If we are just starting then use the global tree - if(cur == NULL) cur = ast; - - // Execute or pre-walk actions - if(depth == 0) beforeVisit( cur, depth ); - - // Setup our locals - children = cur->children(); - it = children->begin(); - - // Visit the tree - beforeChildren(cur,depth); - depth++; - for(; it != children->end(); it++) + if (cur != NULL) { - beforeChild( *it, depth ); - visit( *it, depth ); - afterChild( *it, depth ); + // Execute or pre-walk actions + if(depth == 0) beforeVisit( cur, depth ); + + // Setup our locals + children = cur->children(); + it = children->begin(); + + // Visit the tree + beforeChildren(cur,depth); + depth++; + for(; it != children->end(); it++) + { + beforeChild( *it, depth ); + visit( *it, depth ); + afterChild( *it, depth ); + } + afterChildren(cur,depth); + + // Execute our post-walk actions + if(depth == 1) afterVisit( cur, depth ); } - afterChildren(cur,depth); - - // Execute our post-walk actions - if(depth == 1) afterVisit( cur, depth ); } diff --git a/source/visitor/ivisitor.h b/source/visitor/ivisitor.h index 4d4cb2e..38c1b9c 100644 --- a/source/visitor/ivisitor.h +++ b/source/visitor/ivisitor.h @@ -6,12 +6,10 @@ #include class IVisitor { - protected: - AST* ast; public: - IVisitor(AST* tree) : ast(tree) {}; - ~IVisitor() { delete ast; } - void visit(AST* cur = NULL, int depth = 0); + IVisitor(); + ~IVisitor(); + void visit(AST* cur, int depth = 0); private: virtual void beforeVisit(AST* cur, int depth) = 0; virtual void afterVisit(AST* cur, int depth) = 0; @@ -21,9 +19,4 @@ class IVisitor { virtual void afterChild(AST* cur, int depth) = 0; }; -class IVisitorFactory { - public: - virtual IVisitor* createIVisitor(AST* root) = 0; -}; - #endif -- 2.52.0