From 247de69afc0930f59d770c68ea9dc284c45b4edb Mon Sep 17 00:00:00 2001 From: Nikolai Kudasov Date: Sun, 17 Apr 2022 16:08:46 +0300 Subject: [PATCH] Add basic AST mapping for CompilationUnit --- src/main/java/parser/TreeMappings.kt | 61 ++++++++++++++++++- src/main/java/parser/Visitor.kt | 34 +++++------ .../tree/Compilation/TopLevelComponents.java | 8 +++ src/main/java/tree/Type/TypeArguments.java | 7 +++ src/main/java/tree/Type/TypeList.java | 7 +++ src/main/java/tree/Type/TypeParameters.java | 7 +++ 6 files changed, 106 insertions(+), 18 deletions(-) diff --git a/src/main/java/parser/TreeMappings.kt b/src/main/java/parser/TreeMappings.kt index 48e98d17..b119ebc9 100644 --- a/src/main/java/parser/TreeMappings.kt +++ b/src/main/java/parser/TreeMappings.kt @@ -1,10 +1,69 @@ package parser +import lexer.Token +import lexer.TokenCode import parser.JavaParser.* -import tree.Compilation.Package +import tree.Annotations +import tree.Compilation.* import tree.CompoundName +import tree.Declaration.ClassDeclaration import tree.Declaration.ImportDeclaration +import tree.Declaration.ImportDeclarations +import tree.Declaration.NormalClassDeclaration +import tree.Type.* +fun CompilationUnitContext.toCompilationUnit() : CompilationUnit = + SimpleCompilationUnit( + ImportDeclarations(ArrayList(importDeclaration().map { it.toImportDeclaration() })), + TopLevelComponents(ArrayList(typeDeclaration().map { it.toTopLevelComponent() })) + ) + +fun TypeDeclarationContext.toTopLevelComponent() : TopLevelComponent = + if (classDeclaration() != null) { + TopLevelComponent(classDeclaration().toClassDeclaration()) + } else { + throw java.lang.Exception("Cannot translate") // FIXME + } + +fun ClassDeclarationContext.toClassDeclaration() : ClassDeclaration = + NormalClassDeclaration( + this.identifier().toToken(), + this.typeParameters()?.toTypeParameters(), + this.typeType()?.toType(), + null, + null + ) + +fun TypeParametersContext.toTypeParameters() : TypeParameters = + TypeParameters(ArrayList(this.typeParameter().map { it.toTypeParameter()})) + +fun TypeParameterContext.toTypeParameter() : TypeParameter = + TypeParameter(null, TypeParameterTail(this.identifier().toToken(), null)) + +fun TypeListContext.toTypeList() : TypeList = + TypeList(ArrayList(this.typeType().map { it.toType() })) + +fun TypeTypeContext.toType() : Type = + when (this) { + is TypeClassOrInterfaceTypeContext -> this.toType() + else -> throw Exception("cannot convert type") // FIXME + } +fun TypeClassOrInterfaceTypeContext.toType() : Type = + this.classOrInterfaceType().toType() + +fun ClassOrInterfaceTypeContext.toType() : Type = + TypeName( + CompoundName(this.identifier().map { it.text }), + this.typeArguments().last().toTypeArguments() // FIXME: we use last() since we do not support types like A.C + ) + +fun TypeArgumentsContext.toTypeArguments() : TypeArguments = + TypeArguments(ArrayList(typeArgument().map { it.toTypeArgument() })) + +fun TypeArgumentContext.toTypeArgument() : TypeArgument = + TypeArgument(this.typeType().toType(), 0 /* FIXME */, null) + +fun IdentifierContext.toToken() : Token = Token(TokenCode.Identifier, this.text) fun QualifiedNameContext.toCompoundName(): CompoundName = CompoundName(identifier().map { it.text }) diff --git a/src/main/java/parser/Visitor.kt b/src/main/java/parser/Visitor.kt index 42b5aa37..c8f69241 100644 --- a/src/main/java/parser/Visitor.kt +++ b/src/main/java/parser/Visitor.kt @@ -13,23 +13,23 @@ import tree.Entity import tree.Type.Type class Visitor : JavaParserBaseVisitor() { - override fun visitCompilationUnit(ctx: CompilationUnitContext?): Entity { - return SimpleCompilationUnit( -// ctx!!.packageDeclaration().qualifiedName().toCompoundName(), - ImportDeclarations(ArrayList(ctx!!.importDeclaration().map { it.toImportDeclaration() })), - TopLevelComponents( - TopLevelComponent( - NormalClassDeclaration( - Token(TokenCode.StringLiteral), - null, - Type(null), - null, - null, - ).run { name = "Main"; this } - ) - ), - ) - } + override fun visitCompilationUnit(ctx: CompilationUnitContext?): Entity = ctx!!.toCompilationUnit() +// return SimpleCompilationUnit( +//// ctx!!.packageDeclaration().qualifiedName().toCompoundName(), +// ImportDeclarations(ArrayList(ctx!!.importDeclaration().map { it.toImportDeclaration() })), +// TopLevelComponents( +// TopLevelComponent( +// NormalClassDeclaration( +// Token(TokenCode.StringLiteral), +// null, +// Type(null), +// null, +// null, +// ).run { name = "Main"; this } +// ) +// ), +// ) +// } override fun visitPackageDeclaration(ctx: JavaParser.PackageDeclarationContext?): Entity = ctx!!.toPackage() diff --git a/src/main/java/tree/Compilation/TopLevelComponents.java b/src/main/java/tree/Compilation/TopLevelComponents.java index 1b013a86..a653e4b6 100644 --- a/src/main/java/tree/Compilation/TopLevelComponents.java +++ b/src/main/java/tree/Compilation/TopLevelComponents.java @@ -1,5 +1,6 @@ package tree.Compilation; +import java.lang.reflect.Array; import java.util.ArrayList; import tree.Entity; @@ -21,6 +22,13 @@ public TopLevelComponents(TopLevelComponent tlc) { } } + public TopLevelComponents(ArrayList components) { + this.components = components; + for (var tlc : components) { + tlc.parent = this; + } + } + public TopLevelComponents add(TopLevelComponent tlc) { this.components.add(tlc); if (tlc != null) { diff --git a/src/main/java/tree/Type/TypeArguments.java b/src/main/java/tree/Type/TypeArguments.java index 14392362..7521cc7f 100644 --- a/src/main/java/tree/Type/TypeArguments.java +++ b/src/main/java/tree/Type/TypeArguments.java @@ -29,6 +29,13 @@ public TypeArguments(TypeArgument arg) { } } + public TypeArguments(ArrayList args) { + this.arguments = args; + for (TypeArgument arg : args) { + arg.parent = this; + } + } + public TypeArguments add(TypeArgument arg) { this.arguments.add(arg); if (arg != null) { diff --git a/src/main/java/tree/Type/TypeList.java b/src/main/java/tree/Type/TypeList.java index d3f94439..e0987c26 100644 --- a/src/main/java/tree/Type/TypeList.java +++ b/src/main/java/tree/Type/TypeList.java @@ -1,6 +1,8 @@ package tree.Type; import java.util.ArrayList; +import java.util.List; + import tree.Entity; // ClassTypeList1 @@ -25,6 +27,11 @@ public TypeList(Type t) { } } + public TypeList(ArrayList types) { + this.types = types; + for (Type t : types) { t.parent = this; } + } + public TypeList add(Type t) { this.types.add(t); if (t != null) { diff --git a/src/main/java/tree/Type/TypeParameters.java b/src/main/java/tree/Type/TypeParameters.java index a42bb2b0..8e263be5 100644 --- a/src/main/java/tree/Type/TypeParameters.java +++ b/src/main/java/tree/Type/TypeParameters.java @@ -16,6 +16,13 @@ public TypeParameters(TypeParameter tpar) { } } + public TypeParameters(ArrayList params) { + this.typeParameters = params; + for (var tpar : params) { + tpar.parent = this; + } + } + public TypeParameters add(TypeParameter tpar) { this.typeParameters.add(tpar); if (tpar != null) {