~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to lexer/src/org/netbeans/lib/lexer/inc/RemovedTokenList.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.lib.lexer.inc;
 
43
 
 
44
import java.util.Set;
 
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;
 
55
 
 
56
/**
 
57
 * Token list implementation holding added or removed tokens from a list.
 
58
 *
 
59
 * @author Miloslav Metelka
 
60
 * @version 1.00
 
61
 */
 
62
 
 
63
public final class RemovedTokenList<T extends TokenId> implements TokenList<T> {
 
64
    
 
65
    private final LanguagePath languagePath;
 
66
    
 
67
    private Object[] tokensOrBranches;
 
68
    
 
69
    private int removedTokensStartOffset;
 
70
    
 
71
    public RemovedTokenList(LanguagePath languagePath, Object[] tokensOrBranches) {
 
72
        this.languagePath = languagePath;
 
73
        this.tokensOrBranches = tokensOrBranches;
 
74
    }
 
75
    
 
76
    public LanguagePath languagePath() {
 
77
        return languagePath;
 
78
    }
 
79
    
 
80
    public Object tokenOrEmbeddingContainer(int index) {
 
81
        return (index < tokensOrBranches.length) ? tokensOrBranches[index] : null;
 
82
    }
 
83
 
 
84
    public int lookahead(int index) {
 
85
        return -1;
 
86
    }
 
87
 
 
88
    public Object state(int index) {
 
89
        return null;
 
90
    }
 
91
 
 
92
    public int tokenOffset(int index) {
 
93
        Token<?> token = existingToken(index);
 
94
        if (token.isFlyweight()) {
 
95
            int offset = 0;
 
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);
 
102
                }
 
103
            }
 
104
            // might remove token sequence starting with flyweight
 
105
            return removedTokensStartOffset + offset;
 
106
 
 
107
        } else { // non-flyweight offset
 
108
            return token.offset(null);
 
109
        }
 
110
    }
 
111
 
 
112
    private Token<T> existingToken(int index) {
 
113
        return LexerUtilsConstants.token(tokensOrBranches[index]);
 
114
    }
 
115
 
 
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;
 
120
        return nonFlyToken;
 
121
    }
 
122
 
 
123
    public int tokenCount() {
 
124
        return tokenCountCurrent();
 
125
    }
 
126
 
 
127
    public int tokenCountCurrent() {
 
128
        return tokensOrBranches.length;
 
129
    }
 
130
 
 
131
    public int modCount() {
 
132
        return -1;
 
133
    }
 
134
    
 
135
    public int childTokenOffset(int rawOffset) {
 
136
        // Offsets of contained tokens are absolute
 
137
        return rawOffset;
 
138
    }
 
139
    
 
140
    public char childTokenCharAt(int rawOffset, int index) {
 
141
        throw new IllegalStateException("Querying of text for removed tokens not supported"); // NOI18N
 
142
    }
 
143
 
 
144
    public void wrapToken(int index, EmbeddingContainer embeddingContainer) {
 
145
        throw new IllegalStateException("Branching of removed tokens not supported"); // NOI18N
 
146
    }
 
147
    
 
148
    public TokenList<?> root() {
 
149
        return this;
 
150
    }
 
151
    
 
152
    public TokenHierarchyOperation<?,?> tokenHierarchyOperation() {
 
153
        return null;
 
154
    }
 
155
    
 
156
    public InputAttributes inputAttributes() {
 
157
        return null;
 
158
    }
 
159
 
 
160
    public int startOffset() {
 
161
        if (tokenCountCurrent() > 0 || tokenCount() > 0)
 
162
            return tokenOffset(0);
 
163
        return 0;
 
164
    }
 
165
 
 
166
    public int endOffset() {
 
167
        int cntM1 = tokenCount() - 1;
 
168
        if (cntM1 >= 0)
 
169
            return tokenOffset(cntM1) + LexerUtilsConstants.token(this, cntM1).length();
 
170
        return 0;
 
171
    }
 
172
 
 
173
    public boolean isContinuous() {
 
174
        return true;
 
175
    }
 
176
 
 
177
    public Set<T> skipTokenIds() {
 
178
        return null;
 
179
    }
 
180
 
 
181
}