Path: blob/master/test/jdk/java/text/Format/common/PParser.java
41152 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 Map<String,Object> 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 {93List<Object> array = new ArrayList<>();94int token;9596while ((token = getToken()) != CLOSE_ARRAY) {97if (token == MORE) {98token = getToken();99}100if (token != CLOSE_ARRAY) {101array.add(parseValue(token));102}103}104return array;105}106107protected Map<String,Object> parsePair() throws IOException {108Map<String,Object> ht = new HashMap<>(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}139140@SuppressWarnings("fallthrough")141protected int getToken(boolean wantsWS, boolean inString)142throws IOException {143if (bufferedToken) {144bufferedToken = false;145if (lastToken != WS || wantsWS) {146return lastToken;147}148}149while ((lastChar = reader.read()) != -1) {150// If a line starts with '#', skip the line.151if (column == 0 && lastChar == '#') {152while ((lastChar = reader.read()) != -1153&& lastChar != '\n') {154}155if (lastChar == -1) {156break;157}158}159160column++;161switch(lastChar) {162case '\n':163lineNumber++;164column = 0;165case ' ':166case '\r':167case '\t':168if (wantsWS) {169lastToken = WS;170return WS;171}172break;173case ',':174lastToken = MORE;175return MORE;176case '(':177lastToken = OPEN_ARRAY;178return OPEN_ARRAY;179case ')':180lastToken = CLOSE_ARRAY;181return CLOSE_ARRAY;182case '{':183lastToken = OPEN_PAIR;184return OPEN_PAIR;185case '}':186lastToken = CLOSE_PAIR;187return CLOSE_PAIR;188case '=':189lastToken = EQUAL;190return EQUAL;191case '"':192lastToken = STRING;193if (!inString) {194stringBuffer.setLength(0);195while (true) {196getToken(true, true);197if (lastChar == '"') {198lastToken = STRING;199return STRING;200}201stringBuffer.append((char)lastChar);202}203}204return STRING;205default:206lastToken = STRING;207if (!inString) {208stringBuffer.setLength(0);209stringBuffer.append((char)lastChar);210while (getToken(true, true) == STRING) {211if (lastChar == '"') {212error("Unexpected quote");213}214stringBuffer.append((char)lastChar);215}216ungetToken();217}218return STRING;219}220}221return -1;222}223224protected void error(String errorString) {225throw new RuntimeException(errorString + " at line " + lineNumber + " column " + column);226}227228@SuppressWarnings("unchecked")229public static void dump(Object o) {230if (o instanceof String) {231System.out.print(o);232} else if(o instanceof List) {233dump(" (");234((List)o).forEach((l) -> {235dump(l);236dump(" -- ");237});238dump(" )");239} else {240Map<String,Object> ht = (Map<String,Object>)o;241dump(" {");242ht.keySet().forEach(l -> {243dump(l);244dump(" = ");245dump(ht.get(l));246dump(";");247});248dump(" }");249}250}251252public static void main(String[] args) {253if (args.length == 0) {254System.out.println("need filename");255} else {256try {257FileReader fr = new FileReader(args[0]);258PParser parser = new PParser();259Map<String,Object> ht = parser.parse(fr);260261dump(ht);262System.out.println();263}264catch (IOException ioe) {265System.out.println("Couldn't parse: " + ioe);266}267}268}269}270271272