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;
043:
044: import org.netbeans.api.lexer.TokenId;
045: import org.netbeans.spi.lexer.LexerInput;
046:
047: /**
048: * Used when branching a token with preprocessed text.
049: *
050: * @author Miloslav Metelka
051: * @version 1.00
052: */
053:
054: public final class PreprocessedTextLexerInputOperation<T extends TokenId>
055: extends TextLexerInputOperation<T> {
056:
057: private final PreprocessedTextStorage preprocessedText;
058:
059: private int prepStartIndex;
060:
061: private int prepEndIndex;
062:
063: private int tokenStartRawLengthShift;
064:
065: private int lastRawLengthShift;
066:
067: private int tokenEndRawLengthShift;
068:
069: public PreprocessedTextLexerInputOperation(TokenList<T> tokenList,
070: PreprocessedTextStorage prepText) {
071: this (tokenList, 0, null, prepText, 0, 0, prepText.length());
072: }
073:
074: public PreprocessedTextLexerInputOperation(TokenList<T> tokenList,
075: int tokenIndex, Object lexerRestartState,
076: PreprocessedTextStorage prepText, int prepTextStartOffset,
077: int startOffset, int endOffset) {
078: super (tokenList, tokenIndex, lexerRestartState, prepText,
079: prepTextStartOffset, startOffset, endOffset);
080: this .preprocessedText = prepText;
081: int index = startOffset - prepTextStartOffset;
082: if (index > 0) {
083: tokenStartRawLengthShift = preprocessedText
084: .rawLengthShift(index);
085: lastRawLengthShift = tokenStartRawLengthShift;
086: }
087: preprocessingLevelCount++; // extra level of preprocessing
088: }
089:
090: public int deepRawLength(int length) {
091: return length
092: + preprocessedText.rawLengthShift(tokenStartIndex()
093: + length - 1) - tokenStartRawLengthShift;
094: }
095:
096: public int deepRawLengthShift(int index) {
097: return preprocessedText.rawLengthShift(tokenStartIndex()
098: + index)
099: - tokenStartRawLengthShift;
100: }
101:
102: public int read(int index) { // index >= 0 is guaranteed by contract
103: index += tokenStartIndex();
104: if (index < readEndIndex()) {
105: // Check whether the char is preprocessed
106: int rls = preprocessedText.rawLengthShift(index);
107: if (rls != lastRawLengthShift) { // Preprocessed
108: lastRawLengthShift = rls;
109: if (prepStartIndex >= index) { // prepStartIndex already inited
110: prepStartIndex = index;
111: }
112: prepEndIndex = index + 1;
113: }
114: return preprocessedText.charAt(index);
115: } else { // must read next or return EOF
116: return LexerInput.EOF;
117: }
118: }
119:
120: public void tokenRecognized(int tokenLength) {
121: super .tokenRecognized(tokenLength);
122: tokenEndRawLengthShift = preprocessedText
123: .rawLengthShift(tokenStartIndex() + tokenLength() - 1);
124: }
125:
126: public void tokenApproved() {
127: // Increase base raw length shift by the token's last-char shift
128: tokenStartRawLengthShift += tokenEndRawLengthShift;
129:
130: if (prepStartIndex != Integer.MAX_VALUE) { // some prep chars (may be after token length)
131: if (prepStartIndex < tokenLength()) { // prep chars before token end
132: if (prepEndIndex <= tokenLength()) { // no preprocessed chars past token end
133: prepStartIndex = Integer.MAX_VALUE; // signal no preprocessed chars
134: } else { // prepEndIndex > tokenLength => initial prep chars in the next token
135: prepStartIndex = 0;
136: prepEndIndex -= tokenLength();
137: }
138:
139: } else { // prepStartIndex >= tokenLength
140: prepStartIndex -= tokenLength();
141: prepEndIndex -= tokenLength();
142: }
143: }
144: super .tokenApproved();
145: }
146:
147: public void collectExtraPreprocessedChars(
148: CharProvider.ExtraPreprocessedChars epc,
149: int prepStartIndex, int prepEndIndex, int topPrepEndIndex) {
150: if (prepStartIndex < tokenLength()) { // Some preprocessed characters
151: // Check for any pre-prepChars
152: int preCount = Math.max(prepStartIndex
153: - this .prepStartIndex, 0);
154: // Check for post-prepChars
155: int postCount;
156: if (this .prepEndIndex > tokenLength()) {
157: postCount = tokenLength() - prepEndIndex;
158: if (postCount > 0) {
159: int i = tokenLength() - 2;
160: // Optimize the case when there are lookahead chars
161: // for the present token and the ending chars could possibly
162: // be non-preprocessed (prepEndIndex > tokenLength)
163: while (--i >= prepStartIndex
164: && postCount > 0
165: && preprocessedText.rawLengthShift(i
166: + tokenStartIndex()) == tokenEndRawLengthShift) { // not preprocessed
167: postCount--;
168: }
169: } else
170: // postCount <= 0
171: postCount = 0;
172:
173: } else { // this.prepEndIndex <= tokenLength
174: postCount = this .prepEndIndex - prepEndIndex;
175: }
176:
177: assert (preCount >= 0 && postCount >= 0);
178: epc.ensureExtraLength(preCount + postCount);
179: while (--preCount >= 0) {
180: epc.insert(readExisting(prepStartIndex - 1),
181: deepRawLength(prepStartIndex) - prepStartIndex);
182: prepStartIndex--;
183: }
184: while (--postCount >= 0) {
185: epc.append(readExisting(prepEndIndex),
186: deepRawLength(prepEndIndex) - topPrepEndIndex);
187: prepEndIndex++;
188: topPrepEndIndex++;
189: }
190: }
191: }
192:
193: }
|