001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.lib.lexer.inc;
043:
044: import java.util.Set;
045: import org.netbeans.api.lexer.LanguagePath;
046: import org.netbeans.api.lexer.InputAttributes;
047: import org.netbeans.api.lexer.Token;
048: import org.netbeans.api.lexer.TokenId;
049: import org.netbeans.lib.lexer.EmbeddingContainer;
050: import org.netbeans.lib.lexer.LexerUtilsConstants;
051: import org.netbeans.lib.lexer.TokenHierarchyOperation;
052: import org.netbeans.lib.lexer.TokenList;
053: import org.netbeans.lib.lexer.token.AbstractToken;
054: import org.netbeans.lib.lexer.token.TextToken;
055:
056: /**
057: * Token list implementation holding added or removed tokens from a list.
058: *
059: * @author Miloslav Metelka
060: * @version 1.00
061: */
062:
063: public final class RemovedTokenList<T extends TokenId> implements
064: TokenList<T> {
065:
066: private final LanguagePath languagePath;
067:
068: private Object[] tokensOrBranches;
069:
070: private int removedTokensStartOffset;
071:
072: public RemovedTokenList(LanguagePath languagePath,
073: Object[] tokensOrBranches) {
074: this .languagePath = languagePath;
075: this .tokensOrBranches = tokensOrBranches;
076: }
077:
078: public LanguagePath languagePath() {
079: return languagePath;
080: }
081:
082: public Object tokenOrEmbeddingContainer(int index) {
083: return (index < tokensOrBranches.length) ? tokensOrBranches[index]
084: : null;
085: }
086:
087: public int lookahead(int index) {
088: return -1;
089: }
090:
091: public Object state(int index) {
092: return null;
093: }
094:
095: public int tokenOffset(int index) {
096: Token<?> token = existingToken(index);
097: if (token.isFlyweight()) {
098: int offset = 0;
099: while (--index >= 0) {
100: token = existingToken(index);
101: offset += token.length();
102: if (!token.isFlyweight()) {
103: // Return from here instead of break; - see code after while()
104: return offset + token.offset(null);
105: }
106: }
107: // might remove token sequence starting with flyweight
108: return removedTokensStartOffset + offset;
109:
110: } else { // non-flyweight offset
111: return token.offset(null);
112: }
113: }
114:
115: private Token<T> existingToken(int index) {
116: return LexerUtilsConstants.token(tokensOrBranches[index]);
117: }
118:
119: public synchronized AbstractToken<T> replaceFlyToken(int index,
120: AbstractToken<T> flyToken, int offset) {
121: TextToken<T> nonFlyToken = ((TextToken<T>) flyToken)
122: .createCopy(this , offset);
123: tokensOrBranches[index] = nonFlyToken;
124: return nonFlyToken;
125: }
126:
127: public int tokenCount() {
128: return tokenCountCurrent();
129: }
130:
131: public int tokenCountCurrent() {
132: return tokensOrBranches.length;
133: }
134:
135: public int modCount() {
136: return -1;
137: }
138:
139: public int childTokenOffset(int rawOffset) {
140: // Offsets of contained tokens are absolute
141: return rawOffset;
142: }
143:
144: public char childTokenCharAt(int rawOffset, int index) {
145: throw new IllegalStateException(
146: "Querying of text for removed tokens not supported"); // NOI18N
147: }
148:
149: public void wrapToken(int index,
150: EmbeddingContainer embeddingContainer) {
151: throw new IllegalStateException(
152: "Branching of removed tokens not supported"); // NOI18N
153: }
154:
155: public TokenList<?> root() {
156: return this ;
157: }
158:
159: public TokenHierarchyOperation<?, ?> tokenHierarchyOperation() {
160: return null;
161: }
162:
163: public InputAttributes inputAttributes() {
164: return null;
165: }
166:
167: public int startOffset() {
168: if (tokenCountCurrent() > 0 || tokenCount() > 0)
169: return tokenOffset(0);
170: return 0;
171: }
172:
173: public int endOffset() {
174: int cntM1 = tokenCount() - 1;
175: if (cntM1 >= 0)
176: return tokenOffset(cntM1)
177: + LexerUtilsConstants.token(this , cntM1).length();
178: return 0;
179: }
180:
181: public boolean isRemoved() {
182: return true; // Collects tokens removed from TH
183: }
184:
185: public boolean isContinuous() {
186: return true;
187: }
188:
189: public Set<T> skipTokenIds() {
190: return null;
191: }
192:
193: @Override
194: public String toString() {
195: return LexerUtilsConstants.appendTokenList(null, this)
196: .toString();
197: }
198:
199: }
|