From b699a36444a03a6f7b312e805de31395a2d2ff9c Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Wed, 2 Jun 2021 12:58:08 -0400 Subject: JSONBuilder (#1865) * #include an absolute path didn't work - because paths were taken to always be relative. * WIP JSONWriter/JSONParser. * Checking different Layout styles for JSON. * Add slang-json-parser.h/.cpp * WIP JSONValue. * Added JSONValue::destroy/Recursive. * Improvement to JSONValue. * Improve text double conversion precision. Testing. * Simplify double parsing (just use atof). JSON comparison More testing of conversions and start of JSONValue. * Add for isnan, isinf etc. * Small improvement with object comparison. * Fix typo in getArgsByName. * Removed use of isnan and isinf as includes don't work on linux. * Improve JSON unit test. * Added asInteger/asFloat/asBool to JSONValue. * Add SourceLoc to JSONListener. * Added ability to walk the JSONValue * JSONBuilder. * Add converting from lexemes via JSONBuilder. * Fix VS warning. * Fix warning for res not being used. --- source/compiler-core/slang-json-value.h | 92 ++++++++++++++++++++++++++++++++- 1 file changed, 90 insertions(+), 2 deletions(-) (limited to 'source/compiler-core/slang-json-value.h') diff --git a/source/compiler-core/slang-json-value.h b/source/compiler-core/slang-json-value.h index 48cde5750..acff3ef6e 100644 --- a/source/compiler-core/slang-json-value.h +++ b/source/compiler-core/slang-json-value.h @@ -7,6 +7,8 @@ #include "slang-source-loc.h" #include "slang-diagnostic-sink.h" +#include "slang-json-parser.h" + namespace Slang { typedef uint32_t JSONKey; @@ -74,7 +76,7 @@ struct JSONValue /// As a float value double asFloat() const; - /// True if this is a object like (array or object) + /// True if this is a object like bool isObjectLike() const { return Index(type) >= Index(Type::Array); } /// True if this appears to be a valid value @@ -86,6 +88,12 @@ struct JSONValue /// Get the kind SLANG_FORCE_INLINE Kind getKind() const { return getKindForType(type); } + void reset() + { + type = Type::Invalid; + loc = SourceLoc(); + } + /// Given a type return the associated kind static Kind getKindForType(Type type) { return g_typeToKind[Index(type)]; } @@ -109,6 +117,13 @@ struct JSONKeyValue /// True if it's valid bool isValid() const { return value.type != JSONValue::Type::Invalid; } + void reset() + { + key = JSONKey(0); + keyLoc = SourceLoc(); + value.reset(); + } + JSONKey key; SourceLoc keyLoc; JSONValue value; @@ -183,7 +198,13 @@ public: /// Destroy recursively from value void destroyRecursively(JSONValue& value); - // + /// Traverse a JSON hierarchy from value, outputting to the listener + void traverseRecursively(const JSONValue& value, JSONListener* listener); + + /// Returns the source manager used. + SourceManager* getSourceManager() const { return m_sourceManager; } + + // Ctor JSONContainer(SourceManager* sourceManger); /// Returns true if all the keys are unique @@ -235,7 +256,74 @@ protected: List m_freeRangeIndices; List m_arrayValues; List m_objectValues; +}; + +class JSONBuilder : public JSONListener +{ +public: + + typedef uint32_t Flags; + struct Flag + { + enum Enum : Flags + { + ConvertLexemes = 0x01, + }; + }; + + + virtual void startObject(SourceLoc loc) SLANG_OVERRIDE; + virtual void endObject(SourceLoc loc) SLANG_OVERRIDE; + virtual void startArray(SourceLoc loc) SLANG_OVERRIDE; + virtual void endArray(SourceLoc loc) SLANG_OVERRIDE; + virtual void addKey(const UnownedStringSlice& key, SourceLoc loc) SLANG_OVERRIDE; + virtual void addLexemeValue(JSONTokenType type, const UnownedStringSlice& value, SourceLoc loc) SLANG_OVERRIDE; + virtual void addIntegerValue(int64_t value, SourceLoc loc) SLANG_OVERRIDE; + virtual void addFloatValue(double value, SourceLoc loc) SLANG_OVERRIDE; + virtual void addBoolValue(bool value, SourceLoc loc) SLANG_OVERRIDE; + virtual void addStringValue(const UnownedStringSlice& string, SourceLoc loc) SLANG_OVERRIDE; + virtual void addNullValue(SourceLoc loc) SLANG_OVERRIDE; + + /// Reset the state + void reset(); + + /// Get the root value. Will be set after valid construction + const JSONValue& getRootValue() const { return m_rootValue; } + + JSONBuilder(JSONContainer* container, Flags flags = 0); + +protected: + + struct State + { + enum class Kind : uint8_t + { + Root, + Object, + Array, + }; + Kind m_kind; + Index m_startIndex; + SourceLoc m_loc; + }; + + void _popState(); + void _add(const JSONValue& value); + + Index _findKeyIndex(JSONKey key) const; + + Flags m_flags; + + List m_keyValues; + List m_values; + List m_stateStack; + + State m_state; + + JSONContainer* m_container; + JSONKeyValue m_keyValue; + JSONValue m_rootValue; }; } // namespace Slang -- cgit v1.2.3