mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-30 23:31:12 +00:00
74 lines
2.8 KiB
Text
74 lines
2.8 KiB
Text
app "main"
|
|
packages { pf: "platform/main.roc" }
|
|
imports [Parser.Core.{Parser}, Parser.Str.{RawStr}, Parser.CSV.{CSV}]
|
|
provides [main] to pf
|
|
|
|
# Until issue https://github.com/rtfeldman/roc/issues/3438 is fixed,
|
|
# use the simple 'hello world' platform for testing
|
|
# with hard-coded input.
|
|
|
|
|
|
main = fullTest csvParser "10,20\n\"An escaped field!\"\"\n,,,\",30\n"
|
|
# main = partialTest fieldParser "\"An escaped field with some \"\"<- double quotes\""
|
|
# main = fullTest fieldContentsParser "My very cool,\"\"\r\n string"
|
|
# main = partialTest betweenParser "\"this is a test\"\" to see\""
|
|
# main = partialTest manyParser "this is a very long string\"\""
|
|
|
|
partialTest = \parser, input ->
|
|
when Parser.Str.parseStrPartial parser input is
|
|
Ok result ->
|
|
# val = result.val |> Str.joinWith("\r\n")
|
|
val = result.val
|
|
leftover = result.input
|
|
"Parse success: \(val) (leftover string: \(leftover))\n"
|
|
Err (ParsingFailure problem) ->
|
|
"Parse failure: \(problem)\n"
|
|
|
|
fullTest = \parser, input ->
|
|
when Parser.Str.parseStr parser input is
|
|
Ok result ->
|
|
# val = result |> Str.joinWith(", ")
|
|
val = result |> Str.joinWith("\r\n")
|
|
# val = result
|
|
"Parse success: \(val)\n"
|
|
Err (ParsingFailure problem) ->
|
|
"Parse failure: \(problem)\n"
|
|
Err (ParsingIncomplete leftover) ->
|
|
"Parse failure: Expected to reach end of input, but the following was still left: `\(leftover)`\n"
|
|
|
|
betweenParser : Parser RawStr Str
|
|
betweenParser =
|
|
Parser.Core.between manyParser (Parser.Str.codepoint 34) (Parser.Str.codepoint 34)
|
|
|
|
manyParser : Parser RawStr Str
|
|
manyParser =
|
|
Parser.Str.string "\"\"" |> Parser.Core.map(\_ -> 34) # Escaped double quote
|
|
|> Parser.Core.alt (Parser.Str.codepoint 44) # Comma
|
|
|> Parser.Core.alt (Parser.Str.codepoint 13) # CR
|
|
|> Parser.Core.alt (Parser.Str.codepoint 10) # LF
|
|
|> Parser.Core.alt (Parser.Str.codepointSatisfies (\x -> (x >= 32 && x <= 33) || (x >= 35 && x <= 43) || (x >= 45 && x <= 126))) # Any printable char except " (34) and , (44)
|
|
|> Parser.Core.many
|
|
|> Parser.Core.map (\field -> field |> Str.fromUtf8 |> Result.withDefault "Should not happen")
|
|
|
|
fieldContentsParser : Parser RawStr Str
|
|
fieldContentsParser =
|
|
Parser.CSV.escapedContents
|
|
|> Parser.Core.map (\field -> field |> Str.fromUtf8 |> Result.withDefault "Should not happen")
|
|
|
|
fieldParser : Parser RawStr Str
|
|
fieldParser =
|
|
Parser.CSV.escapedField
|
|
|> Parser.Core.map (\field -> field |> Str.fromUtf8 |> Result.withDefault "Should not happen")
|
|
|
|
csvParser : Parser RawStr (List Str)
|
|
csvParser =
|
|
Parser.CSV.file
|
|
|> Parser.Core.map \records ->
|
|
records
|
|
|> 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 ", ")
|