Skip to content

Commit

Permalink
Add parser and begin convert
Browse files Browse the repository at this point in the history
  • Loading branch information
cretz committed Jul 14, 2018
1 parent c45f9f9 commit c8957e7
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/.gradle
/.idea
build
64 changes: 64 additions & 0 deletions ast-psi/src/main/kotlin/kastree/ast/psi/Converter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package kastree.ast.psi

import kastree.ast.Node
import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget
import org.jetbrains.kotlin.psi.*

open class Converter {

fun convertAnnotation(v: KtAnnotationEntry) = Node.Modifier.AnnotationSet.Annotation(
names = v.typeReference?.names ?: error("Missing annotation name"),
typeArgs = v.typeArguments.map(::convertType),
args = v.valueArguments.map(::convertValueArg)
)

fun convertAnnotationSet(v: KtAnnotation) = Node.Modifier.AnnotationSet(
target = when (v.useSiteTarget?.getAnnotationUseSiteTarget()) {
null -> null
AnnotationUseSiteTarget.FIELD -> Node.Modifier.AnnotationSet.Target.FIELD
AnnotationUseSiteTarget.FILE -> Node.Modifier.AnnotationSet.Target.FILE
AnnotationUseSiteTarget.PROPERTY -> Node.Modifier.AnnotationSet.Target.PROPERTY
AnnotationUseSiteTarget.PROPERTY_GETTER -> Node.Modifier.AnnotationSet.Target.GET
AnnotationUseSiteTarget.PROPERTY_SETTER -> Node.Modifier.AnnotationSet.Target.SET
AnnotationUseSiteTarget.RECEIVER -> Node.Modifier.AnnotationSet.Target.RECEIVER
AnnotationUseSiteTarget.CONSTRUCTOR_PARAMETER -> Node.Modifier.AnnotationSet.Target.PARAM
AnnotationUseSiteTarget.SETTER_PARAMETER -> Node.Modifier.AnnotationSet.Target.SETPARAM
AnnotationUseSiteTarget.PROPERTY_DELEGATE_FIELD -> Node.Modifier.AnnotationSet.Target.DELEGATE
},
anns = v.entries.map(::convertAnnotation)
)

fun convertDecl(v: KtDeclaration): Node.Decl = when (v) {
else -> TODO()
}

fun convertFile(v: KtFile) = Node.File(
anns = v.fileAnnotationList?.annotations?.map(::convertAnnotationSet) ?: emptyList(),
pkg = v.packageDirective?.let(::convertPackage),
imports = v.importDirectives.map(::convertImport),
decls = v.declarations.map(::convertDecl)
)

fun convertImport(v: KtImportDirective) = Node.Import(
names = v.importedFqName?.pathSegments()?.map { it.asString() } ?: error("Missing import path"),
wildcard = v.isAllUnder,
alias = v.aliasName
)

fun convertModifiers(v: KtModifierListOwnerStub<*>): List<Node.Modifier> = TODO()

fun convertPackage(v: KtPackageDirective) = Node.Package(
mods = convertModifiers(v),
names = v.packageNames.map { it.getReferencedName() }
)

fun convertType(v: KtTypeProjection): Node.Type = TODO()

fun convertValueArg(v: ValueArgument): Node.ValueArg = TODO()

companion object : Converter() {
val KtTypeReference.names get() = (typeElement as? KtUserType)?.names ?: emptyList()
val KtUserType.names get(): List<String> =
referencedName?.let { (qualifier?.names ?: emptyList()) + it } ?: emptyList()
}
}
31 changes: 31 additions & 0 deletions ast-psi/src/main/kotlin/kastree/ast/psi/Parser.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package kastree.ast.psi

import com.intellij.openapi.util.Disposer
import com.intellij.psi.PsiManager
import com.intellij.testFramework.LightVirtualFile
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.idea.KotlinFileType
import org.jetbrains.kotlin.psi.KtFile

open class Parser {
fun parse(code: String): KtFile {
val disposable = Disposer.newDisposable()
try {
val env = KotlinCoreEnvironment.createForProduction(
disposable, CompilerConfiguration(), EnvironmentConfigFiles.JVM_CONFIG_FILES)
val file = LightVirtualFile("temp.kt", KotlinFileType.INSTANCE, code)
return PsiManager.getInstance(env.project).findFile(file) as KtFile
} finally {
disposable.dispose()
}
}

companion object : Parser() {
init {
// To hide annoying warning on Windows
System.setProperty("idea.use.native.fs.for.win", "false")
}
}
}
10 changes: 10 additions & 0 deletions ast-psi/src/test/kotlin/kastree/ast/psi/ParserTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package kastree.ast.psi

import kotlin.test.Test

class ParserTest {
@Test
fun testParser() {
Parser.parse("package whatevs\n\nclass Foo\n")
}
}
2 changes: 1 addition & 1 deletion ast/ast-common/src/main/kotlin/kastree/ast/Node.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ sealed class Node {

data class Package(
override val mods: List<Modifier>,
val name: String
val names: List<String>
) : Node(), WithModifiers

data class Import(
Expand Down
15 changes: 15 additions & 0 deletions ast/ast-common/src/main/kotlin/kastree/ast/Visitor.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package kastree.ast

interface Visitor {
fun visit(v: Node) {
when (v) {
is Node.File -> visit(v)
}
}

fun visit(v: Node.File) {

}

// TODO: the rest
}
19 changes: 19 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,23 @@ project(':ast:ast-jvm') {
events 'passed', 'skipped', 'failed'
}
}
}

project(':ast-psi') {
apply plugin: 'kotlin'
sourceCompatibility = '1.8'
compileKotlin {
kotlinOptions.jvmTarget = '1.8'
}
compileTestKotlin {
kotlinOptions.jvmTarget = '1.8'
}
dependencies {
compile project(':ast:ast-jvm')
compile 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
compile "org.jetbrains.kotlin:kotlin-compiler:$kotlin_version"
testCompile 'junit:junit:4.12'
testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
testCompile "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
}
}
3 changes: 2 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
rootProject.name = 'kastree'
include ':ast:ast-common',
':ast:ast-jvm'
':ast:ast-jvm',
':ast-psi'

0 comments on commit c8957e7

Please sign in to comment.