KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > parsec > ParserState


1 /*****************************************************************************
2  * Copyright (C) Zephyr Business Solutions Corp. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  *****************************************************************************/

8 /*
9  * Created on 2004-11-11
10  *
11  * Author Ben Yu
12  */

13 package jfun.parsec;
14
15
16 /**
17  * @author Ben Yu
18  *
19  * 2004-11-11
20  */

21 final class ParserState extends ParseContext{
22   
23   private final Tok[] input;
24   private final ParsecError[] sys_unexpected;
25   //in case a terminating eof token is not explicitly created,
26
//the implicit one is used.
27
private final int end_index;
28   private final ParsecError eof_unexpected;
29   
30   
31   private final ShowToken show;
32
33   boolean isEof(){
34     return at >= input.length; //|| input[at].getToken()==Tokens.eof();
35
}
36   int length(){return input.length;}
37   int getIndex(){
38     if(at==input.length) return end_index;
39     return input[at].getIndex();
40   }
41   
42
43   Tok getToken(){
44     return input[at];
45   }
46   //caller should not change input after it is passed in.
47
ParserState(final Object JavaDoc us, final Tok[] input,
48       final int at, final String JavaDoc module, final PositionMap pmap,
49       final int end_index,
50       final String JavaDoc eof_str, final ShowToken show){
51     super(us, at, module, pmap);
52     this.input = input;
53     this.sys_unexpected = new ParsecError[input.length];
54     this.show = show;
55     this.end_index = end_index;
56     this.eof_unexpected = ParsecError.raiseSysUnexpected(
57         end_index, eof_str);
58   }
59   ParserState(final Object JavaDoc r, final Object JavaDoc us, final Tok[] input,
60       final int at, final String JavaDoc module, final PositionMap pmap,
61       final int end_index,
62       final String JavaDoc eof_str, final ShowToken show){
63     super(r, us, at, module, pmap);
64     this.input = input;
65     this.sys_unexpected = new ParsecError[input.length];
66     this.show = show;
67     this.end_index = end_index;
68     this.eof_unexpected = ParsecError.raiseSysUnexpected(
69         end_index, eof_str);
70   }
71
72   ParsecError getSysUnexpected(){
73     return getSysUnexpected(at);
74   }
75   private ParsecError getSysUnexpected(final int i){
76     if(i>=sys_unexpected.length) return eof_unexpected;
77     ParsecError r = sys_unexpected[i];
78     if(r == null){
79       final Tok ptok = input[i];
80       r = ParsecError.raiseSysUnexpected(ptok.getIndex(),
81           show.show(ptok.getToken()));
82       sys_unexpected[i] = r;
83     }
84     return r;
85   }
86   char peekChar(){
87     throw new IllegalStateException JavaDoc("parser not on char level.");
88   }
89   char peekChar(int i){
90     throw new IllegalStateException JavaDoc("parser not on char level.");
91   }
92   CharSequence JavaDoc getSource(){
93     throw new IllegalStateException JavaDoc("parser not on char level.");
94   }
95 }
96
Popular Tags