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

43 lines
853 B
Idris
Raw Normal View History

2020-05-18 15:59:07 +03:00
module Language.JSON.String.Lexer
import Data.Nat
2020-05-18 15:59:07 +03:00
import Language.JSON.String.Tokens
import Text.Lexer
%default total
export
quo : Lexer
quo = is '"'
export
esc : Lexer -> Lexer
esc = escape '\\'
private
unicodeEscape : Lexer
unicodeEscape = esc $ is 'u' <+> count (exactly 4) hexDigit
private
simpleEscape : Lexer
simpleEscape = esc $ oneOf "\"\\/bfnrt"
private
legalChar : Lexer
legalChar = non (quo <|> is '\\' <|> control)
private
jsonStringTokenMap : TokenMap JSONStringToken
jsonStringTokenMap = toTokenMap $
[ (quo, JSTQuote)
, (unicodeEscape, JSTUnicodeEscape)
, (simpleEscape, JSTSimpleEscape)
, (legalChar, JSTChar)
]
export
lexString : String -> Maybe (List JSONStringToken)
lexString x = case lex jsonStringTokenMap x of
(toks, _, _, "") => Just $ map TokenData.tok toks
_ => Nothing