Skip to content

Commit

Permalink
Add basic AST mapping for CompilationUnit
Browse files Browse the repository at this point in the history
  • Loading branch information
fizruk committed Apr 17, 2022
1 parent 9bc9d3e commit 247de69
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 18 deletions.
61 changes: 60 additions & 1 deletion src/main/java/parser/TreeMappings.kt
Original file line number Diff line number Diff line change
@@ -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<B>.C<D>
)

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 })
Expand Down
34 changes: 17 additions & 17 deletions src/main/java/parser/Visitor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ import tree.Entity
import tree.Type.Type

class Visitor : JavaParserBaseVisitor<Entity>() {
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()

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/tree/Compilation/TopLevelComponents.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tree.Compilation;

import java.lang.reflect.Array;
import java.util.ArrayList;
import tree.Entity;

Expand All @@ -21,6 +22,13 @@ public TopLevelComponents(TopLevelComponent tlc) {
}
}

public TopLevelComponents(ArrayList<TopLevelComponent> components) {
this.components = components;
for (var tlc : components) {
tlc.parent = this;
}
}

public TopLevelComponents add(TopLevelComponent tlc) {
this.components.add(tlc);
if (tlc != null) {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/tree/Type/TypeArguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ public TypeArguments(TypeArgument arg) {
}
}

public TypeArguments(ArrayList<TypeArgument> args) {
this.arguments = args;
for (TypeArgument arg : args) {
arg.parent = this;
}
}

public TypeArguments add(TypeArgument arg) {
this.arguments.add(arg);
if (arg != null) {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/tree/Type/TypeList.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tree.Type;

import java.util.ArrayList;
import java.util.List;

import tree.Entity;

// ClassTypeList1
Expand All @@ -25,6 +27,11 @@ public TypeList(Type t) {
}
}

public TypeList(ArrayList<Type> types) {
this.types = types;
for (Type t : types) { t.parent = this; }
}

public TypeList add(Type t) {
this.types.add(t);
if (t != null) {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/tree/Type/TypeParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ public TypeParameters(TypeParameter tpar) {
}
}

public TypeParameters(ArrayList<TypeParameter> params) {
this.typeParameters = params;
for (var tpar : params) {
tpar.parent = this;
}
}

public TypeParameters add(TypeParameter tpar) {
this.typeParameters.add(tpar);
if (tpar != null) {
Expand Down

0 comments on commit 247de69

Please sign in to comment.