KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > lexer > TextLexerInputOperation


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.lib.lexer;
21
22 import org.netbeans.api.lexer.TokenId;
23 import org.netbeans.spi.lexer.LexerInput;
24 import org.netbeans.lib.lexer.token.AbstractToken;
25
26 /**
27  * Abstract lexer input operation over a character sequence.
28  *
29  * @author Miloslav Metelka
30  * @version 1.00
31  */

32
33 public class TextLexerInputOperation<T extends TokenId> extends LexerInputOperation<T> {
34
35     /**
36      * Input text from which the reading of characters is done.
37      */

38     private final CharSequence JavaDoc inputText;
39
40     private final int inputTextStartOffset;
41     
42     /**
43      * End of valid chars in readCharArray (points to first invalid char).
44      */

45     private int readEndIndex;
46     
47
48     public TextLexerInputOperation(TokenList<T> tokenList, CharSequence JavaDoc inputText) {
49         this(tokenList, 0, null, inputText, 0, 0, inputText.length());
50     }
51
52     public TextLexerInputOperation(TokenList<T> tokenList, int tokenIndex,
53     Object JavaDoc lexerRestartState, CharSequence JavaDoc inputText, int inputTextStartOffset,
54     int startOffset, int endOffset) {
55         super(tokenList, tokenIndex, lexerRestartState);
56         this.inputText = inputText;
57         this.inputTextStartOffset = inputTextStartOffset;
58
59         // Make the offsets relative to the input start offset
60
startOffset -= inputTextStartOffset;
61         endOffset -= inputTextStartOffset;
62         assert (0 <= startOffset) && (startOffset <= endOffset)
63             && (endOffset <= inputText.length());
64         setTokenStartIndex(startOffset);
65         readEndIndex = endOffset;
66     }
67     
68     public int read(int index) { // index >= 0 is guaranteed by contract
69
index += tokenStartIndex();
70         if (index < readEndIndex) {
71             return inputText.charAt(index);
72         } else { // must read next or return EOF
73
return LexerInput.EOF;
74         }
75     }
76
77     public char readExisting(int index) {
78         return inputText.charAt(tokenStartIndex() + index);
79     }
80
81     public void approveToken(AbstractToken<T> token) {
82         int tokenLength = tokenLength();
83         if (isSkipToken(token)) {
84             preventFlyToken();
85
86         } else if (token.isFlyweight()) {
87             assert isFlyTokenAllowed();
88             flyTokenAdded();
89
90         } else { // non-flyweight token
91
token.setTokenList(tokenList());
92             token.setRawOffset(inputTextStartOffset + tokenStartIndex());
93             clearFlySequence();
94         }
95
96         tokenApproved();
97     }
98
99     protected final int readEndIndex() {
100         return readEndIndex;
101     }
102
103 }
104
Popular Tags