-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_json.jl
32 lines (27 loc) · 1.34 KB
/
parse_json.jl
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
32
include("juliaparsec.jl")
# Json = Object | Array
# Array = [ (Value (, Value) *)? ]
# Object = {(String : Value (, String : Value)*)?}
# Number = -?Digit+ # also, decimal point. also e|E.
# String = " _* " # no quote, except \" but not \\"
# Value = String | Number | Object | Array | true | false | null
# need to define: parsewhitespace, parsekeyword,
# oneormore, zeroorone, parsechar, eatnothings
parsenumber = sequence([zeroorone('-'),oneormore(parsedigit)])
parsestring = sequence([parsechar('"'),parseuntil('"'),parsechar('"')])
parseobject = sequence(
[ parsechar('{')
, parsewhitespace
, zeroorone(sequence([parsestring,parsewhitespace,parsechar(':'),parsewhitespace,parsevalue
, zeroormore(sequence([parsewhitespace,parsechar(','),parsewhitespace
,parsestring,parsewhitespace,parsechar(':'),parsewhitespace,parsevalue]))]))
, parsewhitespace
, parsechar('}')])
parsearray = sequence(
[ parsechar('[')
, parsewhitespace
, zeroorone(sequence(parsevalue,zeroormore(parsewhitespace,parsechar(','),parsewhitespace,parsevalue)))
, parsewhitespace
, parsechar(']')])
parsevalue = branch([parsenumber,parsestring,parseobject,parsearray,parsekeyword("true"),parsekeyword("false"),parsekeyword("null")])
parsejson = eatnothings(sequence(parsewhitespace,branch(parsearray,parseobject),parsewhitespace))