ANTLR Support Libraries 2.7.1+
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
20 namespace antlr {
21 #endif
22 
24 public:
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 
96  static RefAST ASTNULL;
97 
98 protected:
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 
145 private:
146  // no copying of treeparser instantiations...
147  TreeParser(const TreeParser& other);
148  TreeParser& operator=(const TreeParser& other);
149 };
150 
151 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
152 }
153 #endif
154 
155 #endif //INC_TreeParser_hpp__
TreeParser(const TreeParserSharedInputState &state)
Definition: TreeParser.hpp:32
Definition: ANTLRException.hpp:15
virtual void match(RefAST t, int ttype)
Definition: TreeParser.hpp:99
virtual void setASTFactory(ASTFactory *factory)
Definition: TreeParser.hpp:63
static RefAST ASTNULL
Definition: TreeParser.hpp:96
virtual void traceIn(const char *rname, RefAST t)
Definition: TreeParser.cpp:47
virtual int getType() const =0
Get the token type for this node.
#define ANTLR_API
Definition: config.hpp:22
const char * text
Definition: TreeParser.hpp:128
Definition: TreeParserSharedInputState.hpp:24
virtual void matchNot(RefAST t, int ttype)
Definition: TreeParser.hpp:106
int traceDepth
Definition: TreeParser.hpp:120
RefAST tree
Definition: TreeParser.hpp:129
TreeParserSharedInputState inputState
The input state of this tree parser.
Definition: TreeParser.hpp:117
virtual ASTFactory * getASTFactory() const
Return pointer to ASTFactory.
Definition: TreeParser.hpp:68
Definition: ASTFactory.hpp:36
Definition: BitSet.hpp:40
Tracer(TreeParser *p, const char *t, RefAST a)
Definition: TreeParser.hpp:131
bool member(unsigned int el) const
Definition: BitSet.cpp:40
Definition: TreeParser.hpp:125
TreeParser * parser
Definition: TreeParser.hpp:127
Definition: RecognitionException.hpp:18
Definition: MismatchedTokenException.hpp:22
~Tracer()
Definition: TreeParser.hpp:136
#define ANTLR_USE_NAMESPACE(_x_)
Definition: config.hpp:18
TreeParser()
Definition: TreeParser.hpp:25
Definition: TreeParser.hpp:23
virtual void traceOut(const char *rname, RefAST t)
Definition: TreeParser.cpp:58
ASTFactory * astFactory
Definition: TreeParser.hpp:114
virtual void match(RefAST t, const BitSet &b)
Definition: TreeParser.hpp:50
virtual ~TreeParser()
Definition: TreeParser.hpp:39