pull/6/head
Matthew Butterick 7 years ago
parent 98cb84bdcc
commit 46ddd5cfd4

@ -109,15 +109,17 @@
[(inferred-id NAME repeat) SUB-PAT ...] [(inferred-id NAME repeat) SUB-PAT ...]
MIN-REPEAT-SUB-PATS) MIN-REPEAT-SUB-PATS)
inferred-rules)))] inferred-rules)))]
[(repeat MIN MAX SUB-PAT) [(repeat MIN MAX SUB-PAT)
;; finite repeat ;; finite repeat
(let ([min (syntax-e #'MIN)] (begin
[max (syntax-e #'MAX)]) (define min (syntax-e #'MIN))
(recur (define max (syntax-e #'MAX))
(with-syntax ([(MIN-SUBPAT ...) (make-list min #'SUB-PAT)] (define new-rule-stx (with-syntax ([(MIN-SUBPAT ...) (make-list min #'SUB-PAT)]
[(EXTRA-SUBPAT ...) (make-list (- max min) #'SUB-PAT)]) [(EXTRA-SUBPAT ...) (make-list (- max min) #'SUB-PAT)])
#'(rule NAME (seq MIN-SUBPAT ... (maybe EXTRA-SUBPAT) ...))) ;; has to keep the same name to work correctly
#f))] #'(rule NAME (seq MIN-SUBPAT ... (maybe EXTRA-SUBPAT) ...))))
(recur new-rule-stx #f))]
[(maybe SUB-PAT) [(maybe SUB-PAT)
(begin (begin

@ -164,9 +164,9 @@
1 #f $1)] 1 #f $1)]
[(regexp-match #px"^\\{(\\d+)?,?(\\d+)?\\}$" $2) ; "{min,max}" with both min & max optional [(regexp-match #px"^\\{(\\d+)?,?(\\d+)?\\}$" $2) ; "{min,max}" with both min & max optional
=> (λ (m) => (λ (m)
(match-define (cons min-repeat max-repeat) (match-define (list min-repeat max-repeat)
(match m (match m
[(list _ min max) (cons (if min (string->number min) 0) [(list _ min max) (list (if min (string->number min) 0)
(and max (string->number max)))])) (and max (string->number max)))]))
(pattern-repeat (position->pos $1-start-pos) (pattern-repeat (position->pos $1-start-pos)
(position->pos $2-end-pos) (position->pos $2-end-pos)

Loading…
Cancel
Save