00001 #ifndef INC_TreeParser_hpp__
00002 #define INC_TreeParser_hpp__
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <antlr/config.hpp>
00012 #include <antlr/AST.hpp>
00013 #include <antlr/ASTFactory.hpp>
00014 #include <antlr/BitSet.hpp>
00015 #include <antlr/RecognitionException.hpp>
00016 #include <antlr/MismatchedTokenException.hpp>
00017 #include <antlr/TreeParserSharedInputState.hpp>
00018
00019 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
00020 namespace antlr {
00021 #endif
00022
00023 class ANTLR_API TreeParser {
00024 public:
00025 TreeParser()
00026 : astFactory(0)
00027 , inputState(new TreeParserInputState())
00028 , traceDepth(0)
00029 {
00030 }
00031
00032 TreeParser(const TreeParserSharedInputState& state)
00033 : astFactory(0)
00034 , inputState(state)
00035 , traceDepth(0)
00036 {
00037 }
00038
00039 virtual ~TreeParser()
00040 {
00041 }
00042
00044 virtual RefAST getAST() = 0;
00045
00050 virtual void match(RefAST t, const BitSet& b)
00051 {
00052 if ( !t || t==ASTNULL || !b.member(t->getType()) )
00053 throw MismatchedTokenException( getTokenNames(), getNumTokens(),
00054 t, b, false );
00055 }
00056
00063 virtual void setASTFactory(ASTFactory* factory)
00064 {
00065 astFactory = factory;
00066 }
00068 virtual ASTFactory* getASTFactory() const
00069 {
00070 return astFactory;
00071 }
00073 virtual const char* getTokenName(int num) const = 0;
00075 virtual int getNumTokens() const = 0;
00077 virtual const char* const* getTokenNames() const = 0;
00078
00080 virtual void reportError(const RecognitionException& ex);
00082 virtual void reportError(const ANTLR_USE_NAMESPACE(std)string& s);
00084 virtual void reportWarning(const ANTLR_USE_NAMESPACE(std)string& s);
00085
00087 virtual void traceIndent();
00088 virtual void traceIn(const char* rname, RefAST t);
00089 virtual void traceOut(const char* rname, RefAST t);
00090
00096 static RefAST ASTNULL;
00097
00098 protected:
00099 virtual void match(RefAST t, int ttype)
00100 {
00101 if (!t || t == ASTNULL || t->getType() != ttype )
00102 throw MismatchedTokenException( getTokenNames(), getNumTokens(),
00103 t, ttype, false );
00104 }
00105
00106 virtual void matchNot(RefAST t, int ttype)
00107 {
00108 if ( !t || t == ASTNULL || t->getType() == ttype )
00109 throw MismatchedTokenException( getTokenNames(), getNumTokens(),
00110 t, ttype, true );
00111 }
00112
00114 ASTFactory* astFactory;
00115
00117 TreeParserSharedInputState inputState;
00118
00120 int traceDepth;
00121
00125 class Tracer {
00126 private:
00127 TreeParser* parser;
00128 const char* text;
00129 RefAST tree;
00130 public:
00131 Tracer(TreeParser* p, const char* t, RefAST a)
00132 : parser(p), text(t), tree(a)
00133 {
00134 parser->traceIn(text,tree);
00135 }
00136 ~Tracer()
00137 {
00138 parser->traceOut(text,tree);
00139 }
00140 private:
00141 Tracer(const Tracer&);
00142 const Tracer& operator=(const Tracer&);
00143 };
00144
00145 private:
00146
00147 TreeParser(const TreeParser& other);
00148 TreeParser& operator=(const TreeParser& other);
00149 };
00150
00151 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
00152 }
00153 #endif
00154
00155 #endif //INC_TreeParser_hpp__