on ca34dec help sign in
60 lines 1.5 kB history raw
1module Language.JSON.String.Tokens
3import Data.String.Extra
4import Text.Token
6%access public export
7%default total
9data JSONStringTokenKind
10 = JSTQuote
11 | JSTChar
12 | JSTSimpleEscape
13 | JSTUnicodeEscape
15JSONStringToken : Type
16JSONStringToken = Token JSONStringTokenKind
18Eq JSONStringTokenKind where
19 (==) JSTQuote JSTQuote = True
20 (==) JSTChar JSTChar = True
21 (==) JSTSimpleEscape JSTSimpleEscape = True
22 (==) JSTUnicodeEscape JSTUnicodeEscape = True
23 (==) _ _ = False
25private
26charValue : String -> Char
27charValue x = case index 0 x of
28 Nothing => '\NUL'
29 Just c => c
31private
32simpleEscapeValue : String -> Char
33simpleEscapeValue x
34 = case index 1 x of
35 Nothing => '\NUL'
36 Just c => case c of
37 '"' => '"'
38 '\\' => '\\'
39 '/' => '/'
40 'b' => '\b'
41 'f' => '\f'
42 'n' => '\n'
43 'r' => '\r'
44 't' => '\t'
45 _ => '\NUL'
47private
48unicodeEscapeValue : String -> Char
49unicodeEscapeValue x = chr $ cast ("0x" ++ drop 2 x)
51TokenKind JSONStringTokenKind where
52 TokType JSTQuote = ()
53 TokType JSTChar = Char
54 TokType JSTSimpleEscape = Char
55 TokType JSTUnicodeEscape = Char
57 tokValue JSTQuote = const ()
58 tokValue JSTChar = charValue
59 tokValue JSTSimpleEscape = simpleEscapeValue
60 tokValue JSTUnicodeEscape = unicodeEscapeValue