Idris2/libs/contrib/Language/JSON/Lexer.idr
Alex Humphreys f3855d7100
Update contrib Text.Parser to match Library.Text.Parser (#1808)
Co-authored-by: Guillaume ALLAIS <guillaume.allais@ens-lyon.org>
2021-08-06 10:03:13 +01:00

42 lines
1019 B
Idris

module Language.JSON.Lexer
import Language.JSON.String
import Text.Lexer
import Text.Token
import public Language.JSON.Tokens
%default total
private
numberLit : Lexer
numberLit
= let sign = is '-'
whole = is '0' <|> range '1' '9' <+> many digit
frac = is '.' <+> digits
exp = like 'e' <+> opt (oneOf "+-") <+> digits in
opt sign <+> whole <+> opt frac <+> opt exp
private
jsonTokenMap : TokenMap JSONToken
jsonTokenMap = toTokenMap $
[ (spaces, JTIgnore)
, (is ',', JTPunct Comma)
, (is ':', JTPunct Colon)
, (is '[', JTPunct $ Square Open)
, (is ']', JTPunct $ Square Close)
, (is '{', JTPunct $ Curly Open)
, (is '}', JTPunct $ Curly Close)
, (exact "null", JTNull)
, (exact strTrue <|> exact strFalse, JTBoolean)
, (numberLit, JTNumber)
, (permissiveStringLit, JTString)
]
export
lexJSON : String -> Maybe (List (WithBounds JSONToken))
lexJSON str
= case lex jsonTokenMap str of
(tokens, _, _, "") => Just $ tokens
_ => Nothing