KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > lexer > demo > StringLexerInput


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.lexer.demo;
21
22 import org.netbeans.api.lexer.Token;
23 import org.netbeans.api.lexer.LexerInput;
24 import org.netbeans.api.lexer.TokenId;
25 import org.netbeans.spi.lexer.util.AbstractCharSequence;
26 import org.netbeans.spi.lexer.util.CharSubSequence;
27 import org.netbeans.spi.lexer.util.Compatibility;
28
29 /**
30  * Token iterator that works over the given char sequence.
31  *
32  * @author Miloslav Metelka
33  * @version 1.00
34  */

35
36 public class StringLexerInput implements LexerInput {
37
38     private String JavaDoc text;
39
40     /** Index from which the read() methods read the next character */
41     private int inputIndex;
42     
43     /** Index of the begining of the current token */
44     private int tokenIndex;
45     
46     /** Helper variable for getReadLookahead() computation. */
47     private int lookaheadIndex;
48     
49     /** 1 if lookahead reached EOF or 0 if not */
50     private int eof;
51
52     /**
53      * Lazily created character sequence representing subsequence of text read
54      * from the lexer input by read() operations.
55      */

56     private CharSubSequence subReadText;
57     
58     public StringLexerInput(String JavaDoc text) {
59         this.text = text;
60     }
61     
62     public int read() {
63         if (inputIndex >= text.length()) {
64             eof = 1;
65             return LexerInput.EOF;
66             
67         } else {
68             return text.charAt(inputIndex++);
69         }
70     }
71     
72     public int getReadLookahead() {
73         return Math.max(lookaheadIndex, inputIndex + eof) - tokenIndex;
74     }
75     
76     public int getReadLength() {
77         return inputIndex - tokenIndex;
78     }
79     
80     public boolean isEOFLookahead() {
81         return (eof != 0);
82     }
83     
84     public void backup(int count) {
85         lookaheadIndex = Math.max(lookaheadIndex, inputIndex + eof);
86         inputIndex -= count;
87         if (inputIndex < tokenIndex) {
88             inputIndex += count;
89             throw new IllegalArgumentException JavaDoc("count=" + count
90                 + " > " + (inputIndex - tokenIndex));
91             
92         } else if (inputIndex > lookaheadIndex - eof) {
93             inputIndex += count;
94             throw new IllegalArgumentException JavaDoc("count=" + count
95                 + " < " + (inputIndex + eof - lookaheadIndex));
96         }
97     }
98     
99     public Token createToken(TokenId id, int tokenLength) {
100         if (tokenLength <= 0) {
101             throw new IllegalArgumentException JavaDoc("tokenLength="
102                 + tokenLength + " <= 0");
103         }
104
105         if (tokenIndex + tokenLength > inputIndex) {
106             throw new IllegalArgumentException JavaDoc("tokenLength="
107                 + tokenLength + " > number-of-read-characters="
108                 + (inputIndex - tokenIndex)
109             );
110         }
111
112         Token ret = new StringToken(id, text.substring(tokenIndex,
113             tokenIndex + tokenLength));
114         tokenIndex += tokenLength;
115         return ret;
116     }
117     
118     public Token createToken(TokenId id) {
119         return createToken(id, inputIndex - tokenIndex);
120     }
121
122     public CharSequence JavaDoc getReadText(int start, int end) {
123         if (subReadText == null) {
124             subReadText = new CharSubSequence(new ReadText());
125         }
126
127         subReadText.setBounds(start, end);
128
129         return subReadText;
130     }
131     
132     char readTextCharAt(int index) {
133         if (index < 0) {
134             throw new IndexOutOfBoundsException JavaDoc("index=" + index + " < 0");
135         }
136
137         if (index >= getReadLength()) {
138             throw new IndexOutOfBoundsException JavaDoc("index=" + index
139                 + " >= getReadLength()=" + getReadLength());
140         }
141
142         return text.charAt(tokenIndex + index);
143     }
144     
145     private class ReadText extends AbstractCharSequence {
146         
147         public int length() {
148             return getReadLength();
149         }
150
151         public char charAt(int index) {
152             return readTextCharAt(index);
153         }
154         
155     }
156
157 }
158
Popular Tags