diff options
| author | Michael Williamson <mike@zwobble.org> | 2025-01-05 23:39:19 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2025-01-05 23:39:19 +0000 |
| commit | 1e6e1f7bb8776fc013f76828ed8df3f9297d095b (patch) | |
| tree | 69033190a65779f6dbd694e15d66763b53408c5d /src/main/java | |
| parent | f54116f39ccb6d1efec95d76c59e9dad1d46e426 (diff) | |
Use java-source-text for JSON5 parsing
Diffstat (limited to 'src/main/java')
8 files changed, 40 insertions, 83 deletions
diff --git a/src/main/java/org/shedlang/compiler/config/BinConfig.java b/src/main/java/org/shedlang/compiler/config/BinConfig.java index 5f2b0a7d..a81b2737 100644 --- a/src/main/java/org/shedlang/compiler/config/BinConfig.java +++ b/src/main/java/org/shedlang/compiler/config/BinConfig.java @@ -2,6 +2,7 @@ package org.shedlang.compiler.config; import org.shedlang.compiler.config.errors.ConfigError; import org.shedlang.compiler.config.json.Json5ObjectWithSource; +import org.shedlang.compiler.config.json.Json5Source; import org.shedlang.compiler.parser.SourceParser; import org.shedlang.compiler.sources.Sources; import org.shedlang.compiler.tokeniser.UnexpectedTokenException; @@ -25,7 +26,7 @@ public record BinConfig(String name, NamespaceName mainNamespace) { exception.getMessage(), exception.source().describe() ), - mainNamespaceString.source() + Json5Source.fromSourceRange(mainNamespaceString.sourceRange()) ); } } diff --git a/src/main/java/org/shedlang/compiler/config/ExampleConfig.java b/src/main/java/org/shedlang/compiler/config/ExampleConfig.java index 1135066f..065eaaac 100644 --- a/src/main/java/org/shedlang/compiler/config/ExampleConfig.java +++ b/src/main/java/org/shedlang/compiler/config/ExampleConfig.java @@ -1,7 +1,7 @@ package org.shedlang.compiler.config; import org.shedlang.compiler.config.json.Json5ObjectWithSource; -import org.shedlang.compiler.config.json.Json5StringWithSource; +import org.zwobble.json5.values.Json5String; import java.util.Optional; @@ -13,8 +13,8 @@ public record ExampleConfig( public static ExampleConfig fromJson5(Json5ObjectWithSource object) { return new ExampleConfig( object.getString("bin").value(), - object.getStringOrNone("expectedOutput").map(Json5StringWithSource::value), - object.getStringOrNone("expectedError").map(Json5StringWithSource::value) + object.getStringOrNone("expectedOutput").map(Json5String::value), + object.getStringOrNone("expectedError").map(Json5String::value) ); } } diff --git a/src/main/java/org/shedlang/compiler/config/ProjectConfig.java b/src/main/java/org/shedlang/compiler/config/ProjectConfig.java index 0408a143..7a4974e4 100644 --- a/src/main/java/org/shedlang/compiler/config/ProjectConfig.java +++ b/src/main/java/org/shedlang/compiler/config/ProjectConfig.java @@ -1,7 +1,7 @@ package org.shedlang.compiler.config; import org.shedlang.compiler.config.json.Json5ObjectWithSource; -import org.shedlang.compiler.sources.Sources; +import org.zwobble.sourcetext.SourceText; import java.io.IOException; import java.nio.file.Files; @@ -17,14 +17,14 @@ public record ProjectConfig( public static ProjectConfig read(Path projectPath) throws IOException { var configPath = projectPath.resolve("shed.json5"); var contents = Files.readString(configPath); - var source = Sources.file(configPath, contents); - return fromJson5(projectPath, Json5ObjectWithSource.parse(source)); + var sourceText = SourceText.fromString(configPath.toString(), contents); + return fromSourceText(projectPath, sourceText); } - public static ProjectConfig fromString(Path projectPath, String config) { + public static ProjectConfig fromSourceText(Path projectPath, SourceText sourceText) { return fromJson5( projectPath, - Json5ObjectWithSource.parse(Sources.string(config)) + Json5ObjectWithSource.parse(sourceText) ); } diff --git a/src/main/java/org/shedlang/compiler/config/errors/Json5SyntaxConfigError.java b/src/main/java/org/shedlang/compiler/config/errors/Json5SyntaxConfigError.java index d0db7f6c..2009af43 100644 --- a/src/main/java/org/shedlang/compiler/config/errors/Json5SyntaxConfigError.java +++ b/src/main/java/org/shedlang/compiler/config/errors/Json5SyntaxConfigError.java @@ -6,10 +6,9 @@ import org.zwobble.json5.parser.Json5ParseError; public class Json5SyntaxConfigError extends ConfigError { public static Json5SyntaxConfigError fromJson5ParseError( - Json5ParseError error, - Source fullSource + Json5ParseError error ) { - var source = Json5Source.fromSourceRange(fullSource, error.sourceRange()); + var source = Json5Source.fromSourceRange(error.sourceRange()); return new Json5SyntaxConfigError(error.getMessage(), source); } diff --git a/src/main/java/org/shedlang/compiler/config/json/Json5ObjectWithSource.java b/src/main/java/org/shedlang/compiler/config/json/Json5ObjectWithSource.java index 0684489d..cd7d811e 100644 --- a/src/main/java/org/shedlang/compiler/config/json/Json5ObjectWithSource.java +++ b/src/main/java/org/shedlang/compiler/config/json/Json5ObjectWithSource.java @@ -3,10 +3,10 @@ package org.shedlang.compiler.config.json; import org.shedlang.compiler.config.errors.ConfigError; import org.shedlang.compiler.config.errors.Json5SyntaxConfigError; import org.shedlang.compiler.config.errors.UnexpectedTypeConfigError; -import org.shedlang.compiler.sources.Source; import org.zwobble.json5.parser.Json5ParseError; import org.zwobble.json5.parser.Json5Parser; import org.zwobble.json5.values.*; +import org.zwobble.sourcetext.SourceText; import java.text.MessageFormat; import java.util.ArrayList; @@ -16,45 +16,34 @@ import java.util.OptionalLong; public class Json5ObjectWithSource { private final Json5Object object; - private final Source fullSource; - private Json5ObjectWithSource( - Json5Object object, - Source fullSource - ) { + private Json5ObjectWithSource(Json5Object object) { this.object = object; - this.fullSource = fullSource; } - public static Json5ObjectWithSource parse(Source source) { + public static Json5ObjectWithSource parse(SourceText sourceText) { try { - var document = Json5Parser.parseText(source.contents()); + var document = Json5Parser.parse(sourceText); if (document instanceof Json5Object object) { - return new Json5ObjectWithSource( - object, - source - ); + return new Json5ObjectWithSource(object); } else { // TODO: write a test for this. throw new UnexpectedTypeConfigError( document.path(), Json5Object.class, document.getClass(), - source + Json5Source.fromSourceRange(document.sourceRange()) ); } } catch (Json5ParseError error) { - throw Json5SyntaxConfigError.fromJson5ParseError(error, source); + throw Json5SyntaxConfigError.fromJson5ParseError(error); } } public Optional<Json5ObjectWithSource> getObject(String memberName) { return getValueOfType(Json5Object.class, memberName) - .map(object -> new Json5ObjectWithSource( - object, - fullSource - )); + .map(object -> new Json5ObjectWithSource(object)); } private Optional<Json5Array> getArray(String memberName) { @@ -68,33 +57,25 @@ public class Json5ObjectWithSource { for (var element : array.elements()) { var object = castJson5Value( Json5Object.class, - element, - fullSource + element ); - objects.add(new Json5ObjectWithSource( - object, - fullSource - )); + objects.add(new Json5ObjectWithSource(object)); index++; } return objects; }); } - public Optional<Json5StringWithSource> getStringOrNone(String memberName) { - return getValueOfType(Json5String.class, memberName) - .map(string -> new Json5StringWithSource( - string, - fullSource - )); + public Optional<Json5String> getStringOrNone(String memberName) { + return getValueOfType(Json5String.class, memberName); } - public Json5StringWithSource getString(String memberName) { + public Json5String getString(String memberName) { return getStringOrNone(memberName) .orElseThrow(() -> new ConfigError( // TODO: escape member name MessageFormat.format("{0} is missing {1}", object.path(), memberName), - Json5Source.fromSourceRange(fullSource, object.sourceRange()) + Json5Source.fromSourceRange(object.sourceRange()) )); } @@ -113,7 +94,7 @@ public class Json5ObjectWithSource { "%s must be a 64-bit integer", numberFinite.get().path() ), - Json5Source.fromSourceRange(fullSource, numberFinite.get().sourceRange()) + Json5Source.fromSourceRange(numberFinite.get().sourceRange()) ); } } @@ -123,7 +104,7 @@ public class Json5ObjectWithSource { .orElseThrow(() -> new ConfigError( // TODO: escape member name MessageFormat.format("{0} is missing {1}", object.path(), memberName), - Json5Source.fromSourceRange(fullSource, object.sourceRange()) + Json5Source.fromSourceRange(object.sourceRange()) )); } @@ -134,15 +115,13 @@ public class Json5ObjectWithSource { return object.getValue(memberName) .map(value -> castJson5Value( valueType, - value, - fullSource + value )); } private static <TValue extends Json5Value> TValue castJson5Value( Class<TValue> valueType, - Json5Value value, - Source fullSource + Json5Value value ) { try { return valueType.cast(value); @@ -151,7 +130,7 @@ public class Json5ObjectWithSource { value.path(), valueType, value.getClass(), - Json5Source.fromSourceRange(fullSource, value.sourceRange()) + Json5Source.fromSourceRange(value.sourceRange()) ); } } diff --git a/src/main/java/org/shedlang/compiler/config/json/Json5Source.java b/src/main/java/org/shedlang/compiler/config/json/Json5Source.java index 409dc265..e2a556e9 100644 --- a/src/main/java/org/shedlang/compiler/config/json/Json5Source.java +++ b/src/main/java/org/shedlang/compiler/config/json/Json5Source.java @@ -1,13 +1,16 @@ package org.shedlang.compiler.config.json; +import org.shedlang.compiler.sources.FileFragmentSource; import org.shedlang.compiler.sources.Source; -import org.zwobble.json5.sources.Json5SourceRange; +import org.zwobble.sourcetext.SourceRange; public class Json5Source { private Json5Source() {} - public static Source fromSourceRange(Source fullSource, Json5SourceRange sourceRange) { - return fullSource.at( + public static Source fromSourceRange(SourceRange sourceRange) { + return new FileFragmentSource( + sourceRange.sourceText().name(), + sourceRange.sourceText().charBuffer(0, sourceRange.sourceText().characterLength()).toString(), sourceRange.start().characterIndex(), sourceRange.end().characterIndex() ); diff --git a/src/main/java/org/shedlang/compiler/config/json/Json5StringWithSource.java b/src/main/java/org/shedlang/compiler/config/json/Json5StringWithSource.java deleted file mode 100644 index ea578b1f..00000000 --- a/src/main/java/org/shedlang/compiler/config/json/Json5StringWithSource.java +++ /dev/null @@ -1,25 +0,0 @@ -package org.shedlang.compiler.config.json; - -import org.shedlang.compiler.sources.Source; -import org.zwobble.json5.values.Json5String; - -public class Json5StringWithSource { - private final Json5String string; - private final Source fullSource; - - public Json5StringWithSource( - Json5String string, - Source fullSource - ) { - this.string = string; - this.fullSource = fullSource; - } - - public String value() { - return string.value(); - } - - public Source source() { - return Json5Source.fromSourceRange(fullSource, string.sourceRange()); - } -} diff --git a/src/main/java/org/shedlang/compiler/sources/Sources.java b/src/main/java/org/shedlang/compiler/sources/Sources.java index e59c91cc..ccfadca6 100644 --- a/src/main/java/org/shedlang/compiler/sources/Sources.java +++ b/src/main/java/org/shedlang/compiler/sources/Sources.java @@ -6,15 +6,15 @@ public class Sources { public static Source NULL = NullSource.INSTANCE; public static Source RUNTIME = RuntimeSource.INSTANCE; - public static Source file(Path path, String contents) { + public static FileFragmentSource file(Path path, String contents) { return FileFragmentSource.file(path.toString(), contents); } - public static Source file(String filename, String contents) { + public static FileFragmentSource file(String filename, String contents) { return FileFragmentSource.file(filename, contents); } - public static Source string(String contents) { + public static FileFragmentSource string(String contents) { return FileFragmentSource.file("<string>", contents); } } |
