ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/antlr/TreeParser.hpp
Revision: 2469
Committed: Fri Dec 2 15:38:03 2005 UTC (18 years, 7 months ago) by tim
File size: 4304 byte(s)
Log Message:
End of the Link --> List
Return of the Oject-Oriented
replace yacc/lex parser with antlr parser

File Contents

# Content
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: TreeParser.hpp,v 1.1 2005-12-02 15:38:02 tim Exp $
9 */
10
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>
18
19 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
20 namespace antlr {
21 #endif
22
23 class ANTLR_API TreeParser {
24 public:
25 TreeParser()
26 : astFactory(0)
27 , inputState(new TreeParserInputState())
28 , traceDepth(0)
29 {
30 }
31
32 TreeParser(const TreeParserSharedInputState& state)
33 : astFactory(0)
34 , inputState(state)
35 , traceDepth(0)
36 {
37 }
38
39 virtual ~TreeParser()
40 {
41 }
42
43 /// Get the AST return value squirreled away in the parser
44 virtual RefAST getAST() = 0;
45
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.
49 */
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
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()
62 */
63 virtual void setASTFactory(ASTFactory* factory)
64 {
65 astFactory = factory;
66 }
67 /// Return pointer to ASTFactory
68 virtual ASTFactory* getASTFactory() const
69 {
70 return astFactory;
71 }
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;
78
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);
85
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);
90
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
94 * everywhere.
95 */
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
113 /** AST support code; parser and treeparser delegate to this object */
114 ASTFactory* astFactory;
115
116 /// The input state of this tree parser.
117 TreeParserSharedInputState inputState;
118
119 /** Used to keep track of indent depth with -traceTreeParser */
120 int traceDepth;
121
122 /** Utility class which allows tracing to work even when exceptions are
123 * thrown.
124 */
125 class Tracer {
126 private:
127 TreeParser* parser;
128 const char* text;
129 RefAST tree;
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 }
136 ~Tracer()
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__