ANTLR Support Libraries 2.7.1+
Loading...
Searching...
No Matches
TreeParser.hpp
Go to the documentation of this file.
1#ifndef INC_TreeParser_hpp__
2#define INC_TreeParser_hpp__
3
4/* ANTLR Translator Generator
5 * Project led by Terence Parr at http://www.jGuru.com
6 * Software rights: http://www.antlr.org/license.html
7 *
8 * $Id: //depot/code/org.antlr/release/antlr-2.7.7/lib/cpp/antlr/TreeParser.hpp#2 $
9 */
10
11#include <antlr/config.hpp>
12#include <antlr/AST.hpp>
13#include <antlr/ASTFactory.hpp>
14#include <antlr/BitSet.hpp>
18
19#ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
20namespace antlr {
21#endif
22
24public:
26 : astFactory(0)
27 , inputState(new TreeParserInputState())
28 , traceDepth(0)
29 {
30 }
31
33 : astFactory(0)
34 , inputState(state)
35 , traceDepth(0)
36 {
37 }
38
39 virtual ~TreeParser()
40 {
41 }
42
44 virtual RefAST getAST() = 0;
45
50 virtual void match(RefAST t, const BitSet& b)
51 {
52 if ( !t || t==ASTNULL || !b.member(t->getType()) )
53 throw MismatchedTokenException( getTokenNames(), getNumTokens(),
54 t, b, false );
55 }
56
63 virtual void setASTFactory(ASTFactory* factory)
64 {
65 astFactory = factory;
66 }
68 virtual ASTFactory* getASTFactory() const
69 {
70 return astFactory;
71 }
73 virtual const char* getTokenName(int num) const = 0;
75 virtual int getNumTokens() const = 0;
77 virtual const char* const* getTokenNames() const = 0;
78
80 virtual void reportError(const RecognitionException& ex);
82 virtual void reportError(const ANTLR_USE_NAMESPACE(std)string& s);
84 virtual void reportWarning(const ANTLR_USE_NAMESPACE(std)string& s);
85
87 virtual void traceIndent();
88 virtual void traceIn(const char* rname, RefAST t);
89 virtual void traceOut(const char* rname, RefAST t);
90
97
98protected:
99 virtual void match(RefAST t, int ttype)
100 {
101 if (!t || t == ASTNULL || t->getType() != ttype )
102 throw MismatchedTokenException( getTokenNames(), getNumTokens(),
103 t, ttype, false );
104 }
105
106 virtual void matchNot(RefAST t, int ttype)
107 {
108 if ( !t || t == ASTNULL || t->getType() == ttype )
109 throw MismatchedTokenException( getTokenNames(), getNumTokens(),
110 t, ttype, true );
111 }
112
115
118
121
125 class Tracer {
126 private:
128 const char* text;
130 public:
131 Tracer(TreeParser* p, const char* t, RefAST a)
132 : parser(p), text(t), tree(a)
133 {
134 parser->traceIn(text,tree);
135 }
137 {
138 parser->traceOut(text,tree);
139 }
140 private:
141 Tracer(const Tracer&); // undefined
142 const Tracer& operator=(const Tracer&); // undefined
143 };
144
145private:
146 // no copying of treeparser instantiations...
147 TreeParser(const TreeParser& other);
149};
150
151#ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
152}
153#endif
154
155#endif //INC_TreeParser_hpp__
Definition: ASTFactory.hpp:36
virtual int getType() const =0
Get the token type for this node.
Definition: BitSet.hpp:40
bool member(unsigned int el) const
Definition: BitSet.cpp:40
Definition: MismatchedTokenException.hpp:22
Definition: RecognitionException.hpp:19
Definition: TreeParserSharedInputState.hpp:24
Definition: TreeParser.hpp:125
~Tracer()
Definition: TreeParser.hpp:136
Tracer(TreeParser *p, const char *t, RefAST a)
Definition: TreeParser.hpp:131
Tracer(const Tracer &)
const Tracer & operator=(const Tracer &)
const char * text
Definition: TreeParser.hpp:128
RefAST tree
Definition: TreeParser.hpp:129
TreeParser * parser
Definition: TreeParser.hpp:127
Definition: TreeParser.hpp:23
virtual void traceIn(const char *rname, RefAST t)
Definition: TreeParser.cpp:47
virtual void matchNot(RefAST t, int ttype)
Definition: TreeParser.hpp:106
TreeParser()
Definition: TreeParser.hpp:25
virtual void setASTFactory(ASTFactory *factory)
Definition: TreeParser.hpp:63
virtual const char *const * getTokenNames() const =0
Return an array of getNumTokens() token names.
virtual int getNumTokens() const =0
Return the number of tokens defined.
virtual void match(RefAST t, const BitSet &b)
Definition: TreeParser.hpp:50
TreeParserSharedInputState inputState
The input state of this tree parser.
Definition: TreeParser.hpp:117
virtual RefAST getAST()=0
Get the AST return value squirreled away in the parser.
TreeParser & operator=(const TreeParser &other)
virtual const char * getTokenName(int num) const =0
Get the name for token 'num'.
TreeParser(const TreeParser &other)
virtual ASTFactory * getASTFactory() const
Return pointer to ASTFactory.
Definition: TreeParser.hpp:68
virtual void traceOut(const char *rname, RefAST t)
Definition: TreeParser.cpp:58
int traceDepth
Definition: TreeParser.hpp:120
static RefAST ASTNULL
Definition: TreeParser.hpp:96
virtual void match(RefAST t, int ttype)
Definition: TreeParser.hpp:99
virtual ~TreeParser()
Definition: TreeParser.hpp:39
ASTFactory * astFactory
Definition: TreeParser.hpp:114
TreeParser(const TreeParserSharedInputState &state)
Definition: TreeParser.hpp:32
#define ANTLR_USE_NAMESPACE(_x_)
Definition: config.hpp:18
#define ANTLR_API
Definition: config.hpp:22
Definition: ANTLRException.hpp:15