-
Notifications
You must be signed in to change notification settings - Fork 392
Add pkl:syntax module
#1569
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
Open
stackoverflow
wants to merge
4
commits into
apple:main
Choose a base branch
from
stackoverflow:pkl-syntax
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add pkl:syntax module
#1569
Changes from all commits
Commits
Show all changes
4 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
61 changes: 61 additions & 0 deletions
61
pkl-core/src/main/java/org/pkl/core/runtime/SyntaxModule.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,61 @@ | ||
| /* | ||
| * Copyright © 2025-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.runtime; | ||
|
|
||
| import com.oracle.truffle.api.CompilerDirectives; | ||
| import java.net.URI; | ||
|
|
||
| public final class SyntaxModule extends StdLibModule { | ||
| private static final VmTyped instance = VmUtils.createEmptyModule(); | ||
|
|
||
| static { | ||
| loadModule(URI.create("pkl:syntax"), instance); | ||
| } | ||
|
|
||
| public static VmTyped getModule() { | ||
| return instance; | ||
| } | ||
|
|
||
| public static VmClass getNodeClass() { | ||
| return NodeClass.instance; | ||
| } | ||
|
|
||
| public static VmClass getSpanClass() { | ||
| return SpanClass.instance; | ||
| } | ||
|
|
||
| public static VmClass getParserErrorClass() { | ||
| return ParserErrorClass.instance; | ||
| } | ||
|
|
||
| private static final class NodeClass { | ||
| static final VmClass instance = loadClass("Node"); | ||
| } | ||
|
|
||
| private static final class SpanClass { | ||
| static final VmClass instance = loadClass("Span"); | ||
| } | ||
|
|
||
| private static final class ParserErrorClass { | ||
| static final VmClass instance = loadClass("ParserError"); | ||
| } | ||
|
|
||
| @CompilerDirectives.TruffleBoundary | ||
| private static VmClass loadClass(String className) { | ||
| var theModule = getModule(); | ||
| return (VmClass) VmUtils.readMember(theModule, Identifier.get(className)); | ||
| } | ||
| } |
187 changes: 187 additions & 0 deletions
187
pkl-core/src/main/java/org/pkl/core/stdlib/syntax/SyntaxNodes.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,187 @@ | ||
| /* | ||
| * Copyright © 2025-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.stdlib.syntax; | ||
|
|
||
| import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; | ||
| import com.oracle.truffle.api.dsl.Specialization; | ||
| import java.util.ArrayList; | ||
| import java.util.Locale; | ||
| import org.jspecify.annotations.Nullable; | ||
| import org.pkl.core.runtime.Identifier; | ||
| import org.pkl.core.runtime.SyntaxModule; | ||
| import org.pkl.core.runtime.VmList; | ||
| import org.pkl.core.runtime.VmNull; | ||
| import org.pkl.core.runtime.VmTyped; | ||
| import org.pkl.core.runtime.VmUtils; | ||
| import org.pkl.core.stdlib.ExternalMethod1Node; | ||
| import org.pkl.core.stdlib.ExternalMethod2Node; | ||
| import org.pkl.core.stdlib.VmObjectFactory; | ||
| import org.pkl.formatter.Formatter; | ||
| import org.pkl.formatter.GrammarVersion; | ||
| import org.pkl.parser.GenericParser; | ||
| import org.pkl.parser.GenericParserError; | ||
| import org.pkl.parser.syntax.generic.FullSpan; | ||
| import org.pkl.parser.syntax.generic.Node; | ||
| import org.pkl.parser.syntax.generic.NodeType; | ||
|
|
||
| public final class SyntaxNodes { | ||
| private SyntaxNodes() {} | ||
|
|
||
| private static final Identifier TYPE_ID = Identifier.get("type"); | ||
| private static final Identifier CHILDREN_ID = Identifier.get("children"); | ||
| private static final Identifier SPAN_ID = Identifier.get("span"); | ||
| private static final Identifier LINE_START_ID = Identifier.get("lineStart"); | ||
| private static final Identifier COL_START_ID = Identifier.get("colStart"); | ||
| private static final Identifier LINE_END_ID = Identifier.get("lineEnd"); | ||
| private static final Identifier COL_END_ID = Identifier.get("colEnd"); | ||
| private static final char[] EMPTY_SOURCE = new char[0]; | ||
|
|
||
| /** Extra storage backing a Pkl {@code Node} instance. */ | ||
| static final class NodeData { | ||
| final Node node; | ||
| final char[] source; | ||
| @Nullable VmTyped parentVm; | ||
| VmList childrenVm; | ||
| VmTyped spanVm; | ||
|
|
||
| NodeData(Node node, char[] source, VmList childrenVm, VmTyped spanVm) { | ||
| this.node = node; | ||
| this.source = source; | ||
| this.childrenVm = childrenVm; | ||
| this.spanVm = spanVm; | ||
| } | ||
| } | ||
|
|
||
| /** Extra storage backing a Pkl {@code ParserError} instance. */ | ||
| static final class ErrorData { | ||
| final String text; | ||
| final VmTyped spanVm; | ||
|
|
||
| ErrorData(String text, VmTyped spanVm) { | ||
| this.text = text; | ||
| this.spanVm = spanVm; | ||
| } | ||
| } | ||
|
|
||
| private static final VmObjectFactory<FullSpan> spanFactory = | ||
| new VmObjectFactory<FullSpan>(SyntaxModule::getSpanClass) | ||
| .addIntProperty("lineStart", FullSpan::lineBegin) | ||
| .addIntProperty("colStart", FullSpan::colBegin) | ||
| .addIntProperty("lineEnd", FullSpan::lineEnd) | ||
| .addIntProperty("colEnd", FullSpan::colEnd); | ||
|
|
||
| private static final VmObjectFactory<NodeData> nodeFactory = | ||
| new VmObjectFactory<NodeData>(SyntaxModule::getNodeClass) | ||
| .addStringProperty("type", nd -> nd.node.type.name().toLowerCase(Locale.ROOT)) | ||
| .addListProperty("children", nd -> nd.childrenVm) | ||
| .addProperty("parent", nd -> VmNull.lift(nd.parentVm)) | ||
| .addProperty( | ||
| "text", | ||
| nd -> nd.node.children.isEmpty() ? nd.node.text(nd.source) : VmNull.withoutDefault()) | ||
| .addTypedProperty("span", nd -> nd.spanVm); | ||
|
|
||
| private static final VmObjectFactory<ErrorData> parserErrorFactory = | ||
| new VmObjectFactory<ErrorData>(SyntaxModule::getParserErrorClass) | ||
| .addStringProperty("text", ed -> ed.text) | ||
| .addTypedProperty("span", ed -> ed.spanVm); | ||
|
|
||
| public abstract static class parseNodes extends ExternalMethod1Node { | ||
| @Specialization | ||
| @TruffleBoundary | ||
| protected Object eval(VmTyped self, String source) { | ||
| var sourceChars = source.toCharArray(); | ||
|
|
||
| try { | ||
| var parser = new GenericParser(); | ||
| var root = parser.parseModule(source); | ||
| return convertNode(root, sourceChars); | ||
| } catch (GenericParserError e) { | ||
| var errorSpanVm = spanFactory.create(e.getSpan()); | ||
| var text = e.getMessage() != null ? e.getMessage() : "Parse error"; | ||
| return parserErrorFactory.create(new ErrorData(text, errorSpanVm)); | ||
| } | ||
| } | ||
|
|
||
| private static VmTyped convertNode(Node genericNode, char[] sourceChars) { | ||
| // convert children recursively | ||
| var childrenList = new ArrayList<VmTyped>(genericNode.children.size()); | ||
| for (var child : genericNode.children) { | ||
| childrenList.add(convertNode(child, sourceChars)); | ||
| } | ||
|
|
||
| var childrenVm = VmList.create(childrenList.toArray()); | ||
| var spanVm = spanFactory.create(genericNode.span); | ||
| var data = new NodeData(genericNode, sourceChars, childrenVm, spanVm); | ||
|
|
||
| var result = nodeFactory.create(data); | ||
|
|
||
| // set parent back-reference on each child | ||
| for (var childVm : childrenList) { | ||
| var childData = (NodeData) childVm.getExtraStorage(); | ||
| childData.parentVm = result; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| } | ||
|
|
||
| public abstract static class formatToString extends ExternalMethod2Node { | ||
| @Specialization | ||
| @TruffleBoundary | ||
| protected String eval(VmTyped self, VmTyped nodeVm, String grammarVersion) { | ||
| var node = convertVmToNode(nodeVm); | ||
| return new Formatter(GrammarVersion.valueOf(grammarVersion)).format(node); | ||
| } | ||
| } | ||
|
|
||
| private static Node convertVmToNode(VmTyped nodeVm) { | ||
| var typeStr = (String) VmUtils.readMember(nodeVm, TYPE_ID); | ||
| var nodeType = NodeType.valueOf(typeStr.toUpperCase(Locale.ROOT)); | ||
|
|
||
| var childrenVm = (VmList) VmUtils.readMember(nodeVm, CHILDREN_ID); | ||
| var children = new ArrayList<Node>(childrenVm.getLength()); | ||
| for (var i = 0; i < childrenVm.getLength(); i++) { | ||
| children.add(convertVmToNode((VmTyped) childrenVm.get(i))); | ||
| } | ||
|
|
||
| var spanVm = (VmTyped) VmUtils.readMember(nodeVm, SPAN_ID); | ||
| var lineStart = ((Long) VmUtils.readMember(spanVm, LINE_START_ID)).intValue(); | ||
| var colStart = ((Long) VmUtils.readMember(spanVm, COL_START_ID)).intValue(); | ||
| var lineEnd = ((Long) VmUtils.readMember(spanVm, LINE_END_ID)).intValue(); | ||
| var colEnd = ((Long) VmUtils.readMember(spanVm, COL_END_ID)).intValue(); | ||
| var span = new FullSpan(0, 0, lineStart, colStart, lineEnd, colEnd); | ||
|
|
||
| Node node; | ||
| if (children.isEmpty()) { | ||
| node = new Node(nodeType, span); | ||
| } else { | ||
| node = new Node(nodeType, span, children); | ||
| } | ||
|
|
||
| var textObj = VmUtils.readMember(nodeVm, Identifier.TEXT); | ||
| if (textObj instanceof String text) { | ||
| node.setText(text); | ||
| } else if (nodeType == NodeType.STRING_CHARS) { | ||
| var sb = new StringBuilder(); | ||
| for (var child : children) { | ||
| sb.append(child.text(EMPTY_SOURCE)); | ||
| } | ||
| node.setText(sb.toString()); | ||
| } | ||
|
|
||
| return node; | ||
| } | ||
| } | ||
4 changes: 4 additions & 0 deletions
4
pkl-core/src/main/java/org/pkl/core/stdlib/syntax/package-info.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,4 @@ | ||
| @NullMarked | ||
| package org.pkl.core.stdlib.syntax; | ||
|
|
||
| import org.jspecify.annotations.NullMarked; |
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.
These should probably be members of
Identifierwith everything else.