Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/text/Format/common/PParser.java
47182 views
/*1* Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.*;24import java.util.*;2526/*27* assignment : key = value;28* key : string29* value : string | array | dict30* nValue : , value31* array : ( value nValue )32* nAssignment: , assignment|value33* dict : { assignment* }34* string : "*" or anything but a ,(){}=35*36* special characters: ,(){}=37*/3839public class PParser {40protected static final int OPEN_PAIR = 1;41protected static final int CLOSE_PAIR = 2;42protected static final int OPEN_ARRAY = 3;43protected static final int CLOSE_ARRAY = 4;44protected static final int MORE = 5;45protected static final int EQUAL = 6;46protected static final int STRING = 7;47protected static final int WS = 8;4849protected Reader reader;50protected boolean bufferedToken;51protected StringBuffer stringBuffer = new StringBuffer();52protected int lastChar;53protected int lastToken;54protected int lineNumber;55protected int column;5657public PParser() {58}5960public Hashtable parse(Reader r) throws IOException {61this.reader = r;62bufferedToken = false;63lineNumber = 0;64column = 0;65if (getToken() != OPEN_PAIR) {66error("No initial open");67}68return parsePair();69}7071protected Object parseValue(int lookAhead) throws IOException {72int token;7374if (lookAhead == -1) {75token = getToken();76} else {77token = lookAhead;78}79switch (token) {80case STRING:81return stringBuffer.toString();82case OPEN_ARRAY:83return parseArray();84case OPEN_PAIR:85return parsePair();86default:87error("Expecting value");88}89return null;90}9192protected Object parseArray() throws IOException {93Vector array = new Vector();94int token;9596while ((token = getToken()) != CLOSE_ARRAY) {97if (token == MORE) {98token = getToken();99}100if (token != CLOSE_ARRAY) {101array.addElement(parseValue(token));102}103}104return array;105}106107protected Hashtable parsePair() throws IOException {108Hashtable ht = new Hashtable(11);109int token;110111while ((token = getToken()) != CLOSE_PAIR) {112if (token != STRING) {113error("Pair expecting string got");114}115String key = stringBuffer.toString();116117if (getToken() != EQUAL) {118error("Expecting = ");119}120121Object value = parseValue(-1);122ht.put(key, value);123}124return ht;125}126127protected void ungetToken() {128if (bufferedToken) {129error("Can not buffer more than one token");130}131bufferedToken = true;132}133134protected int getToken() throws IOException {135int token = getToken(false, false);136137return token;138}139140protected int getToken(boolean wantsWS, boolean inString)141throws IOException {142if (bufferedToken) {143bufferedToken = false;144if (lastToken != WS || wantsWS) {145return lastToken;146}147}148while ((lastChar = reader.read()) != -1) {149// If a line starts with '#', skip the line.150if (column == 0 && lastChar == '#') {151while ((lastChar = reader.read()) != -1152&& lastChar != '\n') {153}154if (lastChar == -1) {155break;156}157}158159column++;160switch(lastChar) {161case '\n':162lineNumber++;163column = 0;164case ' ':165case '\r':166case '\t':167if (wantsWS) {168lastToken = WS;169return WS;170}171break;172case ',':173lastToken = MORE;174return MORE;175case '(':176lastToken = OPEN_ARRAY;177return OPEN_ARRAY;178case ')':179lastToken = CLOSE_ARRAY;180return CLOSE_ARRAY;181case '{':182lastToken = OPEN_PAIR;183return OPEN_PAIR;184case '}':185lastToken = CLOSE_PAIR;186return CLOSE_PAIR;187case '=':188lastToken = EQUAL;189return EQUAL;190case '"':191lastToken = STRING;192if (!inString) {193stringBuffer.setLength(0);194while (true) {195getToken(true, true);196if (lastChar == '"') {197lastToken = STRING;198return STRING;199}200stringBuffer.append((char)lastChar);201}202}203return STRING;204default:205lastToken = STRING;206if (!inString) {207stringBuffer.setLength(0);208stringBuffer.append((char)lastChar);209while (getToken(true, true) == STRING) {210if (lastChar == '"') {211error("Unexpected quote");212}213stringBuffer.append((char)lastChar);214}215ungetToken();216}217return STRING;218}219}220return -1;221}222223protected void error(String errorString) {224throw new RuntimeException(errorString + " at line " + lineNumber + " column " + column);225}226227public static void dump(Object o) {228if (o instanceof String) {229System.out.print(o);230} else if(o instanceof Vector) {231Enumeration e = ((Vector)o).elements();232233dump(" (");234while (e.hasMoreElements()) {235dump(e.nextElement());236dump(" -- ");237}238dump(" )");239} else {240Hashtable ht = (Hashtable)o;241Enumeration e = ht.keys();242243dump(" {");244while (e.hasMoreElements()) {245Object key = e.nextElement();246247dump(key);248dump(" = ");249dump(ht.get(key));250dump(";");251}252dump(" }");253}254}255256public static void main(String[] args) {257if (args.length == 0) {258System.out.println("need filename");259} else {260try {261FileReader fr = new FileReader(args[0]);262PParser parser = new PParser();263Hashtable ht = parser.parse(fr);264265dump(ht);266System.out.println();267}268catch (IOException ioe) {269System.out.println("Couldn't parse: " + ioe);270}271}272}273}274275276