forked from links-lang/links
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonlex.mll
32 lines (27 loc) · 1.48 KB
/
jsonlex.mll
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
(* Simple lexer for reading JSON tokens *)
{
}
let octal_code = ['0'-'3']['0'-'7']['0'-'7']
let hex_code = ['0'-'9''a'-'f''A'-'F']['0'-'9''a'-'f''A'-'F']
let string_contents = ([^ '\"' '\\']* |"\\\"" |"\\\\" | "\\n" | "\\r" | "\\t" | '\\' octal_code | '\\' ['x' 'X'] hex_code)*
let def_nat = ['0'-'9']+
let def_integer = '-'? def_nat
let def_float = def_integer '.' def_nat ('e' def_integer)?
let def_blank = [' ' '\t' '\n']
rule jsonlex = parse
| '{' { Jsonparse.LBRACE }
| '}' { Jsonparse.RBRACE }
| '(' { Jsonparse.LPAREN }
| ')' { Jsonparse.RPAREN }
| '[' { Jsonparse.LBRACKET }
| ']' { Jsonparse.RBRACKET }
| ':' { Jsonparse.COLON }
| ',' { Jsonparse.COMMA }
| '_' { Jsonparse.UNDERSCORE }
| "true" { Jsonparse.TRUE }
| "false" { Jsonparse.FALSE }
| "null" { Jsonparse.NULL }
| ('\"' (string_contents as var) '\"') { Jsonparse.STRING (Utility.decode_escapes var) }
| def_integer as var { Jsonparse.INT (int_of_string var) }
| def_float as var { Jsonparse.FLOAT (float_of_string var) }
| def_blank { jsonlex lexbuf }