diff options
author | 2024-02-04 22:06:56 +0100 | |
---|---|---|
committer | 2024-02-04 22:06:56 +0100 | |
commit | 4dd2b4e1f45bf940d969181ae9b9176fa353c92e (patch) | |
tree | 72e315d726014966d8c701686962bfb5fbdea5a7 /subprojects/generator/src | |
parent | feat(language): import resolution (diff) | |
download | refinery-4dd2b4e1f45bf940d969181ae9b9176fa353c92e.tar.gz refinery-4dd2b4e1f45bf940d969181ae9b9176fa353c92e.tar.zst refinery-4dd2b4e1f45bf940d969181ae9b9176fa353c92e.zip |
feat: filesystem-level import resolution
Modules without an explicitly declared name get a name automatically inferred
from their path.
Diffstat (limited to 'subprojects/generator/src')
-rw-r--r-- | subprojects/generator/src/main/java/tools/refinery/generator/ProblemLoader.java | 30 |
1 files changed, 26 insertions, 4 deletions
diff --git a/subprojects/generator/src/main/java/tools/refinery/generator/ProblemLoader.java b/subprojects/generator/src/main/java/tools/refinery/generator/ProblemLoader.java index 20ea8132..e44dddc0 100644 --- a/subprojects/generator/src/main/java/tools/refinery/generator/ProblemLoader.java +++ b/subprojects/generator/src/main/java/tools/refinery/generator/ProblemLoader.java | |||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | 2 | * SPDX-FileCopyrightText: 2023-2024 The Refinery Authors <https://refinery.tools/> |
3 | * | 3 | * |
4 | * SPDX-License-Identifier: EPL-2.0 | 4 | * SPDX-License-Identifier: EPL-2.0 |
5 | */ | 5 | */ |
@@ -19,6 +19,7 @@ import org.eclipse.xtext.validation.IResourceValidator; | |||
19 | import tools.refinery.language.model.problem.Problem; | 19 | import tools.refinery.language.model.problem.Problem; |
20 | import tools.refinery.language.model.problem.Relation; | 20 | import tools.refinery.language.model.problem.Relation; |
21 | import tools.refinery.language.model.problem.ScopeDeclaration; | 21 | import tools.refinery.language.model.problem.ScopeDeclaration; |
22 | import tools.refinery.language.scoping.imports.ImportAdapter; | ||
22 | import tools.refinery.store.util.CancellationToken; | 23 | import tools.refinery.store.util.CancellationToken; |
23 | 24 | ||
24 | import java.io.ByteArrayOutputStream; | 25 | import java.io.ByteArrayOutputStream; |
@@ -26,11 +27,14 @@ import java.io.File; | |||
26 | import java.io.IOException; | 27 | import java.io.IOException; |
27 | import java.io.InputStream; | 28 | import java.io.InputStream; |
28 | import java.nio.charset.StandardCharsets; | 29 | import java.nio.charset.StandardCharsets; |
30 | import java.nio.file.Path; | ||
29 | import java.util.ArrayList; | 31 | import java.util.ArrayList; |
30 | import java.util.HashSet; | 32 | import java.util.HashSet; |
31 | import java.util.List; | 33 | import java.util.List; |
32 | import java.util.Map; | 34 | import java.util.Map; |
33 | 35 | ||
36 | // This class is used as a fluent builder. | ||
37 | @SuppressWarnings("UnusedReturnValue") | ||
34 | public class ProblemLoader { | 38 | public class ProblemLoader { |
35 | private String fileExtension; | 39 | private String fileExtension; |
36 | 40 | ||
@@ -45,6 +49,8 @@ public class ProblemLoader { | |||
45 | 49 | ||
46 | private CancellationToken cancellationToken = CancellationToken.NONE; | 50 | private CancellationToken cancellationToken = CancellationToken.NONE; |
47 | 51 | ||
52 | private final List<Path> extraPaths = new ArrayList<>(); | ||
53 | |||
48 | @Inject | 54 | @Inject |
49 | public void setFileExtensionProvider(FileExtensionProvider fileExtensionProvider) { | 55 | public void setFileExtensionProvider(FileExtensionProvider fileExtensionProvider) { |
50 | this.fileExtension = fileExtensionProvider.getPrimaryFileExtension(); | 56 | this.fileExtension = fileExtensionProvider.getPrimaryFileExtension(); |
@@ -55,6 +61,15 @@ public class ProblemLoader { | |||
55 | return this; | 61 | return this; |
56 | } | 62 | } |
57 | 63 | ||
64 | public ProblemLoader extraPath(String path) { | ||
65 | return extraPath(Path.of(path)); | ||
66 | } | ||
67 | |||
68 | public ProblemLoader extraPath(Path path) { | ||
69 | extraPaths.add(path.toAbsolutePath().normalize()); | ||
70 | return this; | ||
71 | } | ||
72 | |||
58 | public Problem loadString(String problemString, URI uri) throws IOException { | 73 | public Problem loadString(String problemString, URI uri) throws IOException { |
59 | try (var stream = new LazyStringInputStream(problemString)) { | 74 | try (var stream = new LazyStringInputStream(problemString)) { |
60 | return loadStream(stream, uri); | 75 | return loadStream(stream, uri); |
@@ -66,8 +81,8 @@ public class ProblemLoader { | |||
66 | } | 81 | } |
67 | 82 | ||
68 | public Problem loadStream(InputStream inputStream, URI uri) throws IOException { | 83 | public Problem loadStream(InputStream inputStream, URI uri) throws IOException { |
69 | var resourceSet = resourceSetProvider.get(); | 84 | var resourceSet = createResourceSet(); |
70 | var resourceUri = uri == null ? URI.createFileURI("__synthetic." + fileExtension) : uri; | 85 | var resourceUri = uri == null ? URI.createURI("__synthetic." + fileExtension) : uri; |
71 | var resource = resourceFactory.createResource(resourceUri); | 86 | var resource = resourceFactory.createResource(resourceUri); |
72 | resourceSet.getResources().add(resource); | 87 | resourceSet.getResources().add(resource); |
73 | resource.load(inputStream, Map.of()); | 88 | resource.load(inputStream, Map.of()); |
@@ -87,13 +102,20 @@ public class ProblemLoader { | |||
87 | } | 102 | } |
88 | 103 | ||
89 | public Problem loadUri(URI uri) throws IOException { | 104 | public Problem loadUri(URI uri) throws IOException { |
90 | var resourceSet = resourceSetProvider.get(); | 105 | var resourceSet = createResourceSet(); |
91 | var resource = resourceFactory.createResource(uri); | 106 | var resource = resourceFactory.createResource(uri); |
92 | resourceSet.getResources().add(resource); | 107 | resourceSet.getResources().add(resource); |
93 | resource.load(Map.of()); | 108 | resource.load(Map.of()); |
94 | return loadResource(resource); | 109 | return loadResource(resource); |
95 | } | 110 | } |
96 | 111 | ||
112 | private XtextResourceSet createResourceSet() { | ||
113 | var resourceSet = resourceSetProvider.get(); | ||
114 | var adapter = ImportAdapter.getOrInstall(resourceSet); | ||
115 | adapter.getLibraryPaths().addAll(0, extraPaths); | ||
116 | return resourceSet; | ||
117 | } | ||
118 | |||
97 | public Problem loadResource(Resource resource) { | 119 | public Problem loadResource(Resource resource) { |
98 | var issues = resourceValidator.validate(resource, CheckMode.ALL, () -> { | 120 | var issues = resourceValidator.validate(resource, CheckMode.ALL, () -> { |
99 | cancellationToken.checkCancelled(); | 121 | cancellationToken.checkCancelled(); |