1 |
tim |
770 |
/* ANTLR Translator Generator |
2 |
|
|
* Project led by Terence Parr at http://www.jGuru.com |
3 |
|
|
* Software rights: http://www.antlr.org/license.html |
4 |
|
|
* |
5 |
gezelter |
1442 |
* $Id$ |
6 |
tim |
770 |
*/ |
7 |
|
|
|
8 |
|
|
#include <iostream> |
9 |
|
|
|
10 |
|
|
#include "antlr/Parser.hpp" |
11 |
|
|
|
12 |
|
|
#ifdef ANTLR_CXX_SUPPORTS_NAMESPACE |
13 |
|
|
namespace antlr { |
14 |
|
|
#endif |
15 |
|
|
|
16 |
|
|
/** A generic ANTLR parser (LL(k) for k>=1) containing a bunch of |
17 |
|
|
* utility routines useful at any lookahead depth. We distinguish between |
18 |
|
|
* the LL(1) and LL(k) parsers because of efficiency. This may not be |
19 |
|
|
* necessary in the near future. |
20 |
|
|
* |
21 |
|
|
* Each parser object contains the state of the parse including a lookahead |
22 |
|
|
* cache (the form of which is determined by the subclass), whether or |
23 |
|
|
* not the parser is in guess mode, where tokens come from, etc... |
24 |
|
|
* |
25 |
|
|
* <p> |
26 |
|
|
* During <b>guess</b> mode, the current lookahead token(s) and token type(s) |
27 |
|
|
* cache must be saved because the token stream may not have been informed |
28 |
|
|
* to save the token (via <tt>mark</tt>) before the <tt>try</tt> block. |
29 |
|
|
* Guessing is started by: |
30 |
|
|
* <ol> |
31 |
|
|
* <li>saving the lookahead cache. |
32 |
|
|
* <li>marking the current position in the TokenBuffer. |
33 |
|
|
* <li>increasing the guessing level. |
34 |
|
|
* </ol> |
35 |
|
|
* |
36 |
|
|
* After guessing, the parser state is restored by: |
37 |
|
|
* <ol> |
38 |
|
|
* <li>restoring the lookahead cache. |
39 |
|
|
* <li>rewinding the TokenBuffer. |
40 |
|
|
* <li>decreasing the guessing level. |
41 |
|
|
* </ol> |
42 |
|
|
* |
43 |
|
|
* @see antlr.Token |
44 |
|
|
* @see antlr.TokenBuffer |
45 |
|
|
* @see antlr.TokenStream |
46 |
|
|
* @see antlr.LL1Parser |
47 |
|
|
* @see antlr.LLkParser |
48 |
|
|
*/ |
49 |
|
|
|
50 |
|
|
bool DEBUG_PARSER = false; |
51 |
|
|
|
52 |
|
|
/** Parser error-reporting function can be overridden in subclass */ |
53 |
|
|
void Parser::reportError(const RecognitionException& ex) |
54 |
|
|
{ |
55 |
|
|
ANTLR_USE_NAMESPACE(std)cerr << ex.toString().c_str() << ANTLR_USE_NAMESPACE(std)endl; |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
/** Parser error-reporting function can be overridden in subclass */ |
59 |
|
|
void Parser::reportError(const ANTLR_USE_NAMESPACE(std)string& s) |
60 |
|
|
{ |
61 |
|
|
if ( getFilename()=="" ) |
62 |
|
|
ANTLR_USE_NAMESPACE(std)cerr << "error: " << s.c_str() << ANTLR_USE_NAMESPACE(std)endl; |
63 |
|
|
else |
64 |
|
|
ANTLR_USE_NAMESPACE(std)cerr << getFilename().c_str() << ": error: " << s.c_str() << ANTLR_USE_NAMESPACE(std)endl; |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
/** Parser warning-reporting function can be overridden in subclass */ |
68 |
|
|
void Parser::reportWarning(const ANTLR_USE_NAMESPACE(std)string& s) |
69 |
|
|
{ |
70 |
|
|
if ( getFilename()=="" ) |
71 |
|
|
ANTLR_USE_NAMESPACE(std)cerr << "warning: " << s.c_str() << ANTLR_USE_NAMESPACE(std)endl; |
72 |
|
|
else |
73 |
|
|
ANTLR_USE_NAMESPACE(std)cerr << getFilename().c_str() << ": warning: " << s.c_str() << ANTLR_USE_NAMESPACE(std)endl; |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
/** Set or change the input token buffer */ |
77 |
|
|
// void setTokenBuffer(TokenBuffer<Token>* t); |
78 |
|
|
|
79 |
|
|
void Parser::traceIndent() |
80 |
|
|
{ |
81 |
|
|
for( int i = 0; i < traceDepth; i++ ) |
82 |
|
|
ANTLR_USE_NAMESPACE(std)cout << " "; |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
void Parser::traceIn(const char* rname) |
86 |
|
|
{ |
87 |
|
|
traceDepth++; |
88 |
|
|
|
89 |
|
|
for( int i = 0; i < traceDepth; i++ ) |
90 |
|
|
ANTLR_USE_NAMESPACE(std)cout << " "; |
91 |
|
|
|
92 |
|
|
ANTLR_USE_NAMESPACE(std)cout << "> " << rname |
93 |
|
|
<< "; LA(1)==" << LT(1)->getText().c_str() |
94 |
|
|
<< ((inputState->guessing>0)?" [guessing]":"") |
95 |
|
|
<< ANTLR_USE_NAMESPACE(std)endl; |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
void Parser::traceOut(const char* rname) |
99 |
|
|
{ |
100 |
|
|
for( int i = 0; i < traceDepth; i++ ) |
101 |
|
|
ANTLR_USE_NAMESPACE(std)cout << " "; |
102 |
|
|
|
103 |
|
|
ANTLR_USE_NAMESPACE(std)cout << "< " << rname |
104 |
|
|
<< "; LA(1)==" << LT(1)->getText().c_str() |
105 |
|
|
<< ((inputState->guessing>0)?" [guessing]":"") |
106 |
|
|
<< ANTLR_USE_NAMESPACE(std)endl; |
107 |
|
|
|
108 |
|
|
traceDepth--; |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
#ifdef ANTLR_CXX_SUPPORTS_NAMESPACE |
112 |
|
|
} |
113 |
|
|
#endif |