CSSParser.java revision 0
2668N/A/*
4248N/A * Copyright 1999-2000 Sun Microsystems, Inc. All Rights Reserved.
2668N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2668N/A *
2668N/A * This code is free software; you can redistribute it and/or modify it
2668N/A * under the terms of the GNU General Public License version 2 only, as
2668N/A * published by the Free Software Foundation. Sun designates this
2668N/A * particular file as subject to the "Classpath" exception as provided
2668N/A * by Sun in the LICENSE file that accompanied this code.
2668N/A *
2668N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2668N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2668N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2668N/A * version 2 for more details (a copy is included in the LICENSE file that
2668N/A * accompanied this code).
2668N/A *
2668N/A * You should have received a copy of the GNU General Public License version
2668N/A * 2 along with this work; if not, write to the Free Software Foundation,
2668N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2668N/A *
2668N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
2668N/A * CA 95054 USA or visit www.sun.com if you need additional information or
2668N/A * have any questions.
3883N/A */
2668N/Apackage javax.swing.text.html;
2668N/A
2668N/Aimport java.io.*;
2668N/A
2668N/A/**
2668N/A * A CSS parser. This works by way of a delegate that implements the
2668N/A * CSSParserCallback interface. The delegate is notified of the following
2668N/A * events:
2668N/A * <ul>
2668N/A * <li>Import statement: <code>handleImport</code>
3883N/A * <li>Selectors <code>handleSelector</code>. This is invoked for each
2668N/A * string. For example if the Reader contained p, bar , a {}, the delegate
2668N/A * would be notified 4 times, for 'p,' 'bar' ',' and 'a'.
2668N/A * <li>When a rule starts, <code>startRule</code>
2668N/A * <li>Properties in the rule via the <code>handleProperty</code>. This
2668N/A * is invoked one per property/value key, eg font size: foo;, would
2668N/A * cause the delegate to be notified once with a value of 'font size'.
2668N/A * <li>Values in the rule via the <code>handleValue</code>, this is notified
2668N/A * for the total value.
2668N/A * <li>When a rule ends, <code>endRule</code>
2668N/A * </ul>
2668N/A * This will parse much more than CSS 1, and loosely implements the
3883N/A * recommendation for <i>Forward-compatible parsing</i> in section
3883N/A * 7.1 of the CSS spec found at:
2668N/A * <a href=http://www.w3.org/TR/REC-CSS1>http://www.w3.org/TR/REC-CSS1</a>.
2668N/A * If an error results in parsing, a RuntimeException will be thrown.
2668N/A * <p>
2668N/A * This will preserve case. If the callback wishes to treat certain poritions
2668N/A * case insensitively (such as selectors), it should use toLowerCase, or
2668N/A * something similar.
2668N/A *
2668N/A * @author Scott Violet
2668N/A */
2668N/Aclass CSSParser {
2668N/A // Parsing something like the following:
2668N/A // (@rule | ruleset | block)*
2668N/A //
2668N/A // @rule (block | identifier)*; (block with {} ends @rule)
2668N/A // block matching [] () {} (that is, [()] is a block, [(){}{[]}]
2668N/A // is a block, ()[] is two blocks)
2668N/A // identifier "*" | '*' | anything but a [](){} and whitespace
2668N/A //
2668N/A // ruleset selector decblock
2668N/A // selector (identifier | (block, except block '{}') )*
2668N/A // declblock declaration* block*
2668N/A // declaration (identifier* stopping when identifier ends with :)
2668N/A // (identifier* stopping when identifier ends with ;)
2668N/A //
2668N/A // comments /* */ can appear any where, and are stripped.
2668N/A
2668N/A
2668N/A // identifier - letters, digits, dashes and escaped characters
2668N/A // block starts with { ends with matching }, () [] and {} always occur
2668N/A // in matching pairs, '' and "" also occur in pairs, except " may be
2668N/A
2668N/A
2668N/A // Indicates the type of token being parsed.
2668N/A private static final int IDENTIFIER = 1;
2668N/A private static final int BRACKET_OPEN = 2;
2668N/A private static final int BRACKET_CLOSE = 3;
2668N/A private static final int BRACE_OPEN = 4;
2668N/A private static final int BRACE_CLOSE = 5;
2668N/A private static final int PAREN_OPEN = 6;
2668N/A private static final int PAREN_CLOSE = 7;
2668N/A private static final int END = -1;
2668N/A
2668N/A private static final char[] charMapping = { 0, 0, '[', ']', '{', '}', '(',
2668N/A ')', 0};
2668N/A
2668N/A
2668N/A /** Set to true if one character has been read ahead. */
2668N/A private boolean didPushChar;
2668N/A /** The read ahead character. */
2668N/A private int pushedChar;
2668N/A /** Temporary place to hold identifiers. */
2668N/A private StringBuffer unitBuffer;
2668N/A /** Used to indicate blocks. */
2668N/A private int[] unitStack;
2668N/A /** Number of valid blocks. */
2668N/A private int stackCount;
2668N/A /** Holds the incoming CSS rules. */
2668N/A private Reader reader;
2668N/A /** Set to true when the first non @ rule is encountered. */
2668N/A private boolean encounteredRuleSet;
2668N/A /** Notified of state. */
2668N/A private CSSParserCallback callback;
2668N/A /** nextToken() inserts the string here. */
2668N/A private char[] tokenBuffer;
2668N/A /** Current number of chars in tokenBufferLength. */
2668N/A private int tokenBufferLength;
2668N/A /** Set to true if any whitespace is read. */
2668N/A private boolean readWS;
2668N/A
2668N/A
2668N/A // The delegate interface.
2668N/A static interface CSSParserCallback {
2668N/A /** Called when an @import is encountered. */
2668N/A void handleImport(String importString);
2668N/A // There is currently no way to distinguish between '"foo,"' and
2668N/A // 'foo,'. But this generally isn't valid CSS. If it becomes
2668N/A // a problem, handleSelector will have to be told if the string is
2668N/A // quoted.
2668N/A void handleSelector(String selector);
2668N/A void startRule();
2668N/A // Property names are mapped to lower case before being passed to
2668N/A // the delegate.
2668N/A void handleProperty(String property);
2668N/A void handleValue(String value);
2668N/A void endRule();
2668N/A }
2668N/A
2668N/A CSSParser() {
2668N/A unitStack = new int[2];
2668N/A tokenBuffer = new char[80];
2668N/A unitBuffer = new StringBuffer();
2668N/A }
2668N/A
2668N/A void parse(Reader reader, CSSParserCallback callback,
2668N/A boolean inRule) throws IOException {
2668N/A this.callback = callback;
2668N/A stackCount = tokenBufferLength = 0;
2668N/A this.reader = reader;
2668N/A encounteredRuleSet = false;
2668N/A try {
2668N/A if (inRule) {
2668N/A parseDeclarationBlock();
2668N/A }
2668N/A else {
2668N/A while (getNextStatement());
2668N/A }
2668N/A } finally {
2668N/A callback = null;
2668N/A reader = null;
2668N/A }
2668N/A }
2668N/A
2668N/A /**
2668N/A * Gets the next statement, returning false if the end is reached. A
2668N/A * statement is either an @rule, or a ruleset.
2668N/A */
2668N/A private boolean getNextStatement() throws IOException {
2668N/A unitBuffer.setLength(0);
2668N/A
2668N/A int token = nextToken((char)0);
2668N/A
2668N/A switch (token) {
2668N/A case IDENTIFIER:
2668N/A if (tokenBufferLength > 0) {
2668N/A if (tokenBuffer[0] == '@') {
2668N/A parseAtRule();
2668N/A }
2668N/A else {
2668N/A encounteredRuleSet = true;
2668N/A parseRuleSet();
2668N/A }
2668N/A }
2668N/A return true;
2668N/A case BRACKET_OPEN:
2668N/A case BRACE_OPEN:
2668N/A case PAREN_OPEN:
2668N/A parseTillClosed(token);
3883N/A return true;
3883N/A
3883N/A case BRACKET_CLOSE:
2668N/A case BRACE_CLOSE:
3883N/A case PAREN_CLOSE:
2668N/A // Shouldn't happen...
2668N/A throw new RuntimeException("Unexpected top level block close");
2674N/A
2674N/A case END:
2674N/A return false;
2674N/A }
2674N/A return true;
2674N/A }
2674N/A
2674N/A /**
2674N/A * Parses an @ rule, stopping at a matching brace pair, or ;.
2674N/A */
2674N/A private void parseAtRule() throws IOException {
2674N/A // PENDING: make this more effecient.
2668N/A boolean done = false;
2668N/A boolean isImport = (tokenBufferLength == 7 &&
2668N/A tokenBuffer[0] == '@' && tokenBuffer[1] == 'i' &&
2668N/A tokenBuffer[2] == 'm' && tokenBuffer[3] == 'p' &&
2668N/A tokenBuffer[4] == 'o' && tokenBuffer[5] == 'r' &&
2668N/A tokenBuffer[6] == 't');
2668N/A
2668N/A unitBuffer.setLength(0);
2668N/A while (!done) {
2668N/A int nextToken = nextToken(';');
2668N/A
2668N/A switch (nextToken) {
2668N/A case IDENTIFIER:
2668N/A if (tokenBufferLength > 0 &&
2668N/A tokenBuffer[tokenBufferLength - 1] == ';') {
2668N/A --tokenBufferLength;
2668N/A done = true;
2668N/A }
2668N/A if (tokenBufferLength > 0) {
2668N/A if (unitBuffer.length() > 0 && readWS) {
2668N/A unitBuffer.append(' ');
2668N/A }
2668N/A unitBuffer.append(tokenBuffer, 0, tokenBufferLength);
2668N/A }
2668N/A break;
2668N/A
2668N/A case BRACE_OPEN:
2668N/A if (unitBuffer.length() > 0 && readWS) {
2668N/A unitBuffer.append(' ');
2668N/A }
2668N/A unitBuffer.append(charMapping[nextToken]);
2668N/A parseTillClosed(nextToken);
2668N/A done = true;
2668N/A // Skip a tailing ';', not really to spec.
2668N/A {
2668N/A int nextChar = readWS();
2668N/A if (nextChar != -1 && nextChar != ';') {
2668N/A pushChar(nextChar);
2668N/A }
2668N/A }
2668N/A break;
2668N/A
2668N/A case BRACKET_OPEN: case PAREN_OPEN:
2668N/A unitBuffer.append(charMapping[nextToken]);
2668N/A parseTillClosed(nextToken);
2668N/A break;
2668N/A
2668N/A case BRACKET_CLOSE: case BRACE_CLOSE: case PAREN_CLOSE:
2668N/A throw new RuntimeException("Unexpected close in @ rule");
2668N/A
2668N/A case END:
2668N/A done = true;
2668N/A break;
2668N/A }
2668N/A }
2668N/A if (isImport && !encounteredRuleSet) {
2668N/A callback.handleImport(unitBuffer.toString());
2668N/A }
2668N/A }
2668N/A
2668N/A /**
2668N/A * Parses the next rule set, which is a selector followed by a
2668N/A * declaration block.
2668N/A */
2668N/A private void parseRuleSet() throws IOException {
2668N/A if (parseSelectors()) {
2668N/A callback.startRule();
2668N/A parseDeclarationBlock();
2668N/A callback.endRule();
2668N/A }
2668N/A }
2668N/A
2668N/A /**
2668N/A * Parses a set of selectors, returning false if the end of the stream
2668N/A * is reached.
2668N/A */
2668N/A private boolean parseSelectors() throws IOException {
2668N/A // Parse the selectors
2668N/A int nextToken;
2668N/A
2668N/A if (tokenBufferLength > 0) {
2668N/A callback.handleSelector(new String(tokenBuffer, 0,
2668N/A tokenBufferLength));
2668N/A }
2668N/A
2668N/A unitBuffer.setLength(0);
2668N/A for (;;) {
2668N/A while ((nextToken = nextToken((char)0)) == IDENTIFIER) {
2668N/A if (tokenBufferLength > 0) {
2668N/A callback.handleSelector(new String(tokenBuffer, 0,
2668N/A tokenBufferLength));
2668N/A }
2668N/A }
2668N/A switch (nextToken) {
2668N/A case BRACE_OPEN:
2668N/A return true;
2668N/A
2668N/A case BRACKET_OPEN: case PAREN_OPEN:
2668N/A parseTillClosed(nextToken);
2668N/A // Not too sure about this, how we handle this isn't very
2668N/A // well spec'd.
2668N/A unitBuffer.setLength(0);
2668N/A break;
2668N/A
2668N/A case BRACKET_CLOSE: case BRACE_CLOSE: case PAREN_CLOSE:
2668N/A throw new RuntimeException("Unexpected block close in selector");
2668N/A
2668N/A case END:
2668N/A // Prematurely hit end.
2668N/A return false;
2668N/A }
2668N/A }
2668N/A }
2668N/A
2668N/A /**
2668N/A * Parses a declaration block. Which a number of declarations followed
2668N/A * by a })].
2668N/A */
2668N/A private void parseDeclarationBlock() throws IOException {
2668N/A for (;;) {
2668N/A int token = parseDeclaration();
2668N/A switch (token) {
2668N/A case END: case BRACE_CLOSE:
2668N/A return;
2668N/A
2668N/A case BRACKET_CLOSE: case PAREN_CLOSE:
2668N/A // Bail
2668N/A throw new RuntimeException("Unexpected close in declaration block");
2668N/A case IDENTIFIER:
2668N/A break;
2668N/A }
2668N/A }
2668N/A }
2668N/A
2668N/A /**
2668N/A * Parses a single declaration, which is an identifier a : and another
2668N/A * identifier. This returns the last token seen.
2668N/A */
2668N/A // identifier+: identifier* ;|}
2668N/A private int parseDeclaration() throws IOException {
2668N/A int token;
2668N/A
2668N/A if ((token = parseIdentifiers(':', false)) != IDENTIFIER) {
2668N/A return token;
2668N/A }
2668N/A // Make the property name to lowercase
2668N/A for (int counter = unitBuffer.length() - 1; counter >= 0; counter--) {
2668N/A unitBuffer.setCharAt(counter, Character.toLowerCase
2668N/A (unitBuffer.charAt(counter)));
2668N/A }
2668N/A callback.handleProperty(unitBuffer.toString());
2668N/A
2668N/A token = parseIdentifiers(';', true);
2668N/A callback.handleValue(unitBuffer.toString());
2668N/A return token;
2668N/A }
2668N/A
2668N/A /**
2668N/A * Parses identifiers until <code>extraChar</code> is encountered,
2668N/A * returning the ending token, which will be IDENTIFIER if extraChar
2668N/A * is found.
2668N/A */
2668N/A private int parseIdentifiers(char extraChar,
2668N/A boolean wantsBlocks) throws IOException {
2668N/A int nextToken;
2668N/A int ubl;
2668N/A
2668N/A unitBuffer.setLength(0);
2668N/A for (;;) {
2668N/A nextToken = nextToken(extraChar);
2668N/A
2668N/A switch (nextToken) {
2668N/A case IDENTIFIER:
2668N/A if (tokenBufferLength > 0) {
2668N/A if (tokenBuffer[tokenBufferLength - 1] == extraChar) {
2668N/A if (--tokenBufferLength > 0) {
2668N/A if (readWS && unitBuffer.length() > 0) {
2668N/A unitBuffer.append(' ');
2668N/A }
2668N/A unitBuffer.append(tokenBuffer, 0,
2668N/A tokenBufferLength);
2668N/A }
2668N/A return IDENTIFIER;
2677N/A }
2677N/A if (readWS && unitBuffer.length() > 0) {
2677N/A unitBuffer.append(' ');
2668N/A }
2668N/A unitBuffer.append(tokenBuffer, 0, tokenBufferLength);
2668N/A }
2668N/A break;
2677N/A
2677N/A case BRACKET_OPEN:
2677N/A case BRACE_OPEN:
2668N/A case PAREN_OPEN:
2668N/A ubl = unitBuffer.length();
2668N/A if (wantsBlocks) {
2668N/A unitBuffer.append(charMapping[nextToken]);
2668N/A }
2668N/A parseTillClosed(nextToken);
2668N/A if (!wantsBlocks) {
2668N/A unitBuffer.setLength(ubl);
2668N/A }
2668N/A break;
2668N/A
2668N/A case BRACE_CLOSE:
2668N/A // No need to throw for these two, we return token and
2668N/A // caller can do whatever.
2668N/A case BRACKET_CLOSE:
2668N/A case PAREN_CLOSE:
2668N/A case END:
2668N/A // Hit the end
2668N/A return nextToken;
2668N/A }
2668N/A }
2668N/A }
2668N/A
2668N/A /**
2668N/A * Parses till a matching block close is encountered. This is only
2668N/A * appropriate to be called at the top level (no nesting).
2668N/A */
2668N/A private void parseTillClosed(int openToken) throws IOException {
2668N/A int nextToken;
2668N/A boolean done = false;
2668N/A
2668N/A startBlock(openToken);
2668N/A while (!done) {
2668N/A nextToken = nextToken((char)0);
2668N/A switch (nextToken) {
2668N/A case IDENTIFIER:
2668N/A if (unitBuffer.length() > 0 && readWS) {
2668N/A unitBuffer.append(' ');
2668N/A }
2668N/A if (tokenBufferLength > 0) {
2668N/A unitBuffer.append(tokenBuffer, 0, tokenBufferLength);
2668N/A }
2668N/A break;
2668N/A
2668N/A case BRACKET_OPEN: case BRACE_OPEN: case PAREN_OPEN:
2668N/A if (unitBuffer.length() > 0 && readWS) {
2668N/A unitBuffer.append(' ');
2668N/A }
2668N/A unitBuffer.append(charMapping[nextToken]);
2668N/A startBlock(nextToken);
2668N/A break;
2668N/A
2668N/A case BRACKET_CLOSE: case BRACE_CLOSE: case PAREN_CLOSE:
2668N/A if (unitBuffer.length() > 0 && readWS) {
2668N/A unitBuffer.append(' ');
2668N/A }
2668N/A unitBuffer.append(charMapping[nextToken]);
2668N/A endBlock(nextToken);
2668N/A if (!inBlock()) {
2668N/A done = true;
2668N/A }
2668N/A break;
2668N/A
2668N/A case END:
2668N/A // Prematurely hit end.
2668N/A throw new RuntimeException("Unclosed block");
2668N/A }
2668N/A }
2668N/A }
2668N/A
2668N/A /**
2668N/A * Fetches the next token.
2668N/A */
2668N/A private int nextToken(char idChar) throws IOException {
2668N/A readWS = false;
2668N/A
2668N/A int nextChar = readWS();
2668N/A
2668N/A switch (nextChar) {
2668N/A case '\'':
2668N/A readTill('\'');
2668N/A if (tokenBufferLength > 0) {
2668N/A tokenBufferLength--;
2668N/A }
2668N/A return IDENTIFIER;
2668N/A case '"':
2668N/A readTill('"');
2668N/A if (tokenBufferLength > 0) {
tokenBufferLength--;
}
return IDENTIFIER;
case '[':
return BRACKET_OPEN;
case ']':
return BRACKET_CLOSE;
case '{':
return BRACE_OPEN;
case '}':
return BRACE_CLOSE;
case '(':
return PAREN_OPEN;
case ')':
return PAREN_CLOSE;
case -1:
return END;
default:
pushChar(nextChar);
getIdentifier(idChar);
return IDENTIFIER;
}
}
/**
* Gets an identifier, returning true if the length of the string is greater than 0,
* stopping when <code>stopChar</code>, whitespace, or one of {}()[] is
* hit.
*/
// NOTE: this could be combined with readTill, as they contain somewhat
// similiar functionality.
private boolean getIdentifier(char stopChar) throws IOException {
boolean lastWasEscape = false;
boolean done = false;
int escapeCount = 0;
int escapeChar = 0;
int nextChar;
int intStopChar = (int)stopChar;
// 1 for '\', 2 for valid escape char [0-9a-fA-F], 3 for
// stop character (white space, ()[]{}) 0 otherwise
short type;
int escapeOffset = 0;
tokenBufferLength = 0;
while (!done) {
nextChar = readChar();
switch (nextChar) {
case '\\':
type = 1;
break;
case '0': case '1': case '2': case '3': case '4': case '5':
case '6': case '7': case '8': case '9':
type = 2;
escapeOffset = nextChar - '0';
break;
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
type = 2;
escapeOffset = nextChar - 'a' + 10;
break;
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
type = 2;
escapeOffset = nextChar - 'A' + 10;
break;
case '\'': case '"': case '[': case ']': case '{': case '}':
case '(': case ')':
case ' ': case '\n': case '\t': case '\r':
type = 3;
break;
case '/':
type = 4;
break;
case -1:
// Reached the end
done = true;
type = 0;
break;
default:
type = 0;
break;
}
if (lastWasEscape) {
if (type == 2) {
// Continue with escape.
escapeChar = escapeChar * 16 + escapeOffset;
if (++escapeCount == 4) {
lastWasEscape = false;
append((char)escapeChar);
}
}
else {
// no longer escaped
lastWasEscape = false;
if (escapeCount > 0) {
append((char)escapeChar);
// Make this simpler, reprocess the character.
pushChar(nextChar);
}
else if (!done) {
append((char)nextChar);
}
}
}
else if (!done) {
if (type == 1) {
lastWasEscape = true;
escapeChar = escapeCount = 0;
}
else if (type == 3) {
done = true;
pushChar(nextChar);
}
else if (type == 4) {
// Potential comment
nextChar = readChar();
if (nextChar == '*') {
done = true;
readComment();
readWS = true;
}
else {
append('/');
if (nextChar == -1) {
done = true;
}
else {
pushChar(nextChar);
}
}
}
else {
append((char)nextChar);
if (nextChar == intStopChar) {
done = true;
}
}
}
}
return (tokenBufferLength > 0);
}
/**
* Reads till a <code>stopChar</code> is encountered, escaping characters
* as necessary.
*/
private void readTill(char stopChar) throws IOException {
boolean lastWasEscape = false;
int escapeCount = 0;
int escapeChar = 0;
int nextChar;
boolean done = false;
int intStopChar = (int)stopChar;
// 1 for '\', 2 for valid escape char [0-9a-fA-F], 0 otherwise
short type;
int escapeOffset = 0;
tokenBufferLength = 0;
while (!done) {
nextChar = readChar();
switch (nextChar) {
case '\\':
type = 1;
break;
case '0': case '1': case '2': case '3': case '4':case '5':
case '6': case '7': case '8': case '9':
type = 2;
escapeOffset = nextChar - '0';
break;
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
type = 2;
escapeOffset = nextChar - 'a' + 10;
break;
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
type = 2;
escapeOffset = nextChar - 'A' + 10;
break;
case -1:
// Prematurely reached the end!
throw new RuntimeException("Unclosed " + stopChar);
default:
type = 0;
break;
}
if (lastWasEscape) {
if (type == 2) {
// Continue with escape.
escapeChar = escapeChar * 16 + escapeOffset;
if (++escapeCount == 4) {
lastWasEscape = false;
append((char)escapeChar);
}
}
else {
// no longer escaped
if (escapeCount > 0) {
append((char)escapeChar);
if (type == 1) {
lastWasEscape = true;
escapeChar = escapeCount = 0;
}
else {
if (nextChar == intStopChar) {
done = true;
}
append((char)nextChar);
lastWasEscape = false;
}
}
else {
append((char)nextChar);
lastWasEscape = false;
}
}
}
else if (type == 1) {
lastWasEscape = true;
escapeChar = escapeCount = 0;
}
else {
if (nextChar == intStopChar) {
done = true;
}
append((char)nextChar);
}
}
}
private void append(char character) {
if (tokenBufferLength == tokenBuffer.length) {
char[] newBuffer = new char[tokenBuffer.length * 2];
System.arraycopy(tokenBuffer, 0, newBuffer, 0, tokenBuffer.length);
tokenBuffer = newBuffer;
}
tokenBuffer[tokenBufferLength++] = character;
}
/**
* Parses a comment block.
*/
private void readComment() throws IOException {
int nextChar;
for(;;) {
nextChar = readChar();
switch (nextChar) {
case -1:
throw new RuntimeException("Unclosed comment");
case '*':
nextChar = readChar();
if (nextChar == '/') {
return;
}
else if (nextChar == -1) {
throw new RuntimeException("Unclosed comment");
}
else {
pushChar(nextChar);
}
break;
default:
break;
}
}
}
/**
* Called when a block start is encountered ({[.
*/
private void startBlock(int startToken) {
if (stackCount == unitStack.length) {
int[] newUS = new int[stackCount * 2];
System.arraycopy(unitStack, 0, newUS, 0, stackCount);
unitStack = newUS;
}
unitStack[stackCount++] = startToken;
}
/**
* Called when an end block is encountered )]}
*/
private void endBlock(int endToken) {
int startToken;
switch (endToken) {
case BRACKET_CLOSE:
startToken = BRACKET_OPEN;
break;
case BRACE_CLOSE:
startToken = BRACE_OPEN;
break;
case PAREN_CLOSE:
startToken = PAREN_OPEN;
break;
default:
// Will never happen.
startToken = -1;
break;
}
if (stackCount > 0 && unitStack[stackCount - 1] == startToken) {
stackCount--;
}
else {
// Invalid state, should do something.
throw new RuntimeException("Unmatched block");
}
}
/**
* @return true if currently in a block.
*/
private boolean inBlock() {
return (stackCount > 0);
}
/**
* Skips any white space, returning the character after the white space.
*/
private int readWS() throws IOException {
int nextChar;
while ((nextChar = readChar()) != -1 &&
Character.isWhitespace((char)nextChar)) {
readWS = true;
}
return nextChar;
}
/**
* Reads a character from the stream.
*/
private int readChar() throws IOException {
if (didPushChar) {
didPushChar = false;
return pushedChar;
}
return reader.read();
// Uncomment the following to do case insensitive parsing.
/*
if (retValue != -1) {
return (int)Character.toLowerCase((char)retValue);
}
return retValue;
*/
}
/**
* Supports one character look ahead, this will throw if called twice
* in a row.
*/
private void pushChar(int tempChar) {
if (didPushChar) {
// Should never happen.
throw new RuntimeException("Can not handle look ahead of more than one character");
}
didPushChar = true;
pushedChar = tempChar;
}
}