parser-combinators: refactor <@ and some
parent
6b57e8ced7
commit
c040050b52
|
@ -140,31 +140,35 @@ M: just-parser (parse) ( input parser -- result )
|
||||||
#! fully parsed input string.
|
#! fully parsed input string.
|
||||||
just-parser-p1 parse [ parse-result-unparsed empty? ] lsubset ;
|
just-parser-p1 parse [ parse-result-unparsed empty? ] lsubset ;
|
||||||
|
|
||||||
: <@-parser ( input parser quot -- result )
|
TUPLE: apply-parser p1 quot ;
|
||||||
|
|
||||||
|
: <@ ( parser quot -- parser )
|
||||||
|
<apply-parser> ;
|
||||||
|
|
||||||
|
M: apply-parser (parse) ( input parser -- result )
|
||||||
#! Calls the parser on the input. For each successfull
|
#! Calls the parser on the input. For each successfull
|
||||||
#! parse the quot is call with the parse result on the stack.
|
#! parse the quot is call with the parse result on the stack.
|
||||||
#! The result of that quotation then becomes the new parse result.
|
#! The result of that quotation then becomes the new parse result.
|
||||||
#! This allows modification of parse tree results (like
|
#! This allows modification of parse tree results (like
|
||||||
#! converting strings to integers, etc).
|
#! converting strings to integers, etc).
|
||||||
-rot call [
|
[ apply-parser-p1 ] keep apply-parser-quot
|
||||||
|
-rot parse [
|
||||||
[ parse-result-parsed swap call ] keep
|
[ parse-result-parsed swap call ] keep
|
||||||
parse-result-unparsed <parse-result>
|
parse-result-unparsed <parse-result>
|
||||||
] lmap-with ;
|
] lmap-with ;
|
||||||
|
|
||||||
: <@ ( parser quot -- parser )
|
TUPLE: some-parser p1 ;
|
||||||
#! Return an <@-parser.
|
|
||||||
[ <@-parser ] curry curry ;
|
|
||||||
|
|
||||||
: some-parser ( input parser -- result )
|
: some ( p1 -- parser )
|
||||||
|
<some-parser> ;
|
||||||
|
|
||||||
|
M: some-parser (parse) ( input parser -- result )
|
||||||
#! Calls the parser on the input, guarantees
|
#! Calls the parser on the input, guarantees
|
||||||
#! the parse is complete (the remaining input is empty),
|
#! the parse is complete (the remaining input is empty),
|
||||||
#! picks the first solution and only returns the parse
|
#! picks the first solution and only returns the parse
|
||||||
#! tree since the remaining input is empty.
|
#! tree since the remaining input is empty.
|
||||||
just call car parse-result-parsed ;
|
some-parser-p1 just parse car parse-result-parsed ;
|
||||||
|
|
||||||
: some ( parser -- deterministic-parser )
|
|
||||||
#! Creates a 'some-parser'.
|
|
||||||
[ some-parser ] curry ;
|
|
||||||
|
|
||||||
: <& ( parser1 parser2 -- parser )
|
: <& ( parser1 parser2 -- parser )
|
||||||
#! Same as <&> except discard the results of the second parser.
|
#! Same as <&> except discard the results of the second parser.
|
||||||
|
|
Loading…
Reference in New Issue