-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
143 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/.gradle | ||
/.idea | ||
build |
This file contains 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,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() | ||
} | ||
} |
This file contains 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,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") | ||
} | ||
} | ||
} |
This file contains 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,10 @@ | ||
package kastree.ast.psi | ||
|
||
import kotlin.test.Test | ||
|
||
class ParserTest { | ||
@Test | ||
fun testParser() { | ||
Parser.parse("package whatevs\n\nclass Foo\n") | ||
} | ||
} |
This file contains 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 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,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 | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
rootProject.name = 'kastree' | ||
include ':ast:ast-common', | ||
':ast:ast-jvm' | ||
':ast:ast-jvm', | ||
':ast-psi' | ||
|