2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.lib.lexer.inc;
45
import org.netbeans.api.lexer.LanguagePath;
46
import org.netbeans.api.lexer.InputAttributes;
47
import org.netbeans.api.lexer.Token;
48
import org.netbeans.api.lexer.TokenId;
49
import org.netbeans.lib.lexer.EmbeddingContainer;
50
import org.netbeans.lib.lexer.LexerUtilsConstants;
51
import org.netbeans.lib.lexer.TokenHierarchyOperation;
52
import org.netbeans.lib.lexer.TokenList;
53
import org.netbeans.lib.lexer.token.AbstractToken;
54
import org.netbeans.lib.lexer.token.TextToken;
57
* Token list implementation holding added or removed tokens from a list.
59
* @author Miloslav Metelka
63
public final class RemovedTokenList<T extends TokenId> implements TokenList<T> {
65
private final LanguagePath languagePath;
67
private Object[] tokensOrBranches;
69
private int removedTokensStartOffset;
71
public RemovedTokenList(LanguagePath languagePath, Object[] tokensOrBranches) {
72
this.languagePath = languagePath;
73
this.tokensOrBranches = tokensOrBranches;
76
public LanguagePath languagePath() {
80
public Object tokenOrEmbeddingContainer(int index) {
81
return (index < tokensOrBranches.length) ? tokensOrBranches[index] : null;
84
public int lookahead(int index) {
88
public Object state(int index) {
92
public int tokenOffset(int index) {
93
Token<?> token = existingToken(index);
94
if (token.isFlyweight()) {
96
while (--index >= 0) {
97
token = existingToken(index);
98
offset += token.length();
99
if (!token.isFlyweight()) {
100
// Return from here instead of break; - see code after while()
101
return offset + token.offset(null);
104
// might remove token sequence starting with flyweight
105
return removedTokensStartOffset + offset;
107
} else { // non-flyweight offset
108
return token.offset(null);
112
private Token<T> existingToken(int index) {
113
return LexerUtilsConstants.token(tokensOrBranches[index]);
116
public synchronized AbstractToken<T> replaceFlyToken(
117
int index, AbstractToken<T> flyToken, int offset) {
118
TextToken<T> nonFlyToken = ((TextToken<T>)flyToken).createCopy(this, offset);
119
tokensOrBranches[index] = nonFlyToken;
123
public int tokenCount() {
124
return tokenCountCurrent();
127
public int tokenCountCurrent() {
128
return tokensOrBranches.length;
131
public int modCount() {
135
public int childTokenOffset(int rawOffset) {
136
// Offsets of contained tokens are absolute
140
public char childTokenCharAt(int rawOffset, int index) {
141
throw new IllegalStateException("Querying of text for removed tokens not supported"); // NOI18N
144
public void wrapToken(int index, EmbeddingContainer embeddingContainer) {
145
throw new IllegalStateException("Branching of removed tokens not supported"); // NOI18N
148
public TokenList<?> root() {
152
public TokenHierarchyOperation<?,?> tokenHierarchyOperation() {
156
public InputAttributes inputAttributes() {
160
public int startOffset() {
161
if (tokenCountCurrent() > 0 || tokenCount() > 0)
162
return tokenOffset(0);
166
public int endOffset() {
167
int cntM1 = tokenCount() - 1;
169
return tokenOffset(cntM1) + LexerUtilsConstants.token(this, cntM1).length();
173
public boolean isContinuous() {
177
public Set<T> skipTokenIds() {