KickJava   Java API By Example, From Geeks To Geeks.

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


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.lib.editor.util.ArrayUtilities;
23
24 /**
25  * Provides characters to the clients.
26  * <br/>
27  * It's implemented either by lexer input operation or preprocessor operation.
28  *
29  * @author Miloslav Metelka
30  * @version 1.00
31  */

32
33 public interface CharProvider {
34
35     /**
36      * Read next char from input.
37      */

38     int read();
39
40     /**
41      * Read char that was already read.
42      */

43     char readExisting(int index);
44
45     /**
46      * Present read index.
47      */

48     int readIndex();
49
50     /**
51      * Get cumulative length in the parent character providers
52      * that corresponds to the length in this provider.
53      */

54     int deepRawLength(int length);
55     
56     /**
57      * Get cumulative length shift in the parent character providers
58      * that corresponds to the given index.
59      */

60     int deepRawLengthShift(int index);
61     
62     /**
63      * Backup given number of characters.
64      * <br/>
65      * The EOF cannot be backup-ed.
66      */

67     void backup(int count);
68
69     /**
70      * Retrieve present token length assigned
71      * during tokenRecognized() method.
72      */

73     int tokenLength();
74     
75     /**
76      * Notify this provider that a token with the given length was recognized.
77      * <br/>
78      * The token length for this particular provider may differ from the real
79      * token length in the root lexer input operation due to character
80      * preprocessing.
81      * <br/>
82      * The tokenLength should be cached by this provider.
83      * @param skip whether the token will be skipped due to filtering of its id.
84      * @return true if the token is preprocessed or false otherwise.
85      */

86     void tokenRecognized(int tokenLength);
87     
88     /**
89      * Notify this provider that the token was approved and
90      * that the tokenLength number of characters should be skipped
91      * (tokenLength should be cached by the provider).
92      */

93     void tokenApproved();
94     
95     /**
96      * Collect extra preprocessed characters from the parent providers.
97      * <br/>
98      * They can consist from extra characters before the preprocessed characters
99      * in the top provider and the extra characters after
100      * the preprocessed characters in the top provider.
101      *
102      * @param epc non-null extra preprocessed characters.
103      * @param prepStartIndex first preprocessed index in the parent provider.
104      * @param prepEndIndex first non-preprocessed index in the parent provider.
105      * @param topPrepEndIndex top provider's prep end index - needed for proper
106      * computation of raw length shift.
107      */

108     void collectExtraPreprocessedChars(ExtraPreprocessedChars epc,
109     int prepStartIndex, int prepEndIndex, int topPrepEndIndex);
110     
111     
112     /**
113      * Storage of the extra preprocessed characters in parent providers.
114      */

115     public static final class ExtraPreprocessedChars {
116         
117         private int preStartIndex;
118         
119         private int postEndIndex;
120         
121         private char[] extraPrepChars = ArrayUtilities.emptyCharArray();
122         
123         private int[] extraRawLengthShifts;
124         
125         public void ensureExtraLength(int length) {
126             int preLength = extraPrepChars.length - preStartIndex;
127             length += postEndIndex + preLength;
128             if (length > extraPrepChars.length) {
129                 length <<= 1;
130                 extraPrepChars = ArrayUtilities.charArray(extraPrepChars, length,
131                         postEndIndex, preStartIndex - postEndIndex);
132                 extraRawLengthShifts = ArrayUtilities.intArray(extraRawLengthShifts,
133                         length, postEndIndex, preStartIndex - postEndIndex);
134                 preStartIndex = extraPrepChars.length - preLength;
135             }
136         }
137         
138         public void insert(char ch, int rawLengthShift) {
139             preStartIndex--;
140             extraPrepChars[extraPrepChars.length - preStartIndex] = ch;
141             extraRawLengthShifts[extraPrepChars.length - preStartIndex] = rawLengthShift;
142         }
143         
144         public void append(char ch, int rawLengthShift) {
145             extraPrepChars[postEndIndex] = ch;
146             extraRawLengthShifts[postEndIndex] = rawLengthShift;
147             postEndIndex++;
148         }
149         
150         public void clear() {
151             preStartIndex = extraPrepChars.length;
152             postEndIndex = 0;
153         }
154         
155         public int preStartIndex() {
156             return preStartIndex;
157         }
158         
159         public int postEndIndex() {
160             return postEndIndex;
161         }
162         
163         public char[] extraPrepChars() {
164             return extraPrepChars;
165         }
166         
167         public int[] extraRawLengthShifts() {
168             return extraRawLengthShifts;
169         }
170         
171     }
172     
173 }
174
Popular Tags