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-parser.h | 48 +++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 13 deletions(-) (limited to 'source/compiler-core/slang-json-parser.h') diff --git a/source/compiler-core/slang-json-parser.h b/source/compiler-core/slang-json-parser.h index 2e907abb2..96531aee9 100644 --- a/source/compiler-core/slang-json-parser.h +++ b/source/compiler-core/slang-json-parser.h @@ -11,18 +11,32 @@ class JSONListener { public: /// Start an object - virtual void startObject() = 0; + virtual void startObject(SourceLoc loc) = 0; /// End an object - virtual void endObject() = 0; + virtual void endObject(SourceLoc loc) = 0; /// Start an array - virtual void startArray() = 0; + virtual void startArray(SourceLoc loc) = 0; /// End and array - virtual void endArray() = 0; + virtual void endArray(SourceLoc loc) = 0; - /// Add the key lexeme. Must be followed by addLexemeValue. - virtual void addLexemeKey(const UnownedStringSlice& key) = 0; + + /// Add the key. Must be followed by addXXXValue. + virtual void addKey(const UnownedStringSlice& key, SourceLoc loc) = 0; /// Can be performed in an array or after an addLexemeKey in an object - virtual void addLexemeValue(JSONTokenType type, const UnownedStringSlice& value) = 0; + virtual void addLexemeValue(JSONTokenType type, const UnownedStringSlice& value, SourceLoc loc) = 0; + + /// An integer value + virtual void addIntegerValue(int64_t value, SourceLoc loc) = 0; + /// Add a floating point value + virtual void addFloatValue(double value, SourceLoc loc) = 0; + /// Add a boolean value + virtual void addBoolValue(bool value, SourceLoc loc) = 0; + + /// Add a string value. NOTE! string is unescaped/quoted + virtual void addStringValue(const UnownedStringSlice& string, SourceLoc loc) = 0; + + /// Add a null value + virtual void addNullValue(SourceLoc loc) = 0; }; class JSONWriter : public JSONListener @@ -75,12 +89,17 @@ public: static bool isAfter(Location loc) { return isObjectLike(loc) && (Index(loc) & 2) != 0; } // Implement JSONListener - virtual void startObject() SLANG_OVERRIDE; - virtual void endObject() SLANG_OVERRIDE; - virtual void startArray() SLANG_OVERRIDE; - virtual void endArray() SLANG_OVERRIDE; - virtual void addLexemeKey(const UnownedStringSlice& key) SLANG_OVERRIDE; - virtual void addLexemeValue(JSONTokenType type, const UnownedStringSlice& value) SLANG_OVERRIDE; + 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; /// Get the builder StringBuilder& getBuilder() { return m_builder; } @@ -142,6 +161,9 @@ protected: void _maybeEmitComma(); void _maybeEmitFieldComma(); + void _preValue(SourceLoc loc); + void _postValue(); + void _indent() { m_currentIndent++; } void _dedent() { --m_currentIndent; SLANG_ASSERT(m_currentIndent >= 0); } -- cgit v1.2.3