Skip to content

Instantly share code, notes, and snippets.

@NaeosPsy

NaeosPsy/csvparser.ex Secret

Created Apr 6, 2021
Embed
What would you like to do?
defmodule CSVParser do
import NimbleParsec
quoted_character =
choice([
string("\"\""),
utf8_string([not: 34], 1)
])
regular_value = utf8_string([not: ?\r, not: ?\n, not: ?,], min: 0)
escaped_value =
ignore(string("\""))
|> repeat(quoted_character)
|> ignore(string("\""))
|> reduce({Enum, :join, [""]})
value =
choice([
escaped_value,
regular_value
])
eol =
choice([
string("\r\n"),
string("\n")
])
line =
value
|> repeat(ignore(string(",")) |> concat(value))
|> ignore(eol)
defparsec :file, line |> wrap() |> repeat()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment