json.JSONTreeParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xpresso Show documentation
Show all versions of xpresso Show documentation
The most pythonic way to code in Java
The newest version!
// Generated from JSONTree.g by ANTLR 4.5
package com.wantedtech.common.xpresso.json2;
import com.wantedtech.common.xpresso.types.*;
import com.wantedtech.common.xpresso.x;
import java.io.ByteArrayOutputStream;
import java.io.OutputStreamWriter;
import org.antlr.v4.runtime.atn.*;
import org.antlr.v4.runtime.dfa.DFA;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.misc.*;
import org.antlr.v4.runtime.tree.*;
import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;
@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"})
public class JSONTreeParser extends Parser {
static { RuntimeMetaData.checkVersion("4.5", RuntimeMetaData.VERSION); }
protected static final DFA[] _decisionToDFA;
protected static final PredictionContextCache _sharedContextCache =
new PredictionContextCache();
public static final int
T__0=1, T__1=2, T__2=3, T__3=4, T__4=5, Number=6, Exponent=7, String=8,
WS=9, COMMA=10, TRUE=11, FALSE=12, NULL=13, STRING=14, NUMBER=15, OBJECT=16,
FIELD=17, ARRAY=18;
public static final int
RULE_value = 0, RULE_string = 1, RULE_object = 2, RULE_number = 3, RULE_array = 4,
RULE_elements = 5, RULE_members = 6, RULE_pair = 7;
public static final String[] ruleNames = {
"value", "string", "object", "number", "array", "elements", "members",
"pair"
};
private static final String[] _LITERAL_NAMES = {
null, "'{'", "'}'", "'['", "']'", "':'", null, null, null, null, "','"
};
private static final String[] _SYMBOLIC_NAMES = {
null, null, null, null, null, null, "Number", "Exponent", "String", "WS",
"COMMA", "TRUE", "FALSE", "NULL", "STRING", "NUMBER", "OBJECT", "FIELD",
"ARRAY"
};
public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES);
/**
* @deprecated Use {@link #VOCABULARY} instead.
*/
@Deprecated
public static final String[] tokenNames;
static {
tokenNames = new String[_SYMBOLIC_NAMES.length];
for (int i = 0; i < tokenNames.length; i++) {
tokenNames[i] = VOCABULARY.getLiteralName(i);
if (tokenNames[i] == null) {
tokenNames[i] = VOCABULARY.getSymbolicName(i);
}
if (tokenNames[i] == null) {
tokenNames[i] = "";
}
}
}
@Override
@Deprecated
public String[] getTokenNames() {
return tokenNames;
}
@Override
public Vocabulary getVocabulary() {
return VOCABULARY;
}
@Override
public String getGrammarFileName() { return "JSONTree.g"; }
@Override
public String[] getRuleNames() { return ruleNames; }
@Override
public String getSerializedATN() { return _serializedATN; }
@Override
public ATN getATN() { return _ATN; }
private Object extractNumber(String rawNumber, String rawExponent) {
String numberBody = rawNumber;
String exponent = (rawExponent == null) ? null : rawExponent.substring(1); // remove the 'e' prefix if there
boolean isReal = numberBody.indexOf('.') >= 0 || exponent != null;
if (!isReal) {
return new Integer(numberBody);
} else {
double result = Double.parseDouble(numberBody);
if (exponent != null) {
result = result * Math.pow(10.0f, Double.parseDouble(exponent));
}
return new Double(result);
}
}
private String extractString(String rawString) {
// StringBuffers are an efficient way to modify strings
StringBuffer sb = new StringBuffer(rawString);
// Process character escapes
int startPoint = 1; // skip initial quotation mark
for (;;) {
int slashIndex = sb.indexOf("\\", startPoint); // search for a single backslash
if (slashIndex == -1) break;
// Else, we have a backslash
char escapeType = sb.charAt(slashIndex + 1);
switch (escapeType) {
case'u':
// Unicode escape.
String unicode = extractUnicode(sb, slashIndex);
sb.replace(slashIndex, slashIndex + 6, unicode); // backspace
break; // back to the loop
// note: Java's character escapes match JSON's, which is why it looks like we're replacing
// "\b" with "\b". We're actually replacing 2 characters (slash-b) with one (backspace).
case 'b':
sb.replace(slashIndex, slashIndex + 2, "\b"); // backspace
break;
case 't':
sb.replace(slashIndex, slashIndex + 2, "\t"); // tab
break;
case 'n':
sb.replace(slashIndex, slashIndex + 2, "\n"); // newline
break;
case 'f':
sb.replace(slashIndex, slashIndex + 2, "\f"); // form feed
break;
case 'r':
sb.replace(slashIndex, slashIndex + 2, "\r"); // return
break;
case '\'':
sb.replace(slashIndex, slashIndex + 2, "\'"); // single quote
break;
case '\"':
sb.replace(slashIndex, slashIndex + 2, "\""); // double quote
break;
case '\\':
sb.replace(slashIndex, slashIndex + 2, "\\"); // backslash
break;
case '/':
sb.replace(slashIndex, slashIndex + 2, "/"); // solidus
break;
}
startPoint = slashIndex+1;
}
// remove surrounding quotes
sb.deleteCharAt(0);
sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}
private String extractUnicode(StringBuffer sb, int slashIndex) {
// Gather the 4 hex digits, convert to an integer, translate the number to a unicode char, replace
String result;
String code = sb.substring(slashIndex + 2, slashIndex + 6);
int charNum = Integer.parseInt(code, 16); // hex to integer
// There's no simple way to go from an int to a unicode character.
// We'll have to pass this through an output stream writer to do
// the conversion.
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(baos, "UTF-8");
osw.write(charNum);
osw.flush();
result = baos.toString("UTF-8"); // Thanks to Silvester Pozarnik for the tip about adding "UTF-8" here
} catch (Exception e) {
e.printStackTrace();
result = null;
}
return result;
}
public JSONTreeParser(TokenStream input) {
super(input);
_interp = new ParserATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache);
}
public static class ValueContext extends ParserRuleContext {
public Object result;
public StringContext s;
public NumberContext n;
public ObjectContext o;
public ArrayContext a;
public StringContext string() {
return getRuleContext(StringContext.class,0);
}
public NumberContext number() {
return getRuleContext(NumberContext.class,0);
}
public ObjectContext object() {
return getRuleContext(ObjectContext.class,0);
}
public ArrayContext array() {
return getRuleContext(ArrayContext.class,0);
}
public TerminalNode TRUE() { return getToken(JSONTreeParser.TRUE, 0); }
public TerminalNode FALSE() { return getToken(JSONTreeParser.FALSE, 0); }
public TerminalNode NULL() { return getToken(JSONTreeParser.NULL, 0); }
public ValueContext(ParserRuleContext parent, int invokingState) {
super(parent, invokingState);
}
@Override public int getRuleIndex() { return RULE_value; }
@Override
public void enterRule(ParseTreeListener listener) {
if ( listener instanceof JSONTreeListener ) ((JSONTreeListener)listener).enterValue(this);
}
@Override
public void exitRule(ParseTreeListener listener) {
if ( listener instanceof JSONTreeListener ) ((JSONTreeListener)listener).exitValue(this);
}
}
public final ValueContext value() throws RecognitionException {
ValueContext _localctx = new ValueContext(_ctx, getState());
enterRule(_localctx, 0, RULE_value);
try {
setState(34);
switch (_input.LA(1)) {
case String:
enterOuterAlt(_localctx, 1);
{
setState(16);
((ValueContext)_localctx).s = string();
((ValueContext)_localctx).result = ((ValueContext)_localctx).s.result;
}
break;
case Number:
enterOuterAlt(_localctx, 2);
{
setState(19);
((ValueContext)_localctx).n = number();
((ValueContext)_localctx).result = ((ValueContext)_localctx).n.result;
}
break;
case T__0:
enterOuterAlt(_localctx, 3);
{
setState(22);
((ValueContext)_localctx).o = object();
((ValueContext)_localctx).result = ((ValueContext)_localctx).o.result;
}
break;
case T__2:
enterOuterAlt(_localctx, 4);
{
setState(25);
((ValueContext)_localctx).a = array();
((ValueContext)_localctx).result = ((ValueContext)_localctx).a.lst;
}
break;
case TRUE:
enterOuterAlt(_localctx, 5);
{
setState(28);
match(TRUE);
((ValueContext)_localctx).result = Boolean.TRUE;
}
break;
case FALSE:
enterOuterAlt(_localctx, 6);
{
setState(30);
match(FALSE);
((ValueContext)_localctx).result = Boolean.FALSE;
}
break;
case NULL:
enterOuterAlt(_localctx, 7);
{
setState(32);
match(NULL);
((ValueContext)_localctx).result = null;
}
break;
default:
throw new NoViableAltException(this);
}
}
catch (RecognitionException re) {
_localctx.exception = re;
_errHandler.reportError(this, re);
_errHandler.recover(this, re);
}
finally {
exitRule();
}
return _localctx;
}
public static class StringContext extends ParserRuleContext {
public String result;
public Token st;
public TerminalNode String() { return getToken(JSONTreeParser.String, 0); }
public StringContext(ParserRuleContext parent, int invokingState) {
super(parent, invokingState);
}
@Override public int getRuleIndex() { return RULE_string; }
@Override
public void enterRule(ParseTreeListener listener) {
if ( listener instanceof JSONTreeListener ) ((JSONTreeListener)listener).enterString(this);
}
@Override
public void exitRule(ParseTreeListener listener) {
if ( listener instanceof JSONTreeListener ) ((JSONTreeListener)listener).exitString(this);
}
}
public final StringContext string() throws RecognitionException {
StringContext _localctx = new StringContext(_ctx, getState());
enterRule(_localctx, 2, RULE_string);
try {
enterOuterAlt(_localctx, 1);
{
setState(36);
((StringContext)_localctx).st = match(String);
((StringContext)_localctx).result = extractString((((StringContext)_localctx).st!=null?((StringContext)_localctx).st.getText():null));
}
}
catch (RecognitionException re) {
_localctx.exception = re;
_errHandler.reportError(this, re);
_errHandler.recover(this, re);
}
finally {
exitRule();
}
return _localctx;
}
public static class ObjectContext extends ParserRuleContext {
public dict