Skip to content

Instantly share code, notes, and snippets.

@NaeosPsy
Created April 6, 2021 06:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NaeosPsy/56b23fe0f9c8fa1c66144547759eeb69 to your computer and use it in GitHub Desktop.
Save NaeosPsy/56b23fe0f9c8fa1c66144547759eeb69 to your computer and use it in GitHub Desktop.
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