Skip to content

Commit

Permalink
update version to 1.0.5 (see changelog) and fix merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanBrewer317 committed Sep 12, 2024
2 parents 5a98957 + 5f28f33 commit 46b617c
Show file tree
Hide file tree
Showing 5 changed files with 85 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.5] - 2024-09-11

### Added

- The `between` combinator, the `line` and `line_concat` combinators, and the `any_char` combinator. Thank you [Gonçalo Teixeira](https://github.com/tttardigrado)!!

## [1.0.4] - 2024-08-10

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

licences = ["MPL-2.0"]
Expand All @@ -11,4 +11,4 @@ gleam = ">= 0.32.0"
gleam_stdlib = "~> 0.36" # 0.36 <= version < 0.37

[dev-dependencies]
gleeunit = "~> 0.10"
gleeunit = ">= 0.10.0"
4 changes: 2 additions & 2 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

packages = [
{ name = "gleam_stdlib", version = "0.36.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "C0D14D807FEC6F8A08A7C9EF8DFDE6AE5C10E40E21325B2B29365965D82EB3D4" },
{ name = "gleeunit", version = "0.11.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "1397E5C4AC4108769EE979939AC39BF7870659C5AFB714630DEEEE16B8272AD5" },
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
]

[requirements]
gleam_stdlib = { version = "~> 0.36" }
gleeunit = { version = "~> 0.10" }
gleeunit = { version = ">= 0.10.0" }
31 changes: 31 additions & 0 deletions src/party.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ pub fn satisfy(when pred: fn(String) -> Bool) -> Parser(String, e) {
})
}

/// Parse a single character.
pub fn any_char() -> Parser(String, e) {
satisfy(fn(_) { True })
}

/// Parse a lowercase letter.
pub fn lowercase_letter() -> Parser(String, e) {
satisfy(when: fn(c) { string.contains("abcdefghijklmnopqrstuvwxyz", c) })
Expand Down Expand Up @@ -111,6 +116,32 @@ pub fn either(p: Parser(a, e), q: Parser(a, e)) -> Parser(a, e) {
Parser(fn(source, pos) { result.or(run(p, source, pos), run(q, source, pos)) })
}

/// Parse `open`, followed by `p` and `close`. Returns the value returned by `p`.
/// The values returned by `open` and `close` are discarded.
pub fn between(
open: Parser(_, e),
p: Parser(a, e),
close: Parser(_, e),
) -> Parser(a, e) {
use _ <- do(open)
use x <- do(p)
use _ <- do(close)
return(x)
}

/// Parse the rest of a line and return the array of parsed characters.
/// The newline character at the end is discarded.
pub fn line() -> Parser(List(String), e) {
until(any_char(), char("\n"))
}

/// Parse the rest of a line and return the parsed characters as a String.
/// The newline character at the end is discarded.
pub fn line_concat() -> Parser(String, e) {
line()
|> map(string.concat)
}

/// Parse with the first parser in the list that doesn't fail.
pub fn choice(ps: List(Parser(a, e))) -> Parser(a, e) {
Parser(fn(source, pos) {
Expand Down
44 changes: 44 additions & 0 deletions test/party_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,50 @@ pub fn satisfy_test() {
|> should.equal(Error(party.Unexpected(party.Position(1, 1), "a")))
}

pub fn any_char_test() {
party.go(party.any_char(), "a")
|> should.equal(Ok("a"))
party.go(party.any_char(), "")
|> should.equal(Error(party.Unexpected(party.Position(1, 1), "EOF")))
}

pub fn between_test() {
party.go(
party.between(party.char("{"), party.char("a"), party.char("}")),
"{a}",
)
|> should.equal(Ok("a"))
party.go(
party.between(party.char("{"), party.char("a"), party.char("}")),
"(a}",
)
|> should.equal(Error(party.Unexpected(party.Position(1, 1), "(")))
party.go(
party.between(party.char("{"), party.char("a"), party.char("}")),
"{b}",
)
|> should.equal(Error(party.Unexpected(party.Position(1, 2), "b")))
party.go(
party.between(party.char("{"), party.char("a"), party.char("}")),
"{a)",
)
|> should.equal(Error(party.Unexpected(party.Position(1, 3), ")")))
}

pub fn line_test() {
party.go(party.line(), "abcde fgh \n")
|> should.equal(Ok(["a", "b", "c", "d", "e", " ", "f", "g", "h", " "]))
party.go(party.line(), "abcde fgh ")
|> should.equal(Error(party.Unexpected(party.Position(1, 11), "EOF")))
}

pub fn line_concat_test() {
party.go(party.line_concat(), "abcde fgh \n")
|> should.equal(Ok("abcde fgh "))
party.go(party.line_concat(), "abcde fgh ")
|> should.equal(Error(party.Unexpected(party.Position(1, 11), "EOF")))
}

pub fn lowercase_letter_test() {
party.go(party.lowercase_letter(), "a")
|> should.equal(Ok("a"))
Expand Down

0 comments on commit 46b617c

Please sign in to comment.