From 102178f787aabd5f5e4ca6f9f3e2c61d3447eb91 Mon Sep 17 00:00:00 2001 From: Chris Double Date: Sat, 5 Apr 2008 17:51:42 +1300 Subject: [PATCH] Refactor seq peg parser --- extra/peg/peg.factor | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/extra/peg/peg.factor b/extra/peg/peg.factor index 671b63949f..8c92605c44 100755 --- a/extra/peg/peg.factor +++ b/extra/peg/peg.factor @@ -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 ] % - 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*