]> git.mdlowis.com Git - archive/parse-utils.git/commitdiff
Refactored to clean up interfaces and better reflect the visitor pattern
authorMike D. Lowis <mike@mdlowis.com>
Fri, 9 Mar 2012 22:13:47 +0000 (17:13 -0500)
committerMike D. Lowis <mike@mdlowis.com>
Fri, 9 Mar 2012 22:13:47 +0000 (17:13 -0500)
source/parser/btparser/btparser.cpp
source/parser/btparser/btparser.h
source/parser/iparser.cpp
source/parser/iparser.h
source/parser/llkparser/llkparser.cpp
source/parser/llkparser/llkparser.h
source/visitor/astprinter/astprinter.cpp
source/visitor/astprinter/astprinter.h
source/visitor/ivisitor.cpp
source/visitor/ivisitor.h

index 8213468c7b71d715713ecfb142d45b59777f784b..0981361dbcfc2d764f1d34663fcb638c4e1f2bd7 100644 (file)
@@ -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)
index 5d94cb3832d182f9d7f675847eb25f79ed9213b8..e116f69ebc1fde3662ccd3df74060acbcf7943e6 100644 (file)
@@ -4,24 +4,17 @@
 #include <exception>
 #include <vector>
 #include "iparser.h"
-#include "ilexer.h"
-#include "ast.h"
 
 class BTParser : public IParser
 {
-    private:
-        ILexer* lexer;
+    protected:
         unsigned int current;
         std::vector<unsigned int> markers;
         std::vector<Token> 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
index a6ccb2a1188537b1b3dd812a064c0806e77d2bc9..7985d61b255220c7a613cf920face3794b8551a1 100644 (file)
@@ -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);
 }
-
-
index 47b75c1fb33c1ac6510d8a632b5d66bc27735948..747778dbefa40bf05963df3df57b55c9281b0ce5 100644 (file)
 #include <string>
 #include <sstream>
 #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
index d7a2469da725e2342d6f9f63ceb264675489a82a..8412bc3adafc373807aaebf2b8174ef84b1027c4 100644 (file)
@@ -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 )
index 8971cfa5a2b276f2370bfe1f5dfb57f145ed4ed1..63950e60ed299b10417a76fcc5680a905872f90e 100644 (file)
@@ -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
index fed7d93c7954e08451c3209edb8293f50890189d..11e434ed937950220280fec772a4c63c64017c1f 100644 (file)
@@ -1,11 +1,6 @@
+#include <iostream>
 #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 << "  ";
     }
 }
 
index e7bfda2a491c8280816978ab9e8d1624dd438edf..42c053e10ceb4ac59a0a26b5ead13b949d5db8a5 100644 (file)
@@ -6,11 +6,6 @@
 #include <sstream>
 
 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);
index ef4185cb5d2f17b11c948630d7d1091238be3647..87839b6ed1944542312d934a071a4143f3b75149 100644 (file)
@@ -3,33 +3,41 @@
 
 using namespace std;
 
+IVisitor::IVisitor()
+{
+}
+
+IVisitor::~IVisitor()
+{
+}
+
 void IVisitor::visit(AST* cur, int depth)
 {
     list<AST*>* children;
     list<AST*>::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 );
 }
 
index 4d4cb2ea1feac852b36e882c18808a44336c7c79..38c1b9c77c210a80d15d27a29d6c4c7eaf04fb02 100644 (file)
@@ -6,12 +6,10 @@
 #include <iostream>
 
 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