diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2021-05-27 15:38:52 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-05-27 12:38:52 -0700 |
| commit | 15f5cffc3026a5faed7046ae7d75ec6b56cf4a4c (patch) | |
| tree | e6c59288e453ac3eb788068a155e7aa607399009 /tools | |
| parent | 969943f4b751d3cad8ac548f9cf0f65406935bad (diff) | |
JSON Parser and Writer (#1859)
* #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
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/slang-test/unit-test-json.cpp | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/tools/slang-test/unit-test-json.cpp b/tools/slang-test/unit-test-json.cpp index fff16b136..5a59cdcc3 100644 --- a/tools/slang-test/unit-test-json.cpp +++ b/tools/slang-test/unit-test-json.cpp @@ -1,6 +1,7 @@ #include "../../source/compiler-core/slang-json-lexer.h" #include "../../source/core/slang-string-escape-util.h" +#include "../../source/compiler-core/slang-json-parser.h" #include "test-context.h" @@ -48,6 +49,22 @@ static SlangResult _lex(const char* in, DiagnosticSink* sink, List<JSONToken>& t return SLANG_OK; } +static SlangResult _parse(const char* in, DiagnosticSink* sink, JSONWriter& writer) +{ + SourceManager* sourceManager = sink->getSourceManager(); + + String contents(in); + SourceFile* sourceFile = sourceManager->createSourceFileWithString(PathInfo::makeUnknown(), contents); + SourceView* sourceView = sourceManager->createSourceView(sourceFile, nullptr, SourceLoc()); + + JSONLexer lexer; + lexer.init(sourceView, sink); + + JSONParser parser; + SLANG_RETURN_ON_FAIL(parser.parse(&lexer, sourceView, &writer, sink)); + return SLANG_OK; +} + static bool _areEqual(SourceManager* sourceManager, const List<JSONToken>& toks, const Element* eles, Index elesCount) { if (toks.getCount() != elesCount) @@ -175,6 +192,35 @@ static void jsonUnitTest() SLANG_CHECK(escaped == buf); } } + + { + const char in[] = "{ \"Hello\" : \"Json\", \"!\" : 10, \"array\" : [1, 2, 3.0] }"; + + { + auto style = JSONWriter::IndentationStyle::Allman; + + JSONWriter writer(style); + _parse(in, &sink, writer); + + JSONWriter writerCheck(style); + _parse(writer.getBuilder().getBuffer(), &sink, writerCheck); + + SLANG_CHECK(writerCheck.getBuilder() == writer.getBuilder()); + } + + { + auto style = JSONWriter::IndentationStyle::KNR; + + JSONWriter writer(style, 80); + _parse(in, &sink, writer); + + JSONWriter writerCheck(style); + _parse(writer.getBuilder().getBuffer(), &sink, writerCheck); + + SLANG_CHECK(writerCheck.getBuilder() == writer.getBuilder()); + } + + } } SLANG_UNIT_TEST("JSON", jsonUnitTest); |
