Skip to content

Parser combinator library written entirely in Kotlin

License

Notifications You must be signed in to change notification settings

vorpal-research/kparsec

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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