-
-
Save NaeosPsy/56b23fe0f9c8fa1c66144547759eeb69 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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