diff --git a/module/ice-9/peg/codegen.scm b/module/ice-9/peg/codegen.scm index 3cdf4ccfe..372f7eb49 100644 --- a/module/ice-9/peg/codegen.scm +++ b/module/ice-9/peg/codegen.scm @@ -197,46 +197,6 @@ return EXP." #`(or (#,(peg-sexp-compile #'first accum) #,str #,strlen #,at) #,(cg-or-int #'(rest ...) accum str strlen at))))) -;; Returns a function that parses a BODY element. -(define (cg-body args accum) - (syntax-case args () - ((type pat num) - #`(lambda (str strlen at) - (let ((body '())) - (let lp ((end at) (count 0)) - (let* ((match (#,(peg-sexp-compile #'pat (baf accum)) - str strlen end)) - (new-end (if match (car match) end)) - (count (if (> new-end end) (1+ count) count))) - (if (> new-end end) - (push-not-null! body (single-filter (cadr match)))) - (if (and (> new-end end) - #,(syntax-case #'num (+ * ?) - (n (number? (syntax->datum #'n)) - #'(< count n)) - (+ #t) - (* #t) - (? #'(< count 1)))) - (lp new-end count) - (let ((success #,(syntax-case #'num (+ * ?) - (n (number? (syntax->datum #'n)) - #'(= count n)) - (+ #'(>= count 1)) - (* #t) - (? #t)))) - #,(syntax-case #'type (! & lit) - (! - #`(if success - #f - #,(cggr (baf accum) 'cg-body #''() #'at))) - (& - #`(and success - #,(cggr (baf accum) 'cg-body #''() #'at))) - (lit - #`(and success - #,(cggr (baf accum) 'cg-body - #'(reverse body) #'new-end))))))))))))) - (define (cg-* args accum) (syntax-case args () ((pat) @@ -348,7 +308,6 @@ return EXP." (add-peg-compiler! 'capture cg-capture) (add-peg-compiler! 'and cg-and) (add-peg-compiler! 'or cg-or) -(add-peg-compiler! 'body cg-body) (add-peg-compiler! '* cg-*) (add-peg-compiler! '+ cg-+) (add-peg-compiler! '? cg-?) diff --git a/module/ice-9/peg/string-peg.scm b/module/ice-9/peg/string-peg.scm index c776d1d75..8d27d3bb0 100644 --- a/module/ice-9/peg/string-peg.scm +++ b/module/ice-9/peg/string-peg.scm @@ -154,26 +154,27 @@ RB < ']' ;; Parse a body. (define (peg-body->defn lst for-syntax) - (let ((suffix '()) - (front (datum->syntax for-syntax 'lit))) (cond - ((eq? (car lst) 'peg-suffix) - (set! suffix lst)) - ((string? (car lst)) - (begin (set! front (datum->syntax for-syntax - (string->symbol (car lst)))) - (set! suffix (cadr lst)))) - (else `(peg-parse-body-fail ,lst))) - #`(body #,front #,@(peg-suffix->defn - suffix - for-syntax)))) + ((equal? (car lst) "&") + #`(followed-by #,(peg-suffix->defn (cadr lst) for-syntax))) + ((equal? (car lst) "!") + #`(not-followed-by #,(peg-suffix->defn (cadr lst) for-syntax))) + ((eq? (car lst) 'peg-suffix) + (peg-suffix->defn lst for-syntax)) + (else `(peg-parse-body-fail ,lst)))) ;; Parse a suffix. (define (peg-suffix->defn lst for-syntax) - #`(#,(peg-primary->defn (cadr lst) for-syntax) - #,(if (null? (cddr lst)) - 1 - (datum->syntax for-syntax (string->symbol (caddr lst)))))) + (let ((inner-defn (peg-primary->defn (cadr lst) for-syntax))) + (cond + ((null? (cddr lst)) + inner-defn) + ((equal? (caddr lst) "*") + #`(* #,inner-defn)) + ((equal? (caddr lst) "?") + #`(? #,inner-defn)) + ((equal? (caddr lst) "+") + #`(+ #,inner-defn))))) ;; Parse a primary. (define (peg-primary->defn lst for-syntax)