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.lib.editor.util.ArrayUtilities;
045: import org.netbeans.lib.editor.util.CharSequenceUtilities;
046: import org.netbeans.lib.editor.util.CharSubSequence;
047:
048: /**
049: * Storage of the preprocessed text specific for batch and mutable setups.
050: * <br/>
051: * For mutable setups the extra length shifts need to be stored.
052: * <br/>
053: * For storage costs cutting only a maximum lookahead is stored (not individual
054: * lookaheads).
055: *
056: * @author Miloslav Metelka
057: * @version 1.00
058: */
059:
060: public abstract class PreprocessedTextStorage implements CharSequence {
061:
062: /** Flag for additional correctness checks (may degrade performance). */
063: private static final boolean testing = Boolean
064: .getBoolean("netbeans.debug.lexer.test");
065:
066: /**
067: * Create preprocessed characters storage.
068: *
069: * @param rawText non-null raw text before preprocessing.
070: * @param preprocessedChars non-null array of preprocessed characters
071: * @param preprocessedCharsLength >=0 number of valid characters in the preprocessedChars array.
072: * @param preprocessedStartIndex index of first preprocessed character in the raw text.
073: * @param preprocessedRawLengthShifts non-null array of shifts of the particular
074: * end of a character in the raw text against the preprocessed text.
075: * For example "a\u0062c" will produce preprocessed "abc"
076: * and the raw length shifts would be { 1-1=0, 7-2=5, 8-3=5 }.
077: *
078: */
079: public static PreprocessedTextStorage create(CharSequence rawText,
080: char[] preprocessedChars, int preprocessedCharsLength,
081: int preprocessedStartIndex,
082: int[] preprocessedRawLengthShifts) {
083: char[] preprocessedCharsCopy = ArrayUtilities.charArray(
084: preprocessedChars, preprocessedCharsLength);
085: // Get maximum raw length shift (the one of the last character of preprocessedChars)
086: int totalRawLengthShift = preprocessedRawLengthShifts[preprocessedCharsLength - 1];
087: // Create appropriate storage according to the max. rawLengthShift
088: PreprocessedTextStorage storage;
089: if (totalRawLengthShift <= Byte.MAX_VALUE) { // Use bytes
090: byte[] arr = new byte[preprocessedCharsLength];
091: for (int i = preprocessedCharsLength - 1; i >= 0; i--)
092: arr[i] = (byte) preprocessedRawLengthShifts[i];
093: storage = new ByteRawIndex(rawText, preprocessedCharsCopy,
094: preprocessedStartIndex, preprocessedCharsLength,
095: totalRawLengthShift, arr);
096:
097: } else if (totalRawLengthShift <= Short.MAX_VALUE) { // Use shorts
098: short[] arr = new short[preprocessedCharsLength];
099: for (int i = preprocessedCharsLength - 1; i >= 0; i--)
100: arr[i] = (short) preprocessedRawLengthShifts[i];
101: storage = new ShortRawIndex(rawText, preprocessedCharsCopy,
102: preprocessedStartIndex, preprocessedCharsLength,
103: totalRawLengthShift, arr);
104:
105: } else { // Use ints
106: int[] arr = new int[preprocessedCharsLength];
107: System.arraycopy(preprocessedChars, 0, arr, 0,
108: preprocessedCharsLength);
109: storage = new IntRawIndex(rawText, preprocessedCharsCopy,
110: preprocessedStartIndex, preprocessedCharsLength,
111: totalRawLengthShift, arr);
112: }
113:
114: if (testing)
115: storage.consistencyCheck();
116: return storage;
117: }
118:
119: /**
120: * Create preprocessed characters storage.
121: *
122: * @param rawText non-null raw text before preprocessing.
123: * @param preprocessedChars non-null array of preprocessed characters
124: * @param preprocessedCharsLength >=0 number of valid characters in the preprocessedChars array.
125: * @param preprocessedStartIndex index of first preprocessed character in the raw text.
126: * @param preprocessedRawLengthShifts non-null array of shifts of the particular
127: * end of a character in the raw text against the preprocessed text.
128: * For example "a\u0062c" will produce preprocessed "abc"
129: * and the raw length shifts would be { 1-1=0, 7-2=5, 8-3=5 }.
130: *
131: */
132: public static PreprocessedTextStorage create(CharSequence rawText,
133: char[] preprocessedChars, int preprocessedCharsLength,
134: int preprocessedStartIndex,
135: int[] preprocessedRawLengthShifts,
136: char[] extraPreprocessedChars, int[] extraRawLengthShifts,
137: int preStartIndex, int postEndIndex) {
138: int extraPreCharsLength = (extraPreprocessedChars.length - preStartIndex);
139: preprocessedStartIndex -= extraPreCharsLength;
140: int length = extraPreCharsLength + preprocessedCharsLength
141: + postEndIndex;
142:
143: // Create copy of the characters
144: char[] preprocessedCharsCopy = new char[length];
145: System.arraycopy(extraPreprocessedChars, preStartIndex,
146: preprocessedCharsCopy, 0, extraPreCharsLength);
147: System.arraycopy(preprocessedChars, 0, preprocessedCharsCopy,
148: extraPreCharsLength, preprocessedCharsLength);
149: System.arraycopy(extraPreprocessedChars, 0,
150: preprocessedCharsCopy, extraPreCharsLength
151: + preprocessedCharsLength, postEndIndex);
152:
153: // Get maximum raw length shift (the one of the last character of preprocessedChars)
154: int totalRawLengthShift = (postEndIndex > 0) ? extraRawLengthShifts[postEndIndex - 1]
155: : (preprocessedCharsLength > 0) ? preprocessedRawLengthShifts[preprocessedCharsLength - 1]
156: // Otherwise get last of pre-chars
157: : extraRawLengthShifts[extraPreprocessedChars.length - 1];
158:
159: // Create appropriate storage according to the max. rawLengthShift
160: int ind = length - 1;
161: PreprocessedTextStorage storage;
162: if (totalRawLengthShift <= Byte.MAX_VALUE) { // Use bytes
163: byte[] arr = new byte[length];
164: for (int i = postEndIndex - 1; i >= 0; i--)
165: arr[ind--] = (byte) extraRawLengthShifts[i];
166: for (int i = preprocessedCharsLength - 1; i >= 0; i--)
167: arr[ind--] = (byte) preprocessedRawLengthShifts[i];
168: for (int i = extraPreprocessedChars.length - 1; i >= preStartIndex; i--)
169: arr[ind--] = (byte) extraRawLengthShifts[i];
170: storage = new ByteRawIndex(rawText, preprocessedCharsCopy,
171: preprocessedStartIndex, length,
172: totalRawLengthShift, arr);
173:
174: } else if (totalRawLengthShift <= Short.MAX_VALUE) { // Use shorts
175: short[] arr = new short[length];
176: for (int i = postEndIndex - 1; i >= 0; i--)
177: arr[ind--] = (short) extraRawLengthShifts[i];
178: for (int i = preprocessedCharsLength - 1; i >= 0; i--)
179: arr[ind--] = (short) preprocessedRawLengthShifts[i];
180: for (int i = extraPreprocessedChars.length - 1; i >= preStartIndex; i--)
181: arr[ind--] = (short) extraRawLengthShifts[i];
182: storage = new ShortRawIndex(rawText, preprocessedCharsCopy,
183: preprocessedStartIndex, length,
184: totalRawLengthShift, arr);
185:
186: } else { // Use ints
187: // System.arraycopy() could be used but usually bytes (or shorts for long tokens)
188: // would be used anyway so to eliminate errors use the copy-pasted code from above
189: int[] arr = new int[length];
190: for (int i = postEndIndex - 1; i >= 0; i--)
191: arr[ind--] = extraRawLengthShifts[i];
192: for (int i = preprocessedCharsLength - 1; i >= 0; i--)
193: arr[ind--] = preprocessedRawLengthShifts[i];
194: for (int i = extraPreprocessedChars.length - 1; i >= preStartIndex; i--)
195: arr[ind--] = extraRawLengthShifts[i];
196: storage = new IntRawIndex(rawText, preprocessedCharsCopy,
197: preprocessedStartIndex, length,
198: totalRawLengthShift, arr);
199: }
200:
201: if (testing)
202: storage.consistencyCheck();
203: return storage;
204: }
205:
206: /**
207: * Reference to original non-preprocessed text.
208: */
209: private final CharSequence rawText; // 12 bytes (8-Object + 4)
210:
211: /**
212: * Preprocessed characters.
213: */
214: private final char[] preprocessedChars; // 16 bytes
215:
216: /**
217: * Index of the first preprocessed char in the rawText.
218: */
219: private final int preprocessedStartIndex; // 20 bytes
220:
221: /**
222: * Raw index shift of the first char after preprocessedChars characters.
223: */
224: private final int totalRawLengthShift; // 24 bytes
225:
226: /**
227: * Cache the length of this text storage. Although it could be computed
228: * dynamically by using rawText.length() this avoids nested
229: * length() calls.
230: */
231: private final int length; // 28 bytes
232:
233: protected PreprocessedTextStorage(CharSequence rawText,
234: char[] preprocessedChars, int preprocessedStartIndex,
235: int length, int totalRawLengthShift) {
236: this .rawText = rawText;
237: this .preprocessedChars = preprocessedChars;
238: this .preprocessedStartIndex = preprocessedStartIndex;
239: // Compute end raw index without delegating to rawText methods e.g. length()
240: this .totalRawLengthShift = totalRawLengthShift;
241: this .length = length;
242: }
243:
244: protected abstract int prepRawLengthShift(int index);
245:
246: /**
247: * Raw length for the given length.
248: *
249: * @param length length in this character sequence.
250: * @return raw length for the given length.
251: */
252: public final int rawLength(int length) {
253: if (length > preprocessedStartIndex) {
254: int prepLength = length - preprocessedStartIndex;
255: if (prepLength <= preprocessedChars.length) {
256: length += prepRawLengthShift(prepLength - 1);
257: } else {
258: length += totalRawLengthShift;
259: }
260: }
261: return length;
262: }
263:
264: /**
265: * Raw length shift corresponding to the given index.
266: *
267: * @param index >=0 and <length() index in this text storage.
268: * @return raw length shift corresponding to the given index.
269: */
270: public final int rawLengthShift(int index) {
271: if (index < preprocessedStartIndex) {
272: return index;
273: } else {
274: index -= preprocessedStartIndex;
275: if (index <= preprocessedChars.length) {
276: return prepRawLengthShift(index);
277: } else { // past the end of preprocessed characters
278: return totalRawLengthShift;
279: }
280: }
281: }
282:
283: public final char charAt(int index) {
284: CharSequenceUtilities.checkIndexValid(index, length);
285: if (index < preprocessedStartIndex) {
286: return rawText.charAt(index);
287: } else {
288: int prepIndex = index - preprocessedStartIndex;
289: if (prepIndex < preprocessedChars.length) {
290: return preprocessedChars[prepIndex];
291: } else { // past the end of preprocessed characters
292: return rawText.charAt(index + totalRawLengthShift);
293: }
294: }
295: }
296:
297: public final CharSequence subSequence(int start, int end) {
298: return new CharSubSequence(this , start, end);
299: }
300:
301: public final int length() {
302: return length;
303: }
304:
305: private void consistencyCheck() {
306: // Check that raw length shifts have increasing order
307: int lastRLS = 0;
308: for (int i = 0; i < preprocessedChars.length; i++) {
309: int rls = prepRawLengthShift(i);
310: if (rls < lastRLS) {
311: throw new IllegalStateException("rls=" + rls // NOI18N
312: + " < lastRLS=" + lastRLS + " at index=" + i); // NOI18N
313: }
314: lastRLS = rls;
315: }
316: }
317:
318: private static final class ByteRawIndex extends
319: PreprocessedTextStorage {
320:
321: private final byte[] preprocessedRawLengthShifts; // 24 bytes
322:
323: ByteRawIndex(CharSequence rawText, char[] preprocessedChars,
324: int preprocessedStartIndex, int length,
325: int totalRawLengthShift,
326: byte[] preprocessedRawLengthShifts) {
327: super (rawText, preprocessedChars, preprocessedStartIndex,
328: length, totalRawLengthShift);
329: this .preprocessedRawLengthShifts = preprocessedRawLengthShifts;
330: }
331:
332: protected final int prepRawLengthShift(int index) {
333: return preprocessedRawLengthShifts[index];
334: }
335:
336: }
337:
338: private static final class ShortRawIndex extends
339: PreprocessedTextStorage {
340:
341: private final short[] preprocessedRawLengthShifts; // 24 bytes
342:
343: ShortRawIndex(CharSequence rawText, char[] preprocessedChars,
344: int preprocessedStartIndex, int length,
345: int totalRawLengthShift,
346: short[] preprocessedRawLengthShifts) {
347: super (rawText, preprocessedChars, preprocessedStartIndex,
348: length, totalRawLengthShift);
349: this .preprocessedRawLengthShifts = preprocessedRawLengthShifts;
350: }
351:
352: protected final int prepRawLengthShift(int index) {
353: return preprocessedRawLengthShifts[index];
354: }
355:
356: }
357:
358: private static final class IntRawIndex extends
359: PreprocessedTextStorage {
360:
361: private final int[] preprocessedRawLengthShifts; // 24 bytes
362:
363: IntRawIndex(CharSequence rawText, char[] preprocessedChars,
364: int preprocessedStartIndex, int length,
365: int totalRawLengthShift,
366: int[] preprocessedRawLengthShifts) {
367: super (rawText, preprocessedChars, preprocessedStartIndex,
368: length, totalRawLengthShift);
369: this .preprocessedRawLengthShifts = preprocessedRawLengthShifts;
370: }
371:
372: protected final int prepRawLengthShift(int index) {
373: return preprocessedRawLengthShifts[index];
374: }
375:
376: }
377:
378: }
|