๐Ÿ“ฆ AzimMuradov / kpeg

Kotlin PEG parser with Kotlin DSL

โ˜… 7 stars โ‘‚ 0 forks ๐Ÿ‘ 7 watching โš–๏ธ Apache License 2.0
kotlin-dslparserpeg
๐Ÿ“ฅ Clone https://github.com/AzimMuradov/kpeg.git
HTTPS git clone https://github.com/AzimMuradov/kpeg.git
SSH git clone git@github.com:AzimMuradov/kpeg.git
CLI gh repo clone AzimMuradov/kpeg
Azim Muradov Azim Muradov Rename `lazyRule` to `rule` 4d05b7f 4 years ago ๐Ÿ“ History
๐Ÿ“‚ master View all commits โ†’
๐Ÿ“ .github
๐Ÿ“ buildSrc
๐Ÿ“ docs
๐Ÿ“ examples
๐Ÿ“ gradle
๐Ÿ“ lib
๐Ÿ“„ .gitattributes
๐Ÿ“„ .gitignore
๐Ÿ“„ build.gradle.kts
๐Ÿ“„ gradlew
๐Ÿ“„ gradlew.bat
๐Ÿ“„ LICENSE
๐Ÿ“„ README.md
๐Ÿ“„ README.md

logo.png

Sonatype Nexus (Releases) Sonatype Nexus (Snapshots) GitHub Actions - Build project License

Welcome to the Kotlin PEG parser with Kotlin DSL!

The project is inspired by the pest parser and the kotlin-peg-dsl project.

Quick start

Simple example

val num = Symbol.rule<Int>(name = "Num", ignoreWS = false) {
    seq {
        val sign = +char('+', '-').orDefault('+')
        val digits = +DIGIT.oneOrMore().joinToString()

        value { (sign.get + digits.get).toInt() }
    }
}

val sum = Symbol.rule<Int>(name = "Sum") {
    num.list(separator = char('+'), min = 1u).mapPe { it.sum() }
}


fun evalExpr(expression: String) =
    PegParser.parse(symbol = sum.value(), expression).getOrElse { null }

val results = listOf(
    evalExpr("1"),                         // 1
    evalExpr("+1"),                        // 1
    evalExpr("+ 1"),                       // null
    evalExpr("+1 +"),                      // null
    evalExpr("-17"),                       // -17
    evalExpr("-1 7"),                      // null
    evalExpr("1+2+3+4+5"),                 // 15
    evalExpr("1 + +2 + -3 + +4 + 5"),      // 9
    evalExpr("definitely not expression"), // null
    evalExpr(""),                          // null
)

for (res in results) {
    println(res)
}

Resources

Installation guide

Gradle

Kotlin DSL

dependencies {
    implementation("io.kpeg:kpeg:0.1.2")
}

Groovy DSL

dependencies {
    implementation 'io.kpeg:kpeg:0.1.2'
}

Maven

<dependency>
    <groupId>io.kpeg</groupId>
    <artifactId>kpeg</artifactId>
    <version>0.1.2</version>
</dependency>

Things to improve before stable:

  • add more docs
  • add more tests, improve the code coverage
  • add more built-in characters
  • add ability to define Comment parsing expression
  • better error messages and error handling
  • support left recursion
  • provide more examples
  • multiplatform
Suggestions are welcome!

License

kpeg is released under the Apache 2.0 license.

Copyright 2021-2021 Azim Muradov

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.