maca
June 5, 2021, 5:56am
1
Hi all!
I have this question about elm parser:
Given this string ("aproved","pending")
I expect a list ["aproved","pending"]
I am having a bit of trouble understanding how to parse a quoted string, I don’t care about escaping quotes, but at one point it may come that the quotes are optional.
I’ve tried the following:
items : Parser (List String)
items =
Parser.sequence
{ start = "("
, separator = ","
, end = ")"
, spaces = spaces
, item = quoted
, trailing = Forbidden
}
quoted : Parser String
quoted =
succeed identity
|. token "\""
|= variable
{ start = always True
, inner = (/=) ','
, reserved = Set.fromList []
}
|. token "\""
The error I get is error: [{ col = 26, problem = Expecting "\"", row = 1 },{ col = 12, problem = Expecting ")", row = 1 }]
Perhaps your variable
call needs to exclude "
so it’ can’t eat the closing character before the following token
gets a crack at it.
maca
June 5, 2021, 12:40pm
3
Thanks @wondible
I don’t exactly know what you mean, I’ve try this but it doesn’t work:
quoted : Parser String
quoted =
succeed identity
|. token "\""
|= variable
{ start = (/=) '"'
, inner = (/=) '"'
, reserved = Set.fromList []
}
|. token "\""
maca
June 5, 2021, 4:18pm
4
My string was URL encoded, and that’s the reason the parser was failing
About parsing qouted strings @unsoundscapes pointed me to parser he made:
string : Char -> Parser String
string separator =
Parser.succeed ()
|. Parser.token (String.fromChar separator)
|. Parser.loop separator stringHelp
|> Parser.getChompedString
-- Remove quotes
|> Parser.map (String.dropLeft 1 >> String.dropRight 1)
stringHelp : Char -> Parser (Parser.Step Char ())
stringHelp separator =
Parser.oneOf
[ Parser.succeed (Parser.Done ())
|. Parser.token (String.fromChar separator)
, Parser.succeed (Parser.Loop separator)
|. Parser.chompIf (\char -> char == '\\')
|. Parser.chompIf (\_ -> True)
, Parser.succeed (Parser.Loop separator)
|. Parser.chompIf (\char -> char /= '\\' && char /= separator)
]
1 Like
system
Closed
June 15, 2021, 4:19pm
5
This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.