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 ;
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
ast>> dup ignore = [
drop
] [
swap [ ast>> push ] keep
] if
] [
drop f
] if*
[ remaining>> swap (>>remaining) ] 2keep
ast>> dup ignore? [
drop
] [
drop f
] if
] ;
swap [ ast>> push ] keep
] if
] [
drop f
] if* ;
: parse-seq-element ( result quot -- result )
over [
call calc-seq-result
] [
2drop f
] if ; inline
M: seq-parser (compile) ( parser -- quot )
[
[ input-slice V{ } clone <parse-result> ] %
parsers>> [ compiled-parser \ ?quot seq-pattern match-replace % ] each
parsers>> [ compiled-parser 1quotation , \ parse-seq-element , ] each
] [ ] make ;
TUPLE: choice-parser parsers ;
MATCH-VARS: ?quot ;
: choice-pattern ( -- quot )
[
[ ?quot ] unless*