src/java.management.rest/share/classes/com/oracle/jmx/remote/rest/json/parser/JsonParser.jj
branchjmx-rest-api
changeset 56002 60ab3b595a8e
parent 55985 0c5a02edfdef
child 56006 352a4f213fc6
equal deleted inserted replaced
56001:95c0323f0c1a 56002:60ab3b595a8e
       
     1 /*
       
     2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
     1 
    25 
       
    26  /*
       
    27   * This is the JavaCC grammar file for a JSON parser. The productions are
       
    28   * based on syntax diagrams as specified at json.org
       
    29   * Every type of JSON is represent by a JSONElement. JSON Objects are mapped to
       
    30   * by LinkedHashMap via JSONObject. JSON Arrays are mapped to ArrayList via JSONArray
       
    31   * String, Number, Boolean and null are wrapped into a JSONPrimitive.
       
    32   * Control characters inside a string (JSONPrimitive(String)) are not escaped and are passed through
       
    33   * to ouput JSON string generated by toJsonString method of JSONElement.
       
    34   * Inside a String, only backslash ('\') and double quote ('"') characters
       
    35   * are escaped by a backslash, if they are not already escaped.
       
    36   * Applications consuming json string generated by JSON* objects must take care
       
    37   * to escape control characters within strings by prefixing with a backslash character,
       
    38   * if not already escaped.
       
    39   */
     2 options {
    40 options {
     3     STATIC=false;
    41     STATIC=false;
     4     ERROR_REPORTING=true;
    42     ERROR_REPORTING=true;
     5     JAVA_UNICODE_ESCAPE=true;
    43     JAVA_UNICODE_ESCAPE=true;
     6     UNICODE_INPUT=true;
    44     UNICODE_INPUT=true;
       
    45     IGNORE_CASE=true;
       
    46     CACHE_TOKENS=true;
       
    47 //    DEBUG_PARSER=true;
       
    48 //    DEBUG_LOOKAHEAD=true;
       
    49 //    DEBUG_TOKEN_MANAGER=true;
     7 }
    50 }
     8 
    51 
     9 PARSER_BEGIN(JSONParser)
    52 PARSER_BEGIN(JSONParser)
    10 
    53 
    11 package com.oracle.jmx.remote.rest.json.parser;
    54 package com.oracle.jmx.remote.rest.json.parser;
    42 |   < #DIGITS : (<DIGIT>)+>
    85 |   < #DIGITS : (<DIGIT>)+>
    43 |   < #DIGIT: ["0"-"9"]>
    86 |   < #DIGIT: ["0"-"9"]>
    44 |   <QUOTED_STRING: "\"" ((~["\"","\\"]) | ("\\" ( ["n","t","b","r","f","\\","\""])))* "\"">
    87 |   <QUOTED_STRING: "\"" ((~["\"","\\"]) | ("\\" ( ["n","t","b","r","f","\\","\""])))* "\"">
    45 |   <BOOL_LITERAL : "true" | "false">   
    88 |   <BOOL_LITERAL : "true" | "false">   
    46 |   <NULL: "null">
    89 |   <NULL: "null">
    47 |   <IDENTIFIER: ["_","a"-"z", "A"-"Z"] (["a"-"z", "A"-"Z","0"-"9","_","-"])* >
       
    48 }
    90 }
    49 
    91 
    50 JSONElement jsonValue() : {
    92 JSONElement jsonValue() : {
    51 	JSONElement x;
    93 	JSONElement x;
    52 }{
    94 }{
    64 	final JSONObject jobject = new JSONObject();
   106 	final JSONObject jobject = new JSONObject();
    65 	JSONPrimitive key;
   107 	JSONPrimitive key;
    66 	JSONElement value;
   108 	JSONElement value;
    67 }{
   109 }{
    68     "{"
   110     "{"
    69         (key = string() | key = identifier())
   111         (key = string())
    70         ":"
   112         ":"
    71         value = jsonValue()
   113         value = jsonValue()
    72         { jobject.put((String)key.getValue(), value);}
   114         { jobject.put((String)key.getValue(), value);}
    73         (
   115         (
    74                 ","
   116                 ","
    75                 (key = string() | key = identifier())
   117                 (key = string())
    76                 ":"
   118                 ":"
    77                 value = jsonValue()
   119                 value = jsonValue()
    78                 { jobject.put((String)key.getValue(), value);}
   120                 { jobject.put((String)key.getValue(), value);}
    79         )*
   121         )*
    80     "}"
   122     "}"
   120     Token t;
   162     Token t;
   121 }{
   163 }{
   122     (t = <QUOTED_STRING>)
   164     (t = <QUOTED_STRING>)
   123     { return new JSONPrimitive(t.image.substring(1,t.image.length()-1)); }
   165     { return new JSONPrimitive(t.image.substring(1,t.image.length()-1)); }
   124 }
   166 }
   125 
       
   126 JSONPrimitive identifier() : {}
       
   127 {
       
   128     <IDENTIFIER>
       
   129     { return new JSONPrimitive(token.image); }
       
   130 }