/* * This software is licensed under the terms of the ISC License. * (ISCL http://www.opensource.org/licenses/isc-license.txt * It is functionally equivalent to the 2-clause BSD licence, * with language "made unnecessary by the Berne convention" removed). * * Copyright (c) 2011, Mike Norman * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE * USE OR PERFORMANCE OF THIS SOFTWARE. * */ options { STATIC = false; SUPPORT_CLASS_VISIBILITY_PUBLIC = true; ERROR_REPORTING = false; JAVA_UNICODE_ESCAPE = true; UNICODE_INPUT = true; NODE_USES_PARSER = false; VISITOR = false; } PARSER_BEGIN(JSONParser) /* * This software is licensed under the terms of the ISC License. * (ISCL http://www.opensource.org/licenses/isc-license.txt * It is functionally equivalent to the 2-clause BSD licence, * with language "made unnecessary by the Berne convention" removed). * * Copyright (c) 2011, Mike Norman * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE * USE OR PERFORMANCE OF THIS SOFTWARE. * */ package org.mwnorman.json; //javase imports import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; public class JSONParser { public JSONParser() { super(); } } PARSER_END(JSONParser) // Pls see http://www.ietf.org/rfc/rfc4627.txt for JSON spec details // white space SKIP: { " " | "\t" | "\n" | "\r" | "\f" } // comments: not really part of JSON spec, but parser shouldn't blow-up if present SKIP: { } SKIP: { } // JSON reserved keywords (prefix with K_ to avoid naming conflicts): only lower case allowed! TOKEN: { | | } // JSON operators (prefix with O_ to avoid naming conflicts) TOKEN: { | | | | | | | | } // numeric literals TOKEN: { <#DIGIT: ["0" - "9"] > | <#NONZERO_DIGIT: ["1" - "9"] > | <#EXP: ["e", "E"] ( | )? > } // JSON numbers do not support octal or hexadecimal formats TOKEN: { | | | > | )? ( | ) > | > | > | )+ > } // string literals TOKEN: { )* "\"" > | } Object parse() #void: { Object o = null; } { ( o=object() | o=array() ) { return o; } } Object object() #void: { Map m = new LinkedHashMap(); } { ( members(m) )? { return m; } } void members(Map m) #void: { } { pair(m) [ members(m) ] } void pair(Map m) #void: { Token t = null; Object o; } { t= o=value() { m.put(t.image, o); } } Object array() #void: { List a=new ArrayList(); } { ( elements(a) )? { Collections.reverse(a); return a; } } void elements(List a) #void: { Object o = null; } { o=value() [ elements(a) ] { a.add(o); } } Object value() #void: { Token t = null; Object o = null; } { ( o=object() | o=array() | t= {o = t.image;} | t= { try { o = Integer.valueOf(t.image); } catch (NumberFormatException nfe1) { try { o = Long.valueOf(t.image); } catch (NumberFormatException nfe2) { try { o = Float.valueOf(t.image); } catch (NumberFormatException nfe3) { try { o = Double.valueOf(t.image); } catch (NumberFormatException nfe4) { o = Double.NaN; } } } } } | {o = Boolean.TRUE;} | {o = Boolean.TRUE;} | ) { return o; } }