Skip to content

Commit

Permalink
refactor: split up ast into multiple files
Browse files Browse the repository at this point in the history
  • Loading branch information
simonseyock committed Dec 22, 2024
1 parent 10f9150 commit 788497c
Show file tree
Hide file tree
Showing 8 changed files with 535 additions and 526 deletions.
526 changes: 0 additions & 526 deletions src/main/scala/java2typescript/ast/ast.scala

Large diffs are not rendered by default.

55 changes: 55 additions & 0 deletions src/main/scala/java2typescript/ast/declaration.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package java2typescript.ast

case class ClassDeclaration(
name: Identifier,
typeParameters: List[Type] = List(),
heritageClauses: List[HeritageClause] = List(),
members: List[Member] = List(),
modifiers: List[Modifier] = List()
) extends Statement {
val kind: SyntaxKind = SyntaxKind.ClassDeclaration
}

case class VariableDeclaration(
name: Identifier,
`type`: Option[Type],
initializer: Option[Expression] = None
) extends Statement {
val kind: SyntaxKind = SyntaxKind.VariableDeclaration
}

case class InterfaceDeclaration(
name: Identifier,
typeParameters: List[Type] = List(),
heritageClauses: List[HeritageClause] = List(),
members: List[Member] = List(),
modifiers: List[Modifier] = List()
) extends Statement {
val kind: SyntaxKind = SyntaxKind.InterfaceDeclaration
}

case class EnumDeclaration(
name: Identifier,
members: List[Member] = List(),
modifiers: List[Modifier] = List()
) extends Statement {
val kind: SyntaxKind = SyntaxKind.EnumDeclaration
}

case class ImportDeclaration(
importClause: ImportClause,
moduleSpecifier: StringLiteral
) extends Node {
val kind: SyntaxKind = SyntaxKind.ImportDeclaration
}

case class FunctionDeclaration(
name: Identifier,
`type`: Option[Type],
parameters: List[Parameter] = List(),
typeParameters: List[Type] = List(),
body: Option[Block] = None,
modifiers: List[Modifier] = List(),
) extends Statement {
val kind: SyntaxKind = SyntaxKind.FunctionDeclaration
}
104 changes: 104 additions & 0 deletions src/main/scala/java2typescript/ast/expression.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package java2typescript.ast

trait Expression extends Node

case class ArrayLiteralExpression(
elements: List[Expression]
) extends Expression {
val kind: SyntaxKind = SyntaxKind.ArrayLiteralExpression
}

case class PropertyAccessExpression(
expression: Expression,
name: Identifier
) extends Expression {
val kind: SyntaxKind = SyntaxKind.PropertyAccessExpression
}

case class ElementAccessExpression(
expression: Expression,
argumentExpression: Expression
) extends Expression {
val kind: SyntaxKind = SyntaxKind.ElementAccessExpression
}

case class CallExpression(
expression: Expression,
arguments: List[Expression] = List(),
typeArguments: List[Type] = List()
) extends Expression {
val kind: SyntaxKind = SyntaxKind.CallExpression
}

case class NewExpression (
expression: Identifier,
arguments: List[Expression] = List(),
typeArguments: List[Type] = List()
) extends Expression {
val kind: SyntaxKind = SyntaxKind.NewExpression
}

case class ParenthesizedExpression(
expression: Expression
) extends Expression {
val kind: SyntaxKind = SyntaxKind.ParenthesizedExpression
}

case class TypeOfExpression(
expression: Expression
) extends Expression {
val kind: SyntaxKind = SyntaxKind.TypeOfExpression
}

case class PrefixUnaryExpression(
operator: SyntaxKind,
operand: Expression
) extends Expression {
val kind: SyntaxKind = SyntaxKind.PrefixUnaryExpression
}

case class PostfixUnaryExpression(
operator: SyntaxKind,
operand: Expression
) extends Expression {
val kind: SyntaxKind = SyntaxKind.PostfixUnaryExpression
}

case class BinaryExpression(
left: Expression,
right: Expression,
operatorToken: Token
) extends Expression {
val kind: SyntaxKind = SyntaxKind.BinaryExpression
}

case class ConditionalExpression(
condition: Expression,
whenTrue: Expression,
whenFalse: Expression,
) extends Expression {
val questionToken: QuestionToken = QuestionToken()
val colonToken: ColonToken = ColonToken()
val kind: SyntaxKind = SyntaxKind.ConditionalExpression
}

case class ExpressionWithTypeArguments(
expression: Expression,
typeArguments: List[Type] = List()
) extends Expression {
val kind: SyntaxKind = SyntaxKind.ExpressionWithTypeArguments
}

case class AsExpression(
expression: Expression,
`type`: Type
) extends Expression {
val kind: SyntaxKind = SyntaxKind.AsExpression
}

case class VariableDeclarationList(
declarations: List[VariableDeclaration],
override val flags: Int = 1 // 1 = Let
) extends Expression {
val kind: SyntaxKind = SyntaxKind.VariableDeclarationList
}
80 changes: 80 additions & 0 deletions src/main/scala/java2typescript/ast/keyword.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package java2typescript.ast

// Keyword

case class ExportKeyword() extends Modifier {
val kind: SyntaxKind = SyntaxKind.ExportKeyword
}

case class ExtendsKeyword() extends Token {
val kind: SyntaxKind = SyntaxKind.ExtendsKeyword
}

case class FalseKeyword() extends Literal {
val kind: SyntaxKind = SyntaxKind.FalseKeyword
}

case class InstanceOfKeyword() extends Token {
val kind: SyntaxKind = SyntaxKind.InstanceOfKeyword
}

case class NullKeyword() extends Literal {
val kind: SyntaxKind = SyntaxKind.NullKeyword
}

case class SuperKeyword() extends Expression {
val kind: SyntaxKind = SyntaxKind.SuperKeyword
}

case class ThisKeyword() extends Expression {
val kind: SyntaxKind = SyntaxKind.ThisKeyword
}

case class TrueKeyword() extends Literal {
val kind: SyntaxKind = SyntaxKind.TrueKeyword
}

case class VoidKeyword() extends Type {
val kind: SyntaxKind = SyntaxKind.VoidKeyword
}

case class ImplementsKeyword() extends Token {
val kind: SyntaxKind = SyntaxKind.ImplementsKeyword
}

case class PrivateKeyword() extends Modifier {
val kind: SyntaxKind = SyntaxKind.PrivateKeyword
}

case class ProtectedKeyword() extends Modifier {
val kind: SyntaxKind = SyntaxKind.ProtectedKeyword
}

case class PublicKeyword() extends Modifier {
val kind: SyntaxKind = SyntaxKind.PublicKeyword
}

case class StaticKeyword() extends Modifier {
val kind: SyntaxKind = SyntaxKind.StaticKeyword
}

case class AbstractKeyword() extends Modifier {
val kind: SyntaxKind = SyntaxKind.AbstractKeyword
}

case class AnyKeyword() extends Type {
val kind: SyntaxKind = SyntaxKind.AnyKeyword
}

case class BooleanKeyword() extends Type {
val kind: SyntaxKind = SyntaxKind.BooleanKeyword
}

case class NumberKeyword() extends Type {
val kind: SyntaxKind = SyntaxKind.NumberKeyword
}

case class StringKeyword() extends Type {
val kind: SyntaxKind = SyntaxKind.StringKeyword
}

14 changes: 14 additions & 0 deletions src/main/scala/java2typescript/ast/literal.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package java2typescript.ast

trait Literal extends Expression

case class NumericLiteral(text: String) extends Literal {
val kind: SyntaxKind = SyntaxKind.NumericLiteral
}

case class StringLiteral(
text: String
) extends Literal {
val kind: SyntaxKind = SyntaxKind.StringLiteral
val hasExtendedUnicodeEscape = false
}
38 changes: 38 additions & 0 deletions src/main/scala/java2typescript/ast/member.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package java2typescript.ast

trait Member extends Node

case class PropertyDeclaration(
name: Identifier,
`type`: Option[Type],
initializer: Option[Expression],
modifiers: List[Modifier] = List()
) extends Member {
val kind: SyntaxKind = SyntaxKind.PropertyDeclaration
}

case class MethodDeclaration(
name: Identifier,
`type`: Option[Type],
parameters: List[Parameter] = List(),
typeParameters: List[Type] = List(),
body: Option[Block] = None,
modifiers: List[Modifier] = List(),
) extends Member {
val kind: SyntaxKind = SyntaxKind.MethodDeclaration
}

case class Constructor(
parameters: List[Parameter] = List(),
body: Option[Block] = None,
modifiers: List[Modifier] = List(),
) extends Member {
val kind: SyntaxKind = SyntaxKind.Constructor
override val flags: Int = 66048
}

case class EnumMember(
name: Identifier
) extends Member {
val kind: SyntaxKind = SyntaxKind.EnumMember
}
101 changes: 101 additions & 0 deletions src/main/scala/java2typescript/ast/statement.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package java2typescript.ast

trait Statement extends Node

case class Block(
statements: List[Statement]
) extends Statement {
val kind: SyntaxKind = SyntaxKind.Block
val multiLine: Boolean = true
}

case class VariableStatement(
declarationList: VariableDeclarationList
) extends Statement {
val kind: SyntaxKind = SyntaxKind.VariableStatement
}

case class ExpressionStatement(
expression: Expression
) extends Statement {
val kind: SyntaxKind = SyntaxKind.ExpressionStatement
}

case class IfStatement(
expression: Expression,
thenStatement: Statement,
elseStatement: Option[Statement] = None
) extends Statement {
val kind: SyntaxKind = SyntaxKind.IfStatement
}

case class DoStatement(
statement: Statement,
expression: Expression
) extends Statement {
val kind: SyntaxKind = SyntaxKind.DoStatement
}

case class WhileStatement(
expression: Expression,
statement: Statement
) extends Statement {
val kind: SyntaxKind = SyntaxKind.WhileStatement
}

case class ForStatement(
initializer: Option[Expression],
condition: Option[Expression],
incrementor: Option[Expression],
statement: Statement
) extends Statement {
val kind: SyntaxKind = SyntaxKind.ForStatement
}

case class ForOfStatement(
initializer: VariableDeclarationList,
expression: Expression,
statement: Statement
) extends Statement {
val kind: SyntaxKind = SyntaxKind.ForOfStatement
}

case class ContinueStatement() extends Statement {
val kind: SyntaxKind = SyntaxKind.ContinueStatement
}

case class BreakStatement() extends Statement {
val kind: SyntaxKind = SyntaxKind.BreakStatement
}

case class ReturnStatement(
expression: Option[Expression]
) extends Statement {
val kind: SyntaxKind = SyntaxKind.ReturnStatement
}

case class SwitchStatement(
expression: Expression,
caseBlock: CaseBlock
) extends Statement {
val kind: SyntaxKind = SyntaxKind.SwitchStatement
}

case class ThrowStatement(
expression: Expression
) extends Statement {
val kind: SyntaxKind = SyntaxKind.ThrowStatement
}

case class TryStatement(
tryBlock: Block,
catchClause: Option[CatchClause],
finallyBlock: Option[Block]
) extends Statement {
val kind: SyntaxKind = SyntaxKind.TryStatement
}

case class EmptyStatement() extends Statement {
val kind: SyntaxKind = SyntaxKind.EmptyStatement
}

Loading

0 comments on commit 788497c

Please sign in to comment.