src/jdk.jcmd/share/classes/sun/tools/jstat/Parser.java
author jdv
Wed, 18 Apr 2018 13:22:53 +0530
changeset 49996 39dc39093c5e
parent 47216 71c04702a3d5
child 49891 61b0342b5711
permissions -rw-r--r--
6574555: PNGImageWriter incorrectly sets bKGD chunk Reviewed-by: prr, pnarayanan
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) 2004, 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 sun.tools.jstat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * A class implementing a simple predictive parser for output format
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * specification language for the jstat command.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @author Brian Doherty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class Parser {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static boolean pdebug = Boolean.getBoolean("jstat.parser.debug");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static boolean ldebug = Boolean.getBoolean("jstat.lex.debug");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static final char OPENBLOCK = '{';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static final char CLOSEBLOCK = '}';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static final char DOUBLEQUOTE = '"';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static final char PERCENT_CHAR = '%';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final char OPENPAREN = '(';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static final char CLOSEPAREN = ')';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final char OPERATOR_PLUS = '+';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final char OPERATOR_MINUS = '-';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static final char OPERATOR_MULTIPLY = '*';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final char OPERATOR_DIVIDE = '/';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static final String OPTION = "option";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static final String COLUMN = "column";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private static final String DATA = "data";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static final String HEADER = "header";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static final String WIDTH = "width";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static final String FORMAT = "format";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static final String ALIGN = "align";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static final String SCALE = "scale";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private static final String START = OPTION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
24871
224e298c3978 8044865: Fix raw and unchecked lint warnings in management-related code
sjiang
parents: 5506
diff changeset
    66
    private static final Set<String> scaleKeyWords = Scale.keySet();
224e298c3978 8044865: Fix raw and unchecked lint warnings in management-related code
sjiang
parents: 5506
diff changeset
    67
    private static final Set<String> alignKeyWords = Alignment.keySet();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static String[] otherKeyWords = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        OPTION, COLUMN, DATA, HEADER, WIDTH, FORMAT, ALIGN, SCALE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private static char[] infixOps = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        OPERATOR_PLUS, OPERATOR_MINUS, OPERATOR_MULTIPLY, OPERATOR_DIVIDE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private static char[] delimiters = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        OPENBLOCK, CLOSEBLOCK, PERCENT_CHAR, OPENPAREN, CLOSEPAREN
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static Set<String> reservedWords;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private StreamTokenizer st;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private String filename;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private Token lookahead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private Token previous;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private int columnCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private OptionFormat optionFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public Parser(String filename) throws FileNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        this.filename = filename;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        Reader r = new BufferedReader(new FileReader(filename));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public Parser(Reader r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        st = new StreamTokenizer(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        // allow both c++ style comments
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        st.ordinaryChar('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        st.wordChars('_','_');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        st.slashSlashComments(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        st.slashStarComments(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        reservedWords = new HashSet<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        for (int i = 0; i < otherKeyWords.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            reservedWords.add(otherKeyWords[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        for (int i = 0; i < delimiters.length; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            st.ordinaryChar(delimiters[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        for (int i = 0; i < infixOps.length; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            st.ordinaryChar(infixOps[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * push back the lookahead token and restore the lookahead token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * to the previous token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private void pushBack() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        lookahead = previous;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        st.pushBack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * retrieve the next token, placing the token value in the lookahead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * member variable, storing its previous value in the previous member
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * variable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private void nextToken() throws ParserException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        int t = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        previous = lookahead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        lookahead = new Token(st.ttype, st.sval, st.nval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        log(ldebug, "lookahead = " + lookahead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * match one of the token values in the given set of key words
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * token is assumed to be of type TT_WORD, and the set is assumed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * to contain String objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
24871
224e298c3978 8044865: Fix raw and unchecked lint warnings in management-related code
sjiang
parents: 5506
diff changeset
   144
    private Token matchOne(Set<String> keyWords) throws ParserException, IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if ((lookahead.ttype == StreamTokenizer.TT_WORD)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                && keyWords.contains(lookahead.sval)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            Token t = lookahead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        throw new SyntaxException(st.lineno(), keyWords, lookahead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * match a token with TT_TYPE=type, and the token value is a given sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * of characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    private void match(int ttype, String token)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                 throws ParserException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (lookahead.ttype == ttype && lookahead.sval.compareTo(token) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
           throw new SyntaxException(st.lineno(), new Token(ttype, token),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                                     lookahead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * match a token with TT_TYPE=type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    private void match(int ttype) throws ParserException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (lookahead.ttype == ttype) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
           throw new SyntaxException(st.lineno(), new Token(ttype), lookahead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * match a token with TT_TYPE=char, where the token value is the given char.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private void match(char ttype) throws ParserException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
      if (lookahead.ttype == (int)ttype) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
          nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
      else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
          throw new SyntaxException(st.lineno(), new Token((int)ttype),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                    lookahead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * match a token with TT_TYPE='"', where the token value is a sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * of characters between matching quote characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    private void matchQuotedString() throws ParserException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        match(DOUBLEQUOTE);
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
     * match a TT_NUMBER token that matches a parsed number value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    private void matchNumber() throws ParserException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        match(StreamTokenizer.TT_NUMBER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * match a TT_WORD token that matches an arbitrary, not quoted token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    private void matchID() throws ParserException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        match(StreamTokenizer.TT_WORD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * match a TT_WORD token that matches the given string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    private void match(String token) throws ParserException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        match(StreamTokenizer.TT_WORD, token);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * determine if the given word is a reserved key word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    private boolean isReservedWord(String word) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        return reservedWords.contains(word);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * determine if the give work is a reserved key word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    private boolean isInfixOperator(char op) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        for (int i = 0; i < infixOps.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            if (op == infixOps[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * scalestmt -> 'scale' scalespec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * scalespec -> <see above scaleTerminals array>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    private void scaleStmt(ColumnFormat cf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                 throws ParserException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        match(SCALE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        Token t = matchOne(scaleKeyWords);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        cf.setScale(Scale.toScale(t.sval));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        String scaleString = t.sval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        log(pdebug, "Parsed: scale -> " + scaleString);
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
     * alignstmt -> 'align' alignspec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * alignspec -> <see above alignTerminals array>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    private void alignStmt(ColumnFormat cf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                 throws ParserException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        match(ALIGN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        Token t = matchOne(alignKeyWords);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        cf.setAlignment(Alignment.toAlignment(t.sval));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        String alignString = t.sval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        log(pdebug, "Parsed: align -> " + alignString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * headerstmt -> 'header' quotedstring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    private void headerStmt(ColumnFormat cf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                 throws ParserException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        match(HEADER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        String headerString = lookahead.sval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        matchQuotedString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        cf.setHeader(headerString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        log(pdebug, "Parsed: header -> " + headerString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * widthstmt -> 'width' integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    private void widthStmt(ColumnFormat cf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                 throws ParserException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        match(WIDTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        double width = lookahead.nval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        matchNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        cf.setWidth((int)width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        log(pdebug, "Parsed: width -> " + width );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * formatstmt -> 'format' quotedstring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    private void formatStmt(ColumnFormat cf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                 throws ParserException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        match(FORMAT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        String formatString = lookahead.sval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        matchQuotedString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        cf.setFormat(formatString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        log(pdebug, "Parsed: format -> " + formatString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *  Primary -> Literal | Identifier | '(' Expression ')'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    private Expression primary() throws ParserException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        Expression e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        switch (lookahead.ttype) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        case OPENPAREN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            match(OPENPAREN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            e = expression();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            match(CLOSEPAREN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        case StreamTokenizer.TT_WORD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            String s = lookahead.sval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            if (isReservedWord(s)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                throw new SyntaxException(st.lineno(), "IDENTIFIER",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                                          "Reserved Word: " + lookahead.sval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            matchID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            e = new Identifier(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            log(pdebug, "Parsed: ID -> " + s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        case StreamTokenizer.TT_NUMBER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            double literal = lookahead.nval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            matchNumber();
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 25859
diff changeset
   327
            e = new Literal(Double.valueOf(literal));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            log(pdebug, "Parsed: number -> " + literal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            throw new SyntaxException(st.lineno(), "IDENTIFIER", lookahead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        log(pdebug, "Parsed: primary -> " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        return e;
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
     * Unary -> ('+'|'-') Unary | Primary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    private Expression unary() throws ParserException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        Expression e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        Operator op = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            switch (lookahead.ttype) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            case OPERATOR_PLUS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                match(OPERATOR_PLUS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                op = Operator.PLUS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            case OPERATOR_MINUS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                match(OPERATOR_MINUS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                op = Operator.MINUS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                e = primary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                log(pdebug, "Parsed: unary -> " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            Expression e1 = new Expression();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            e1.setOperator(op);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            e1.setRight(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            log(pdebug, "Parsed: unary -> " + e1);
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 25859
diff changeset
   363
            e1.setLeft(new Literal(Double.valueOf(0)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            e = e1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *  MultExpression -> Unary (('*' | '/') Unary)*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    private Expression multExpression() throws ParserException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        Expression e = unary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        Operator op = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            switch (lookahead.ttype) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            case OPERATOR_MULTIPLY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                match(OPERATOR_MULTIPLY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                op = Operator.MULTIPLY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            case OPERATOR_DIVIDE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                match(OPERATOR_DIVIDE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                op = Operator.DIVIDE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                log(pdebug, "Parsed: multExpression -> " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            Expression e1 = new Expression();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            e1.setOperator(op);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            e1.setLeft(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            e1.setRight(unary());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            e = e1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            log(pdebug, "Parsed: multExpression -> " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *  AddExpression -> MultExpression (('+' | '-') MultExpression)*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    private Expression addExpression() throws ParserException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        Expression e = multExpression();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        Operator op = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            switch (lookahead.ttype) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            case OPERATOR_PLUS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                match(OPERATOR_PLUS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                op = Operator.PLUS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            case OPERATOR_MINUS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                match(OPERATOR_MINUS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                op = Operator.MINUS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                log(pdebug, "Parsed: addExpression -> " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            Expression e1 = new Expression();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            e1.setOperator(op);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            e1.setLeft(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            e1.setRight(multExpression());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            e = e1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            log(pdebug, "Parsed: addExpression -> " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *  Expression -> AddExpression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    private Expression expression() throws ParserException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        Expression e = addExpression();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        log(pdebug, "Parsed: expression -> " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * datastmt -> 'data' expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    private void dataStmt(ColumnFormat cf) throws ParserException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        match(DATA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        Expression e = expression();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        cf.setExpression(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        log(pdebug, "Parsed: data -> " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * statementlist -> optionalstmt statementlist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * optionalstmt -> 'data' expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *                 'header' quotedstring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *                 'width' integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     *                 'format' formatstring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *                 'align' alignspec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *                 'scale' scalespec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    private void statementList(ColumnFormat cf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                 throws ParserException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            if (lookahead.ttype != StreamTokenizer.TT_WORD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            if (lookahead.sval.compareTo(DATA) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                dataStmt(cf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            } else if (lookahead.sval.compareTo(HEADER) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                headerStmt(cf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            } else if (lookahead.sval.compareTo(WIDTH) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                widthStmt(cf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            } else if (lookahead.sval.compareTo(FORMAT) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                formatStmt(cf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            } else if (lookahead.sval.compareTo(ALIGN) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                alignStmt(cf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            } else if (lookahead.sval.compareTo(SCALE) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                scaleStmt(cf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * optionlist -> columspec optionlist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *               null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * columspec -> 'column' '{' statementlist '}'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    private void optionList(OptionFormat of)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                 throws ParserException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            if (lookahead.ttype != StreamTokenizer.TT_WORD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            match(COLUMN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            match(OPENBLOCK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            ColumnFormat cf = new ColumnFormat(columnCount++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            statementList(cf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
              match(CLOSEBLOCK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            cf.validate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            of.addSubFormat(cf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * optionstmt -> 'option' ID '{' optionlist '}'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    private OptionFormat optionStmt() throws ParserException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        match(OPTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        String optionName=lookahead.sval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        matchID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        match(OPENBLOCK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        OptionFormat of = new OptionFormat(optionName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        optionList(of);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        match(CLOSEBLOCK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        return of;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * parse the specification for the given option identifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    public OptionFormat parse(String option)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                        throws ParserException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
         * this search stops on the first occurance of an option
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
         * statement with a name matching the given option. Any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
         * duplicate options are ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        while (lookahead.ttype != StreamTokenizer.TT_EOF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            // look for the start symbol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            if ((lookahead.ttype != StreamTokenizer.TT_WORD)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                    || (lookahead.sval.compareTo(START) != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                // skip tokens until a start symbol is found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            // check if the option name is the one we are interested in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            match(START);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            if ((lookahead.ttype == StreamTokenizer.TT_WORD)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                    && (lookahead.sval.compareTo(option) == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                // this is the one we are looking for, parse it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                pushBack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                return optionStmt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                // not what we are looking for, start skipping tokens
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    public Set<OptionFormat> parseOptions() throws ParserException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        Set<OptionFormat> options = new HashSet<OptionFormat>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        while (lookahead.ttype != StreamTokenizer.TT_EOF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            // look for the start symbol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            if ((lookahead.ttype != StreamTokenizer.TT_WORD)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                    || (lookahead.sval.compareTo(START) != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                // skip tokens until a start symbol is found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            // note: if a duplicate option statement exists, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            // first one encountered is the chosen definition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            OptionFormat of = optionStmt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            options.add(of);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        return options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    OptionFormat getOptionFormat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
       return optionFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    private void log(boolean logging, String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        if (logging) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            System.out.println(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
}