] 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 ;
! 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
'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 ;
! 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
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
<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 -- ? )
: <&> ( parser1 parser2 -- parser )
over and-parser? [
- >r and-parser-parsers r> suffix
+ >r parsers>> r> suffix
] [
2array
] if and-parser boa ;
: 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 ;