KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > lexer > inc > RemovedTokenList


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.lexer.inc;
21
22 import java.util.Set JavaDoc;
23 import org.netbeans.api.lexer.LanguagePath;
24 import org.netbeans.api.lexer.InputAttributes;
25 import org.netbeans.api.lexer.Token;
26 import org.netbeans.api.lexer.TokenId;
27 import org.netbeans.lib.lexer.EmbeddingContainer;
28 import org.netbeans.lib.lexer.LexerUtilsConstants;
29 import org.netbeans.lib.lexer.TokenHierarchyOperation;
30 import org.netbeans.lib.lexer.TokenList;
31 import org.netbeans.lib.lexer.token.AbstractToken;
32 import org.netbeans.lib.lexer.token.TextToken;
33
34 /**
35  * Token list implementation holding added or removed tokens from a list.
36  *
37  * @author Miloslav Metelka
38  * @version 1.00
39  */

40
41 public final class RemovedTokenList<T extends TokenId> implements TokenList<T> {
42     
43     private final LanguagePath languagePath;
44     
45     private Object JavaDoc[] tokensOrBranches;
46     
47     private int removedTokensStartOffset;
48     
49     public RemovedTokenList(LanguagePath languagePath, Object JavaDoc[] tokensOrBranches) {
50         this.languagePath = languagePath;
51         this.tokensOrBranches = tokensOrBranches;
52     }
53     
54     public LanguagePath languagePath() {
55         return languagePath;
56     }
57     
58     public Object JavaDoc tokenOrEmbeddingContainer(int index) {
59         return (index < tokensOrBranches.length) ? tokensOrBranches[index] : null;
60     }
61
62     public int lookahead(int index) {
63         return -1;
64     }
65
66     public Object JavaDoc state(int index) {
67         return null;
68     }
69
70     public int tokenOffset(int index) {
71         Token<? extends TokenId> token = existingToken(index);
72         if (token.isFlyweight()) {
73             int offset = 0;
74             while (--index >= 0) {
75                 token = existingToken(index);
76                 offset += token.length();
77                 if (!token.isFlyweight()) {
78                     // Return from here instead of break; - see code after while()
79
return offset + token.offset(null);
80                 }
81             }
82             // might remove token sequence starting with flyweight
83
return removedTokensStartOffset + offset;
84
85         } else { // non-flyweight offset
86
return token.offset(null);
87         }
88     }
89
90     private Token<T> existingToken(int index) {
91         return LexerUtilsConstants.token(tokensOrBranches[index]);
92     }
93
94     public synchronized AbstractToken<T> replaceFlyToken(
95     int index, AbstractToken<T> flyToken, int offset) {
96         TextToken<T> nonFlyToken = ((TextToken<T>)flyToken).createCopy(this, offset);
97         tokensOrBranches[index] = nonFlyToken;
98         return nonFlyToken;
99     }
100
101     public int tokenCount() {
102         return tokenCountCurrent();
103     }
104
105     public int tokenCountCurrent() {
106         return tokensOrBranches.length;
107     }
108
109     public int modCount() {
110         return -1;
111     }
112     
113     public int childTokenOffset(int rawOffset) {
114         // Offsets of contained tokens are absolute
115
return rawOffset;
116     }
117     
118     public char childTokenCharAt(int rawOffset, int index) {
119         throw new IllegalStateException JavaDoc("Querying of text for removed tokens not supported"); // NOI18N
120
}
121
122     public void wrapToken(int index, EmbeddingContainer embeddingContainer) {
123         throw new IllegalStateException JavaDoc("Branching of removed tokens not supported"); // NOI18N
124
}
125     
126     public TokenList<? extends TokenId> root() {
127         return this;
128     }
129     
130     public TokenHierarchyOperation<?,? extends TokenId> tokenHierarchyOperation() {
131         return null;
132     }
133     
134     public InputAttributes inputAttributes() {
135         return null;
136     }
137
138     public boolean isContinuous() {
139         return true;
140     }
141
142     public Set JavaDoc<T> skipTokenIds() {
143         return null;
144     }
145
146 }
147
Popular Tags