jdk/src/share/classes/java/io/StreamTokenizer.java
author smarks
Mon, 20 Dec 2010 13:47:04 -0800
changeset 7803 56bc97d69d93
parent 5506 202f599c92aa
child 11676 7e75ec031b97
permissions -rw-r--r--
6880112: Project Coin: Port JDK core library code to use diamond operator Reviewed-by: darcy, lancea, alanb, briangoetz, mduigou, mchung
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) 1995, 2005, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * The <code>StreamTokenizer</code> class takes an input stream and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * parses it into "tokens", allowing the tokens to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * read one at a time. The parsing process is controlled by a table
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * and a number of flags that can be set to various states. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * stream tokenizer can recognize identifiers, numbers, quoted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * strings, and various comment styles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Each byte read from the input stream is regarded as a character
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * in the range <code>'&#92;u0000'</code> through <code>'&#92;u00FF'</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * The character value is used to look up five possible attributes of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * the character: <i>white space</i>, <i>alphabetic</i>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <i>numeric</i>, <i>string quote</i>, and <i>comment character</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Each character can have zero or more of these attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * In addition, an instance has four flags. These flags indicate:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <li>Whether line terminators are to be returned as tokens or treated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *     as white space that merely separates tokens.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <li>Whether C-style comments are to be recognized and skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <li>Whether C++-style comments are to be recognized and skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <li>Whether the characters of identifiers are converted to lowercase.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * A typical application first constructs an instance of this class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * sets up the syntax tables, and then repeatedly loops calling the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <code>nextToken</code> method in each iteration of the loop until
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * it returns the value <code>TT_EOF</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @author  James Gosling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @see     java.io.StreamTokenizer#nextToken()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @see     java.io.StreamTokenizer#TT_EOF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @since   JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
public class StreamTokenizer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /* Only one of these will be non-null */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private Reader reader = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private InputStream input = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private char buf[] = new char[20];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * The next character to be considered by the nextToken method.  May also
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * be NEED_CHAR to indicate that a new character should be read, or SKIP_LF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * to indicate that a new character should be read and, if it is a '\n'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * character, it should be discarded and a second new character should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private int peekc = NEED_CHAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private static final int NEED_CHAR = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private static final int SKIP_LF = Integer.MAX_VALUE - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private boolean pushedBack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private boolean forceLower;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /** The line number of the last token read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private int LINENO = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private boolean eolIsSignificantP = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private boolean slashSlashCommentsP = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private boolean slashStarCommentsP = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private byte ctype[] = new byte[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private static final byte CT_WHITESPACE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private static final byte CT_DIGIT = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private static final byte CT_ALPHA = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private static final byte CT_QUOTE = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private static final byte CT_COMMENT = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * After a call to the <code>nextToken</code> method, this field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * contains the type of the token just read. For a single character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * token, its value is the single character, converted to an integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * For a quoted string token, its value is the quote character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Otherwise, its value is one of the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * <li><code>TT_WORD</code> indicates that the token is a word.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * <li><code>TT_NUMBER</code> indicates that the token is a number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * <li><code>TT_EOL</code> indicates that the end of line has been read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *     The field can only have this value if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *     <code>eolIsSignificant</code> method has been called with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *     argument <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * <li><code>TT_EOF</code> indicates that the end of the input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *     has been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * The initial value of this field is -4.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @see     java.io.StreamTokenizer#eolIsSignificant(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @see     java.io.StreamTokenizer#nextToken()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @see     java.io.StreamTokenizer#quoteChar(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @see     java.io.StreamTokenizer#TT_EOF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @see     java.io.StreamTokenizer#TT_EOL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @see     java.io.StreamTokenizer#TT_NUMBER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @see     java.io.StreamTokenizer#TT_WORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public int ttype = TT_NOTHING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * A constant indicating that the end of the stream has been read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public static final int TT_EOF = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * A constant indicating that the end of the line has been read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public static final int TT_EOL = '\n';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * A constant indicating that a number token has been read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public static final int TT_NUMBER = -2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * A constant indicating that a word token has been read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public static final int TT_WORD = -3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /* A constant indicating that no token has been read, used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * initializing ttype.  FIXME This could be made public and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * made available as the part of the API in a future release.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    private static final int TT_NOTHING = -4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * If the current token is a word token, this field contains a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * string giving the characters of the word token. When the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * token is a quoted string token, this field contains the body of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * The current token is a word when the value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * <code>ttype</code> field is <code>TT_WORD</code>. The current token is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * a quoted string token when the value of the <code>ttype</code> field is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * a quote character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * The initial value of this field is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @see     java.io.StreamTokenizer#quoteChar(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @see     java.io.StreamTokenizer#TT_WORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @see     java.io.StreamTokenizer#ttype
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public String sval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * If the current token is a number, this field contains the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * of that number. The current token is a number when the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * the <code>ttype</code> field is <code>TT_NUMBER</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * The initial value of this field is 0.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @see     java.io.StreamTokenizer#TT_NUMBER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @see     java.io.StreamTokenizer#ttype
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    public double nval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /** Private constructor that initializes everything except the streams. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    private StreamTokenizer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        wordChars('a', 'z');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        wordChars('A', 'Z');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        wordChars(128 + 32, 255);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        whitespaceChars(0, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        commentChar('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        quoteChar('"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        quoteChar('\'');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        parseNumbers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Creates a stream tokenizer that parses the specified input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * stream. The stream tokenizer is initialized to the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * default state:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * <li>All byte values <code>'A'</code> through <code>'Z'</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *     <code>'a'</code> through <code>'z'</code>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *     <code>'&#92;u00A0'</code> through <code>'&#92;u00FF'</code> are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *     considered to be alphabetic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * <li>All byte values <code>'&#92;u0000'</code> through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *     <code>'&#92;u0020'</code> are considered to be white space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * <li><code>'/'</code> is a comment character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * <li>Single quote <code>'&#92;''</code> and double quote <code>'"'</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *     are string quote characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * <li>Numbers are parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * <li>Ends of lines are treated as white space, not as separate tokens.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * <li>C-style and C++-style comments are not recognized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @deprecated As of JDK version 1.1, the preferred way to tokenize an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * input stream is to convert it into a character stream, for example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *   Reader r = new BufferedReader(new InputStreamReader(is));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *   StreamTokenizer st = new StreamTokenizer(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @param      is        an input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @see        java.io.BufferedReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @see        java.io.InputStreamReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @see        java.io.StreamTokenizer#StreamTokenizer(java.io.Reader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    public StreamTokenizer(InputStream is) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        this();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (is == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        input = is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Create a tokenizer that parses the given character stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @param r  a Reader object providing the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public StreamTokenizer(Reader r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        this();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (r == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        reader = r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * Resets this tokenizer's syntax table so that all characters are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * "ordinary." See the <code>ordinaryChar</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * for more information on a character being ordinary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @see     java.io.StreamTokenizer#ordinaryChar(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    public void resetSyntax() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        for (int i = ctype.length; --i >= 0;)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            ctype[i] = 0;
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
     * Specifies that all characters <i>c</i> in the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * <code>low&nbsp;&lt;=&nbsp;<i>c</i>&nbsp;&lt;=&nbsp;high</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * are word constituents. A word token consists of a word constituent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * followed by zero or more word constituents or number constituents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @param   low   the low end of the range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @param   hi    the high end of the range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public void wordChars(int low, int hi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (low < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            low = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (hi >= ctype.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            hi = ctype.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        while (low <= hi)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            ctype[low++] |= CT_ALPHA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Specifies that all characters <i>c</i> in the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * <code>low&nbsp;&lt;=&nbsp;<i>c</i>&nbsp;&lt;=&nbsp;high</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * are white space characters. White space characters serve only to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * separate tokens in the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * <p>Any other attribute settings for the characters in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * range are cleared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @param   low   the low end of the range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @param   hi    the high end of the range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    public void whitespaceChars(int low, int hi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        if (low < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            low = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (hi >= ctype.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            hi = ctype.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        while (low <= hi)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            ctype[low++] = CT_WHITESPACE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * Specifies that all characters <i>c</i> in the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * <code>low&nbsp;&lt;=&nbsp;<i>c</i>&nbsp;&lt;=&nbsp;high</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * are "ordinary" in this tokenizer. See the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * <code>ordinaryChar</code> method for more information on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * character being ordinary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @param   low   the low end of the range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @param   hi    the high end of the range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @see     java.io.StreamTokenizer#ordinaryChar(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    public void ordinaryChars(int low, int hi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (low < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            low = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (hi >= ctype.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            hi = ctype.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        while (low <= hi)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            ctype[low++] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * Specifies that the character argument is "ordinary"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * in this tokenizer. It removes any special significance the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * character has as a comment character, word component, string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * delimiter, white space, or number character. When such a character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * is encountered by the parser, the parser treats it as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * single-character token and sets <code>ttype</code> field to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * character value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * <p>Making a line terminator character "ordinary" may interfere
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * with the ability of a <code>StreamTokenizer</code> to count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * lines. The <code>lineno</code> method may no longer reflect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * the presence of such terminator characters in its line count.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @param   ch   the character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @see     java.io.StreamTokenizer#ttype
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    public void ordinaryChar(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        if (ch >= 0 && ch < ctype.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            ctype[ch] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * Specified that the character argument starts a single-line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * comment. All characters from the comment character to the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * the line are ignored by this stream tokenizer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * <p>Any other attribute settings for the specified character are cleared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @param   ch   the character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public void commentChar(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        if (ch >= 0 && ch < ctype.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            ctype[ch] = CT_COMMENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * Specifies that matching pairs of this character delimit string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * constants in this tokenizer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * When the <code>nextToken</code> method encounters a string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * constant, the <code>ttype</code> field is set to the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * delimiter and the <code>sval</code> field is set to the body of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * If a string quote character is encountered, then a string is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * recognized, consisting of all characters after (but not including)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * the string quote character, up to (but not including) the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * occurrence of that same string quote character, or a line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * terminator, or end of file. The usual escape sequences such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * <code>"&#92;n"</code> and <code>"&#92;t"</code> are recognized and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * converted to single characters as the string is parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * <p>Any other attribute settings for the specified character are cleared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * @param   ch   the character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @see     java.io.StreamTokenizer#nextToken()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @see     java.io.StreamTokenizer#sval
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @see     java.io.StreamTokenizer#ttype
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    public void quoteChar(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        if (ch >= 0 && ch < ctype.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            ctype[ch] = CT_QUOTE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * Specifies that numbers should be parsed by this tokenizer. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * syntax table of this tokenizer is modified so that each of the twelve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * characters:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *      0 1 2 3 4 5 6 7 8 9 . -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * has the "numeric" attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * When the parser encounters a word token that has the format of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * double precision floating-point number, it treats the token as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * number rather than a word, by setting the <code>ttype</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * field to the value <code>TT_NUMBER</code> and putting the numeric
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * value of the token into the <code>nval</code> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @see     java.io.StreamTokenizer#nval
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @see     java.io.StreamTokenizer#TT_NUMBER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @see     java.io.StreamTokenizer#ttype
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    public void parseNumbers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        for (int i = '0'; i <= '9'; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            ctype[i] |= CT_DIGIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        ctype['.'] |= CT_DIGIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        ctype['-'] |= CT_DIGIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * Determines whether or not ends of line are treated as tokens.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * If the flag argument is true, this tokenizer treats end of lines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * as tokens; the <code>nextToken</code> method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * <code>TT_EOL</code> and also sets the <code>ttype</code> field to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * this value when an end of line is read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * A line is a sequence of characters ending with either a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * carriage-return character (<code>'&#92;r'</code>) or a newline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * character (<code>'&#92;n'</code>). In addition, a carriage-return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * character followed immediately by a newline character is treated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * as a single end-of-line token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * If the <code>flag</code> is false, end-of-line characters are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * treated as white space and serve only to separate tokens.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * @param   flag   <code>true</code> indicates that end-of-line characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *                 are separate tokens; <code>false</code> indicates that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     *                 end-of-line characters are white space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * @see     java.io.StreamTokenizer#nextToken()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * @see     java.io.StreamTokenizer#ttype
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * @see     java.io.StreamTokenizer#TT_EOL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    public void eolIsSignificant(boolean flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        eolIsSignificantP = flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * Determines whether or not the tokenizer recognizes C-style comments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * If the flag argument is <code>true</code>, this stream tokenizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * recognizes C-style comments. All text between successive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * occurrences of <code>/*</code> and <code>*&#47;</code> are discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * If the flag argument is <code>false</code>, then C-style comments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * are not treated specially.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @param   flag   <code>true</code> indicates to recognize and ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *                 C-style comments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    public void slashStarComments(boolean flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        slashStarCommentsP = flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * Determines whether or not the tokenizer recognizes C++-style comments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * If the flag argument is <code>true</code>, this stream tokenizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * recognizes C++-style comments. Any occurrence of two consecutive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * slash characters (<code>'/'</code>) is treated as the beginning of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * a comment that extends to the end of the line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * If the flag argument is <code>false</code>, then C++-style
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * comments are not treated specially.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @param   flag   <code>true</code> indicates to recognize and ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *                 C++-style comments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    public void slashSlashComments(boolean flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        slashSlashCommentsP = flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * Determines whether or not word token are automatically lowercased.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * If the flag argument is <code>true</code>, then the value in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * <code>sval</code> field is lowercased whenever a word token is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * returned (the <code>ttype</code> field has the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * value <code>TT_WORD</code> by the <code>nextToken</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * of this tokenizer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * If the flag argument is <code>false</code>, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * <code>sval</code> field is not modified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * @param   fl   <code>true</code> indicates that all word tokens should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *               be lowercased.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * @see     java.io.StreamTokenizer#nextToken()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * @see     java.io.StreamTokenizer#ttype
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * @see     java.io.StreamTokenizer#TT_WORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    public void lowerCaseMode(boolean fl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        forceLower = fl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    /** Read the next character */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    private int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        if (reader != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            return reader.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        else if (input != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            return input.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * Parses the next token from the input stream of this tokenizer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * The type of the next token is returned in the <code>ttype</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * field. Additional information about the token may be in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * <code>nval</code> field or the <code>sval</code> field of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * tokenizer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * Typical clients of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * class first set up the syntax tables and then sit in a loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * calling nextToken to parse successive tokens until TT_EOF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * @return     the value of the <code>ttype</code> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * @see        java.io.StreamTokenizer#nval
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * @see        java.io.StreamTokenizer#sval
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * @see        java.io.StreamTokenizer#ttype
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    public int nextToken() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        if (pushedBack) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            pushedBack = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            return ttype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        byte ct[] = ctype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        sval = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        int c = peekc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        if (c < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            c = NEED_CHAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        if (c == SKIP_LF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            c = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            if (c < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                return ttype = TT_EOF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            if (c == '\n')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                c = NEED_CHAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        if (c == NEED_CHAR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            c = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            if (c < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                return ttype = TT_EOF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        ttype = c;              /* Just to be safe */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        /* Set peekc so that the next invocation of nextToken will read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
         * another character unless peekc is reset in this invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        peekc = NEED_CHAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        int ctype = c < 256 ? ct[c] : CT_ALPHA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        while ((ctype & CT_WHITESPACE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            if (c == '\r') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                LINENO++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                if (eolIsSignificantP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                    peekc = SKIP_LF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                    return ttype = TT_EOL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                c = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                if (c == '\n')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                    c = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                if (c == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                    LINENO++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                    if (eolIsSignificantP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                        return ttype = TT_EOL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                c = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            if (c < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                return ttype = TT_EOF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            ctype = c < 256 ? ct[c] : CT_ALPHA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        if ((ctype & CT_DIGIT) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            boolean neg = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            if (c == '-') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                c = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                if (c != '.' && (c < '0' || c > '9')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                    peekc = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                    return ttype = '-';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                neg = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            double v = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            int decexp = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            int seendot = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                if (c == '.' && seendot == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                    seendot = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                else if ('0' <= c && c <= '9') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                    v = v * 10 + (c - '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                    decexp += seendot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                c = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            peekc = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            if (decexp != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                double denom = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                decexp--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                while (decexp > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                    denom *= 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                    decexp--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                /* Do one division of a likely-to-be-more-accurate number */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                v = v / denom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            nval = neg ? -v : v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            return ttype = TT_NUMBER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        if ((ctype & CT_ALPHA) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                if (i >= buf.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                    buf = Arrays.copyOf(buf, buf.length * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                buf[i++] = (char) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                c = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                ctype = c < 0 ? CT_WHITESPACE : c < 256 ? ct[c] : CT_ALPHA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            } while ((ctype & (CT_ALPHA | CT_DIGIT)) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            peekc = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            sval = String.copyValueOf(buf, 0, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            if (forceLower)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                sval = sval.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            return ttype = TT_WORD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        if ((ctype & CT_QUOTE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            ttype = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            /* Invariants (because \Octal needs a lookahead):
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
             *   (i)  c contains char value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
             *   (ii) d contains the lookahead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            int d = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            while (d >= 0 && d != ttype && d != '\n' && d != '\r') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                if (d == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                    c = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                    int first = c;   /* To allow \377, but not \477 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                    if (c >= '0' && c <= '7') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                        c = c - '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                        int c2 = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                        if ('0' <= c2 && c2 <= '7') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                            c = (c << 3) + (c2 - '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                            c2 = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                            if ('0' <= c2 && c2 <= '7' && first <= '3') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                                c = (c << 3) + (c2 - '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                                d = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                                d = c2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                          d = c2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                        switch (c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                        case 'a':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                            c = 0x7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                        case 'b':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                            c = '\b';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                        case 'f':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                            c = 0xC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                        case 'n':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                            c = '\n';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                        case 'r':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                            c = '\r';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                        case 't':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                            c = '\t';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                        case 'v':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                            c = 0xB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                        d = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                    c = d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                    d = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                if (i >= buf.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                    buf = Arrays.copyOf(buf, buf.length * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                buf[i++] = (char)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            /* If we broke out of the loop because we found a matching quote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
             * character then arrange to read a new character next time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
             * around; otherwise, save the character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            peekc = (d == ttype) ? NEED_CHAR : d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            sval = String.copyValueOf(buf, 0, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            return ttype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        if (c == '/' && (slashSlashCommentsP || slashStarCommentsP)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            c = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            if (c == '*' && slashStarCommentsP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                int prevc = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                while ((c = read()) != '/' || prevc != '*') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                    if (c == '\r') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                        LINENO++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                        c = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                        if (c == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                            c = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                        if (c == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                            LINENO++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                            c = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                    if (c < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                        return ttype = TT_EOF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                    prevc = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                return nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            } else if (c == '/' && slashSlashCommentsP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                while ((c = read()) != '\n' && c != '\r' && c >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                peekc = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                return nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                /* Now see if it is still a single line comment */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                if ((ct['/'] & CT_COMMENT) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                    while ((c = read()) != '\n' && c != '\r' && c >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                    peekc = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                    return nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                    peekc = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                    return ttype = '/';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        if ((ctype & CT_COMMENT) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            while ((c = read()) != '\n' && c != '\r' && c >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            peekc = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            return nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        return ttype = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * Causes the next call to the <code>nextToken</code> method of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * tokenizer to return the current value in the <code>ttype</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * field, and not to modify the value in the <code>nval</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * <code>sval</code> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * @see     java.io.StreamTokenizer#nextToken()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * @see     java.io.StreamTokenizer#nval
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * @see     java.io.StreamTokenizer#sval
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * @see     java.io.StreamTokenizer#ttype
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    public void pushBack() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        if (ttype != TT_NOTHING)   /* No-op if nextToken() not called */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            pushedBack = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * Return the current line number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * @return  the current line number of this stream tokenizer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    public int lineno() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        return LINENO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * Returns the string representation of the current stream token and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * the line number it occurs on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * <p>The precise string returned is unspecified, although the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * example can be considered typical:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * <blockquote><pre>Token['a'], line 10</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * @return  a string representation of the token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * @see     java.io.StreamTokenizer#nval
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * @see     java.io.StreamTokenizer#sval
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * @see     java.io.StreamTokenizer#ttype
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        String ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        switch (ttype) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
          case TT_EOF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            ret = "EOF";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
          case TT_EOL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            ret = "EOL";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
          case TT_WORD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            ret = sval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
          case TT_NUMBER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            ret = "n=" + nval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
          case TT_NOTHING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            ret = "NOTHING";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
          default: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                 * ttype is the first character of either a quoted string or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                 * is an ordinary character. ttype can definitely not be less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                 * than 0, since those are reserved values used in the previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                 * case statements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                if (ttype < 256 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                    ((ctype[ttype] & CT_QUOTE) != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                    ret = sval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                char s[] = new char[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                s[0] = s[2] = '\'';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                s[1] = (char) ttype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                ret = new String(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        return "Token[" + ret + "], line " + LINENO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
}