-
Notifications
You must be signed in to change notification settings - Fork 12
Revisited AST transformers for Kolasu 1.7 #429
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
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
60a5243
#417 rename NodeFactory -> Transform and remove deprecated elements f…
alessiostalla f0b0ba2
#417 make AST transformers stateless and introduce context object #202
alessiostalla 04c4b06
AST transformers in case of parse errors #395
alessiostalla 0ffa8de
#291 make KolasuParser aware of AST transformers and skip assignParen…
alessiostalla 50be0df
#417 move the Source to the transformation context (to make the AST t…
alessiostalla 6be07ec
#417 extra checks for exception handling in AST transformers
alessiostalla 52949e2
#417 #374 revisit childrenSetAtConstruction
alessiostalla f2fd22f
#417 revisit fault tolerance flags
alessiostalla d3443ed
#417 documentation, refactoring
alessiostalla d0714c9
#417 address review remarks
alessiostalla 06806c8
#417 address review remarks
alessiostalla 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,12 +3,13 @@ package com.strumenta.starlasu.mapping | |
| import com.strumenta.starlasu.model.ASTNode | ||
| import com.strumenta.starlasu.model.Node | ||
| import com.strumenta.starlasu.model.Origin | ||
| import com.strumenta.starlasu.model.Source | ||
| import com.strumenta.starlasu.parsing.ParseTreeOrigin | ||
| import com.strumenta.starlasu.parsing.withParseTreeNode | ||
| import com.strumenta.starlasu.transformation.ASTTransformer | ||
| import com.strumenta.starlasu.transformation.NodeFactory | ||
| import com.strumenta.starlasu.validation.Issue | ||
| import com.strumenta.starlasu.transformation.FailingASTTransformation | ||
| import com.strumenta.starlasu.transformation.FaultTolerance | ||
| import com.strumenta.starlasu.transformation.TransformationContext | ||
| import com.strumenta.starlasu.transformation.TransformationRule | ||
| import org.antlr.v4.runtime.ParserRuleContext | ||
| import org.antlr.v4.runtime.tree.ParseTree | ||
| import kotlin.reflect.KClass | ||
|
|
@@ -20,29 +21,41 @@ import kotlin.reflect.KClass | |
| open class ParseTreeToASTTransformer | ||
| @JvmOverloads | ||
| constructor( | ||
| issues: MutableList<Issue> = mutableListOf(), | ||
| allowGenericNode: Boolean = true, | ||
| val source: Source? = null, | ||
| throwOnUnmappedNode: Boolean = true, | ||
| ) : ASTTransformer(issues, allowGenericNode, throwOnUnmappedNode) { | ||
| faultTolerance: FaultTolerance = FaultTolerance.THROW_ONLY_ON_UNMAPPED, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
| ) : ASTTransformer(faultTolerance) { | ||
| /** | ||
| * Performs the transformation of a node and, recursively, its descendants. In addition to the overridden method, | ||
| * it also assigns the parseTreeNode to the AST node so that it can keep track of its position. | ||
| * However, a node factory can override the parseTreeNode of the nodes it creates (but not the parent). | ||
| */ | ||
| override fun transformIntoNodes( | ||
| source: Any?, | ||
| parent: ASTNode?, | ||
| context: TransformationContext, | ||
| expectedType: KClass<out ASTNode>, | ||
| ): List<ASTNode> { | ||
| val transformed = super.transformIntoNodes(source, parent, expectedType) | ||
| if (source is ParserRuleContext && source.exception != null) { | ||
| if (faultTolerance == FaultTolerance.STRICT) { | ||
| throw RuntimeException("Failed to transform $source into $expectedType", source.exception) | ||
| } | ||
| val origin = | ||
| FailingASTTransformation( | ||
| asOrigin(source, context), | ||
| "Failed to transform $source into $expectedType because of an error (${source.exception.message})", | ||
| ) | ||
| val nodes = defaultNodes(source, context, expectedType) | ||
| nodes.forEach { node -> | ||
| node.origin = origin | ||
| } | ||
| return nodes | ||
| } | ||
| val transformed = super.transformIntoNodes(source, context, expectedType) | ||
| return transformed | ||
| .map { node -> | ||
| if (source is ParserRuleContext) { | ||
| if (node.origin == null) { | ||
| node.withParseTreeNode(source, this.source) | ||
| node.withParseTreeNode(source, context.source) | ||
| } else if (node.position != null && node.source == null) { | ||
| node.position!!.source = this.source | ||
| node.position!!.source = context.source | ||
| } | ||
| } | ||
| return listOf(node) | ||
|
|
@@ -57,7 +70,15 @@ open class ParseTreeToASTTransformer | |
| return if (origin is ParseTreeOrigin) origin.parseTree else source | ||
| } | ||
|
|
||
| override fun asOrigin(source: Any): Origin? = if (source is ParseTree) ParseTreeOrigin(source) else null | ||
| override fun asOrigin( | ||
| source: Any, | ||
| context: TransformationContext, | ||
| ): Origin? = | ||
| if (source is ParseTree) { | ||
| ParseTreeOrigin(source, context.source) | ||
| } else { | ||
| null | ||
| } | ||
|
|
||
| override fun asString(source: Any): String? = | ||
| if (source is ParseTree) { | ||
|
|
@@ -71,19 +92,19 @@ open class ParseTreeToASTTransformer | |
| * wrapper. When there is only a ParserRuleContext child we can transform | ||
| * that child and return that result. | ||
| */ | ||
| fun <P : ParserRuleContext> registerNodeFactoryUnwrappingChild(kclass: KClass<P>): NodeFactory<P, Node> = | ||
| registerNodeFactory(kclass) { source, transformer, _ -> | ||
| fun <P : ParserRuleContext> registerRuleUnwrappingChild(kclass: KClass<P>): TransformationRule<P, Node> = | ||
| registerRule(kclass) { source, context, _ -> | ||
| val nodeChildren = source.children.filterIsInstance<ParserRuleContext>() | ||
| require(nodeChildren.size == 1) { | ||
| "Node $source (${source.javaClass}) has ${nodeChildren.size} " + | ||
| "node children: $nodeChildren" | ||
| } | ||
| transformer.transform(nodeChildren[0]) as Node | ||
| transform(nodeChildren[0], context) as Node | ||
| } | ||
|
|
||
| /** | ||
| * Alternative to registerNodeFactoryUnwrappingChild(KClass) which is slightly more concise. | ||
| */ | ||
| inline fun <reified P : ParserRuleContext> registerNodeFactoryUnwrappingChild(): NodeFactory<P, Node> = | ||
| registerNodeFactoryUnwrappingChild(P::class) | ||
| inline fun <reified P : ParserRuleContext> registerRuleUnwrappingChild(): TransformationRule<P, Node> = | ||
| registerRuleUnwrappingChild(P::class) | ||
| } | ||
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
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
27 changes: 0 additions & 27 deletions
27
core/src/main/kotlin/com/strumenta/starlasu/transformation/GenericNodes.kt
This file was deleted.
Oops, something went wrong.
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.
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.
Uh oh!
There was an error while loading. Please reload this page.