1 #ifndef INC_TreeParser_hpp__
2 #define INC_TreeParser_hpp__
4 /* ANTLR Translator Generator
5 * Project led by Terence Parr at http://www.jGuru.com
6 * Software rights: http://www.antlr.org/license.html
8 * $Id: //depot/code/org.antlr/release/antlr-2.7.7/lib/cpp/antlr/TreeParser.hpp#2 $
11 #include <antlr/config.hpp>
12 #include <antlr/AST.hpp>
13 #include <antlr/ASTFactory.hpp>
14 #include <antlr/BitSet.hpp>
15 #include <antlr/RecognitionException.hpp>
16 #include <antlr/MismatchedTokenException.hpp>
17 #include <antlr/TreeParserSharedInputState.hpp>
19 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
23 class ANTLR_API TreeParser {
27 , inputState(new TreeParserInputState())
32 TreeParser(const TreeParserSharedInputState& state)
43 /// Get the AST return value squirreled away in the parser
44 virtual RefAST getAST() = 0;
46 /** Make sure current lookahead symbol matches the given set
47 * Throw an exception upon mismatch, which is caught by either the
48 * error handler or by a syntactic predicate.
50 virtual void match(RefAST t, const BitSet& b)
52 if ( !t || t==ASTNULL || !b.member(t->getType()) )
53 throw MismatchedTokenException( getTokenNames(), getNumTokens(),
57 /** Specify the AST factory to be used during tree building. (Compulsory)
58 * Setting the factory is compulsory (if you intend to modify
59 * the tree in the treeparser). The AST Factory is shared between
60 * parser (who builds the initial AST) and treeparser.
61 * @see Parser::getASTFactory()
63 virtual void setASTFactory(ASTFactory* factory)
67 /// Return pointer to ASTFactory
68 virtual ASTFactory* getASTFactory() const
72 /// Get the name for token 'num'
73 virtual const char* getTokenName(int num) const = 0;
74 /// Return the number of tokens defined
75 virtual int getNumTokens() const = 0;
76 /// Return an array of getNumTokens() token names
77 virtual const char* const* getTokenNames() const = 0;
79 /// Parser error-reporting function can be overridden in subclass
80 virtual void reportError(const RecognitionException& ex);
81 /// Parser error-reporting function can be overridden in subclass
82 virtual void reportError(const ANTLR_USE_NAMESPACE(std)string& s);
83 /// Parser warning-reporting function can be overridden in subclass
84 virtual void reportWarning(const ANTLR_USE_NAMESPACE(std)string& s);
86 /// These are used during when traceTreeParser commandline option is passed.
87 virtual void traceIndent();
88 virtual void traceIn(const char* rname, RefAST t);
89 virtual void traceOut(const char* rname, RefAST t);
91 /** The AST Null object; the parsing cursor is set to this when
92 * it is found to be null. This way, we can test the
93 * token type of a node without having to have tests for 0
96 static RefAST ASTNULL;
99 virtual void match(RefAST t, int ttype)
101 if (!t || t == ASTNULL || t->getType() != ttype )
102 throw MismatchedTokenException( getTokenNames(), getNumTokens(),
106 virtual void matchNot(RefAST t, int ttype)
108 if ( !t || t == ASTNULL || t->getType() == ttype )
109 throw MismatchedTokenException( getTokenNames(), getNumTokens(),
113 /** AST support code; parser and treeparser delegate to this object */
114 ASTFactory* astFactory;
116 /// The input state of this tree parser.
117 TreeParserSharedInputState inputState;
119 /** Used to keep track of indent depth with -traceTreeParser */
122 /** Utility class which allows tracing to work even when exceptions are
131 Tracer(TreeParser* p, const char* t, RefAST a)
132 : parser(p), text(t), tree(a)
134 parser->traceIn(text,tree);
138 parser->traceOut(text,tree);
141 Tracer(const Tracer&); // undefined
142 const Tracer& operator=(const Tracer&); // undefined
146 // no copying of treeparser instantiations...
147 TreeParser(const TreeParser& other);
148 TreeParser& operator=(const TreeParser& other);
151 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
155 #endif //INC_TreeParser_hpp__