Idris2/libs/contrib/Language/JSON/String/Parser.idr

28 lines
746 B
Idris
Raw Normal View History

2020-05-18 15:59:07 +03:00
module Language.JSON.String.Parser
import Language.JSON.String.Tokens
import Text.Lexer
import Text.Parser
-- TODO: totality checking for codata
-- %default total
2020-05-18 15:59:07 +03:00
private
stringChar : Grammar JSONStringToken True Char
stringChar = match JSTChar
<|> match JSTSimpleEscape
<|> match JSTUnicodeEscape
private
quotedString : Grammar JSONStringToken True String
quotedString = let q = match JSTQuote in
do chars <- between q q (many stringChar)
eof
pure $ pack chars
export
parseString : List JSONStringToken -> Maybe String
parseString toks = case parse quotedString toks of
Right (str, []) => Just str
_ => Nothing