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: import org.netbeans.lib.lexer.token.AbstractToken;
047:
048: /**
049: * Abstract lexer input operation over a character sequence.
050: *
051: * @author Miloslav Metelka
052: * @version 1.00
053: */
054:
055: public class TextLexerInputOperation<T extends TokenId> extends
056: LexerInputOperation<T> {
057:
058: /**
059: * Input text from which the reading of characters is done.
060: */
061: private final CharSequence inputText;
062:
063: private final int inputTextStartOffset;
064:
065: /**
066: * End of valid chars in readCharArray (points to first invalid char).
067: */
068: private int readEndIndex;
069:
070: public TextLexerInputOperation(TokenList<T> tokenList,
071: CharSequence inputText) {
072: this (tokenList, 0, null, inputText, 0, 0, inputText.length());
073: }
074:
075: public TextLexerInputOperation(TokenList<T> tokenList,
076: int tokenIndex, Object lexerRestartState,
077: CharSequence inputText, int inputTextStartOffset,
078: int startOffset, int endOffset) {
079: super (tokenList, tokenIndex, lexerRestartState);
080: this .inputText = inputText;
081: this .inputTextStartOffset = inputTextStartOffset;
082:
083: // Make the offsets relative to the input start offset
084: startOffset -= inputTextStartOffset;
085: endOffset -= inputTextStartOffset;
086: assert (0 <= startOffset) && (startOffset <= endOffset)
087: && (endOffset <= inputText.length()) : "startOffset="
088: + startOffset + ", endOffset=" + endOffset
089: + ", inputText.length()=" + inputText.length();
090: setTokenStartIndex(startOffset);
091: readEndIndex = endOffset;
092: }
093:
094: public int read(int index) { // index >= 0 is guaranteed by contract
095: index += tokenStartIndex();
096: if (index < readEndIndex) {
097: return inputText.charAt(index);
098: } else { // must read next or return EOF
099: return LexerInput.EOF;
100: }
101: }
102:
103: public char readExisting(int index) {
104: return inputText.charAt(tokenStartIndex() + index);
105: }
106:
107: public void approveToken(AbstractToken<T> token) {
108: if (isSkipToken(token)) {
109: preventFlyToken();
110:
111: } else if (token.isFlyweight()) {
112: assert isFlyTokenAllowed();
113: flyTokenAdded();
114:
115: } else { // non-flyweight token
116: token.setTokenList(tokenList());
117: token
118: .setRawOffset(inputTextStartOffset
119: + tokenStartIndex());
120: clearFlySequence();
121: }
122:
123: tokenApproved();
124: }
125:
126: protected final int readEndIndex() {
127: return readEndIndex;
128: }
129:
130: }
|