summaryrefslogtreecommitdiff
path: root/source/compiler-core/slang-json-parser.h
diff options
context:
space:
mode:
authorEllie Hermaszewska <ellieh@nvidia.com>2024-10-29 14:49:26 +0800
committerGitHub <noreply@github.com>2024-10-29 14:49:26 +0800
commitf65d756bff8d4c5cbc15bd0322a2ae8e6b896a21 (patch)
treeea1d61342cd29368e19135000ec2948813096205 /source/compiler-core/slang-json-parser.h
parenta729c15e9dce9f5116a38afc66329ab2ca4cea54 (diff)
format
* format * Minor test fixes * enable checking cpp format in ci
Diffstat (limited to 'source/compiler-core/slang-json-parser.h')
-rw-r--r--source/compiler-core/slang-json-parser.h93
1 files changed, 58 insertions, 35 deletions
diff --git a/source/compiler-core/slang-json-parser.h b/source/compiler-core/slang-json-parser.h
index 2391ea0d2..d226e7fb9 100644
--- a/source/compiler-core/slang-json-parser.h
+++ b/source/compiler-core/slang-json-parser.h
@@ -5,54 +5,58 @@
#include "slang-json-lexer.h"
-namespace Slang {
+namespace Slang
+{
class JSONListener
{
public:
- /// Start an object
+ /// Start an object
virtual void startObject(SourceLoc loc) = 0;
- /// End an object
+ /// End an object
virtual void endObject(SourceLoc loc) = 0;
- /// Start an array
+ /// Start an array
virtual void startArray(SourceLoc loc) = 0;
- /// End and array
+ /// End and array
virtual void endArray(SourceLoc loc) = 0;
- /// Add the key. Must be followed by addXXXValue.
+ /// Add the key. Must be followed by addXXXValue.
virtual void addQuotedKey(const UnownedStringSlice& key, SourceLoc loc) = 0;
virtual void addUnquotedKey(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, SourceLoc loc) = 0;
+ /// Can be performed in an array or after an addLexemeKey in an object
+ virtual void addLexemeValue(
+ JSONTokenType type,
+ const UnownedStringSlice& value,
+ SourceLoc loc) = 0;
- /// An integer value
+ /// An integer value
virtual void addIntegerValue(int64_t value, SourceLoc loc) = 0;
- /// Add a floating point value
+ /// Add a floating point value
virtual void addFloatValue(double value, SourceLoc loc) = 0;
- /// Add a boolean value
+ /// Add a boolean value
virtual void addBoolValue(bool value, SourceLoc loc) = 0;
- /// Add a string value. NOTE! string is unescaped/quoted
+ /// Add a string value. NOTE! string is unescaped/quoted
virtual void addStringValue(const UnownedStringSlice& string, SourceLoc loc) = 0;
- /// Add a null value
+ /// Add a null value
virtual void addNullValue(SourceLoc loc) = 0;
};
class JSONWriter : public JSONListener
{
public:
- /*
+ /*
https://en.wikipedia.org/wiki/Indentation_style
*/
enum class IndentationStyle
{
- Allman, ///< After every value, and opening, closing all other types
- KNR, ///< K&R like. Fields have CR.
+ Allman, ///< After every value, and opening, closing all other types
+ KNR, ///< K&R like. Fields have CR.
};
- enum class LocationType : uint8_t
+ enum class LocationType : uint8_t
{
Object,
Array,
@@ -78,11 +82,22 @@ public:
CountOf,
};
- static LocationType getLocationType(Location loc) { return isObject(loc) ? LocationType::Object : (isComma(loc) ? LocationType::Comma : LocationType::Array); }
+ static LocationType getLocationType(Location loc)
+ {
+ return isObject(loc) ? LocationType::Object
+ : (isComma(loc) ? LocationType::Comma : LocationType::Array);
+ }
- static bool isObjectLike(Location loc) { return Index(loc) <= Index(Location::AfterCloseArray); }
+ static bool isObjectLike(Location loc)
+ {
+ return Index(loc) <= Index(Location::AfterCloseArray);
+ }
static bool isObject(Location loc) { return Index(loc) <= Index(Location::AfterCloseObject); }
- static bool isArray(Location loc) { return Index(loc) >= Index(Location::BeforeOpenArray) && Index(loc) <= Index(Location::AfterCloseArray); }
+ static bool isArray(Location loc)
+ {
+ return Index(loc) >= Index(Location::BeforeOpenArray) &&
+ Index(loc) <= Index(Location::AfterCloseArray);
+ }
static bool isComma(Location loc) { return Index(loc) >= Index(Location::FieldComma); }
static bool isOpen(Location loc) { return isObjectLike(loc) && (Index(loc) & 1) == 0; }
static bool isClose(Location loc) { return isObjectLike(loc) && (Index(loc) & 1) != 0; }
@@ -96,14 +111,15 @@ public:
virtual void endArray(SourceLoc loc) SLANG_OVERRIDE;
virtual void addQuotedKey(const UnownedStringSlice& key, SourceLoc loc) SLANG_OVERRIDE;
virtual void addUnquotedKey(const UnownedStringSlice& key, SourceLoc loc) SLANG_OVERRIDE;
- virtual void addLexemeValue(JSONTokenType type, const UnownedStringSlice& value, 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
+ /// Get the builder
StringBuilder& getBuilder() { return m_builder; }
JSONWriter(IndentationStyle format, Index lineLengthLimit = -1)
@@ -131,7 +147,7 @@ protected:
enum Enum : Flags
{
HasPrevious = 0x01,
- HasKey = 0x02,
+ HasKey = 0x02,
};
};
@@ -139,10 +155,10 @@ protected:
{
switch (m_kind)
{
- case Kind::Root: return (m_flags & Flag::HasPrevious) == 0;
- case Kind::Array: return true;
- case Kind::Object: return (m_flags & Flag::HasKey) != 0;
- default: return false;
+ case Kind::Root: return (m_flags & Flag::HasPrevious) == 0;
+ case Kind::Array: return true;
+ case Kind::Object: return (m_flags & Flag::HasKey) != 0;
+ default: return false;
}
}
@@ -156,7 +172,7 @@ protected:
Index _getLineLengthAfterIndent();
- /// Only emits the indent if at start of line
+ /// Only emits the indent if at start of line
void _maybeEmitIndent();
void _emitIndent();
@@ -167,20 +183,24 @@ protected:
void _postValue();
void _indent() { m_currentIndent++; }
- void _dedent() { --m_currentIndent; SLANG_ASSERT(m_currentIndent >= 0); }
+ void _dedent()
+ {
+ --m_currentIndent;
+ SLANG_ASSERT(m_currentIndent >= 0);
+ }
- /// True if the line is indented at the required level
+ /// True if the line is indented at the required level
bool _hasIndent() { return m_emittedIndent >= 0 && m_emittedIndent == m_currentIndent; }
-
+
Index m_currentIndent = 0;
char m_indentChar = ' ';
Index m_indentCharCount = 4;
Index m_lineIndex = 0;
Index m_lineStart = 0;
- Index m_emittedIndent = -1; /// If -1 for current line there is no indent emitted
+ Index m_emittedIndent = -1; /// If -1 for current line there is no indent emitted
- Index m_lineLengthLimit = -1; /// The limit is only applied *AFTER* indentation
+ Index m_lineLengthLimit = -1; /// The limit is only applied *AFTER* indentation
IndentationStyle m_format;
@@ -192,7 +212,11 @@ protected:
class JSONParser
{
public:
- SlangResult parse(JSONLexer* lexer, SourceView* sourceView, JSONListener* listener, DiagnosticSink* sink);
+ SlangResult parse(
+ JSONLexer* lexer,
+ SourceView* sourceView,
+ JSONListener* listener,
+ DiagnosticSink* sink);
protected:
SlangResult _parseValue();
@@ -206,7 +230,6 @@ protected:
};
-
} // namespace Slang
#endif