Skip to content

Commit

Permalink
add stateful combinators
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanBrewer317 committed Jul 31, 2024
1 parent 6b95f79 commit dde78b5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.3] - 2024-07-31

### Added

- The `stateful_many` and `stateful_many1` combinators.

## [1.0.2] - 2024-04-05

### Added
Expand Down
2 changes: 1 addition & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name = "party"
version = "1.0.2"
version = "1.0.3"
description = "A little parser combinator library in Gleam."

licences = ["MPL-2.0"]
Expand Down
32 changes: 32 additions & 0 deletions src/party.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,35 @@ pub fn until(
},
)
}

/// A `many` parser that also gets to update some state with each success
pub fn stateful_many(state: s, p: Parser(fn(s)->#(a, s), e)) -> Parser(#(List(a), s), e) {
Parser(fn(source, pos) {
case run(p, source, pos) {
Error(_) -> Ok(#(#([], state), source, pos))
Ok(#(f, r, pos2)) -> {
let #(x, s) = f(state)
result.map(run(stateful_many(s, p), r, pos2), fn(res) {
let #(#(rest, s2), r2, pos3) = res
#(#([x, ..rest], s2), r2, pos3)
})
}
}
})
}

/// A `many1` parser that also gets to update some state with each success
pub fn stateful_many1(state: s, p: Parser(fn(s)->#(a, s), e)) -> Parser(#(List(a), s), e) {
Parser(fn(source, pos) {
case run(p, source, pos) {
Error(e) -> Error(e)
Ok(#(f, r, pos2)) -> {
let #(x, s) = f(state)
result.map(run(stateful_many(s, p), r, pos2), fn(res) {
let #(#(rest, s), r2, pos3) = res
#(#([x, ..rest], s), r2, pos3)
})
}
}
})
}
17 changes: 14 additions & 3 deletions test/party_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

import gleeunit
import gleeunit/should
import party
import gleam/int
import gleam/list
import gleam/string
import gleeunit
import gleeunit/should
import party

pub fn main() {
gleeunit.main()
Expand Down Expand Up @@ -298,3 +298,14 @@ pub fn multiline_comment_test() {
)
|> should.equal(Ok("hello! * "))
}

pub fn stateful_many_test() {
let stateful_char = fn(c) {
party.char(c)
|> party.map(fn(x) { fn(counter) { #(x, counter /. 2.0) } })
}
party.go(party.stateful_many(100.0, stateful_char("a")), "aaab")
|> should.equal(Ok(#(["a", "a", "a"], 12.5)))
party.go(party.stateful_many(100.0, stateful_char("b")), "aaab")
|> should.equal(Ok(#([], 100.0)))
}

0 comments on commit dde78b5

Please sign in to comment.