Kotlin parser library with an easy-to-use DSL

Overview

Pratt

Library for parsing expressions and a beautiful Kotlin DSL

Just define your operators and operands with the Kotlin DSL and the parser is ready!

Dawn and trees

Photographer: EmirVildanov

Example

We will parse simple mathematical expressions that contain

  • Operands: lowercase latin letters 'a'..'z'
  • Prefix unary operators: + and -
  • Infix binary operators: +, -, '*' and right associative power operators ^
  • Postfix unary operator: factorial !
  • Parenthesis: ( and )
enum class TokenType {
    ID, PLUS, MULTIPLY, MINUS, POWER, BANG, LPAREN, RPAREN
}

fun getTokenType(char: Char) = when (char) {
    in 'a'..'z' -> ID
    '+' -> PLUS
    '*' -> MULTIPLY
    '-' -> MINUS
    '^' -> POWER
    '!' -> BANG
    '(' -> LPAREN
    ')' -> RPAREN
    else -> error("Unknown token")
}

val parser = pratt<Char, TokenType> {
    getTokenType = ::getTokenType

    atomic(ID)

    grouping(LPAREN to RPAREN)
    // prefix
    prefix(PLUS) precedence 3
    prefix(MINUS) precedence 3
    // infix
    infix(PLUS) precedence 1
    infix(MINUS) precedence 1
    infix(MULTIPLY) precedence 2
    infix(POWER) precedence 4 associativity RIGHT
    // postfix
    postfix(BANG) precedence 5
}

fun main() {
    // builds AST
    val tree = parser.parse("-(a+b)*c!".asIterable())
    println(tree)
}

Use as a dependency

  • Go to the Releases page and find out the latest version
  • Then add pratt as a Jitpack dependency
repositories {
    maven(url = "https://jitpack.io")
}

dependencies {
    implementation("com.github.Furetur:pratt:<latest version>")
}
You might also like...
Nice and simple DSL for Espresso in Kotlin
Nice and simple DSL for Espresso in Kotlin

Kakao Nice and simple DSL for Espresso in Kotlin Introduction At Agoda, we have more than 1000 automated tests to ensure our application's quality and

DSL for constructing the drawables in Kotlin instead of in XML

Android Drawable Kotlin DSL DSL for constructing the drawables in Kotlin instead of in XML Examples Shape drawables ?xml version="1.0" encoding="utf-

Code generation of Kotlin DSL for AWS CDK

Code generation of Kotlin DSL for AWS CDK

Regular expression DSL on Kotlin

Examples Characters Construct Equivalent Matches x character(Char) The character x \\ character('\\') The backslash character \0n octal(OctalValue(7))

Kotlin Object Notation - Lightweight DSL to build fluid JSON trees

Kotlin Object Notation Lightweight kotlin MPP DSL for building JSON trees Setup Just drop the dependency in your commonMain sourceSet kotlin { sourc

Kotlin DSL for Junit5

Kupiter is Kotlin DSL for Junit5. Current API is only for dynamic tests. Get it repositories { maven { url 'https://jitpack.io' } } dependencies

Kotlin-dsl-sample - Preferences project on android

kotlin-dsl-example Sample preferences project on android. How to use val

GitHub Actions Kotlin DSL

GitHub Actions Kotlin DSL Work in progress! The goal is to be able to describe GH Actions in Kotlin with all its perks, like: workflow( name = "Te

Kotlin DSL inspired by bhailang.js

Kotlin DSL inspired by bhailang.js

Releases(0.1.2)
Owner
furetur
telegram @furetur
furetur
A TOML 1.0 parser library for Kotlin

4koma A small, stand-alone, easy to use TOML parser library for Kotlin. 4koma supports an array of convenient features, such as full TOML 1.0 complian

Anton Ekblad 53 Dec 12, 2022
A simple, classic Kotlin MVI implementation based on coroutines with Android support, clean DSL and easy to understand logic

A simple, classic Kotlin MVI implementation based on coroutines with Android support, clean DSL and easy to understand logic

Nek.12 4 Oct 31, 2022
A small DSL to make building a conversation in Bukkit easy.

Konversation Konversation provides a simple builder to construct chains of prompts to be used in the conversation API present in Bukkit. Bukkit only p

Tim Hagemann 2 Dec 4, 2022
A Kotlin DSL wrapper around the mikepenz/MaterialDrawer library.

MaterialDrawerKt Create navigation drawers in your Activities and Fragments without having to write any XML, in pure Kotlin code, with access to all t

Márton Braun 517 Nov 19, 2022
The most complete and powerful data-binding library and persistence infra for Kotlin 1.3, Android & Splitties Views DSL, JavaFX & TornadoFX, JSON, JDBC & SQLite, SharedPreferences.

Lychee (ex. reactive-properties) Lychee is a library to rule all the data. ToC Approach to declaring data Properties Other data-binding libraries Prop

Mike 112 Dec 9, 2022
Lightweight Kotlin DSL dependency injection library

Warehouse DSL Warehouse is a lightweight Kotlin DSL dependency injection library this library has an extremely faster learning curve and more human fr

Osama Raddad 18 Jul 17, 2022
A light weight Compose Animation library to choreograph low level Animation API through Kotlin DSL.

Koreography Choreograph your Compose Animation ?? ?? A lightweight Compose Animation utility library to choreograph low-level Animation API (https://d

Sagar Viradiya 107 Jan 8, 2023
Android + Kotlin + Github Actions + ktlint + Detekt + Gradle Kotlin DSL + buildSrc = ❤️

kotlin-android-template ?? A simple Github template that lets you create an Android/Kotlin project and be up and running in a few seconds. This templa

Nicola Corti 1.5k Jan 3, 2023
{ } Declarative Kotlin DSL for choreographing Android transitions

Transition X Kotlin DSL for choreographing Android Transitions TransitionManager makes it easy to animate simple changes to layout without needing to

Arunkumar 520 Dec 16, 2022
Kotlin Dsl for Android RecyclerView

KRecyclerDsl Kotlin Dsl for Android RecyclerView Exemple Sample project recyclerView.adapter = dataClassAdapter<MyView, MyDataClass>(R.layout.my_view,

Thomas Girard 14 Mar 31, 2019