aoqi@0: /* aoqi@0: * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: /* aoqi@0: * Copyright (C) 2004-2012 aoqi@0: * aoqi@0: * Permission is hereby granted, free of charge, to any person obtaining a copy aoqi@0: * of this software and associated documentation files (the "Software"), to deal aoqi@0: * in the Software without restriction, including without limitation the rights aoqi@0: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell aoqi@0: * copies of the Software, and to permit persons to whom the Software is aoqi@0: * furnished to do so, subject to the following conditions: aoqi@0: * aoqi@0: * The above copyright notice and this permission notice shall be included in aoqi@0: * all copies or substantial portions of the Software. aoqi@0: * aoqi@0: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR aoqi@0: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE aoqi@0: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER aoqi@0: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, aoqi@0: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN aoqi@0: * THE SOFTWARE. aoqi@0: */ aoqi@0: /* Generated By:JavaCC: Do not edit this line. UCode_UCodeESC_CharStream.java Version 0.7pre6 */ aoqi@0: /* The previous line keeps JavaCC quiet. In fact, the JavaCC generated file aoqi@0: has been edited to fix some bugs. */ aoqi@0: package com.sun.xml.internal.rngom.parse.compact; aoqi@0: aoqi@0: import com.sun.xml.internal.rngom.util.Utf16; aoqi@0: import com.sun.xml.internal.rngom.ast.builder.BuildException; aoqi@0: aoqi@0: import java.io.IOException; aoqi@0: aoqi@0: /** aoqi@0: * An implementation of interface CharStream, where the stream is assumed to aoqi@0: * contain 16-bit unicode characters. aoqi@0: */ aoqi@0: public final class UCode_UCodeESC_CharStream { aoqi@0: aoqi@0: public static final boolean staticFlag = false; aoqi@0: aoqi@0: static final int hexval(char c) { aoqi@0: switch (c) { aoqi@0: case '0': aoqi@0: return 0; aoqi@0: case '1': aoqi@0: return 1; aoqi@0: case '2': aoqi@0: return 2; aoqi@0: case '3': aoqi@0: return 3; aoqi@0: case '4': aoqi@0: return 4; aoqi@0: case '5': aoqi@0: return 5; aoqi@0: case '6': aoqi@0: return 6; aoqi@0: case '7': aoqi@0: return 7; aoqi@0: case '8': aoqi@0: return 8; aoqi@0: case '9': aoqi@0: return 9; aoqi@0: aoqi@0: case 'a': aoqi@0: case 'A': aoqi@0: return 10; aoqi@0: case 'b': aoqi@0: case 'B': aoqi@0: return 11; aoqi@0: case 'c': aoqi@0: case 'C': aoqi@0: return 12; aoqi@0: case 'd': aoqi@0: case 'D': aoqi@0: return 13; aoqi@0: case 'e': aoqi@0: case 'E': aoqi@0: return 14; aoqi@0: case 'f': aoqi@0: case 'F': aoqi@0: return 15; aoqi@0: } aoqi@0: return -1; aoqi@0: } aoqi@0: public int bufpos = -1; aoqi@0: int bufsize; aoqi@0: int available; aoqi@0: int tokenBegin; aoqi@0: private int bufline[]; aoqi@0: private int bufcolumn[]; aoqi@0: private int column = 0; aoqi@0: private int line = 1; aoqi@0: private java.io.Reader inputStream; aoqi@0: private boolean closed = false; aoqi@0: private boolean prevCharIsLF = false; aoqi@0: private char[] nextCharBuf; aoqi@0: private char[] buffer; aoqi@0: private int maxNextCharInd = 0; aoqi@0: private int nextCharInd = -1; aoqi@0: private int inBuf = 0; aoqi@0: aoqi@0: private final void ExpandBuff(boolean wrapAround) { aoqi@0: char[] newbuffer = new char[bufsize + 2048]; aoqi@0: int newbufline[] = new int[bufsize + 2048]; aoqi@0: int newbufcolumn[] = new int[bufsize + 2048]; aoqi@0: aoqi@0: if (wrapAround) { aoqi@0: System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); aoqi@0: System.arraycopy(buffer, 0, newbuffer, aoqi@0: bufsize - tokenBegin, bufpos); aoqi@0: buffer = newbuffer; aoqi@0: aoqi@0: System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); aoqi@0: System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos); aoqi@0: bufline = newbufline; aoqi@0: aoqi@0: System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); aoqi@0: System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos); aoqi@0: bufcolumn = newbufcolumn; aoqi@0: aoqi@0: bufpos += (bufsize - tokenBegin); aoqi@0: } else { aoqi@0: System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); aoqi@0: buffer = newbuffer; aoqi@0: aoqi@0: System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); aoqi@0: bufline = newbufline; aoqi@0: aoqi@0: System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); aoqi@0: bufcolumn = newbufcolumn; aoqi@0: aoqi@0: bufpos -= tokenBegin; aoqi@0: } aoqi@0: aoqi@0: available = (bufsize += 2048); aoqi@0: tokenBegin = 0; aoqi@0: } aoqi@0: aoqi@0: private final void FillBuff() throws EOFException { aoqi@0: int i; aoqi@0: if (maxNextCharInd == 4096) { aoqi@0: maxNextCharInd = nextCharInd = 0; aoqi@0: } aoqi@0: aoqi@0: if (closed) { aoqi@0: throw new EOFException(); aoqi@0: } aoqi@0: try { aoqi@0: if ((i = inputStream.read(nextCharBuf, maxNextCharInd, 4096 - maxNextCharInd)) == -1) { aoqi@0: closed = true; aoqi@0: inputStream.close(); aoqi@0: throw new EOFException(); aoqi@0: } else { aoqi@0: maxNextCharInd += i; aoqi@0: } aoqi@0: } catch (IOException e) { aoqi@0: throw new BuildException(e); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: private final char ReadChar() throws EOFException { aoqi@0: if (++nextCharInd >= maxNextCharInd) { aoqi@0: FillBuff(); aoqi@0: } aoqi@0: aoqi@0: return nextCharBuf[nextCharInd]; aoqi@0: } aoqi@0: aoqi@0: private final char PeekChar() throws EOFException { aoqi@0: char c = ReadChar(); aoqi@0: --nextCharInd; aoqi@0: return c; aoqi@0: } aoqi@0: aoqi@0: public final char BeginToken() throws EOFException { aoqi@0: if (inBuf > 0) { aoqi@0: --inBuf; aoqi@0: return buffer[tokenBegin = (bufpos == bufsize - 1) ? (bufpos = 0) aoqi@0: : ++bufpos]; aoqi@0: } aoqi@0: aoqi@0: tokenBegin = 0; aoqi@0: bufpos = -1; aoqi@0: aoqi@0: return readChar(); aoqi@0: } aoqi@0: aoqi@0: private final void AdjustBuffSize() { aoqi@0: if (available == bufsize) { aoqi@0: if (tokenBegin > 2048) { aoqi@0: bufpos = 0; aoqi@0: available = tokenBegin; aoqi@0: } else { aoqi@0: ExpandBuff(false); aoqi@0: } aoqi@0: } else if (available > tokenBegin) { aoqi@0: available = bufsize; aoqi@0: } else if ((tokenBegin - available) < 2048) { aoqi@0: ExpandBuff(true); aoqi@0: } else { aoqi@0: available = tokenBegin; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: private final void UpdateLineColumn(char c) { aoqi@0: column++; aoqi@0: aoqi@0: if (prevCharIsLF) { aoqi@0: prevCharIsLF = false; aoqi@0: line += (column = 1); aoqi@0: } aoqi@0: aoqi@0: switch (c) { aoqi@0: case NEWLINE_MARKER: aoqi@0: prevCharIsLF = true; aoqi@0: break; aoqi@0: case '\t': aoqi@0: column--; aoqi@0: column += (8 - (column & 07)); aoqi@0: break; aoqi@0: default: aoqi@0: break; aoqi@0: } aoqi@0: aoqi@0: bufline[bufpos] = line; aoqi@0: bufcolumn[bufpos] = column; aoqi@0: } aoqi@0: private static final char NEWLINE_MARKER = '\u0000'; aoqi@0: aoqi@0: public final char readChar() throws EOFException { aoqi@0: if (inBuf > 0) { aoqi@0: --inBuf; aoqi@0: return buffer[(bufpos == bufsize - 1) ? (bufpos = 0) : ++bufpos]; aoqi@0: } aoqi@0: aoqi@0: char c; aoqi@0: try { aoqi@0: c = ReadChar(); aoqi@0: switch (c) { aoqi@0: case '\r': aoqi@0: c = NEWLINE_MARKER; aoqi@0: try { aoqi@0: if (PeekChar() == '\n') { aoqi@0: ReadChar(); aoqi@0: } aoqi@0: } catch (EOFException e) { aoqi@0: } aoqi@0: break; aoqi@0: case '\n': aoqi@0: c = NEWLINE_MARKER; aoqi@0: break; aoqi@0: case '\t': aoqi@0: break; aoqi@0: default: aoqi@0: if (c >= 0x20) { aoqi@0: if (Utf16.isSurrogate(c)) { aoqi@0: if (Utf16.isSurrogate2(c)) { aoqi@0: throw new EscapeSyntaxException("illegal_surrogate_pair", line, column + 1); aoqi@0: } aoqi@0: if (++bufpos == available) { aoqi@0: AdjustBuffSize(); aoqi@0: } aoqi@0: buffer[bufpos] = c; aoqi@0: // UpdateLineColumn(c); aoqi@0: try { aoqi@0: c = ReadChar(); aoqi@0: } catch (EOFException e) { aoqi@0: throw new EscapeSyntaxException("illegal_surrogate_pair", line, column + 1); aoqi@0: } aoqi@0: if (!Utf16.isSurrogate2(c)) { aoqi@0: throw new EscapeSyntaxException("illegal_surrogate_pair", line, column + 2); aoqi@0: } aoqi@0: } aoqi@0: break; aoqi@0: } aoqi@0: // fall through aoqi@0: case '\uFFFE': aoqi@0: case '\uFFFF': aoqi@0: throw new EscapeSyntaxException("illegal_char_code", line, column + 1); aoqi@0: } aoqi@0: } catch (EOFException e) { aoqi@0: if (bufpos == -1) { aoqi@0: if (++bufpos == available) { aoqi@0: AdjustBuffSize(); aoqi@0: } aoqi@0: bufline[bufpos] = line; aoqi@0: bufcolumn[bufpos] = column; aoqi@0: } aoqi@0: throw e; aoqi@0: } aoqi@0: if (++bufpos == available) { aoqi@0: AdjustBuffSize(); aoqi@0: } aoqi@0: buffer[bufpos] = c; aoqi@0: UpdateLineColumn(c); aoqi@0: try { aoqi@0: if (c != '\\' || PeekChar() != 'x') { aoqi@0: return c; aoqi@0: } aoqi@0: } catch (EOFException e) { aoqi@0: return c; aoqi@0: } aoqi@0: aoqi@0: int xCnt = 1; aoqi@0: for (;;) { aoqi@0: ReadChar(); aoqi@0: if (++bufpos == available) { aoqi@0: AdjustBuffSize(); aoqi@0: } aoqi@0: buffer[bufpos] = 'x'; aoqi@0: UpdateLineColumn('x'); aoqi@0: try { aoqi@0: c = PeekChar(); aoqi@0: } catch (EOFException e) { aoqi@0: backup(xCnt); aoqi@0: return '\\'; aoqi@0: } aoqi@0: if (c == '{') { aoqi@0: ReadChar(); aoqi@0: column++; aoqi@0: // backup past the 'x's aoqi@0: bufpos -= xCnt; aoqi@0: if (bufpos < 0) { aoqi@0: bufpos += bufsize; aoqi@0: } aoqi@0: break; aoqi@0: } aoqi@0: if (c != 'x') { aoqi@0: backup(xCnt); aoqi@0: return '\\'; aoqi@0: } aoqi@0: xCnt++; aoqi@0: } aoqi@0: try { aoqi@0: int scalarValue = hexval(ReadChar()); aoqi@0: column++; aoqi@0: if (scalarValue < 0) { aoqi@0: throw new EscapeSyntaxException("illegal_hex_digit", line, column); aoqi@0: } aoqi@0: while ((c = ReadChar()) != '}') { aoqi@0: column++; aoqi@0: int n = hexval(c); aoqi@0: if (n < 0) { aoqi@0: throw new EscapeSyntaxException("illegal_hex_digit", line, column); aoqi@0: } aoqi@0: scalarValue <<= 4; aoqi@0: scalarValue |= n; aoqi@0: if (scalarValue >= 0x110000) { aoqi@0: throw new EscapeSyntaxException("char_code_too_big", line, column); aoqi@0: } aoqi@0: } aoqi@0: column++; // for the '}' aoqi@0: if (scalarValue <= 0xFFFF) { aoqi@0: c = (char) scalarValue; aoqi@0: switch (c) { aoqi@0: case '\n': aoqi@0: case '\r': aoqi@0: case '\t': aoqi@0: break; aoqi@0: default: aoqi@0: if (c >= 0x20 && !Utf16.isSurrogate(c)) { aoqi@0: break; aoqi@0: } aoqi@0: // fall through aoqi@0: case '\uFFFE': aoqi@0: case '\uFFFF': aoqi@0: throw new EscapeSyntaxException("illegal_char_code_ref", line, column); aoqi@0: } aoqi@0: buffer[bufpos] = c; aoqi@0: return c; aoqi@0: } aoqi@0: c = Utf16.surrogate1(scalarValue); aoqi@0: buffer[bufpos] = c; aoqi@0: int bufpos1 = bufpos; aoqi@0: if (++bufpos == bufsize) { aoqi@0: bufpos = 0; aoqi@0: } aoqi@0: buffer[bufpos] = Utf16.surrogate2(scalarValue); aoqi@0: bufline[bufpos] = bufline[bufpos1]; aoqi@0: bufcolumn[bufpos] = bufcolumn[bufpos1]; aoqi@0: backup(1); aoqi@0: return c; aoqi@0: } catch (EOFException e) { aoqi@0: throw new EscapeSyntaxException("incomplete_escape", line, column); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * @deprecated @see #getEndColumn aoqi@0: */ aoqi@0: public final int getColumn() { aoqi@0: return bufcolumn[bufpos]; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * @deprecated @see #getEndLine aoqi@0: */ aoqi@0: public final int getLine() { aoqi@0: return bufline[bufpos]; aoqi@0: } aoqi@0: aoqi@0: public final int getEndColumn() { aoqi@0: return bufcolumn[bufpos]; aoqi@0: } aoqi@0: aoqi@0: public final int getEndLine() { aoqi@0: return bufline[bufpos]; aoqi@0: } aoqi@0: aoqi@0: public final int getBeginColumn() { aoqi@0: return bufcolumn[tokenBegin]; aoqi@0: } aoqi@0: aoqi@0: public final int getBeginLine() { aoqi@0: return bufline[tokenBegin]; aoqi@0: } aoqi@0: aoqi@0: public final void backup(int amount) { aoqi@0: aoqi@0: inBuf += amount; aoqi@0: if ((bufpos -= amount) < 0) { aoqi@0: bufpos += bufsize; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public UCode_UCodeESC_CharStream(java.io.Reader dstream, aoqi@0: int startline, int startcolumn, int buffersize) { aoqi@0: inputStream = dstream; aoqi@0: line = startline; aoqi@0: column = startcolumn - 1; aoqi@0: aoqi@0: available = bufsize = buffersize; aoqi@0: buffer = new char[buffersize]; aoqi@0: bufline = new int[buffersize]; aoqi@0: bufcolumn = new int[buffersize]; aoqi@0: nextCharBuf = new char[4096]; aoqi@0: skipBOM(); aoqi@0: } aoqi@0: aoqi@0: public UCode_UCodeESC_CharStream(java.io.Reader dstream, aoqi@0: int startline, int startcolumn) { aoqi@0: this(dstream, startline, startcolumn, 4096); aoqi@0: } aoqi@0: aoqi@0: public void ReInit(java.io.Reader dstream, aoqi@0: int startline, int startcolumn, int buffersize) { aoqi@0: inputStream = dstream; aoqi@0: closed = false; aoqi@0: line = startline; aoqi@0: column = startcolumn - 1; aoqi@0: aoqi@0: if (buffer == null || buffersize != buffer.length) { aoqi@0: available = bufsize = buffersize; aoqi@0: buffer = new char[buffersize]; aoqi@0: bufline = new int[buffersize]; aoqi@0: bufcolumn = new int[buffersize]; aoqi@0: nextCharBuf = new char[4096]; aoqi@0: } aoqi@0: prevCharIsLF = false; aoqi@0: tokenBegin = inBuf = maxNextCharInd = 0; aoqi@0: nextCharInd = bufpos = -1; aoqi@0: skipBOM(); aoqi@0: } aoqi@0: aoqi@0: public void ReInit(java.io.Reader dstream, aoqi@0: int startline, int startcolumn) { aoqi@0: ReInit(dstream, startline, startcolumn, 4096); aoqi@0: } aoqi@0: aoqi@0: public UCode_UCodeESC_CharStream(java.io.InputStream dstream, int startline, aoqi@0: int startcolumn, int buffersize) { aoqi@0: this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096); aoqi@0: } aoqi@0: aoqi@0: public UCode_UCodeESC_CharStream(java.io.InputStream dstream, int startline, aoqi@0: int startcolumn) { aoqi@0: this(dstream, startline, startcolumn, 4096); aoqi@0: } aoqi@0: aoqi@0: public void ReInit(java.io.InputStream dstream, int startline, aoqi@0: int startcolumn, int buffersize) { aoqi@0: ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096); aoqi@0: } aoqi@0: aoqi@0: public void ReInit(java.io.InputStream dstream, int startline, aoqi@0: int startcolumn) { aoqi@0: ReInit(dstream, startline, startcolumn, 4096); aoqi@0: } aoqi@0: static private final char BOM = '\ufeff'; aoqi@0: aoqi@0: private void skipBOM() { aoqi@0: try { aoqi@0: if (PeekChar() == BOM) { aoqi@0: ReadChar(); aoqi@0: } aoqi@0: } catch (EOFException e) { aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public final String GetImage() { aoqi@0: if (bufpos >= tokenBegin) { aoqi@0: return new String(buffer, tokenBegin, bufpos - tokenBegin + 1); aoqi@0: } else { aoqi@0: return new String(buffer, tokenBegin, bufsize - tokenBegin) aoqi@0: + new String(buffer, 0, bufpos + 1); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public final char[] GetSuffix(int len) { aoqi@0: char[] ret = new char[len]; aoqi@0: aoqi@0: if ((bufpos + 1) >= len) { aoqi@0: System.arraycopy(buffer, bufpos - len + 1, ret, 0, len); aoqi@0: } else { aoqi@0: System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0, aoqi@0: len - bufpos - 1); aoqi@0: System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1); aoqi@0: } aoqi@0: aoqi@0: return ret; aoqi@0: } aoqi@0: aoqi@0: public void Done() { aoqi@0: nextCharBuf = null; aoqi@0: buffer = null; aoqi@0: bufline = null; aoqi@0: bufcolumn = null; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Method to adjust line and column numbers for the start of a token.
aoqi@0: */ aoqi@0: public void adjustBeginLineColumn(int newLine, int newCol) { aoqi@0: int start = tokenBegin; aoqi@0: int len; aoqi@0: aoqi@0: if (bufpos >= tokenBegin) { aoqi@0: len = bufpos - tokenBegin + inBuf + 1; aoqi@0: } else { aoqi@0: len = bufsize - tokenBegin + bufpos + 1 + inBuf; aoqi@0: } aoqi@0: aoqi@0: int i = 0, j = 0, k = 0; aoqi@0: int nextColDiff, columnDiff = 0; aoqi@0: aoqi@0: while (i < len aoqi@0: && bufline[j = start % bufsize] == bufline[k = ++start % bufsize]) { aoqi@0: bufline[j] = newLine; aoqi@0: nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j]; aoqi@0: bufcolumn[j] = newCol + columnDiff; aoqi@0: columnDiff = nextColDiff; aoqi@0: i++; aoqi@0: } aoqi@0: aoqi@0: if (i < len) { aoqi@0: bufline[j] = newLine++; aoqi@0: bufcolumn[j] = newCol + columnDiff; aoqi@0: aoqi@0: while (i++ < len) { aoqi@0: if (bufline[j = start % bufsize] != bufline[++start % bufsize]) { aoqi@0: bufline[j] = newLine++; aoqi@0: } else { aoqi@0: bufline[j] = newLine; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: line = bufline[j]; aoqi@0: column = bufcolumn[j]; aoqi@0: } aoqi@0: }