-
Notifications
You must be signed in to change notification settings - Fork 378
Resolve variables at parse time #1429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bioball
merged 2 commits into
apple:main
from
stackoverflow:resolve-variables-at-parse-time
May 26, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
pkl-core/src/generator/java/org/pkl/core/generator/BaseModuleMembersGenerator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| /* | ||
| * Copyright © 2026 Apple Inc. and the Pkl project authors. All rights reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.pkl.core.generator; | ||
|
|
||
| import com.palantir.javapoet.CodeBlock; | ||
| import com.palantir.javapoet.JavaFile; | ||
| import com.palantir.javapoet.MethodSpec; | ||
| import com.palantir.javapoet.TypeName; | ||
| import com.palantir.javapoet.TypeSpec; | ||
| import java.io.IOException; | ||
| import java.io.UncheckedIOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import javax.lang.model.element.Modifier; | ||
| import org.pkl.parser.Parser; | ||
| import org.pkl.parser.syntax.Modifier.ModifierValue; | ||
|
|
||
| public final class BaseModuleMembersGenerator { | ||
| record Members(Set<String> properties, Set<String> methods) {} | ||
|
|
||
| public static void main(String[] args) { | ||
| if (args.length < 2) { | ||
| throw new IllegalArgumentException( | ||
| "Usage: BaseModuleMembersGenerator <path-to-base.pkl> <output-dir>"); | ||
| } | ||
| var members = buildMembers(args[0]); | ||
| generateJavaCode(members, args[1]); | ||
| } | ||
|
|
||
| private static void generateJavaCode(Members members, String outputDir) { | ||
| var privateConstructor = MethodSpec.constructorBuilder().addModifiers(Modifier.PRIVATE).build(); | ||
|
|
||
| var hasPropertyMethod = | ||
| buildHasMethod("hasProperty", members.properties().stream().sorted().toList()); | ||
| var hasMethodMethod = buildHasMethod("hasMethod", members.methods().stream().sorted().toList()); | ||
|
|
||
| var classSpec = | ||
| TypeSpec.classBuilder("BaseModuleMembers") | ||
| .addModifiers(Modifier.PUBLIC, Modifier.FINAL) | ||
| .addMethod(privateConstructor) | ||
| .addMethod(hasPropertyMethod) | ||
| .addMethod(hasMethodMethod) | ||
| .build(); | ||
|
|
||
| var javaFile = | ||
| JavaFile.builder("org.pkl.core.runtime", classSpec) | ||
| .addFileComment("DO NOT EDIT — generated by BaseModuleMembersGenerator") | ||
| .build(); | ||
|
|
||
| try { | ||
| javaFile.writeTo(Path.of(outputDir)); | ||
| } catch (IOException e) { | ||
| throw new UncheckedIOException(e); | ||
| } | ||
| } | ||
|
|
||
| private static MethodSpec buildHasMethod(String methodName, List<String> names) { | ||
| var code = CodeBlock.builder(); | ||
| code.add("return switch (name) {\n"); | ||
| code.indent(); | ||
| code.add("case $S", names.get(0)); | ||
| if (names.size() == 1) { | ||
| code.add(" -> true;\n"); | ||
| } else { | ||
| code.add(",\n"); | ||
| code.indent(); | ||
| for (var i = 1; i < names.size() - 1; i++) { | ||
| code.add("$S,\n", names.get(i)); | ||
| } | ||
| code.add("$S -> true;\n", names.get(names.size() - 1)); | ||
| code.unindent(); | ||
| } | ||
| code.add("default -> false;\n"); | ||
| code.unindent(); | ||
| code.add("};\n"); | ||
|
|
||
| return MethodSpec.methodBuilder(methodName) | ||
| .addModifiers(Modifier.PUBLIC, Modifier.STATIC) | ||
| .returns(TypeName.BOOLEAN) | ||
| .addParameter(String.class, "name") | ||
| .addCode(code.build()) | ||
| .build(); | ||
| } | ||
|
|
||
| private static String getBaseModuleText(String path) { | ||
| try { | ||
| return Files.readString(Path.of(path)); | ||
| } catch (IOException e) { | ||
| throw new UncheckedIOException(e); | ||
| } | ||
| } | ||
|
|
||
| private static Members buildMembers(String basePklPath) { | ||
| var text = getBaseModuleText(basePklPath); | ||
| var parsed = new Parser().parseModule(text); | ||
| var properties = new HashSet<String>(); | ||
| var methods = new HashSet<String>(); | ||
| for (var property : parsed.getProperties()) { | ||
| if (isLocal(property.getModifiers())) { | ||
| continue; | ||
| } | ||
| properties.add(property.getName().getValue()); | ||
| } | ||
| for (var clazz : parsed.getClasses()) { | ||
| if (isLocal(clazz.getModifiers())) { | ||
| continue; | ||
| } | ||
| properties.add(clazz.getName().getValue()); | ||
| } | ||
| for (var typealias : parsed.getTypeAliases()) { | ||
| if (isLocal(typealias.getModifiers())) { | ||
| continue; | ||
| } | ||
| properties.add(typealias.getName().getValue()); | ||
| } | ||
| for (var method : parsed.getMethods()) { | ||
| if (isLocal(method.getModifiers())) { | ||
| continue; | ||
| } | ||
| methods.add(method.getName().getValue()); | ||
| } | ||
| return new Members(properties, methods); | ||
| } | ||
|
|
||
| private static boolean isLocal(List<org.pkl.parser.syntax.Modifier> modifiers) { | ||
| return modifiers.stream().anyMatch((it) -> it.getValue() == ModifierValue.LOCAL); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently,
amends "..."REPL statements don't do anything. In this PR, the header still mostly does nothing, but it will throw if the amends has a relative path.This is because our REPL logic has changed; before the AstBuilder can visit an expression, it first needs to visit the module to collect names. In the process of visiting the module, it will validate this module header.