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.modules.lexer.demo;
043:
044: import org.netbeans.api.lexer.Token;
045: import org.netbeans.api.lexer.LexerInput;
046: import org.netbeans.api.lexer.TokenId;
047: import org.netbeans.spi.lexer.util.AbstractCharSequence;
048: import org.netbeans.spi.lexer.util.CharSubSequence;
049: import org.netbeans.spi.lexer.util.Compatibility;
050:
051: /**
052: * Token iterator that works over the given char sequence.
053: *
054: * @author Miloslav Metelka
055: * @version 1.00
056: */
057:
058: public class StringLexerInput implements LexerInput {
059:
060: private String text;
061:
062: /** Index from which the read() methods read the next character */
063: private int inputIndex;
064:
065: /** Index of the begining of the current token */
066: private int tokenIndex;
067:
068: /** Helper variable for getReadLookahead() computation. */
069: private int lookaheadIndex;
070:
071: /** 1 if lookahead reached EOF or 0 if not */
072: private int eof;
073:
074: /**
075: * Lazily created character sequence representing subsequence of text read
076: * from the lexer input by read() operations.
077: */
078: private CharSubSequence subReadText;
079:
080: public StringLexerInput(String text) {
081: this .text = text;
082: }
083:
084: public int read() {
085: if (inputIndex >= text.length()) {
086: eof = 1;
087: return LexerInput.EOF;
088:
089: } else {
090: return text.charAt(inputIndex++);
091: }
092: }
093:
094: public int getReadLookahead() {
095: return Math.max(lookaheadIndex, inputIndex + eof) - tokenIndex;
096: }
097:
098: public int getReadLength() {
099: return inputIndex - tokenIndex;
100: }
101:
102: public boolean isEOFLookahead() {
103: return (eof != 0);
104: }
105:
106: public void backup(int count) {
107: lookaheadIndex = Math.max(lookaheadIndex, inputIndex + eof);
108: inputIndex -= count;
109: if (inputIndex < tokenIndex) {
110: inputIndex += count;
111: throw new IllegalArgumentException("count=" + count + " > "
112: + (inputIndex - tokenIndex));
113:
114: } else if (inputIndex > lookaheadIndex - eof) {
115: inputIndex += count;
116: throw new IllegalArgumentException("count=" + count + " < "
117: + (inputIndex + eof - lookaheadIndex));
118: }
119: }
120:
121: public Token createToken(TokenId id, int tokenLength) {
122: if (tokenLength <= 0) {
123: throw new IllegalArgumentException("tokenLength="
124: + tokenLength + " <= 0");
125: }
126:
127: if (tokenIndex + tokenLength > inputIndex) {
128: throw new IllegalArgumentException("tokenLength="
129: + tokenLength + " > number-of-read-characters="
130: + (inputIndex - tokenIndex));
131: }
132:
133: Token ret = new StringToken(id, text.substring(tokenIndex,
134: tokenIndex + tokenLength));
135: tokenIndex += tokenLength;
136: return ret;
137: }
138:
139: public Token createToken(TokenId id) {
140: return createToken(id, inputIndex - tokenIndex);
141: }
142:
143: public CharSequence getReadText(int start, int end) {
144: if (subReadText == null) {
145: subReadText = new CharSubSequence(new ReadText());
146: }
147:
148: subReadText.setBounds(start, end);
149:
150: return subReadText;
151: }
152:
153: char readTextCharAt(int index) {
154: if (index < 0) {
155: throw new IndexOutOfBoundsException("index=" + index
156: + " < 0");
157: }
158:
159: if (index >= getReadLength()) {
160: throw new IndexOutOfBoundsException("index=" + index
161: + " >= getReadLength()=" + getReadLength());
162: }
163:
164: return text.charAt(tokenIndex + index);
165: }
166:
167: private class ReadText extends AbstractCharSequence {
168:
169: public int length() {
170: return getReadLength();
171: }
172:
173: public char charAt(int index) {
174: return readTextCharAt(index);
175: }
176:
177: }
178:
179: }
|