diff --git a/src/main/java/io/usethesource/vallang/ISourceLocation.java b/src/main/java/io/usethesource/vallang/ISourceLocation.java index 9e677b9b..ce24c38b 100644 --- a/src/main/java/io/usethesource/vallang/ISourceLocation.java +++ b/src/main/java/io/usethesource/vallang/ISourceLocation.java @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (c) 2007 IBM Corporation. +* Copyright (c) 2007 IBM Corporation, 2023 NWO-I Centrum Wiskunde & Informatica * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -7,12 +7,13 @@ * * Contributors: * Robert Fuhrer (rfuhrer@watson.ibm.com) - initial API and implementation - +* Jurgen Vinju (Jurgen.Vinju@cwi.nl) - extensions and maintenance *******************************************************************************/ package io.usethesource.vallang; import java.net.URI; +import java.net.URISyntaxException; import io.usethesource.vallang.visitors.IValueVisitor; @@ -134,6 +135,30 @@ public interface ISourceLocation extends IValue { * @return the source location without any offset & length information. */ public ISourceLocation top(); + + public String getFileName(); + + public boolean hasFileName(); + + public ISourceLocation changeScheme(String newScheme) throws URISyntaxException; + + public ISourceLocation changeAuthority(String newAuthority) throws URISyntaxException; + + public ISourceLocation changePath(String newPath) throws URISyntaxException; + + public ISourceLocation changeFile(String newFile) throws URISyntaxException; + + public ISourceLocation changeExtension(String newExtension) throws URISyntaxException; + + public ISourceLocation changeFragment(String newFragment) throws URISyntaxException; + + public ISourceLocation changeQuery(String newQuery) throws URISyntaxException; + + public ISourceLocation changeFileName(String newFileName) throws URISyntaxException; + + public ISourceLocation makeChildLocation(String childPath) throws URISyntaxException; + + public ISourceLocation getParentLocation(); @Override default T accept(IValueVisitor v) throws E { diff --git a/src/main/java/io/usethesource/vallang/IValueFactory.java b/src/main/java/io/usethesource/vallang/IValueFactory.java index f126af41..b020b805 100644 --- a/src/main/java/io/usethesource/vallang/IValueFactory.java +++ b/src/main/java/io/usethesource/vallang/IValueFactory.java @@ -443,7 +443,7 @@ public ISourceLocation sourceLocation(String scheme, String authority, /** * Get a set writer of which the element type will be the least upper bound - * of the element types + * of the element types. ISetWriter instances are stream of IValue collectors. * * @return a set writer */ @@ -461,7 +461,7 @@ public ISourceLocation sourceLocation(String scheme, String authority, /** * Get a list writer of which the element type will be the least upper bound - * of the element types + * of the element types. IListWriter instances are stream of IValue collectors. * * @return a list writer */ @@ -478,7 +478,8 @@ public ISourceLocation sourceLocation(String scheme, String authority, /** * Get a map writer of which the key and value types will be the least upper - * bound of the keys and values that are put in. + * bound of the keys and values that are put in. IMapWriter instances are + * stream collectors; the stream must be made of binary ITuple instances. * * @return a list writer */ diff --git a/src/main/java/io/usethesource/vallang/impl/primitive/SourceLocationURIValues.java b/src/main/java/io/usethesource/vallang/impl/primitive/SourceLocationURIValues.java index 2e2fa0e8..2bdf75eb 100644 --- a/src/main/java/io/usethesource/vallang/impl/primitive/SourceLocationURIValues.java +++ b/src/main/java/io/usethesource/vallang/impl/primitive/SourceLocationURIValues.java @@ -273,11 +273,70 @@ public boolean hasOffsetLength() { return false; } - @Override public ISourceLocation top() { return this; } + + @Override + public ISourceLocation changeAuthority(String authority) throws URISyntaxException { + return newURI(scheme, authority, null, null, null); + } + + @Override + public ISourceLocation changeScheme(String scheme) throws URISyntaxException { + return newURI(scheme, null, null, null, null); + } + + @Override + public ISourceLocation changeFragment(String fragment) throws URISyntaxException { + return newURI(scheme, null, null, null, fragment); + } + + @Override + public ISourceLocation changeQuery(String fragment) throws URISyntaxException { + return newURI(scheme, null, null, null, fragment); + } + + @Override + public ISourceLocation getParentLocation() { + return this; + } + + @Override + public boolean hasFileName() { + return false; + } + + @Override + public ISourceLocation changeExtension(String ext) { + throw new UnsupportedOperationException(); + } + + @Override + public String getFileName() { + throw new UnsupportedOperationException(); + } + + @Override + public ISourceLocation changeFileName(String file) throws URISyntaxException { + throw new UnsupportedOperationException(); + } + + @Override + public ISourceLocation changeFile(String file) throws URISyntaxException { + throw new UnsupportedOperationException(); + } + + @Override + public ISourceLocation changePath(String path) throws URISyntaxException { + throw new UnsupportedOperationException(); + } + + @Override + public ISourceLocation makeChildLocation(String path) throws URISyntaxException { + return newURI(scheme, null, path, null, null); + } } private static final Pattern squareBrackets = Pattern.compile("(\\[|\\])"); @@ -361,6 +420,7 @@ public String getAuthority() { public int hashCode() { return scheme.hashCode() + authority.hashCode(); } + @Override public boolean equals(@Nullable Object obj) { if (obj == null) { @@ -500,7 +560,7 @@ public QueryURI(String scheme, String query) { @Override @SuppressWarnings("nullness") // CF doesn't have a model for URI -public URI getURI() { + public URI getURI() { try { URI result = new URI(scheme, "", "/", query, null); return new URI(result.toASCIIString()); @@ -598,7 +658,7 @@ public QueryPathURI(String scheme, String path, String query) { @Override @SuppressWarnings("nullness") // CF doesn't have a model for URI -public URI getURI() { + public URI getURI() { try { URI result = new URI(scheme, "", path, query, null); return new URI(result.toASCIIString()); diff --git a/src/main/java/io/usethesource/vallang/impl/primitive/SourceLocationValues.java b/src/main/java/io/usethesource/vallang/impl/primitive/SourceLocationValues.java index fa318c86..3ccdfcf3 100644 --- a/src/main/java/io/usethesource/vallang/impl/primitive/SourceLocationValues.java +++ b/src/main/java/io/usethesource/vallang/impl/primitive/SourceLocationValues.java @@ -269,6 +269,7 @@ public int getLength() throws UnsupportedOperationException { public int getOffset() throws UnsupportedOperationException { throw new UnsupportedOperationException(); } + } private static class IntIntIntIntIntInt extends Complete { diff --git a/src/test/java/io/usethesource/vallang/specification/SetTests.java b/src/test/java/io/usethesource/vallang/specification/SetTests.java index f316be2e..5a872862 100644 --- a/src/test/java/io/usethesource/vallang/specification/SetTests.java +++ b/src/test/java/io/usethesource/vallang/specification/SetTests.java @@ -4,8 +4,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Random; -import java.util.stream.StreamSupport; - import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ArgumentsSource;