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