https://github.com/mgadda/swift-parse
A small parser combinator library written in Swift 5
https://github.com/mgadda/swift-parse
combinators parse parser swift
Last synced: 7 months ago
JSON representation
A small parser combinator library written in Swift 5
- Host: GitHub
- URL: https://github.com/mgadda/swift-parse
- Owner: mgadda
- License: mit
- Created: 2016-09-27T05:45:25.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2024-04-14T14:39:56.000Z (almost 2 years ago)
- Last Synced: 2025-06-21T16:43:48.351Z (8 months ago)
- Topics: combinators, parse, parser, swift
- Language: Swift
- Homepage:
- Size: 101 KB
- Stars: 4
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Overview
SwiftParse is a parser combinator library. This project is still iterating rapidly and is not stable.
[](https://travis-ci.org/mgadda/swift-parse)
## Example
Let's build a parser for the brainfuck language. It's a terrible language but
makes for a nice example of how to use SwiftParse.
```swift
import SwiftParse
enum Instruction {
case incPointer, decPointer, incByte, decByte, writeByte, readByte
case loop([Instruction])
}
typealias LoopParser = StandardParser
struct BrainfuckParser {
static let incPointer = match(">") ^^ { _ in Instruction.incPointer }
static let decPointer = match("<") ^^ { _ in Instruction.decPointer }
static let incByte = match("+") ^^ { _ in Instruction.incByte }
static let decByte = match("-") ^^ { _ in Instruction.decByte }
static let writeByte = match(".") ^^ { _ in Instruction.writeByte }
static let readByte = match(",") ^^ { _ in Instruction.readByte }
static let pointerOps = incPointer | decPointer
static let byteOps = incByte | decByte
static let ioOps = writeByte | readByte
static let operation = pointerOps | byteOps | ioOps | loop()
// loop must defined as a static function because it references
// operation which in turn references loop.
static let loop: () -> LoopParser = {
loopStart ~ operation* ~ loopEnd ^^ { Instruction.loop($0.0.1) }
}
static let loopStart = match("[")
static let loopEnd = match("]")
static let program = operation+
}
```
Now let's execute it and see what it parsed:
```swift
let result = BrainfuckParser.program(AnyCollection("+[>,]"))
let parseTree = try! result.get().value
```
This generates a parse tree of:
```swift
[
SwiftParse.Brainfuck.incByte,
SwiftParse.Brainfuck.loop([
SwiftParse.Brainfuck.incPointer,
SwiftParse.Brainfuck.readByte
])
]
```
And no unparsed characters left over. Neat!
For more complete examples check out https://github.com/mgadda/swift-parse-examples