juzu.impl.common.JSONParser Maven / Gradle / Ivy
/* Generated By:JavaCC: Do not edit this line. JSONParser.java */
package juzu.impl.common;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
// Based on https://github.com/mwnorman/JSONParser
// at https://github.com/mwnorman/JSONParser/tree/90baa1b93e5779e4aff771bd1d9aa12578fbad0a
// with very few modifications
public class JSONParser implements JSONParserConstants {
final public Object parse() throws ParseException {
Object o = null;
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case O_OPENBRACE:
o = object();
break;
case O_OPENBRACKET:
o = array();
break;
default:
jj_consume_token(-1);
throw new ParseException();
}
{if (true) return o;}
throw new Error("Missing return statement in function");
}
final public JSON object() throws ParseException {
JSON m = JSON.json();
jj_consume_token(O_OPENBRACE);
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case SINGLE_QUOTED_STRING:
case QUOTED_STRING:
case UNQUOTED_STRING:
members(m);
break;
default:
;
}
jj_consume_token(O_CLOSEBRACE);
{if (true) return m;}
throw new Error("Missing return statement in function");
}
final public void members(JSON m) throws ParseException {
pair(m);
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case O_COMMA:
jj_consume_token(O_COMMA);
members(m);
break;
default:
;
}
}
final public void pair(JSON m) throws ParseException {
Token t = null;
Object o;
String fieldName = null;
fieldName = fieldName();
jj_consume_token(O_COLON);
o = value();
m.set(fieldName, o);
}
final public String fieldName() throws ParseException {
String fieldName = null;
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case SINGLE_QUOTED_STRING:
jj_consume_token(SINGLE_QUOTED_STRING);
fieldName = stripOffQuotes(decodeBackslash(token.image));
break;
case QUOTED_STRING:
jj_consume_token(QUOTED_STRING);
fieldName = stripOffQuotes(decodeBackslash(token.image));
break;
case UNQUOTED_STRING:
jj_consume_token(UNQUOTED_STRING);
fieldName = decodeBackslash(token.image);
break;
default:
jj_consume_token(-1);
throw new ParseException();
}
{if (true) return fieldName;}
throw new Error("Missing return statement in function");
}
String stripOffQuotes(String quotedString) throws ParseException {
if (!(quotedString.startsWith("\u005c"") || quotedString.startsWith("'"))) {
return quotedString;
}
return quotedString.substring(1, quotedString.length() - 1);
}
String decodeBackslash(String s) throws ParseException {
StringBuilder sb = new StringBuilder();
int inputLength = s.length();
boolean isAfterSlash = false;
for (int i = 0; i < inputLength; i ++) {
char c = s.charAt(i);
if (c == '\u005c\u005c') {
if (!isAfterSlash) {
isAfterSlash = true;
}
else {
sb.append(c);
isAfterSlash = false;
}
}
else {
if (isAfterSlash) {
switch (c) {
case 'n':
sb.append('\u005cn');
break;
case 'r':
sb.append('\u005cr');
break;
case 't':
sb.append('\u005ct');
break;
case 'b':
sb.append('\u005cb');
break;
case 'f':
sb.append('\u005cf');
break;
case '\u005c'':
sb.append('\u005c'');
break;
case '/':
sb.append('/');
break;
case '\u005c"':
sb.append('\u005c"');
break;
//handle Unicode-escaping
case 'u':
int u = Integer.parseInt(s.substring(i + 1, i + 5), 16);
sb.append((char)u);
i += 4;
break;
}
isAfterSlash = false;
}
else {
sb.append(c);
}
}
}
return sb.toString();
}
final public ArrayList