jdk/src/share/classes/javax/swing/text/html/CSSParser.java
author darcy
Wed, 22 Jan 2014 23:20:58 -0800
changeset 22567 5816a47fa4dd
parent 21278 ef8a3a2a72f2
child 23010 6dadb192ad81
permissions -rw-r--r--
8032047: Fix static lint warnings in client libraries 8032048: Add static lint warning to build of jdk repository Reviewed-by: pchelko, serb, erikj
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1999, 2000, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.text.html;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * A CSS parser. This works by way of a delegate that implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * CSSParserCallback interface. The delegate is notified of the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * events:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *   <li>Import statement: <code>handleImport</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *   <li>Selectors <code>handleSelector</code>. This is invoked for each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *       string. For example if the Reader contained p, bar , a {}, the delegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *       would be notified 4 times, for 'p,' 'bar' ',' and 'a'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *   <li>When a rule starts, <code>startRule</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *   <li>Properties in the rule via the <code>handleProperty</code>. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *       is invoked one per property/value key, eg font size: foo;, would
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *       cause the delegate to be notified once with a value of 'font size'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *   <li>Values in the rule via the <code>handleValue</code>, this is notified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *       for the total value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *   <li>When a rule ends, <code>endRule</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * This will parse much more than CSS 1, and loosely implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * recommendation for <i>Forward-compatible parsing</i> in section
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * 7.1 of the CSS spec found at:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <a href=http://www.w3.org/TR/REC-CSS1>http://www.w3.org/TR/REC-CSS1</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * If an error results in parsing, a RuntimeException will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * This will preserve case. If the callback wishes to treat certain poritions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * case insensitively (such as selectors), it should use toLowerCase, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * something similar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @author Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
class CSSParser {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // Parsing something like the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // (@rule | ruleset | block)*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // @rule       (block | identifier)*; (block with {} ends @rule)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // block       matching [] () {} (that is, [()] is a block, [(){}{[]}]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    //                                is a block, ()[] is two blocks)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // identifier  "*" | '*' | anything but a [](){} and whitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // ruleset     selector decblock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // selector    (identifier | (block, except block '{}') )*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // declblock   declaration* block*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // declaration (identifier* stopping when identifier ends with :)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    //             (identifier* stopping when identifier ends with ;)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // comments /* */ can appear any where, and are stripped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // identifier - letters, digits, dashes and escaped characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    // block starts with { ends with matching }, () [] and {} always occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    //   in matching pairs, '' and "" also occur in pairs, except " may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // Indicates the type of token being parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private static final int   IDENTIFIER = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private static final int   BRACKET_OPEN = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private static final int   BRACKET_CLOSE = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private static final int   BRACE_OPEN = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private static final int   BRACE_CLOSE = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private static final int   PAREN_OPEN = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private static final int   PAREN_CLOSE = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private static final int   END = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private static final char[] charMapping = { 0, 0, '[', ']', '{', '}', '(',
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                                               ')', 0};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /** Set to true if one character has been read ahead. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private boolean        didPushChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /** The read ahead character. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private int            pushedChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /** Temporary place to hold identifiers. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private StringBuffer   unitBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /** Used to indicate blocks. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private int[]          unitStack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /** Number of valid blocks. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private int            stackCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /** Holds the incoming CSS rules. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private Reader         reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /** Set to true when the first non @ rule is encountered. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private boolean        encounteredRuleSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /** Notified of state. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private CSSParserCallback callback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /** nextToken() inserts the string here. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private char[]         tokenBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /** Current number of chars in tokenBufferLength. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private int            tokenBufferLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /** Set to true if any whitespace is read. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private boolean        readWS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    // The delegate interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    static interface CSSParserCallback {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        /** Called when an @import is encountered. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        void handleImport(String importString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        // There is currently no way to distinguish between '"foo,"' and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        // 'foo,'. But this generally isn't valid CSS. If it becomes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        // a problem, handleSelector will have to be told if the string is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        // quoted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        void handleSelector(String selector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        void startRule();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        // Property names are mapped to lower case before being passed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        // the delegate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        void handleProperty(String property);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        void handleValue(String value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        void endRule();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    CSSParser() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        unitStack = new int[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        tokenBuffer = new char[80];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        unitBuffer = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    void parse(Reader reader, CSSParserCallback callback,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
               boolean inRule) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        this.callback = callback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        stackCount = tokenBufferLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        this.reader = reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        encounteredRuleSet = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if (inRule) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                parseDeclarationBlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                while (getNextStatement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            callback = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            reader = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Gets the next statement, returning false if the end is reached. A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * statement is either an @rule, or a ruleset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private boolean getNextStatement() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        unitBuffer.setLength(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        int token = nextToken((char)0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        switch (token) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        case IDENTIFIER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            if (tokenBufferLength > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                if (tokenBuffer[0] == '@') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    parseAtRule();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    encounteredRuleSet = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    parseRuleSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        case BRACKET_OPEN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        case BRACE_OPEN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        case PAREN_OPEN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            parseTillClosed(token);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        case BRACKET_CLOSE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        case BRACE_CLOSE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        case PAREN_CLOSE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            // Shouldn't happen...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            throw new RuntimeException("Unexpected top level block close");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        case END:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * Parses an @ rule, stopping at a matching brace pair, or ;.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    private void parseAtRule() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        // PENDING: make this more effecient.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        boolean        done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        boolean isImport = (tokenBufferLength == 7 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                            tokenBuffer[0] == '@' && tokenBuffer[1] == 'i' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                            tokenBuffer[2] == 'm' && tokenBuffer[3] == 'p' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                            tokenBuffer[4] == 'o' && tokenBuffer[5] == 'r' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                            tokenBuffer[6] == 't');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        unitBuffer.setLength(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        while (!done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            int       nextToken = nextToken(';');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            switch (nextToken) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            case IDENTIFIER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                if (tokenBufferLength > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    tokenBuffer[tokenBufferLength - 1] == ';') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    --tokenBufferLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                if (tokenBufferLength > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    if (unitBuffer.length() > 0 && readWS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                        unitBuffer.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    unitBuffer.append(tokenBuffer, 0, tokenBufferLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            case BRACE_OPEN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                if (unitBuffer.length() > 0 && readWS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    unitBuffer.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                unitBuffer.append(charMapping[nextToken]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                parseTillClosed(nextToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                // Skip a tailing ';', not really to spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    int nextChar = readWS();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    if (nextChar != -1 && nextChar != ';') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                        pushChar(nextChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            case BRACKET_OPEN: case PAREN_OPEN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                unitBuffer.append(charMapping[nextToken]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                parseTillClosed(nextToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            case BRACKET_CLOSE: case BRACE_CLOSE: case PAREN_CLOSE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                throw new RuntimeException("Unexpected close in @ rule");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            case END:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if (isImport && !encounteredRuleSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            callback.handleImport(unitBuffer.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * Parses the next rule set, which is a selector followed by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * declaration block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    private void parseRuleSet() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if (parseSelectors()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            callback.startRule();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            parseDeclarationBlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            callback.endRule();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * Parses a set of selectors, returning false if the end of the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    private boolean parseSelectors() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        // Parse the selectors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        int       nextToken;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if (tokenBufferLength > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            callback.handleSelector(new String(tokenBuffer, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                                               tokenBufferLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        unitBuffer.setLength(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            while ((nextToken = nextToken((char)0)) == IDENTIFIER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                if (tokenBufferLength > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    callback.handleSelector(new String(tokenBuffer, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                                                       tokenBufferLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            switch (nextToken) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            case BRACE_OPEN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            case BRACKET_OPEN: case PAREN_OPEN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                parseTillClosed(nextToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                // Not too sure about this, how we handle this isn't very
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                // well spec'd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                unitBuffer.setLength(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            case BRACKET_CLOSE: case BRACE_CLOSE: case PAREN_CLOSE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                throw new RuntimeException("Unexpected block close in selector");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            case END:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                // Prematurely hit end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * Parses a declaration block. Which a number of declarations followed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * by a })].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    private void parseDeclarationBlock() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            int token = parseDeclaration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            switch (token) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            case END: case BRACE_CLOSE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            case BRACKET_CLOSE: case PAREN_CLOSE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                // Bail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                throw new RuntimeException("Unexpected close in declaration block");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            case IDENTIFIER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * Parses a single declaration, which is an identifier a : and another
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * identifier. This returns the last token seen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    // identifier+: identifier* ;|}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    private int parseDeclaration() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        int    token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        if ((token = parseIdentifiers(':', false)) != IDENTIFIER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            return token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        // Make the property name to lowercase
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        for (int counter = unitBuffer.length() - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            unitBuffer.setCharAt(counter, Character.toLowerCase
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                                 (unitBuffer.charAt(counter)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        callback.handleProperty(unitBuffer.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        token = parseIdentifiers(';', true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        callback.handleValue(unitBuffer.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        return token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * Parses identifiers until <code>extraChar</code> is encountered,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * returning the ending token, which will be IDENTIFIER if extraChar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    private int parseIdentifiers(char extraChar,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                                 boolean wantsBlocks) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        int   nextToken;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        int   ubl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        unitBuffer.setLength(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            nextToken = nextToken(extraChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            switch (nextToken) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            case IDENTIFIER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                if (tokenBufferLength > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    if (tokenBuffer[tokenBufferLength - 1] == extraChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                        if (--tokenBufferLength > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                            if (readWS && unitBuffer.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                                unitBuffer.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                            unitBuffer.append(tokenBuffer, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                                              tokenBufferLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                        return IDENTIFIER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                    if (readWS && unitBuffer.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                        unitBuffer.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                    unitBuffer.append(tokenBuffer, 0, tokenBufferLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            case BRACKET_OPEN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            case BRACE_OPEN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            case PAREN_OPEN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                ubl = unitBuffer.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                if (wantsBlocks) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    unitBuffer.append(charMapping[nextToken]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                parseTillClosed(nextToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                if (!wantsBlocks) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    unitBuffer.setLength(ubl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            case BRACE_CLOSE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                // No need to throw for these two, we return token and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                // caller can do whatever.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            case BRACKET_CLOSE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            case PAREN_CLOSE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            case END:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                // Hit the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                return nextToken;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * Parses till a matching block close is encountered. This is only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * appropriate to be called at the top level (no nesting).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    private void parseTillClosed(int openToken) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        int       nextToken;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        boolean   done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        startBlock(openToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        while (!done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            nextToken = nextToken((char)0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            switch (nextToken) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            case IDENTIFIER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                if (unitBuffer.length() > 0 && readWS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                    unitBuffer.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                if (tokenBufferLength > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    unitBuffer.append(tokenBuffer, 0, tokenBufferLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            case BRACKET_OPEN: case BRACE_OPEN: case PAREN_OPEN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                if (unitBuffer.length() > 0 && readWS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    unitBuffer.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                unitBuffer.append(charMapping[nextToken]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                startBlock(nextToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            case BRACKET_CLOSE: case BRACE_CLOSE: case PAREN_CLOSE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                if (unitBuffer.length() > 0 && readWS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    unitBuffer.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                unitBuffer.append(charMapping[nextToken]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                endBlock(nextToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                if (!inBlock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            case END:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                // Prematurely hit end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                throw new RuntimeException("Unclosed block");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * Fetches the next token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    private int nextToken(char idChar) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        readWS = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        int     nextChar = readWS();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        switch (nextChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        case '\'':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            readTill('\'');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            if (tokenBufferLength > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                tokenBufferLength--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            return IDENTIFIER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        case '"':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            readTill('"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            if (tokenBufferLength > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                tokenBufferLength--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            return IDENTIFIER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        case '[':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            return BRACKET_OPEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        case ']':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            return BRACKET_CLOSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        case '{':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            return BRACE_OPEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        case '}':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            return BRACE_CLOSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        case '(':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            return PAREN_OPEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        case ')':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            return PAREN_CLOSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        case -1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            return END;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            pushChar(nextChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            getIdentifier(idChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            return IDENTIFIER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * Gets an identifier, returning true if the length of the string is greater than 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * stopping when <code>stopChar</code>, whitespace, or one of {}()[] is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * hit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    // NOTE: this could be combined with readTill, as they contain somewhat
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 5506
diff changeset
   516
    // similar functionality.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    private boolean getIdentifier(char stopChar) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        boolean lastWasEscape = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        boolean done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        int escapeCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        int escapeChar = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        int nextChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        int intStopChar = (int)stopChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        // 1 for '\', 2 for valid escape char [0-9a-fA-F], 3 for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        // stop character (white space, ()[]{}) 0 otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        short type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        int escapeOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        tokenBufferLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        while (!done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            nextChar = readChar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            switch (nextChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            case '\\':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                type = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            case '0': case '1': case '2': case '3': case '4': case '5':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            case '6': case '7': case '8': case '9':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                type = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                escapeOffset = nextChar - '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                type = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                escapeOffset = nextChar - 'a' + 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                type = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                escapeOffset = nextChar - 'A' + 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            case '\'': case '"': case '[': case ']': case '{': case '}':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            case '(': case ')':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            case ' ': case '\n': case '\t': case '\r':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                type = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            case '/':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                type = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            case -1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                // Reached the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                type = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                type = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            if (lastWasEscape) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                if (type == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                    // Continue with escape.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                    escapeChar = escapeChar * 16 + escapeOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                    if (++escapeCount == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                        lastWasEscape = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                        append((char)escapeChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                    // no longer escaped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                    lastWasEscape = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                    if (escapeCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                        append((char)escapeChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                        // Make this simpler, reprocess the character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                        pushChar(nextChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                    else if (!done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                        append((char)nextChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            else if (!done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                if (type == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                    lastWasEscape = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                    escapeChar = escapeCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                else if (type == 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                    done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                    pushChar(nextChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                else if (type == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                    // Potential comment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                    nextChar = readChar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                    if (nextChar == '*') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                        done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                        readComment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                        readWS = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                        append('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                        if (nextChar == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                            done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                            pushChar(nextChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                    append((char)nextChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                    if (nextChar == intStopChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                        done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        return (tokenBufferLength > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * Reads till a <code>stopChar</code> is encountered, escaping characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * as necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    private void readTill(char stopChar) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        boolean lastWasEscape = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        int escapeCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        int escapeChar = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        int nextChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        boolean done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        int intStopChar = (int)stopChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        // 1 for '\', 2 for valid escape char [0-9a-fA-F], 0 otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        short type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        int escapeOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        tokenBufferLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        while (!done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            nextChar = readChar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            switch (nextChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            case '\\':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                type = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            case '0': case '1': case '2': case '3': case '4':case '5':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            case '6': case '7': case '8': case '9':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                type = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                escapeOffset = nextChar - '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                type = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                escapeOffset = nextChar - 'a' + 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                type = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                escapeOffset = nextChar - 'A' + 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            case -1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                // Prematurely reached the end!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                throw new RuntimeException("Unclosed " + stopChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                type = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            if (lastWasEscape) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                if (type == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                    // Continue with escape.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                    escapeChar = escapeChar * 16 + escapeOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                    if (++escapeCount == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                        lastWasEscape = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                        append((char)escapeChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                    // no longer escaped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                    if (escapeCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                        append((char)escapeChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                        if (type == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                            lastWasEscape = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                            escapeChar = escapeCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                            if (nextChar == intStopChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                                done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                            append((char)nextChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                            lastWasEscape = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                        append((char)nextChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                        lastWasEscape = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            else if (type == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                lastWasEscape = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                escapeChar = escapeCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                if (nextChar == intStopChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                    done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                append((char)nextChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    private void append(char character) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        if (tokenBufferLength == tokenBuffer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            char[] newBuffer = new char[tokenBuffer.length * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            System.arraycopy(tokenBuffer, 0, newBuffer, 0, tokenBuffer.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            tokenBuffer = newBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        tokenBuffer[tokenBufferLength++] = character;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * Parses a comment block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    private void readComment() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        int nextChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        for(;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            nextChar = readChar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            switch (nextChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            case -1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                throw new RuntimeException("Unclosed comment");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            case '*':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                nextChar = readChar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                if (nextChar == '/') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                else if (nextChar == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                    throw new RuntimeException("Unclosed comment");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                    pushChar(nextChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * Called when a block start is encountered ({[.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    private void startBlock(int startToken) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        if (stackCount == unitStack.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            int[]     newUS = new int[stackCount * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            System.arraycopy(unitStack, 0, newUS, 0, stackCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            unitStack = newUS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        unitStack[stackCount++] = startToken;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * Called when an end block is encountered )]}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    private void endBlock(int endToken) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        int    startToken;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        switch (endToken) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        case BRACKET_CLOSE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            startToken = BRACKET_OPEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        case BRACE_CLOSE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            startToken = BRACE_OPEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        case PAREN_CLOSE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            startToken = PAREN_OPEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            // Will never happen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            startToken = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        if (stackCount > 0 && unitStack[stackCount - 1] == startToken) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            stackCount--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            // Invalid state, should do something.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            throw new RuntimeException("Unmatched block");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * @return true if currently in a block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    private boolean inBlock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        return (stackCount > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * Skips any white space, returning the character after the white space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    private int readWS() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        int nextChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        while ((nextChar = readChar()) != -1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
               Character.isWhitespace((char)nextChar)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            readWS = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        return nextChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * Reads a character from the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    private int readChar() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        if (didPushChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            didPushChar = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            return pushedChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        return reader.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        // Uncomment the following to do case insensitive parsing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        if (retValue != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            return (int)Character.toLowerCase((char)retValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * Supports one character look ahead, this will throw if called twice
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * in a row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    private void pushChar(int tempChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        if (didPushChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            // Should never happen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            throw new RuntimeException("Can not handle look ahead of more than one character");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        didPushChar = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        pushedChar = tempChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
}