1 /* FOREGEJ - FOrmatting REfactoring GEnerating Java
2 *
3 * Copyright (C) 2003 Andreas Arrgard
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 package com.octagroup.foregej.java.lang;
20 import java.io.Reader;
21 import java.io.StringReader;
22 import com.octagroup.foregej.antlr.BaseToken;
23 import com.octagroup.foregej.java.lang.tok.TOK_JAVA_COMMENT;
24 import com.octagroup.foregej.java.lang.tok.TOK_WS;
25 import antlr.ParserSharedInputState;
26 import antlr.TokenBuffer;
27 import antlr.TokenStream;
28 import antlr.TokenStreamException;
29 import antlr.TokenStreamHiddenTokenFilter;
30 /***
31 */
32 public class JavaRecognizerBase extends antlr.LLkParser
33 {
34 public JavaToken lastAstToken_=null;
35 /***
36 */
37 public JavaRecognizerBase(int arg0)
38 {
39 super(arg0);
40 }
41 /***
42 */
43 public JavaRecognizerBase(ParserSharedInputState arg0,int arg1)
44 {
45 super(arg0, arg1);
46 }
47 /***
48 */
49 public JavaRecognizerBase(TokenBuffer arg0,int arg1)
50 {
51 super(arg0, arg1);
52 }
53 /***
54 */
55 public JavaRecognizerBase(TokenStream arg0,int arg1)
56 {
57 super(arg0, arg1);
58 }
59 /***
60 * Helper method that sets up a <code>JavaRecognizer</code>that parses
61 * the supplied string.
62 *
63 * @param string the stream to parse
64 * @return the java recognizer
65 */
66 public static JavaRecognizer setup(String string)
67 {
68 return setup("<internal>", new StringReader(string));
69 }
70 /***
71 * Helper method that sets up a <code>JavaRecognizer</code>that parses
72 * the supplied stream.
73 *
74 * @param fileName the stream to parse
75 * @param source
76 * @return the java recognizer
77 */
78 public static JavaRecognizer setup(String fileName,Reader source)
79 {
80 // setup the lexer to read the source
81 JavaLexer lexer=new JavaLexer(source);
82 lexer.setFilename(fileName);
83 // filter white spaces and comments
84 TokenStreamHiddenTokenFilter filter=new TokenStreamHiddenTokenFilter(lexer);
85 filter.hide(JavaTokenTypes.WS);
86 filter.hide(JavaTokenTypes.SL_COMMENT);
87 filter.hide(JavaTokenTypes.ML_COMMENT);
88 filter.hide(JavaTokenTypes.JAVA_COMMENT);
89 // Create a parser that reads from the scanner
90 JavaRecognizer javaRecognizer=new JavaRecognizer(filter);
91 javaRecognizer.setASTFactory(JavaASTFactory.newInstance());
92 javaRecognizer.setFilename(fileName);
93 return javaRecognizer;
94 }
95 /***
96 * If we have not created an AST for the token that we are consuming -
97 * add it to the hidden token set.
98 */
99 public void consume()
100 {
101 try{
102 JavaToken javaToken=(JavaToken)LT(1);
103 // The parser can actually cache tokens and reset after we have consumed em.
104 // Since the tokens have been created and the ASTs that are mapped on to tokens
105 // wont change we just have to check that we are not backtracking
106 if(lastAstToken_!=null&&
107 false==javaToken.isAfter(lastAstToken_)) {
108 return;
109 }
110 if(false==javaToken.isAstCreated()) {
111 if(lastAstToken_==null) {
112 return;
113 }
114 //
115 // make sure that the AST is correctly linked in the hiddenl list
116 //
117 if(javaToken.getHiddenAfter()!=null) {
118 ((JavaToken)javaToken.getHiddenAfter()).setHiddenBefore(javaToken);
119 }
120 if(javaToken.getHiddenBefore()!=null) {
121 ((JavaToken)javaToken.getHiddenBefore()).setHiddenAfter(javaToken);
122 }
123 JavaAST ast=(JavaAST)lastAstToken_.getCreatedAst();
124 attachAfter(ast, findHead(javaToken));
125 } else {
126 lastAstToken_=javaToken;
127 // more tokens might have been added to the hidden list -
128 // check the end of the hidden tokens
129 JavaAST ast=(JavaAST)lastAstToken_.getCreatedAst();
130 JavaToken lastToken=(JavaToken)ast.getHiddenBefore();
131 if(lastToken!=null) {
132 while(lastToken.getHiddenAfter()!=null){
133 lastToken=(JavaToken)lastToken.getHiddenAfter();
134 }
135 }
136 ast.setHiddenBefore(lastToken);
137 //
138 // if the last comment in the hidden list was a javadoc
139 // comment then we remove that comment from the list and
140 // assign it to the ast
141 //
142 JavaToken javadocProspect=lastToken;
143 while(javadocProspect instanceof TOK_WS){
144 javadocProspect=(JavaToken)javadocProspect.getHiddenBefore();
145 }
146 if(javadocProspect instanceof TOK_JAVA_COMMENT) {
147 ast.setJavadoc((TOK_JAVA_COMMENT)javadocProspect);
148 //
149 // unlink it!
150 //
151 javadocProspect.setHiddenAfter(null);
152 ast.setHiddenBefore((BaseToken)javadocProspect.getHiddenBefore());
153 }
154 }
155 }catch (TokenStreamException e) {
156 throw new RuntimeException("!!!");
157 }finally {
158 //
159 // always invoke the supermethod
160 //
161 super.consume();
162 }
163 }
164 /***
165 * Helper method that attaches hidden tokens after an ast.
166 *
167 * @param ast
168 * @param token
169 */
170 private void attachAfter(JavaAST ast,JavaToken token)
171 {
172 if(ast.getHiddenAfter()==null) {
173 ast.setHiddenAfter(token);
174 token.setHiddenBefore(null);
175 } else {
176 append((JavaToken)ast.getHiddenAfter(), token);
177 }
178 }
179 /***
180 * Helper method that attaches all the hidden tokens in one linked
181 * list to another.
182 *
183 * @param head
184 * @param tail
185 */
186 public void append(JavaToken head,JavaToken tail)
187 {
188 if(head==null||tail==null) {
189 return;
190 }
191 JavaToken lastHead=head;
192 if(lastHead==tail) {
193 //System.out.println("WARNING: trying to appendnodeto itself!");
194 return;
195 }
196 while(lastHead.getHiddenAfter()!=null){
197 lastHead=(JavaToken)lastHead.getHiddenAfter();
198 // make sure token not already connected...
199 if(lastHead==tail) {
200 return;
201 }
202 }
203 lastHead.setHiddenAfter(tail);
204 tail.setHiddenBefore(lastHead);
205 }
206 /***
207 * Helper method that finds the head of a list.
208 *
209 * @param list
210 * @return
211 */
212 private JavaToken findHead(JavaToken list)
213 {
214 if(list==null) {
215 return null;
216 }
217 while(list.getHiddenBefore()!=null){
218 if(list.getHiddenBefore().getHiddenAfter()!=list) {
219 throw new RuntimeException("List messed up!");
220 }
221 list=(JavaToken)list.getHiddenBefore();
222 }
223 return list;
224 }
225 /***
226 * Helper method that finds the tail of a list.
227 *
228 * @param list
229 * @return
230 */
231 private JavaToken findTail(JavaToken list)
232 {
233 if(list==null) {
234 return null;
235 }
236 while(list.getHiddenAfter()!=null){
237 if(list.getHiddenAfter().getHiddenBefore()!=list) {
238 throw new RuntimeException("List messed up!");
239 }
240 list=(JavaToken)list.getHiddenAfter();
241 }
242 return list;
243 }
244 /***
245 * Helper method that dumps the list
246 *
247 * @param first
248 */
249 public void dump(JavaToken first)
250 {
251 System.out.println("vvvvvv LIST vvvvvv");
252 while(first!=null){
253 System.out.println(first);
254 first=(JavaToken)first.getHiddenAfter();
255 }
256 System.out.println("^^^^^^ LIST ^^^^^^");
257 }
258 }
This page was automatically generated by Maven