1 /*
   2  * Copyright (c) 2015, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_UTILITIES_JSON_HPP
  26 #define SHARE_VM_UTILITIES_JSON_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "utilities/globalDefinitions.hpp"
  30 
  31 class JSON : public ResourceObj {
  32  protected:
  33   JSON(const char* text, bool silent);
  34   void parse();
  35   bool valid();
  36 
  37   typedef enum {
  38     JSON_NONE,
  39     JSON_OBJECT_BEGIN,
  40     JSON_OBJECT_END,
  41     JSON_ARRAY_BEGIN,
  42     JSON_ARRAY_END,
  43     JSON_KEY,
  44     JSON_STRING,
  45     JSON_NUMBER_INT,
  46     JSON_NUMBER_FLOAT,
  47     JSON_TRUE,
  48     JSON_FALSE,
  49     JSON_NULL
  50   } JSON_TYPE;
  51 
  52   typedef union {
  53     int64_t int_value;
  54     double double_value;
  55 
  56     struct {
  57       const char* start;
  58       size_t length;
  59     } str;
  60   } JSON_VAL;
  61 
  62   typedef enum {
  63     INTERNAL_ERROR,
  64     SYNTAX_ERROR,
  65     KEY_ERROR,
  66     VALUE_ERROR
  67   } JSON_ERROR;
  68 
  69   void error(JSON_ERROR e, const char* format, ...) ATTRIBUTE_PRINTF(3, 4);
  70 
  71  private:
  72   const char* start;
  73   const char* pos;
  74 
  75   // For error printing
  76   const char* mark; // Error marker
  77   uint level;
  78   uint line;
  79   uint column;
  80 
  81   bool silent;
  82   bool _valid;
  83 
  84   bool parse_json_value();
  85   bool parse_json_object();
  86   bool parse_json_array();
  87   bool parse_json_string(bool key = false);
  88   bool parse_json_key();
  89   bool parse_json_number();
  90   bool parse_json_symbol(const char* name, JSON_TYPE symbol);
  91 
  92   virtual bool callback(JSON_TYPE t, JSON_VAL* v, uint level) = 0;
  93 
  94   void mark_pos();
  95   u_char next();
  96   u_char peek();
  97   u_char peek(size_t i);
  98   int expect_any(const char* valid_chars, const char* error_msg, JSON_ERROR e = SYNTAX_ERROR);
  99   bool expect_string(const char* expected_string, const char* error_msg = "", JSON_ERROR e = SYNTAX_ERROR);
 100   size_t skip(size_t i);
 101   int skip_to_token();
 102   u_char skip_to(u_char want);
 103   u_char skip_line_comment();
 104   int skip_block_comment();
 105 
 106   const char* strerror(JSON_ERROR e);
 107 };
 108 
 109 #ifndef PRODUCT
 110 class JSONTest : public JSON {
 111  public:
 112   static bool test();
 113 
 114  private:
 115   JSONTest(const char* text);
 116   static void test(const char* json, bool valid);
 117 
 118   void log(uint level, const char* format, ...) ATTRIBUTE_PRINTF(3, 4);
 119 
 120   bool callback(JSON_TYPE t, JSON_VAL* v, uint level);
 121   JSON_TYPE prev;
 122 };
 123 #endif
 124 
 125 #endif // SHARE_VM_UTILITIES_JSON_HPP