From 523338f719809fb92c4e53af5003d505bedd9793 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20Teixeira?= Date: Fri, 6 Sep 2024 23:34:30 +0100 Subject: [PATCH] fix issues - simplify any_char, rename list to list_concat and add list --- src/party.gleam | 23 ++++++++++------------- test/party_test.gleam | 15 +++++++++++---- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/party.gleam b/src/party.gleam index f8175e0..ce9b752 100644 --- a/src/party.gleam +++ b/src/party.gleam @@ -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. @@ -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) } diff --git a/test/party_test.gleam b/test/party_test.gleam index 408361c..4ba0a94 100644 --- a/test/party_test.gleam +++ b/test/party_test.gleam @@ -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() {