Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wish example parsing CSV #920

Open
frehberg opened this issue Jan 2, 2022 · 0 comments
Open

Wish example parsing CSV #920

frehberg opened this issue Jan 2, 2022 · 0 comments

Comments

@frehberg
Copy link
Contributor

frehberg commented Jan 2, 2022

Hi,

I would like to use gluon to parse CSV files. I am stuck implementing a CSV parser myself. I would appreciate to find an example in github repo, a parser for CSV strings, maybe covering quoted elements as well.

I did a first draft, but I am stuck. The following simple parser is just parsing the first line. Any idea what combinator is missing to consume the newline and to parse the second line?

let io @ { ? } = import! std.io
let prelude = import! std.prelude
let { Result } = import! std.result
let { (*>), (<*), wrap } = import! std.applicative
let list @ { List } = import! std.list
let csv = "11,12,13\n21,22,23"

let parse : String -> Result String (List (List String)) =
  let { (<|>) } = import! std.alternative
  let parser @ {
        spaces,
        token,
        digit,
        skip_many1,
        recognize,
        sep_by1,
        (<?>),
        ? } = import! std.parser
  let int = import! std.int
  let lex x = x <* spaces
  let literal =
        do i = lex (recognize (skip_many1 digit))
        wrap i
  let record = sep_by1 literal (token ',')
  let records = sep_by1 record (token '\n')
  let parse = parser.parse (records <* spaces)
  parse

let result = parse csv
result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant