summaryrefslogtreecommitdiff
path: root/source/compiler-core/slang-json-lexer.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/compiler-core/slang-json-lexer.cpp')
-rw-r--r--source/compiler-core/slang-json-lexer.cpp28
1 files changed, 28 insertions, 0 deletions
diff --git a/source/compiler-core/slang-json-lexer.cpp b/source/compiler-core/slang-json-lexer.cpp
index 9f96ef4f5..428afca63 100644
--- a/source/compiler-core/slang-json-lexer.cpp
+++ b/source/compiler-core/slang-json-lexer.cpp
@@ -10,6 +10,34 @@ https://www.json.org/json-en.html
namespace Slang {
+/* static */UnownedStringSlice JSONLexer::calcLexemeLocation(const UnownedStringSlice& text)
+{
+ SourceManager sourceManager;
+ sourceManager.initialize(nullptr, nullptr);
+ DiagnosticSink sink;
+ sink.init(&sourceManager, nullptr);
+
+ String contents(text);
+ SourceFile* sourceFile = sourceManager.createSourceFileWithString(PathInfo::makeUnknown(), contents);
+ SourceView* sourceView = sourceManager.createSourceView(sourceFile, nullptr, SourceLoc());
+
+ JSONLexer lexer;
+
+ lexer.init(sourceView, &sink);
+
+ if (lexer.peekType() != JSONTokenType::Invalid)
+ {
+ // Get the start offset
+ auto offset = sourceView->getRange().getOffset(lexer.peekLoc());
+
+ return text.subString(offset, lexer.peekLexeme().getLength());
+ }
+ else
+ {
+ return text.head(0);
+ }
+}
+
SlangResult JSONLexer::init(SourceView* sourceView, DiagnosticSink* sink)
{
m_sourceView = sourceView;