From f54116f39ccb6d1efec95d76c59e9dad1d46e426 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 1 Jan 2025 22:37:02 +0000 Subject: Use JSON5 paths from parser --- .../config/errors/UnexpectedTypeConfigError.java | 2 +- .../config/json/Json5ObjectWithSource.java | 22 +++---------- .../shedlang/compiler/config/json/Json5Path.java | 36 ---------------------- .../config/json/Json5StringWithSource.java | 3 -- 4 files changed, 6 insertions(+), 57 deletions(-) delete mode 100644 src/main/java/org/shedlang/compiler/config/json/Json5Path.java (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/config/errors/UnexpectedTypeConfigError.java b/src/main/java/org/shedlang/compiler/config/errors/UnexpectedTypeConfigError.java index 2281f0f8..b13658b3 100644 --- a/src/main/java/org/shedlang/compiler/config/errors/UnexpectedTypeConfigError.java +++ b/src/main/java/org/shedlang/compiler/config/errors/UnexpectedTypeConfigError.java @@ -1,7 +1,7 @@ package org.shedlang.compiler.config.errors; -import org.shedlang.compiler.config.json.Json5Path; import org.shedlang.compiler.sources.Source; +import org.zwobble.json5.paths.Json5Path; import org.zwobble.json5.values.*; import java.text.MessageFormat; 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 f073f267..0684489d 100644 --- a/src/main/java/org/shedlang/compiler/config/json/Json5ObjectWithSource.java +++ b/src/main/java/org/shedlang/compiler/config/json/Json5ObjectWithSource.java @@ -16,16 +16,13 @@ import java.util.OptionalLong; public class Json5ObjectWithSource { private final Json5Object object; - private final Json5Path path; private final Source fullSource; private Json5ObjectWithSource( Json5Object object, - Json5Path path, Source fullSource ) { this.object = object; - this.path = path; this.fullSource = fullSource; } @@ -36,13 +33,12 @@ public class Json5ObjectWithSource { if (document instanceof Json5Object object) { return new Json5ObjectWithSource( object, - Json5Path.TOP, source ); } else { // TODO: write a test for this. throw new UnexpectedTypeConfigError( - Json5Path.TOP, + document.path(), Json5Object.class, document.getClass(), source @@ -57,7 +53,6 @@ public class Json5ObjectWithSource { return getValueOfType(Json5Object.class, memberName) .map(object -> new Json5ObjectWithSource( object, - path.plusMemberName(memberName), fullSource )); } @@ -70,18 +65,14 @@ public class Json5ObjectWithSource { return getArray(memberName).map(array -> { var objects = new ArrayList(); var index = 0; - var arrayPath = path.plusMemberName(memberName); for (var element : array.elements()) { - var objectPath = arrayPath.plusArrayIndex(index); var object = castJson5Value( Json5Object.class, - objectPath, element, fullSource ); objects.add(new Json5ObjectWithSource( object, - objectPath, fullSource )); index++; @@ -94,7 +85,6 @@ public class Json5ObjectWithSource { return getValueOfType(Json5String.class, memberName) .map(string -> new Json5StringWithSource( string, - path.plusMemberName(memberName), fullSource )); } @@ -103,7 +93,7 @@ public class Json5ObjectWithSource { return getStringOrNone(memberName) .orElseThrow(() -> new ConfigError( // TODO: escape member name - MessageFormat.format("{0} is missing {1}", path, memberName), + MessageFormat.format("{0} is missing {1}", object.path(), memberName), Json5Source.fromSourceRange(fullSource, object.sourceRange()) )); } @@ -121,7 +111,7 @@ public class Json5ObjectWithSource { throw new ConfigError( String.format( "%s must be a 64-bit integer", - path.plusMemberName(memberName) + numberFinite.get().path() ), Json5Source.fromSourceRange(fullSource, numberFinite.get().sourceRange()) ); @@ -132,7 +122,7 @@ public class Json5ObjectWithSource { return getLongOrNone(memberName) .orElseThrow(() -> new ConfigError( // TODO: escape member name - MessageFormat.format("{0} is missing {1}", path, memberName), + MessageFormat.format("{0} is missing {1}", object.path(), memberName), Json5Source.fromSourceRange(fullSource, object.sourceRange()) )); } @@ -144,7 +134,6 @@ public class Json5ObjectWithSource { return object.getValue(memberName) .map(value -> castJson5Value( valueType, - path.plusMemberName(memberName), value, fullSource )); @@ -152,7 +141,6 @@ public class Json5ObjectWithSource { private static TValue castJson5Value( Class valueType, - Json5Path path, Json5Value value, Source fullSource ) { @@ -160,7 +148,7 @@ public class Json5ObjectWithSource { return valueType.cast(value); } catch (ClassCastException exception) { throw new UnexpectedTypeConfigError( - path, + value.path(), valueType, value.getClass(), Json5Source.fromSourceRange(fullSource, value.sourceRange()) diff --git a/src/main/java/org/shedlang/compiler/config/json/Json5Path.java b/src/main/java/org/shedlang/compiler/config/json/Json5Path.java deleted file mode 100644 index f63e7ae4..00000000 --- a/src/main/java/org/shedlang/compiler/config/json/Json5Path.java +++ /dev/null @@ -1,36 +0,0 @@ -package org.shedlang.compiler.config.json; - -import org.pcollections.PVector; -import org.shedlang.compiler.util.CheckReturnValue; -import org.shedlang.compiler.util.P; - -import java.text.MessageFormat; - -public class Json5Path { - public static final Json5Path TOP = new Json5Path(P.vector()); - - private final PVector components; - - private Json5Path(PVector components) { - this.components = components; - } - - @CheckReturnValue - public Json5Path plusMemberName(String memberName) { - // TODO: handle string escaping - var component = components.isEmpty() ? memberName : "." + memberName; - return new Json5Path(this.components.plus(component)); - } - - @CheckReturnValue - public Json5Path plusArrayIndex(int index) { - var component = MessageFormat.format("[{0}]", index); - return new Json5Path(this.components.plus(component)); - } - - @Override - public String toString() { - // TODO: handle empty path - return String.join("", this.components); - } -} diff --git a/src/main/java/org/shedlang/compiler/config/json/Json5StringWithSource.java b/src/main/java/org/shedlang/compiler/config/json/Json5StringWithSource.java index bc9cf4b9..ea578b1f 100644 --- a/src/main/java/org/shedlang/compiler/config/json/Json5StringWithSource.java +++ b/src/main/java/org/shedlang/compiler/config/json/Json5StringWithSource.java @@ -5,16 +5,13 @@ import org.zwobble.json5.values.Json5String; public class Json5StringWithSource { private final Json5String string; - private final Json5Path path; private final Source fullSource; public Json5StringWithSource( Json5String string, - Json5Path path, Source fullSource ) { this.string = string; - this.path = path; this.fullSource = fullSource; } -- cgit v1.2.3