Refactor seq peg parser

db4
Chris Double 2008-04-05 17:51:42 +13:00
parent 7b73d2734f
commit 102178f787
1 changed files with 21 additions and 17 deletions

View File

@ -313,34 +313,38 @@ M: range-parser (compile) ( parser -- quot )
TUPLE: seq-parser parsers ; TUPLE: seq-parser parsers ;
MATCH-VARS: ?quot ; : ignore? ( ast -- bool )
ignore = ;
: seq-pattern ( -- quot ) : calc-seq-result ( prev-result current-result -- next-result )
[ [
dup [
?quot [
[ remaining>> swap (>>remaining) ] 2keep [ remaining>> swap (>>remaining) ] 2keep
ast>> dup ignore = [ ast>> dup ignore? [
drop drop
] [ ] [
swap [ ast>> push ] keep swap [ ast>> push ] keep
] if ] if
] [ ] [
drop f drop f
] if* ] if* ;
: parse-seq-element ( result quot -- result )
over [
call calc-seq-result
] [ ] [
drop f 2drop f
] if ] if ; inline
] ;
M: seq-parser (compile) ( parser -- quot ) M: seq-parser (compile) ( parser -- quot )
[ [
[ input-slice V{ } clone <parse-result> ] % [ input-slice V{ } clone <parse-result> ] %
parsers>> [ compiled-parser \ ?quot seq-pattern match-replace % ] each parsers>> [ compiled-parser 1quotation , \ parse-seq-element , ] each
] [ ] make ; ] [ ] make ;
TUPLE: choice-parser parsers ; TUPLE: choice-parser parsers ;
MATCH-VARS: ?quot ;
: choice-pattern ( -- quot ) : choice-pattern ( -- quot )
[ [
[ ?quot ] unless* [ ?quot ] unless*