Skip to content

Commit

Permalink
fix issues - simplify any_char, rename list to list_concat and add list
Browse files Browse the repository at this point in the history
  • Loading branch information
tttardigrado committed Sep 6, 2024
1 parent f6bdbf7 commit 523338f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
23 changes: 10 additions & 13 deletions src/party.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,7 @@ pub fn satisfy(when pred: fn(String) -> Bool) -> Parser(String, e) {

/// Parse a single character.
pub fn any_char() -> Parser(String, e) {
Parser(fn(source, pos) {
let assert Position(row, col) = pos
case source {
[h, ..t] ->
case h {
"\n" -> Ok(#(h, t, Position(row + 1, 0)))
_ -> Ok(#(h, t, Position(row, col + 1)))
}
[] -> Error(Unexpected(pos, "EOF"))
}
})
satisfy(fn(_) { True })
}

/// Parse a lowercase letter.
Expand Down Expand Up @@ -138,9 +128,16 @@ pub fn between(
return(x)
}

/// Parse a line of characters as a String. The new line at the end is discarded
pub fn line() -> Parser(String, e) {
/// 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)
}

Expand Down
15 changes: 11 additions & 4 deletions test/party_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,17 @@ pub fn between_test() {
}

pub fn line_test() {
party.go(party.line(), "abcdefg hij klmnop \n")
|> should.equal(Ok("abcdefg hij klmnop "))
party.go(party.line(), "abcdefg hij klmnop ")
|> should.equal(Error(party.Unexpected(party.Position(1, 20), "EOF")))
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() {
Expand Down

0 comments on commit 523338f

Please sign in to comment.