Skip to content

Commit

Permalink
fix comma bug
Browse files Browse the repository at this point in the history
  • Loading branch information
bcpeinhardt committed Jan 6, 2024
1 parent 5511115 commit 7e612eb
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 23 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

## v1.2.1 - 5 January 2024
- Fix bug with double commas producing error, now produce empty string

## v1.2.0 - 29 December 2023
- Add `to_lists_or_error` function

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 = "gsv"
version = "1.2.0"
version = "1.2.1"
gleam = ">= 0.32.0"
description = "A simple csv parser and generator written in gleam "

Expand Down
25 changes: 17 additions & 8 deletions src/gsv.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ pub fn to_lists_or_panic(input: String) -> List(List(String)) {
Ok(lol) -> lol
Error(ParseError(Location(line, column), msg)) -> {
panic as {
"[" <> "line " <> int.to_string(line) <> " column " <> int.to_string(
column,
) <> "] of csv: " <> msg
"["
<> "line "
<> int.to_string(line)
<> " column "
<> int.to_string(column)
<> "] of csv: "
<> msg
}
[[]]
}
Expand All @@ -48,7 +52,13 @@ pub fn to_lists_or_error(input: String) -> Result(List(List(String)), String) {
|> ast.parse
|> result.map_error(fn(e) {
let ParseError(Location(line, column), msg) = e
"[" <> "line " <> int.to_string(line) <> " column " <> int.to_string(column) <> "] of csv: " <> msg
"["
<> "line "
<> int.to_string(line)
<> " column "
<> int.to_string(column)
<> "] of csv: "
<> msg
})
}

Expand Down Expand Up @@ -85,10 +95,9 @@ pub fn from_lists(

// If the string contains a , \n \r\n or " it needs to be escaped by wrapping in double quotes
case
string.contains(entry, separator) || string.contains(entry, "\n") || string.contains(
entry,
"\"",
)
string.contains(entry, separator)
|| string.contains(entry, "\n")
|| string.contains(entry, "\"")
{
True -> "\"" <> entry <> "\""
False -> entry
Expand Down
22 changes: 15 additions & 7 deletions src/gsv/internal/ast.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ fn parse_p(
"Expected \"\\n\" after \"\\r\", found: " <> token.to_lexeme(tok),
))

// If we just parsed a comma, we're expecting an Escaped or Non-Escaped string
// If we just parsed a comma, we're expecting an Escaped or Non-Escaped string, or another comma
// (indicating an empty string)
[#(Textdata(str), _), ..remaining_tokens], JustParsedComma, [
curr_line,
..previously_parsed_lines
Expand All @@ -109,12 +110,20 @@ fn parse_p(
..previously_parsed_lines
])

[#(Comma, _), ..remaining_tokens], JustParsedComma, [
curr_line,
..previously_parsed_lines
] ->
parse_p(remaining_tokens, JustParsedComma, [
["", ..curr_line],
..previously_parsed_lines
])

[#(tok, loc), ..], JustParsedComma, _ ->
Error(ParseError(
loc,
"Expected escaped or non-escaped string after comma, found: " <> token.to_lexeme(
tok,
),
"Expected escaped or non-escaped string after comma, found: "
<> token.to_lexeme(tok),
))

// If we just parsed a new line, we're expecting an escaped or non-escaped string
Expand All @@ -133,9 +142,8 @@ fn parse_p(
[#(tok, loc), ..], JustParsedNewline, _ ->
Error(ParseError(
loc,
"Expected escaped or non-escaped string after newline, found: " <> token.to_lexeme(
tok,
),
"Expected escaped or non-escaped string after newline, found: "
<> token.to_lexeme(tok),
))

// If we're inside an escaped string, we can take anything until we get a double quote,
Expand Down
12 changes: 5 additions & 7 deletions test/gsv_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,15 @@ pub fn error_cases_test() {
}
}

produce_error("Ben, 25,, TRUE")
produce_error("Ben, 25,\n, TRUE")
|> should.equal(#(
Location(1, 9),
"Expected escaped or non-escaped string after comma, found: ,",
"Expected escaped or non-escaped string after comma, found: \n",
))
produce_error("Austin, 25, FALSE\n\"Ben Peinhardt\", 25,, TRUE")
produce_error("Austin, 25, FALSE\n\"Ben Peinhardt\", 25,\n, TRUE")
|> should.equal(#(
Location(2, 21),
"Expected escaped or non-escaped string after comma, found: ,",
"Expected escaped or non-escaped string after comma, found: \n",
))
}

Expand All @@ -178,7 +178,5 @@ pub fn error_cases_test() {
pub fn totally_errors_test() {
"Ben, 25,, TRUE"
|> gsv.to_lists_or_error
|> should.equal(Error(
"[line 1 column 9] of csv: Expected escaped or non-escaped string after comma, found: ,",
))
|> should.equal(Ok([["Ben", " 25", "", " TRUE"]]))
}

0 comments on commit 7e612eb

Please sign in to comment.