mirror of
https://github.com/GayPizzaSpecifications/pork.git
synced 2025-08-03 13:11:32 +00:00
parser: use ast user data to store attribution
This commit is contained in:
@ -18,6 +18,7 @@ class AstCodegen(val pkg: String, val outputDirectory: Path, val world: AstWorld
|
||||
for (type in world.typeRegistry.types) {
|
||||
writeAstType(type)
|
||||
}
|
||||
writeNodeExtensions()
|
||||
writeNodeType()
|
||||
writeNodeVisitors()
|
||||
writeNodeCoalescer()
|
||||
@ -124,7 +125,6 @@ class AstCodegen(val pkg: String, val outputDirectory: Path, val world: AstWorld
|
||||
visitAllFunction.body.add(
|
||||
"nodeLists.asSequence().flatten().filterNotNull().map { visit(it) }.toList()")
|
||||
visitorExtensionSet.functions.add(visitAllFunction)
|
||||
|
||||
write("NodeVisitorExtensions.kt", KotlinWriter(visitorExtensionSet))
|
||||
}
|
||||
|
||||
@ -190,12 +190,22 @@ class AstCodegen(val pkg: String, val outputDirectory: Path, val world: AstWorld
|
||||
}
|
||||
|
||||
if (role == AstTypeRole.RootNode) {
|
||||
kotlinClassLike.imports.add("kotlinx.serialization.Transient")
|
||||
val typeMember = KotlinMember(
|
||||
"type",
|
||||
"NodeType",
|
||||
abstract = true
|
||||
)
|
||||
kotlinClassLike.members.add(typeMember)
|
||||
val dataMember = KotlinMember(
|
||||
"data",
|
||||
"Any?",
|
||||
value = "null",
|
||||
mutable = true,
|
||||
notInsideConstructor = true,
|
||||
annotations = mutableListOf("@Transient")
|
||||
)
|
||||
kotlinClassLike.members.add(dataMember)
|
||||
|
||||
val abstractVisitChildrenFunction = KotlinFunction(
|
||||
"visitChildren",
|
||||
@ -389,6 +399,21 @@ class AstCodegen(val pkg: String, val outputDirectory: Path, val world: AstWorld
|
||||
write("${type.name}.kt", KotlinWriter(kotlinClassLike))
|
||||
}
|
||||
|
||||
private fun writeNodeExtensions() {
|
||||
val nodeExtensionSet = KotlinFunctionSet(pkg)
|
||||
val dataFunction = KotlinFunction(
|
||||
"data",
|
||||
typeParameters = mutableListOf("P"),
|
||||
extensionOf = "Node",
|
||||
returnType = "P?",
|
||||
isImmediateExpression = true,
|
||||
annotations = mutableListOf("""@Suppress("UNCHECKED_CAST")""")
|
||||
)
|
||||
dataFunction.body.add("data as? P?")
|
||||
nodeExtensionSet.functions.add(dataFunction)
|
||||
write("NodeExtensions.kt", KotlinWriter(nodeExtensionSet))
|
||||
}
|
||||
|
||||
private fun toKotlinType(typeRef: AstTypeRef): String {
|
||||
val baseType = typeRef.type?.name ?: typeRef.primitive?.id
|
||||
?: throw RuntimeException("Unable to determine base type.")
|
||||
|
@ -6,5 +6,9 @@ class KotlinMember(
|
||||
var abstract: Boolean = false,
|
||||
var overridden: Boolean = false,
|
||||
var value: String? = null,
|
||||
var mutable: Boolean = false
|
||||
var mutable: Boolean = false,
|
||||
var private: Boolean = false,
|
||||
var protected: Boolean = false,
|
||||
var notInsideConstructor: Boolean = false,
|
||||
var annotations: MutableList<String> = mutableListOf()
|
||||
)
|
||||
|
@ -15,7 +15,7 @@ class KotlinWriter() {
|
||||
}
|
||||
writeClassLike(classType, kotlinClass)
|
||||
val members = kotlinClass.members.filter {
|
||||
it.abstract || (it.overridden && it.value != null)
|
||||
it.abstract || (it.overridden && it.value != null) || it.notInsideConstructor
|
||||
}
|
||||
if (members.isEmpty() && kotlinClass.functions.isEmpty()) {
|
||||
appendLine()
|
||||
@ -23,13 +23,23 @@ class KotlinWriter() {
|
||||
appendLine(" {")
|
||||
}
|
||||
|
||||
for (member in members) {
|
||||
val form = if (member.mutable) "var" else "val"
|
||||
for ((index, member) in members.withIndex()) {
|
||||
for (annotation in member.annotations) {
|
||||
appendLine(" $annotation")
|
||||
}
|
||||
|
||||
val privacy = when {
|
||||
member.private -> "private "
|
||||
member.protected -> "protected "
|
||||
else -> ""
|
||||
}
|
||||
val form = if (member.mutable) "${privacy}var" else "${privacy}val"
|
||||
if (member.abstract) {
|
||||
appendLine(" abstract $form ${member.name}: ${member.type}")
|
||||
} else {
|
||||
append(" ")
|
||||
if (member.overridden) {
|
||||
append(" override ")
|
||||
append("override ")
|
||||
}
|
||||
append("$form ${member.name}: ${member.type}")
|
||||
if (member.value != null) {
|
||||
@ -38,6 +48,10 @@ class KotlinWriter() {
|
||||
}
|
||||
appendLine()
|
||||
}
|
||||
|
||||
if (index != members.size - 1) {
|
||||
appendLine()
|
||||
}
|
||||
}
|
||||
|
||||
if (members.isNotEmpty() && kotlinClass.functions.isNotEmpty()) {
|
||||
@ -109,7 +123,7 @@ class KotlinWriter() {
|
||||
}
|
||||
|
||||
val contructedMembers = kotlinClass.members.filter {
|
||||
!it.abstract && !(it.overridden && it.value != null)
|
||||
!it.abstract && !(it.overridden && it.value != null) && !it.notInsideConstructor
|
||||
}
|
||||
|
||||
if (contructedMembers.isNotEmpty()) {
|
||||
|
Reference in New Issue
Block a user