new accessors
parent
3d88fec7e5
commit
978adcf90c
|
@ -353,11 +353,11 @@ M: quotation fjsc-parse ( object -- ast )
|
||||||
] with-string-writer ;
|
] with-string-writer ;
|
||||||
|
|
||||||
: fjsc-compile* ( string -- string )
|
: fjsc-compile* ( string -- string )
|
||||||
'statement' parse parse-result-ast fjsc-compile ;
|
'statement' parse ast>> fjsc-compile ;
|
||||||
|
|
||||||
: fc* ( string -- string )
|
: fc* ( string -- string )
|
||||||
[
|
[
|
||||||
'statement' parse parse-result-ast values>> do-expressions
|
'statement' parse ast>> values>> do-expressions
|
||||||
] { } make [ write ] each ;
|
] { } make [ write ] each ;
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: kernel parser-combinators namespaces sequences promises strings
|
USING: kernel parser-combinators namespaces sequences promises strings
|
||||||
assocs math math.parser math.vectors math.functions math.order
|
assocs math math.parser math.vectors math.functions math.order
|
||||||
lists hashtables ascii ;
|
lists hashtables ascii accessors ;
|
||||||
IN: json.reader
|
IN: json.reader
|
||||||
|
|
||||||
! Grammar for JSON from RFC 4627
|
! Grammar for JSON from RFC 4627
|
||||||
|
@ -169,11 +169,12 @@ LAZY: 'value' ( -- parser )
|
||||||
'array' ,
|
'array' ,
|
||||||
'number' ,
|
'number' ,
|
||||||
] [<|>] spaced ;
|
] [<|>] spaced ;
|
||||||
|
ERROR: could-not-parse-json ;
|
||||||
|
|
||||||
: json> ( string -- object )
|
: json> ( string -- object )
|
||||||
#! Parse a json formatted string to a factor object
|
#! Parse a json formatted string to a factor object
|
||||||
'value' parse dup nil? [
|
'value' parse dup nil? [
|
||||||
"Could not parse json" throw
|
could-not-parse-json
|
||||||
] [
|
] [
|
||||||
car parse-result-parsed
|
car parsed>>
|
||||||
] if ;
|
] if ;
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: lists lists.lazy promises kernel sequences strings math
|
USING: lists lists.lazy promises kernel sequences strings math
|
||||||
arrays splitting quotations combinators namespaces
|
arrays splitting quotations combinators namespaces
|
||||||
unicode.case unicode.categories sequences.deep ;
|
unicode.case unicode.categories sequences.deep accessors ;
|
||||||
IN: parser-combinators
|
IN: parser-combinators
|
||||||
|
|
||||||
! Parser combinator protocol
|
! Parser combinator protocol
|
||||||
|
@ -13,11 +13,13 @@ M: promise parse ( input parser -- list )
|
||||||
|
|
||||||
TUPLE: parse-result parsed unparsed ;
|
TUPLE: parse-result parsed unparsed ;
|
||||||
|
|
||||||
|
ERROR: cannot-parse input ;
|
||||||
|
|
||||||
: parse-1 ( input parser -- result )
|
: parse-1 ( input parser -- result )
|
||||||
dupd parse dup nil? [
|
dupd parse dup nil? [
|
||||||
"Cannot parse " rot append throw
|
rot cannot-parse
|
||||||
] [
|
] [
|
||||||
nip car parse-result-parsed
|
nip car parsed>>
|
||||||
] if ;
|
] if ;
|
||||||
|
|
||||||
C: <parse-result> parse-result
|
C: <parse-result> parse-result
|
||||||
|
@ -26,12 +28,12 @@ C: <parse-result> parse-result
|
||||||
<parse-result> 1list ;
|
<parse-result> 1list ;
|
||||||
|
|
||||||
: parse-result-parsed-slice ( parse-result -- slice )
|
: parse-result-parsed-slice ( parse-result -- slice )
|
||||||
dup parse-result-parsed empty? [
|
dup parsed>> empty? [
|
||||||
parse-result-unparsed 0 0 rot <slice>
|
unparsed>> 0 0 rot <slice>
|
||||||
] [
|
] [
|
||||||
dup parse-result-unparsed
|
dup unparsed>>
|
||||||
dup slice-from [ rot parse-result-parsed length - ] keep
|
dup from>> [ rot parsed>> length - ] keep
|
||||||
rot slice-seq <slice>
|
rot seq>> <slice>
|
||||||
] if ;
|
] if ;
|
||||||
|
|
||||||
: string= ( str1 str2 ignore-case -- ? )
|
: string= ( str1 str2 ignore-case -- ? )
|
||||||
|
@ -132,7 +134,7 @@ TUPLE: and-parser parsers ;
|
||||||
|
|
||||||
: <&> ( parser1 parser2 -- parser )
|
: <&> ( parser1 parser2 -- parser )
|
||||||
over and-parser? [
|
over and-parser? [
|
||||||
>r and-parser-parsers r> suffix
|
>r parsers>> r> suffix
|
||||||
] [
|
] [
|
||||||
2array
|
2array
|
||||||
] if and-parser boa ;
|
] if and-parser boa ;
|
||||||
|
@ -142,11 +144,11 @@ TUPLE: and-parser parsers ;
|
||||||
|
|
||||||
: and-parser-parse ( list p1 -- list )
|
: and-parser-parse ( list p1 -- list )
|
||||||
swap [
|
swap [
|
||||||
dup parse-result-unparsed rot parse
|
dup unparsed>> rot parse
|
||||||
[
|
[
|
||||||
>r parse-result-parsed r>
|
>r parsed>> r>
|
||||||
[ parse-result-parsed 2array ] keep
|
[ parsed>> 2array ] keep
|
||||||
parse-result-unparsed <parse-result>
|
unparsed>> <parse-result>
|
||||||
] lazy-map-with
|
] lazy-map-with
|
||||||
] lazy-map-with lconcat ;
|
] lazy-map-with lconcat ;
|
||||||
|
|
||||||
|
|
|
@ -508,10 +508,10 @@ M: ebnf-non-terminal (transform) ( ast -- parser )
|
||||||
|
|
||||||
: check-parse-result ( result -- result )
|
: check-parse-result ( result -- result )
|
||||||
dup [
|
dup [
|
||||||
dup parse-result-remaining [ blank? ] trim empty? [
|
dup remaining>> [ blank? ] trim empty? [
|
||||||
[
|
[
|
||||||
"Unable to fully parse EBNF. Left to parse was: " %
|
"Unable to fully parse EBNF. Left to parse was: " %
|
||||||
parse-result-remaining %
|
remaining>> %
|
||||||
] "" make throw
|
] "" make throw
|
||||||
] unless
|
] unless
|
||||||
] [
|
] [
|
||||||
|
|
Loading…
Reference in New Issue