Skip to content

Latest commit

 

History

History
31 lines (23 loc) · 1.37 KB

README.md

File metadata and controls

31 lines (23 loc) · 1.37 KB

Download Build Status

kparsec

Parser combinator library written entirely in Kotlin. Inspired by parsec, jparsec and scala parser combinators.

Example

Why not a full-fledged JSON parser for an example?

object SimpleJSONParser: StringsAsParsers {
    val string = Literals.JSTRING
    val number = Literals.FLOAT
    val boolean = Literals.BOOLEAN
    val nully = (+"null").map { null }

    val arrayElements = defer { element } joinedBy -',' orElse emptyList()
    val array = -'[' + arrayElements + -']'

    val entry_ = string + -':' + defer { element }
    val entry = entry_.map { (a, b) -> a to b }

    val objectElements = entry joinedBy -',' orElse emptyList()
    val obj = -'{' + objectElements + -'}' 
    
    val element: Parser<Char, Any?> = nully or string or number or boolean or array or obj
    val whole = element + eof()
}

See this and other examples here