Kotlin codebase

Tags

  • eng

An exercise in exploring the Kotlin codebase.

Kotlin history

Search commits for “initial” and sort by oldest, or just go to last page of the repo. Possibly also useful is JetBrains’ issue tracker starting from earliest Kotlin issues. Apparently Kotlin was initially named JetL (presumably JetBrains Language?). Though it’s been renamed fairly quickly.

According to Wikipedia, Kotlin was publicly announced in July 2011. Let’s find commits around that time.

Compiler, lexer, grammar and JetFile

1st commit shows early language prototypes. The project started with an initial grammar definition (formatted as ANTLR4) which auto-generates a lexer. Cf. Flex and Bison basics and simple Flex and Bison examples to find out more about syntax. The spec has since been extracted to a separate repo (2019 removal commit and link to other grammar updates).

Elsewhere, they define Program Structure Interface (PSI; GitHub API, GitHub Impl). JetFile is a collection of imports and declarations. Standard visitor pattern based on PSI. JetElement similarly extends ASTWrapperPsiElement. The entire tree structure is defined in jet/lang/psi.

SemanticWhitespaceAwarePsiBuilder extends PsiBuilderAdapter implements the PsiBuilder interface, used by the state-machine implementation in JetParsing. The whitespace-awareness comes into play because Kotlin has an optional ; termination.

Structure remained mostly similar to this day, apart from renaming to KtElementImpl with the interface definition in Kotlin.

Introduction of project compilation from IDEA.

Some interesting commits