Refactor repeat0 and repeat1 peg parsers

db4
Chris Double 2008-04-05 18:01:18 +13:00
parent 226d211342
commit d4897fa007
1 changed files with 15 additions and 27 deletions

View File

@ -351,48 +351,36 @@ M: choice-parser (compile) ( parser -- quot )
TUPLE: repeat0-parser p1 ; TUPLE: repeat0-parser p1 ;
MATCH-VARS: ?quot ; : (repeat) ( quot result -- result )
: (repeat0) ( quot result -- result )
over call [ over call [
[ remaining>> swap (>>remaining) ] 2keep [ remaining>> swap (>>remaining) ] 2keep
ast>> swap [ ast>> push ] keep ast>> swap [ ast>> push ] keep
(repeat0) (repeat)
] [ ] [
nip nip
] if* ; inline ] if* ; inline
: repeat0-pattern ( -- quot )
[
[ ?quot ] swap (repeat0)
] ;
M: repeat0-parser (compile) ( parser -- quot ) M: repeat0-parser (compile) ( parser -- quot )
[ p1>> compiled-parser 1quotation '[
[ input-slice V{ } clone <parse-result> ] % input-slice V{ } clone <parse-result> , swap (repeat)
p1>> compiled-parser \ ?quot repeat0-pattern match-replace % ] ;
] [ ] make ;
TUPLE: repeat1-parser p1 ; TUPLE: repeat1-parser p1 ;
: repeat1-pattern ( -- quot ) : repeat1-empty-check ( result -- result )
[ [
[ ?quot ] swap (repeat0) [ dup ast>> empty? [ drop f ] when
dup ast>> empty? [ ] [
drop f f
] when ] if* ;
] [
f
] if*
] ;
M: repeat1-parser (compile) ( parser -- quot ) M: repeat1-parser (compile) ( parser -- quot )
[ p1>> compiled-parser 1quotation '[
[ input-slice V{ } clone <parse-result> ] % input-slice V{ } clone <parse-result> , swap (repeat) repeat1-empty-check
p1>> compiled-parser \ ?quot repeat1-pattern match-replace % ] ;
] [ ] make ;
TUPLE: optional-parser p1 ; TUPLE: optional-parser p1 ;
MATCH-VARS: ?quot ;
: optional-pattern ( -- quot ) : optional-pattern ( -- quot )
[ [