From 5d06c54a3ae4ef2c7b758de04571e2bbbe1898fd Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Wed, 17 May 2017 16:16:07 -0400 Subject: [PATCH 03/22] FIXME: split out json.h from json.c --- gcc/json.c | 172 ++----------------------------------------------------------- gcc/json.h | 139 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+), 169 deletions(-) create mode 100644 gcc/json.h diff --git a/gcc/json.c b/gcc/json.c index a920972..05448de 100644 --- a/gcc/json.c +++ b/gcc/json.c @@ -20,178 +20,13 @@ along with GCC; see the file COPYING3. If not see #include "config.h" #include "system.h" #include "coretypes.h" -#include "obstack.h" +#include "json.h" #include "selftest.h" -namespace json -{ - -class value; - class string; - class number; - class object; - class array; - class literal; - -/* FIXME: do we want a context to own everything, or do we want - explicit deallocation? - TODO: how do we run the dtors for the vec/hash_map within object/array ? */ -#if 0 -class context -{ - public: - context (); - ~context (); - - string *new_string (const char *buf); - number *new_number (double value); - object *new_object (); - array *new_array (); - - private: - struct obstack m_obstack; -}; -#endif - -enum kind -{ - JSON_OBJECT, - JSON_ARRAY, - JSON_NUMBER, - JSON_STRING, - JSON_TRUE, - JSON_FALSE, - JSON_NULL -}; - -/* Base class of JSON value. */ - -class value -{ - public: - virtual ~value () {} - virtual enum kind get_kind () const = 0; -}; - -/* Subclass of value for objects: key/value pairs. */ - -class object : public value -{ - public: - ~object (); - - enum kind get_kind () const FINAL OVERRIDE { return JSON_OBJECT; } - - value *get (const char *key) const; - void set (char *key, value *v); - - private: - typedef hash_map > map_t; - map_t m_map; -}; - -/* Subclass of value for arrays. */ - -class array : public value -{ - public: - ~array (); - - enum kind get_kind () const FINAL OVERRIDE { return JSON_ARRAY; } - - unsigned get_length () const { return m_elements.length (); } - value *get (int idx) const { return m_elements[idx]; } - void append (value *v) { m_elements.safe_push (v); } - - private: - auto_vec m_elements; -}; - -/* Subclass of value for numbers. */ - -class number : public value -{ - public: - number (double value) : m_value (value) {} - - enum kind get_kind () const FINAL OVERRIDE { return JSON_NUMBER; } - - double get () const { return m_value; } - - private: - double m_value; -}; - -/* Subclass of value for strings. */ - -class string : public value -{ - public: - string (const char *utf8) : m_utf8 (xstrdup (utf8)) {} - ~string () { free (m_utf8); } - - enum kind get_kind () const FINAL OVERRIDE { return JSON_STRING; } - - const char *get_string () const { return m_utf8; } - - private: - char *m_utf8; -}; - -/* Subclass of value for the literals "true", "false", and "null". */ - -class literal : public value -{ - public: - literal (enum kind kind) : m_kind (kind) {} - - enum kind get_kind () const FINAL OVERRIDE { return m_kind; } - - private: - enum kind m_kind; -}; +using namespace json; /* Implemenation. */ -#if 0 -/* class context. */ - -context::context () -{ - gcc_obstack_init (&m_obstack); -} - -context::~context () -{ - obstack_free (&m_obstack, NULL); -} - -string * -context::new_string (const char *buf) -{ - return new (XOBNEW (&m_obstack, json::string)) json::string (buf); -} - -number * -context::new_number (double value) -{ - return new (XOBNEW (&m_obstack, json::number)) json::number (value); -} - -object * -context::new_object () -{ - return new (XOBNEW (&m_obstack, json::object)) json::object (); -} - -array * -context::new_array () -{ - return new (XOBNEW (&m_obstack, json::array)) json::array (); -} -#endif - object::~object () { for (map_t::iterator it = m_map.begin (); it != m_map.end (); ++it) @@ -226,10 +61,9 @@ array::~array () delete v; } - /* Parsing decls. */ -extern value *parse_utf8_string (const char *utf8); +namespace json { typedef unsigned unichar; diff --git a/gcc/json.h b/gcc/json.h new file mode 100644 index 0000000..4f77d8f --- /dev/null +++ b/gcc/json.h @@ -0,0 +1,139 @@ +/* JSON parsing + Copyright (C) 2017 Free Software Foundation, Inc. + +This file is part of GCC. + +GCC is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 3, or (at your option) any later +version. + +GCC is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +. */ + +#ifndef GCC_JSON_H +#define GCC_JSON_H + +namespace json +{ + +class value; + class string; + class number; + class object; + class array; + class literal; + +enum kind +{ + JSON_OBJECT, + JSON_ARRAY, + JSON_NUMBER, + JSON_STRING, + JSON_TRUE, + JSON_FALSE, + JSON_NULL +}; + +/* Base class of JSON value. */ + +class value +{ + public: + virtual ~value () {} + virtual enum kind get_kind () const = 0; +}; + +/* Subclass of value for objects: key/value pairs. */ + +class object : public value +{ + public: + ~object (); + + enum kind get_kind () const FINAL OVERRIDE { return JSON_OBJECT; } + + value *get (const char *key) const; + void set (char *key, value *v); + + private: + typedef hash_map > map_t; + map_t m_map; +}; + +/* Subclass of value for arrays. */ + +class array : public value +{ + public: + ~array (); + + enum kind get_kind () const FINAL OVERRIDE { return JSON_ARRAY; } + + unsigned get_length () const { return m_elements.length (); } + value *get (int idx) const { return m_elements[idx]; } + void append (value *v) { m_elements.safe_push (v); } + + private: + auto_vec m_elements; +}; + +/* Subclass of value for numbers. */ + +class number : public value +{ + public: + number (double value) : m_value (value) {} + + enum kind get_kind () const FINAL OVERRIDE { return JSON_NUMBER; } + + double get () const { return m_value; } + + private: + double m_value; +}; + +/* Subclass of value for strings. */ + +class string : public value +{ + public: + string (const char *utf8) : m_utf8 (xstrdup (utf8)) {} + ~string () { free (m_utf8); } + + enum kind get_kind () const FINAL OVERRIDE { return JSON_STRING; } + + const char *get_string () const { return m_utf8; } + + private: + char *m_utf8; +}; + +/* Subclass of value for the literals "true", "false", and "null". */ + +class literal : public value +{ + public: + literal (enum kind kind) : m_kind (kind) {} + + enum kind get_kind () const FINAL OVERRIDE { return m_kind; } + + private: + enum kind m_kind; +}; + +/* Parsing decls. */ + +extern value *parse_utf8_string (const char *utf8); + +} // namespace json + +#endif /* GCC_JSON_H */ + -- 1.8.5.3