on ca34dec help sign in
38 lines 821 B history raw
1module Language.JSON.String.Lexer
3import Language.JSON.String.Tokens
4import Text.Lexer
6%access private
7%default total
9export
10quo : Lexer
11quo = is '"'
13export
14esc : Lexer -> Lexer
15esc = escape '\\'
17unicodeEscape : Lexer
18unicodeEscape = esc $ is 'u' <+> count (exactly 4) hexDigit
20simpleEscape : Lexer
21simpleEscape = esc $ oneOf "\"\\/bfnrt"
23legalChar : Lexer
24legalChar = non (quo <|> is '\\' <|> control)
26jsonStringTokenMap : TokenMap JSONStringToken
27jsonStringTokenMap = toTokenMap $
28 [ (quo, JSTQuote)
29 , (unicodeEscape, JSTUnicodeEscape)
30 , (simpleEscape, JSTSimpleEscape)
31 , (legalChar, JSTChar)
32 ]
34export
35lexString : String -> Maybe (List JSONStringToken)
36lexString x = case lex jsonStringTokenMap x of
37 (toks, _, _, "") => Just $ map TokenData.tok toks
38 _ => Nothing