1 ! Copyright (C) 2006 Chris Double.
3 ! Redistribution and use in source and binary forms, with or without
4 ! modification, are permitted provided that the following conditions are met:
6 ! 1. Redistributions of source code must retain the above copyright notice,
7 ! this list of conditions and the following disclaimer.
9 ! 2. Redistributions in binary form must reproduce the above copyright notice,
10 ! this list of conditions and the following disclaimer in the documentation
11 ! and/or other materials provided with the distribution.
13 ! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
14 ! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
15 ! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
16 ! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17 ! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
18 ! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
19 ! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
20 ! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
21 ! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 ! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 USING: kernel namespaces sequences strings math hashtables parser-combinators lazy-lists ;
27 ! Grammar for JSON from RFC 4627
30 : [<&>] ( quot - quot )
31 { } make unclip [ <&> ] reduce ;
33 : [<|>] ( quot - quot )
34 { } make unclip [ <|> ] reduce ;
36 LAZY: 'ws' ( -- parser )
43 LAZY: spaced ( parser -- parser )
44 'ws' swap &> 'ws' <& ;
46 LAZY: 'begin-array' ( -- parser )
49 LAZY: 'begin-object' ( -- parser )
52 LAZY: 'end-array' ( -- parser )
55 LAZY: 'end-object' ( -- parser )
58 LAZY: 'name-separator' ( -- parser )
61 LAZY: 'value-separator' ( -- parser )
64 LAZY: 'false' ( -- parser )
67 LAZY: 'null' ( -- parser )
70 LAZY: 'true' ( -- parser )
73 LAZY: 'quot' ( -- parser )
76 LAZY: 'string' ( -- parser )
78 [ quotable? ] satisfy <+> &>
79 'quot' <& [ >string ] <@ ;
83 LAZY: 'member' ( -- parser )
88 : object>hashtable ( object -- hashtable )
89 #! Convert json object to hashtable
90 H{ } clone dup rot [ dup second swap first rot set-hash ] each-with ;
92 LAZY: 'object' ( -- parser )
95 'value-separator' 'member' &> <*> <&:>
96 'end-object' <& [ object>hashtable ] <@ ;
98 LAZY: 'array' ( -- parser )
101 'value-separator' 'value' &> <*> <&:>
104 LAZY: 'minus' ( -- parser )
107 LAZY: 'plus' ( -- parser )
110 LAZY: 'zero' ( -- parser )
111 "0" token [ drop 0 ] <@ ;
113 LAZY: 'decimal-point' ( -- parser )
116 LAZY: 'digit1-9' ( -- parser )
119 CHAR: 1 CHAR: 9 between?
123 ] satisfy [ digit> ] <@ ;
125 LAZY: 'digit0-9' ( -- parser )
126 [ digit? ] satisfy [ digit> ] <@ ;
128 : sequence>number ( seq -- num )
130 0 [ swap 10 * + ] reduce ;
132 LAZY: 'int' ( -- parser )
134 'digit1-9' 'digit0-9' <*> <&:> [ sequence>number ] <@ <|> ;
136 LAZY: 'e' ( -- parser )
137 "e" token "E" token <|> ;
139 : sign-number ( { minus? num } -- number )
140 #! Convert the json number value to a factor number
141 dup second swap first [ -1 * ] when ;
143 LAZY: 'exp' ( -- parser )
145 'minus' 'plus' <|> <?> &>
146 'digit0-9' <+> [ sequence>number ] <@ <&> [ sign-number ] <@ ;
148 : sequence>frac ( seq -- num )
149 #! { 1 2 3 } => 0.123
150 reverse 0 [ swap 10 / + ] reduce 10 / >float ;
152 LAZY: 'frac' ( -- parser )
153 'decimal-point' 'digit0-9' <+> &> [ sequence>frac ] <@ ;
155 : raise-to-power ( { num exp } -- num )
156 #! Multiply 'num' by 10^exp
157 dup second dup [ 10 swap first ^ swap first * ] [ drop first ] if ;
159 LAZY: 'number' ( -- parser )
161 [ 'int' , 'frac' 0 succeed <|> , ] [<&>] [ sum ] <@
162 'exp' <?> <&> [ raise-to-power ] <@ <&> [ sign-number ] <@ ;
164 LAZY: 'value' ( -- parser )
175 : json> ( string -- object )
176 #! Parse a json formatted string to a factor object
177 'value' some parse force ;