new accessors

db4
Doug Coleman 2008-08-30 21:59:46 -05:00
parent 3d88fec7e5
commit 978adcf90c
4 changed files with 23 additions and 20 deletions

View File

@ -353,11 +353,11 @@ M: quotation fjsc-parse ( object -- ast )
] with-string-writer ;
: fjsc-compile* ( string -- string )
'statement' parse parse-result-ast fjsc-compile ;
'statement' parse ast>> fjsc-compile ;
: fc* ( string -- string )
[
'statement' parse parse-result-ast values>> do-expressions
'statement' parse ast>> values>> do-expressions
] { } make [ write ] each ;

View File

@ -2,7 +2,7 @@
! See http://factorcode.org/license.txt for BSD license.
USING: kernel parser-combinators namespaces sequences promises strings
assocs math math.parser math.vectors math.functions math.order
lists hashtables ascii ;
lists hashtables ascii accessors ;
IN: json.reader
! Grammar for JSON from RFC 4627
@ -169,11 +169,12 @@ LAZY: 'value' ( -- parser )
'array' ,
'number' ,
] [<|>] spaced ;
ERROR: could-not-parse-json ;
: json> ( string -- object )
#! Parse a json formatted string to a factor object
'value' parse dup nil? [
"Could not parse json" throw
could-not-parse-json
] [
car parse-result-parsed
car parsed>>
] if ;

View File

@ -2,7 +2,7 @@
! See http://factorcode.org/license.txt for BSD license.
USING: lists lists.lazy promises kernel sequences strings math
arrays splitting quotations combinators namespaces
unicode.case unicode.categories sequences.deep ;
unicode.case unicode.categories sequences.deep accessors ;
IN: parser-combinators
! Parser combinator protocol
@ -13,11 +13,13 @@ M: promise parse ( input parser -- list )
TUPLE: parse-result parsed unparsed ;
ERROR: cannot-parse input ;
: parse-1 ( input parser -- result )
dupd parse dup nil? [
"Cannot parse " rot append throw
rot cannot-parse
] [
nip car parse-result-parsed
nip car parsed>>
] if ;
C: <parse-result> parse-result
@ -26,12 +28,12 @@ C: <parse-result> parse-result
<parse-result> 1list ;
: parse-result-parsed-slice ( parse-result -- slice )
dup parse-result-parsed empty? [
parse-result-unparsed 0 0 rot <slice>
dup parsed>> empty? [
unparsed>> 0 0 rot <slice>
] [
dup parse-result-unparsed
dup slice-from [ rot parse-result-parsed length - ] keep
rot slice-seq <slice>
dup unparsed>>
dup from>> [ rot parsed>> length - ] keep
rot seq>> <slice>
] if ;
: string= ( str1 str2 ignore-case -- ? )
@ -132,7 +134,7 @@ TUPLE: and-parser parsers ;
: <&> ( parser1 parser2 -- parser )
over and-parser? [
>r and-parser-parsers r> suffix
>r parsers>> r> suffix
] [
2array
] if and-parser boa ;
@ -142,11 +144,11 @@ TUPLE: and-parser parsers ;
: and-parser-parse ( list p1 -- list )
swap [
dup parse-result-unparsed rot parse
dup unparsed>> rot parse
[
>r parse-result-parsed r>
[ parse-result-parsed 2array ] keep
parse-result-unparsed <parse-result>
>r parsed>> r>
[ parsed>> 2array ] keep
unparsed>> <parse-result>
] lazy-map-with
] lazy-map-with lconcat ;

View File

@ -508,10 +508,10 @@ M: ebnf-non-terminal (transform) ( ast -- parser )
: check-parse-result ( result -- result )
dup [
dup parse-result-remaining [ blank? ] trim empty? [
dup remaining>> [ blank? ] trim empty? [
[
"Unable to fully parse EBNF. Left to parse was: " %
parse-result-remaining %
remaining>> %
] "" make throw
] unless
] [