KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > languages > parser > TokenInput


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.languages.parser;
21
22 import org.netbeans.api.languages.*;
23 import org.netbeans.api.languages.CharInput;
24 import org.netbeans.api.languages.ASTToken;
25 import org.netbeans.modules.languages.Feature;
26 import org.netbeans.modules.languages.parser.Parser;
27 import org.netbeans.modules.languages.parser.Parser.Cookie;
28 import java.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Set JavaDoc;
32
33
34 public abstract class TokenInput {
35
36     public static TokenInput create (
37         String JavaDoc mimeType,
38         Parser parser,
39         CharInput input,
40         Set JavaDoc skip
41     ) {
42         return new TokenInputImpl (
43             new TokenReader (mimeType, parser, input, skip),
44             input
45         );
46     }
47
48     public static TokenInput create (ASTToken[] array) {
49         return new ArrayInput (array);
50     }
51
52     public static TokenInput create (List JavaDoc list) {
53         return new ListInput (list);
54     }
55
56     public abstract ASTToken read ();
57
58     public abstract void setIndex (int index);
59
60     public abstract int getIndex ();
61
62     public abstract int getOffset ();
63
64     public abstract ASTToken next (int index);
65
66     public abstract boolean eof ();
67
68     
69     // innerclasses ............................................................
70

71     private static class TokenReader {
72         
73         private String JavaDoc mimeType;
74         private Parser parser;
75         private CharInput input;
76         private Set JavaDoc skip;
77         private int state = -1;
78         private Cookie cookie = new MyCookie ();
79
80
81         private TokenReader (
82             String JavaDoc mimeType,
83             Parser parser,
84             CharInput input,
85             Set JavaDoc skip
86         ) {
87             this.mimeType = mimeType;
88             this.parser = parser;
89             this.input = input;
90             this.skip = skip;
91         }
92
93         private ASTToken next;
94
95         public ASTToken nextToken (CharInput input) {
96             if (next == null)
97                 next = readToken (input);
98             return next;
99         }
100
101         public ASTToken readToken (CharInput input) {
102             if (next != null) {
103                 ASTToken p = next;
104                 next = null;
105                 return p;
106             }
107             ASTToken token = null;
108             do {
109                 int start = input.getIndex ();
110                 token = parser.read (cookie, input, mimeType);
111                 if (token == null) return null;
112             } while (skip.contains (token.getType ()));
113             return token;
114         }
115     
116         private class MyCookie implements Cookie {
117             public int getState () {
118                 return state;
119             }
120
121             public void setState (int state) {
122                 TokenReader.this.state = state;
123             }
124             
125             public void setProperties (Feature tokenProperties) {
126             }
127         }
128     }
129
130     private static class TokenInputImpl extends TokenInput {
131
132         private TokenReader tokenReader;
133         private List JavaDoc<ASTToken> tokens = new ArrayList JavaDoc<ASTToken> ();
134         private int index = 0;
135         private CharInput input;
136
137
138         TokenInputImpl (TokenReader tokenReader, CharInput input) {
139             this.input = input;
140             this.tokenReader = tokenReader;
141         }
142
143         public ASTToken next (int i) {
144             while (index + i - 1 >= tokens.size ())
145                 tokens.add (tokenReader.readToken (input));
146             return (ASTToken) tokens.get (index + i - 1);
147         }
148
149         public boolean eof () {
150             return next (1) == null;
151         }
152
153         public int getIndex () {
154             return index;
155         }
156
157         public int getOffset () {
158             ASTToken t = null;
159             if (eof ()) {
160                 if (getIndex () == 0) return 0;
161                 int i = tokens.size () - 1;
162                 do {
163                     t = ((ASTToken) tokens.get (i--));
164                 } while (t == null && i > 0);
165                 if (t == null) return 0;
166                 return t.getOffset () + t.getLength ();
167             } else {
168                 t = (ASTToken) next (1);
169                 return t.getOffset ();
170             }
171         }
172
173         public ASTToken read () {
174             ASTToken t = next (1);
175             if (t != null) index++;
176             return t;
177         }
178
179         public void setIndex (int index) {
180             if (index > tokens.size ())
181                 throw new IndexOutOfBoundsException JavaDoc ();
182             this.index = index;
183         }
184
185         public String JavaDoc getString (int from) {
186             throw new InternalError JavaDoc ();
187         }
188         
189         public String JavaDoc toString () {
190             return input.toString ();
191         }
192     }
193
194     private static class ArrayInput extends TokenInput {
195
196         private ASTToken[] array;
197         private int index = 0;
198         private int length;
199
200         private ArrayInput (ASTToken[] array) {
201             this.array = array;
202             length = array.length;
203         }
204
205         public ASTToken read () {
206             if (index < length)
207                 return array [index++];
208             return null;
209         }
210
211         public void setIndex (int index) {
212             this.index = index;
213         }
214
215         public int getIndex () {
216             return index;
217         }
218
219         public int getOffset () {
220             ASTToken t = null;
221             if (eof ()) {
222                 if (getIndex () == 0) return 0;
223                 t = (ASTToken) array [array.length - 1];
224                 return t.getOffset () + t.getLength ();
225             } else {
226                 t = (ASTToken) next (1);
227                 return t.getOffset ();
228             }
229         }
230         
231         public boolean eof () {
232             return index >= length;
233         }
234
235         public ASTToken next (int i) {
236             if (index + i - 1 < length)
237                 return array [index + i - 1];
238             return null;
239         }
240
241         public String JavaDoc toString () {
242             StringBuilder JavaDoc sb = new StringBuilder JavaDoc ();
243             int i = index, j = 0;
244             while (j < 10 && i < length) {
245                 sb.append (array [i]).append (" ");
246                 i++; j++;
247             }
248             return sb.toString ();
249         }
250     }
251
252     private static class ListInput extends TokenInput {
253
254         private List JavaDoc list;
255         private int index = 0;
256         private int length;
257
258         private ListInput (List JavaDoc list) {
259             this.list = list;
260             length = list.size ();
261         }
262
263         public ASTToken read () {
264             if (index < length)
265                 return (ASTToken) list.get (index++);
266             return null;
267         }
268
269         public void setIndex (int index) {
270             this.index = index;
271         }
272
273         public int getIndex () {
274             return index;
275         }
276
277         public int getOffset () {
278             ASTToken t = null;
279             if (eof ()) {
280                 if (getIndex () == 0) return 0;
281                 t = ((ASTToken) list.get (list.size () - 1));
282                 return t.getOffset () + t.getLength ();
283             } else {
284                 t = (ASTToken) next (1);
285                 return t.getOffset ();
286             }
287         }
288
289         public boolean eof () {
290             return index >= length;
291         }
292
293         public ASTToken next (int i) {
294             if (index + i - 1 < length)
295                 return (ASTToken) list.get (index + i - 1);
296             return null;
297         }
298
299         public String JavaDoc toString () {
300             StringBuilder JavaDoc sb = new StringBuilder JavaDoc ();
301             int i = index, j = 0;
302             while (j < 10 && i < length) {
303                 sb.append (list.get (i)).append (" ");
304                 i++; j++;
305             }
306             return sb.toString ();
307         }
308     }
309 }
Popular Tags