Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/turbolent/parsercombinators

A parser-combinator library for Swift
https://github.com/turbolent/parsercombinators

parser parser-combinators parsing swift

Last synced: 27 days ago
JSON representation

A parser-combinator library for Swift

Awesome Lists containing this project

README

        

[![Build Status](https://travis-ci.org/turbolent/ParserCombinators.svg?branch=master)](https://travis-ci.org/turbolent/ParserCombinators)

# ParserCombinators

A *parser-combinator* library for Swift.

Parsers are simply functions that accept any kind of input, such as strings or custom data structures, and return an output.

Parser combinators are higher-order functions which allow the composition of parsers to create more expressive parsers.

## Examples

For example, a simple calculator with the grammar in BNF can be implemented as follows:

```
::= |
::= |
::= '(' ')' |
::= '0' | '1' | ...
::= |
::= '+' | '-'
::= '*' | '/'
```

```swift

import ParserCombinators
import ParserCombinatorOperators

typealias Op = (Int, Int) -> Int
typealias OpParser = Parser

let addOp: OpParser =
char("+") ^^^ (+) ||
char("-") ^^^ (-)

let mulOp: OpParser =
char("*") ^^^ (*) ||
char("/") ^^^ (/)

let digit = `in`(.decimalDigits, kind: "digit")
let num = digit.rep(min: 1) ^^ { Int(String($0))! }

let expr: Parser =
Parser.recursive { expr in
let factor = (char("(") ~> expr) <~ char(")")
|| num

let term = factor.chainLeft(
separator: mulOp,
min: 1
).map { $0 ?? 0 }

return term.chainLeft(
separator: addOp,
min: 1
).map { $0 ?? 0 }
}

let r = CollectionReader(collection: "(23+42)*3")

guard case .success(let value, _) = expr.parse(r) else {
fatalError()
}

assert(value == 195)
```