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

Parser fails on “foo.)”? #895

Open
JonasWanke opened this issue Jan 18, 2024 · 0 comments
Open

Parser fails on “foo.)”? #895

JonasWanke opened this issue Jan 18, 2024 · 0 comments
Labels
good first issue Good for newcomers P: Compiler: Frontend Package: The compiler frontend T: Fix Type: Bug Fixes

Comments

@JonasWanke
Copy link
Member

Describe the bug

The parser couldn't parse this rest.

[bool, checkEquals, ifElse, int, list, recursive, text] = use "Core"
[parser] = use "Parser"

decode csv :=
  needs (text.is csv)
  recursive [Lines: (,), Fields: (,), FieldStartOffset: 0, P: parser.new csv] {
    recurse [lines, fields, fieldStartOffset, p] ->
    Parser (Cursor [source, Offset: oldOffset]) = p
    p | parser.next %
      Ok [Parser: p, character] ->
        Parser c = p
        Cursor [Offset: newOffset] = c
        character %
          "," ->
            recurse [
              lines,
              Fields: fields | list.append (source | text.getRange fieldStartOffset oldOffset),
              FieldStartOffset: newOffset,
              p,
            ]
          "
            
          
          " ->
            fields = fields | list.append (source | text.getRange fieldStartOffset oldOffset)
            lines = lines | list.append fields
            ifElse (c | cursor.)
            recurse [
              Lines: lines | list.append fields,
              Fields: (,),
              FieldStartOffset: newOffset,
              p,
            ]
          # TODO(JonasWanke): handle quoted field
          _ -> recurse [lines, fields, fieldStartOffset, p]
      Error Empty ->
        finalField = source | text.getRange fieldStartOffset oldOffset
        Ok
          ifElse
            finalField | text.isEmpty | bool.lazyAnd { fields | list.isEmpty }
            { lines }
            { lines | list.append (fields | list.append finalField) }
  }

test =
  checkEquals (decode "") (Ok (,))
  checkEquals (decode "{text.newline}") (Ok (,))

  checkEquals (decode "aaa") (Ok (("aaa",),))
  checkEquals (decode "aaa{text.newline}") (Ok (("aaa",),))

testRfcExamples =
  # From https://datatracker.ietf.org/doc/html/rfc4180#section-2
  checkEquals (decode "aaa,bbb,ccc") (Ok (("aaa", "bbb", "ccc"),))

  checkEquals
    decode "aaa,bbb,ccc{text.newline}zzz,yyy,xxx{text.newline}"
    Ok (("aaa", "bbb", "ccc"), ("zzz", "yyy", "xxx"))
  checkEquals
    decode "
      field_name,field_name,field_name{text.newline}aaa,bbb,ccc{text.newline}zzz,yyy,xxx{text.newline}
    "
    Ok (("field_name", "field_name", "field_name"), ("aaa", "bbb", "ccc"), ("zzz", "yyy", "xxx"))
  # TODO(JonasWanke): handle quoted field
  # checkEquals
  #   decode '"aaa,"b{{text.newline}}bb",ccc{{text.newline}}zzz,yyy,xxx{{text.newline}}"'
  #   Ok (("aaa", "b{text.newline}bb", "ccc"), ("zzz", "yyy", "xxx"))
  # checkEquals (decode '"aaa,"b""bb",ccc{{text.newline}}"') (Ok (("aaa", '"b"bb"', "ccc"),))
@JonasWanke JonasWanke added P: Compiler: Frontend Package: The compiler frontend T: Fix Type: Bug Fixes labels Jan 18, 2024
@JonasWanke JonasWanke added the good first issue Good for newcomers label Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers P: Compiler: Frontend Package: The compiler frontend T: Fix Type: Bug Fixes
Projects
Status: No status
Development

No branches or pull requests

1 participant