mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-30 15:21:12 +00:00
Usage of the CSV parser in the example
This commit is contained in:
parent
1300a56918
commit
7dfbfcba29
2 changed files with 28 additions and 8 deletions
|
@ -1,5 +1,8 @@
|
|||
interface Parser.CSV
|
||||
exposes [
|
||||
CSV,
|
||||
file,
|
||||
record
|
||||
]
|
||||
imports [
|
||||
Parser.Core.{Parser, fail, const, alt, map, map2, apply, many, oneorMore, sepBy1, between, ignore},
|
||||
|
@ -16,10 +19,19 @@ CSVField : RawStr
|
|||
CSVRecord : List CSVField
|
||||
CSV : List CSVRecord
|
||||
|
||||
file : Parser RawStr CSV
|
||||
file = many recordNewline
|
||||
|
||||
recordNewline : Parser RawStr CSVRecord
|
||||
recordNewline = map2 record crlf (\rec, _ -> rec)
|
||||
|
||||
record : Parser RawStr CSVRecord
|
||||
record = sepBy1 field comma
|
||||
|
||||
field : Parser RawStr CSVField
|
||||
field = alt escapedField nonescapedField
|
||||
|
||||
escapedField : Parser RawStr CSVField
|
||||
escapedField = between (many escapedContents) dquote dquote
|
||||
escapedContents = oneOf [
|
||||
twodquotes |> map (\_ -> 34), # An escaped double quote
|
||||
|
@ -29,6 +41,8 @@ escapedContents = oneOf [
|
|||
textdata
|
||||
]
|
||||
twodquotes = string "\"\""
|
||||
|
||||
nonescapedField : Parser RawStr CSVField
|
||||
nonescapedField = many textdata
|
||||
comma = codepoint 44 # ','
|
||||
cr = codepoint 13 # '\r'
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue