KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > ate > syntax > generic > ATESyntaxParser


1 package org.antlr.works.ate.syntax.generic;
2
3 import org.antlr.works.ate.syntax.misc.ATEToken;
4
5 import java.util.List JavaDoc;
6 import java.util.Stack JavaDoc;
7 /*
8
9 [The "BSD licence"]
10 Copyright (c) 2005 Jean Bovet
11 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions
15 are met:
16
17 1. Redistributions of source code must retain the above copyright
18 notice, this list of conditions and the following disclaimer.
19 2. Redistributions in binary form must reproduce the above copyright
20 notice, this list of conditions and the following disclaimer in the
21 documentation and/or other materials provided with the distribution.
22 3. The name of the author may not be used to endorse or promote products
23 derived from this software without specific prior written permission.
24
25 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
36 */

37
38 public abstract class ATESyntaxParser {
39
40     private List JavaDoc<ATEToken> tokens;
41     private Stack JavaDoc<Integer JavaDoc> marks = new Stack JavaDoc<Integer JavaDoc>();
42     private int position;
43
44     private ATEToken t0;
45     private ATEToken t1;
46
47     public ATESyntaxParser() {
48     }
49
50     public void parse(List JavaDoc<ATEToken> tokens) {
51         this.tokens = tokens;
52         marks.clear();
53         position = -1;
54         clearTokenCache();
55         parseTokens();
56     }
57
58     public abstract void parseTokens();
59
60     public List JavaDoc<ATEToken> getTokens() {
61         return tokens;
62     }
63
64     public int getPosition() {
65         return position;
66     }
67
68     public void mark() {
69         marks.push(position);
70     }
71
72     public void rewind() {
73         position = marks.pop();
74         clearTokenCache();
75     }
76
77     public boolean previousToken() {
78         position--;
79         clearTokenCache();
80         return position >= 0;
81     }
82
83     public boolean nextToken() {
84         position++;
85         clearTokenCache();
86         return position<tokens.size();
87     }
88
89     public boolean moreTokens() {
90         return position<tokens.size();
91     }
92
93     public boolean skip(int count) {
94         if(count == 1) {
95             return nextToken();
96         } else {
97             for(int i=0; i<count; i++) {
98                 if(!nextToken()) return false;
99             }
100         }
101         return true;
102     }
103     
104     public ATEToken T(int index) {
105         if(index == 0) {
106             if(t0 == null)
107                 t0 = getToken(0);
108             return t0;
109         } else if(index == 1) {
110             if(t1 == null)
111                 t1 = getToken(1);
112             return t1;
113         } else
114             return getToken(index);
115     }
116
117     public ATEToken getToken(int index) {
118         if(position+index >= 0 && position+index < tokens.size())
119             return tokens.get(position+index);
120         else
121             return null;
122     }
123
124     private void clearTokenCache() {
125         t0 = null;
126         t1 = null;
127     }
128
129     public boolean matchSingleComment(int index) {
130         if(isSingleComment(index)) {
131             nextToken();
132             return true;
133         } else {
134             return false;
135         }
136     }
137
138     public boolean matchComplexComment(int index) {
139         if(isComplexComment(index)) {
140             nextToken();
141             return true;
142         } else {
143             return false;
144         }
145     }
146
147     public boolean isChar(int index, String JavaDoc c) {
148         return isTokenType(index, ATESyntaxLexer.TOKEN_CHAR) && T(index).getAttribute().equals(c);
149     }
150
151     public boolean isSingleComment(int index) {
152         return isTokenType(index, ATESyntaxLexer.TOKEN_SINGLE_COMMENT);
153     }
154
155     public boolean isComplexComment(int index) {
156         return isTokenType(index, ATESyntaxLexer.TOKEN_COMPLEX_COMMENT);
157     }
158
159     public boolean isID(int index) {
160         return isTokenType(index, ATESyntaxLexer.TOKEN_ID);
161     }
162
163     public boolean isID(int index, String JavaDoc attribute) {
164         return isTokenType(index, ATESyntaxLexer.TOKEN_ID) && T(index).getAttribute().equals(attribute);
165     }
166
167     public boolean isTokenType(int index, int type) {
168         return T(index) != null && T(index).type == type;
169     }
170
171 }
172
Popular Tags