2006-08-07 05:21:34 -04:00
|
|
|
! Copyright (C) 2006 Chris Double.
|
|
|
|
!
|
|
|
|
! Redistribution and use in source and binary forms, with or without
|
|
|
|
! modification, are permitted provided that the following conditions are met:
|
|
|
|
!
|
|
|
|
! 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
! this list of conditions and the following disclaimer.
|
|
|
|
!
|
|
|
|
! 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
! this list of conditions and the following disclaimer in the documentation
|
|
|
|
! and/or other materials provided with the distribution.
|
|
|
|
!
|
|
|
|
! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
!
|
2006-10-08 07:30:45 -04:00
|
|
|
USING: kernel namespaces sequences strings math hashtables parser-combinators lazy-lists ;
|
2006-08-07 05:21:34 -04:00
|
|
|
IN: json
|
|
|
|
|
|
|
|
! Grammar for JSON from RFC 4627
|
2006-09-05 01:29:26 -04:00
|
|
|
USE: tools
|
2006-08-07 05:21:34 -04:00
|
|
|
|
|
|
|
: [<&>] ( quot - quot )
|
2006-08-07 07:06:22 -04:00
|
|
|
{ } make unclip [ <&> ] reduce ;
|
2006-08-07 05:21:34 -04:00
|
|
|
|
|
|
|
: [<|>] ( quot - quot )
|
|
|
|
{ } make unclip [ <|> ] reduce ;
|
|
|
|
|
2006-10-08 07:30:45 -04:00
|
|
|
LAZY: 'ws' ( -- parser )
|
2006-08-07 07:06:22 -04:00
|
|
|
" " token
|
|
|
|
"\n" token <|>
|
|
|
|
"\r" token <|>
|
|
|
|
"\t" token <|>
|
|
|
|
"" token <|> ;
|
|
|
|
|
2006-10-08 07:30:45 -04:00
|
|
|
LAZY: spaced ( parser -- parser )
|
2006-08-07 07:06:22 -04:00
|
|
|
'ws' swap &> 'ws' <& ;
|
|
|
|
|
2006-10-08 07:30:45 -04:00
|
|
|
LAZY: 'begin-array' ( -- parser )
|
2006-08-07 07:06:22 -04:00
|
|
|
"[" token spaced ;
|
2006-08-07 05:21:34 -04:00
|
|
|
|
2006-10-08 07:30:45 -04:00
|
|
|
LAZY: 'begin-object' ( -- parser )
|
2006-08-07 07:06:22 -04:00
|
|
|
"{" token spaced ;
|
2006-08-07 05:21:34 -04:00
|
|
|
|
2006-10-08 07:30:45 -04:00
|
|
|
LAZY: 'end-array' ( -- parser )
|
2006-08-07 07:06:22 -04:00
|
|
|
"]" token spaced ;
|
2006-08-07 05:21:34 -04:00
|
|
|
|
2006-10-08 07:30:45 -04:00
|
|
|
LAZY: 'end-object' ( -- parser )
|
2006-08-07 07:06:22 -04:00
|
|
|
"}" token spaced ;
|
2006-08-07 05:21:34 -04:00
|
|
|
|
2006-10-08 07:30:45 -04:00
|
|
|
LAZY: 'name-separator' ( -- parser )
|
2006-08-07 07:06:22 -04:00
|
|
|
":" token spaced ;
|
2006-08-07 05:21:34 -04:00
|
|
|
|
2006-10-08 07:30:45 -04:00
|
|
|
LAZY: 'value-separator' ( -- parser )
|
2006-08-07 07:06:22 -04:00
|
|
|
"," token spaced ;
|
2006-08-07 05:21:34 -04:00
|
|
|
|
2006-10-08 07:30:45 -04:00
|
|
|
LAZY: 'false' ( -- parser )
|
2006-08-07 05:21:34 -04:00
|
|
|
"false" token ;
|
|
|
|
|
2006-10-08 07:30:45 -04:00
|
|
|
LAZY: 'null' ( -- parser )
|
2006-08-07 05:21:34 -04:00
|
|
|
"null" token ;
|
|
|
|
|
2006-10-08 07:30:45 -04:00
|
|
|
LAZY: 'true' ( -- parser )
|
2006-08-07 05:21:34 -04:00
|
|
|
"true" token ;
|
|
|
|
|
2006-10-08 07:30:45 -04:00
|
|
|
LAZY: 'quot' ( -- parser )
|
2006-08-07 05:21:34 -04:00
|
|
|
"\"" token ;
|
|
|
|
|
2006-10-08 07:30:45 -04:00
|
|
|
LAZY: 'string' ( -- parser )
|
2006-08-07 05:21:34 -04:00
|
|
|
'quot'
|
|
|
|
[ quotable? ] satisfy <+> &>
|
|
|
|
'quot' <& [ >string ] <@ ;
|
|
|
|
|
|
|
|
DEFER: 'value'
|
|
|
|
|
2006-10-08 07:30:45 -04:00
|
|
|
LAZY: 'member' ( -- parser )
|
2006-08-07 05:21:34 -04:00
|
|
|
'string'
|
|
|
|
'name-separator' <&
|
2006-10-08 07:30:45 -04:00
|
|
|
'value' <&> ;
|
2006-08-07 07:06:22 -04:00
|
|
|
|
|
|
|
: object>hashtable ( object -- hashtable )
|
|
|
|
#! Convert json object to hashtable
|
|
|
|
H{ } clone dup rot [ dup second swap first rot set-hash ] each-with ;
|
2006-08-07 05:21:34 -04:00
|
|
|
|
2006-10-08 07:30:45 -04:00
|
|
|
LAZY: 'object' ( -- parser )
|
2006-08-07 05:21:34 -04:00
|
|
|
'begin-object'
|
|
|
|
'member' &>
|
2006-08-07 07:06:22 -04:00
|
|
|
'value-separator' 'member' &> <*> <&:>
|
|
|
|
'end-object' <& [ object>hashtable ] <@ ;
|
2006-08-07 05:21:34 -04:00
|
|
|
|
2006-10-08 07:30:45 -04:00
|
|
|
LAZY: 'array' ( -- parser )
|
2006-08-07 05:21:34 -04:00
|
|
|
'begin-array'
|
2006-10-08 07:30:45 -04:00
|
|
|
'value' &>
|
|
|
|
'value-separator' 'value' &> <*> <&:>
|
2006-08-07 05:21:34 -04:00
|
|
|
'end-array' <& ;
|
|
|
|
|
2006-10-08 07:30:45 -04:00
|
|
|
LAZY: 'minus' ( -- parser )
|
2006-08-07 05:21:34 -04:00
|
|
|
"-" token ;
|
|
|
|
|
2006-10-08 07:30:45 -04:00
|
|
|
LAZY: 'plus' ( -- parser )
|
2006-08-07 05:21:34 -04:00
|
|
|
"+" token ;
|
|
|
|
|
2006-10-08 07:30:45 -04:00
|
|
|
LAZY: 'zero' ( -- parser )
|
2006-08-07 05:21:34 -04:00
|
|
|
"0" token [ drop 0 ] <@ ;
|
|
|
|
|
2006-10-08 07:30:45 -04:00
|
|
|
LAZY: 'decimal-point' ( -- parser )
|
2006-08-07 05:21:34 -04:00
|
|
|
"." token ;
|
|
|
|
|
2006-10-08 07:30:45 -04:00
|
|
|
LAZY: 'digit1-9' ( -- parser )
|
2006-08-07 05:21:34 -04:00
|
|
|
[
|
|
|
|
dup integer? [
|
|
|
|
CHAR: 1 CHAR: 9 between?
|
|
|
|
] [
|
|
|
|
drop f
|
|
|
|
] if
|
|
|
|
] satisfy [ digit> ] <@ ;
|
|
|
|
|
2006-10-08 07:30:45 -04:00
|
|
|
LAZY: 'digit0-9' ( -- parser )
|
2006-08-07 05:21:34 -04:00
|
|
|
[ digit? ] satisfy [ digit> ] <@ ;
|
|
|
|
|
|
|
|
: sequence>number ( seq -- num )
|
|
|
|
#! { 1 2 3 } => 123
|
|
|
|
0 [ swap 10 * + ] reduce ;
|
|
|
|
|
2006-10-08 07:30:45 -04:00
|
|
|
LAZY: 'int' ( -- parser )
|
2006-08-07 05:21:34 -04:00
|
|
|
'zero'
|
|
|
|
'digit1-9' 'digit0-9' <*> <&:> [ sequence>number ] <@ <|> ;
|
|
|
|
|
2006-10-08 07:30:45 -04:00
|
|
|
LAZY: 'e' ( -- parser )
|
2006-08-07 05:21:34 -04:00
|
|
|
"e" token "E" token <|> ;
|
|
|
|
|
2006-08-07 07:06:22 -04:00
|
|
|
: sign-number ( { minus? num } -- number )
|
|
|
|
#! Convert the json number value to a factor number
|
|
|
|
dup second swap first [ -1 * ] when ;
|
|
|
|
|
2006-10-08 07:30:45 -04:00
|
|
|
LAZY: 'exp' ( -- parser )
|
2006-08-07 07:06:22 -04:00
|
|
|
'e'
|
|
|
|
'minus' 'plus' <|> <?> &>
|
|
|
|
'digit0-9' <+> [ sequence>number ] <@ <&> [ sign-number ] <@ ;
|
|
|
|
|
|
|
|
: sequence>frac ( seq -- num )
|
|
|
|
#! { 1 2 3 } => 0.123
|
|
|
|
reverse 0 [ swap 10 / + ] reduce 10 / >float ;
|
2006-08-07 05:21:34 -04:00
|
|
|
|
2006-10-08 07:30:45 -04:00
|
|
|
LAZY: 'frac' ( -- parser )
|
2006-08-07 07:06:22 -04:00
|
|
|
'decimal-point' 'digit0-9' <+> &> [ sequence>frac ] <@ ;
|
|
|
|
|
|
|
|
: raise-to-power ( { num exp } -- num )
|
|
|
|
#! Multiply 'num' by 10^exp
|
|
|
|
dup second dup [ 10 swap first ^ swap first * ] [ drop first ] if ;
|
2006-08-07 05:21:34 -04:00
|
|
|
|
2006-10-08 07:30:45 -04:00
|
|
|
LAZY: 'number' ( -- parser )
|
2006-08-07 07:06:22 -04:00
|
|
|
'minus' <?>
|
|
|
|
[ 'int' , 'frac' 0 succeed <|> , ] [<&>] [ sum ] <@
|
|
|
|
'exp' <?> <&> [ raise-to-power ] <@ <&> [ sign-number ] <@ ;
|
2006-08-07 05:21:34 -04:00
|
|
|
|
2006-10-08 07:30:45 -04:00
|
|
|
LAZY: 'value' ( -- parser )
|
2006-08-07 05:21:34 -04:00
|
|
|
[
|
|
|
|
'false' ,
|
|
|
|
'null' ,
|
|
|
|
'true' ,
|
|
|
|
'string' ,
|
|
|
|
'object' ,
|
|
|
|
'array' ,
|
|
|
|
'number' ,
|
|
|
|
] [<|>] ;
|
2006-08-07 07:06:22 -04:00
|
|
|
|
|
|
|
: json> ( string -- object )
|
|
|
|
#! Parse a json formatted string to a factor object
|
2006-10-08 07:30:45 -04:00
|
|
|
'value' some parse force ;
|