01: package persistence.antlr;
02:
03: /* ANTLR Translator Generator
04: * Project led by Terence Parr at http://www.jGuru.com
05: * Software rights: http://www.antlr.org/license.html
06: *
07: */
08:
09: import java.io.IOException;
10:
11: /**An LL(k) parser.
12: *
13: * @see persistence.antlr.Token
14: * @see persistence.antlr.TokenBuffer
15: */
16: public class LLkParser extends Parser {
17: int k;
18:
19: public LLkParser(int k_) {
20: k = k_;
21: }
22:
23: public LLkParser(ParserSharedInputState state, int k_) {
24: super (state);
25: k = k_;
26: }
27:
28: public LLkParser(TokenBuffer tokenBuf, int k_) {
29: k = k_;
30: setTokenBuffer(tokenBuf);
31: }
32:
33: public LLkParser(TokenStream lexer, int k_) {
34: k = k_;
35: TokenBuffer tokenBuf = new TokenBuffer(lexer);
36: setTokenBuffer(tokenBuf);
37: }
38:
39: /**Consume another token from the input stream. Can only write sequentially!
40: * If you need 3 tokens ahead, you must consume() 3 times.
41: * <p>
42: * Note that it is possible to overwrite tokens that have not been matched.
43: * For example, calling consume() 3 times when k=2, means that the first token
44: * consumed will be overwritten with the 3rd.
45: */
46: public void consume() {
47: inputState.input.consume();
48: }
49:
50: public int LA(int i) throws TokenStreamException {
51: return inputState.input.LA(i);
52: }
53:
54: public Token LT(int i) throws TokenStreamException {
55: return inputState.input.LT(i);
56: }
57:
58: private void trace(String ee, String rname)
59: throws TokenStreamException {
60: traceIndent();
61: System.out.print(ee + rname
62: + ((inputState.guessing > 0) ? "; [guessing]" : "; "));
63: for (int i = 1; i <= k; i++) {
64: if (i != 1) {
65: System.out.print(", ");
66: }
67: if (LT(i) != null) {
68: System.out.print("LA(" + i + ")==" + LT(i).getText());
69: } else {
70: System.out.print("LA(" + i + ")==null");
71: }
72: }
73: System.out.println("");
74: }
75:
76: public void traceIn(String rname) throws TokenStreamException {
77: traceDepth += 1;
78: trace("> ", rname);
79: }
80:
81: public void traceOut(String rname) throws TokenStreamException {
82: trace("< ", rname);
83: traceDepth -= 1;
84: }
85: }
|