mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-01 07:41: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
|
interface Parser.CSV
|
||||||
exposes [
|
exposes [
|
||||||
|
CSV,
|
||||||
|
file,
|
||||||
|
record
|
||||||
]
|
]
|
||||||
imports [
|
imports [
|
||||||
Parser.Core.{Parser, fail, const, alt, map, map2, apply, many, oneorMore, sepBy1, between, ignore},
|
Parser.Core.{Parser, fail, const, alt, map, map2, apply, many, oneorMore, sepBy1, between, ignore},
|
||||||
|
@ -16,10 +19,19 @@ CSVField : RawStr
|
||||||
CSVRecord : List CSVField
|
CSVRecord : List CSVField
|
||||||
CSV : List CSVRecord
|
CSV : List CSVRecord
|
||||||
|
|
||||||
|
file : Parser RawStr CSV
|
||||||
file = many recordNewline
|
file = many recordNewline
|
||||||
|
|
||||||
|
recordNewline : Parser RawStr CSVRecord
|
||||||
recordNewline = map2 record crlf (\rec, _ -> rec)
|
recordNewline = map2 record crlf (\rec, _ -> rec)
|
||||||
|
|
||||||
|
record : Parser RawStr CSVRecord
|
||||||
record = sepBy1 field comma
|
record = sepBy1 field comma
|
||||||
|
|
||||||
|
field : Parser RawStr CSVField
|
||||||
field = alt escapedField nonescapedField
|
field = alt escapedField nonescapedField
|
||||||
|
|
||||||
|
escapedField : Parser RawStr CSVField
|
||||||
escapedField = between (many escapedContents) dquote dquote
|
escapedField = between (many escapedContents) dquote dquote
|
||||||
escapedContents = oneOf [
|
escapedContents = oneOf [
|
||||||
twodquotes |> map (\_ -> 34), # An escaped double quote
|
twodquotes |> map (\_ -> 34), # An escaped double quote
|
||||||
|
@ -29,6 +41,8 @@ escapedContents = oneOf [
|
||||||
textdata
|
textdata
|
||||||
]
|
]
|
||||||
twodquotes = string "\"\""
|
twodquotes = string "\"\""
|
||||||
|
|
||||||
|
nonescapedField : Parser RawStr CSVField
|
||||||
nonescapedField = many textdata
|
nonescapedField = many textdata
|
||||||
comma = codepoint 44 # ','
|
comma = codepoint 44 # ','
|
||||||
cr = codepoint 13 # '\r'
|
cr = codepoint 13 # '\r'
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
app "main"
|
app "main"
|
||||||
packages { pf: "platform/main.roc" }
|
packages { pf: "platform/main.roc" }
|
||||||
imports [Parser.Core.{Parser}, Parser.Str.{RawStr}]
|
imports [Parser.Core.{Parser}, Parser.Str.{RawStr}, Parser.CSV.{CSV}]
|
||||||
provides [main] to pf
|
provides [main] to pf
|
||||||
|
|
||||||
# Until issue https://github.com/rtfeldman/roc/issues/3438 is fixed,
|
# Until issue https://github.com/rtfeldman/roc/issues/3438 is fixed,
|
||||||
|
@ -8,12 +8,12 @@ app "main"
|
||||||
# with hard-coded input.
|
# with hard-coded input.
|
||||||
|
|
||||||
main : Str
|
main : Str
|
||||||
main = fullTest myparser "[10,20,30,40,50,60,1234,1337,101010101]"
|
main = fullTest myparser "10,20\r\n30,40\r\n"
|
||||||
|
|
||||||
partialTest = \parser, input ->
|
partialTest = \parser, input ->
|
||||||
when Parser.Str.runPartialStr parser input is
|
when Parser.Str.runPartialStr parser input is
|
||||||
Ok result ->
|
Ok result ->
|
||||||
val = result.val |> Str.joinWith(" -- ")
|
val = result.val |> Str.joinWith("\r\n")
|
||||||
# val = result.val
|
# val = result.val
|
||||||
leftover = result.input
|
leftover = result.input
|
||||||
"Parse success: \(val) (leftover string: \(leftover))\n"
|
"Parse success: \(val) (leftover string: \(leftover))\n"
|
||||||
|
@ -24,7 +24,7 @@ fullTest = \parser, input ->
|
||||||
when Parser.Str.runStr parser input is
|
when Parser.Str.runStr parser input is
|
||||||
Ok result ->
|
Ok result ->
|
||||||
# val = result |> Str.joinWith(", ")
|
# val = result |> Str.joinWith(", ")
|
||||||
val = result |> Str.joinWith(" -- ")
|
val = result |> Str.joinWith("\r\n")
|
||||||
# val = result
|
# val = result
|
||||||
"Parse success: \(val)\n"
|
"Parse success: \(val)\n"
|
||||||
Err (ParsingFailure problem) ->
|
Err (ParsingFailure problem) ->
|
||||||
|
@ -34,7 +34,13 @@ fullTest = \parser, input ->
|
||||||
|
|
||||||
myparser : Parser RawStr (List Str)
|
myparser : Parser RawStr (List Str)
|
||||||
myparser =
|
myparser =
|
||||||
Parser.Str.digits
|
Parser.CSV.file
|
||||||
|> Parser.Core.map Num.toStr
|
|> Parser.Core.map \records ->
|
||||||
|> Parser.Core.sepBy1 (Parser.Str.scalar ',')
|
records
|
||||||
|> Parser.Core.between (Parser.Str.scalar '[') (Parser.Str.scalar ']')
|
|> List.map (\record ->
|
||||||
|
record
|
||||||
|
|> List.map (\field ->
|
||||||
|
field
|
||||||
|
|> Str.fromUtf8
|
||||||
|
|> Result.withDefault "Unexpected problem while turning a List U8 back into a Str")
|
||||||
|
|> Str.joinWith ", ")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue