pull/6/head
Matthew Butterick 7 years ago
parent 3985aee8eb
commit 99f97b399c

@ -1,7 +1,8 @@
#lang racket/base #lang racket/base
(require brag/rules/stx-types (require brag/rules/stx-types
racket/list
(for-syntax racket/base)) (for-syntax racket/base))
(require (except-in sugar/debug repeat))
(provide flatten-rule (provide flatten-rule
flatten-rules flatten-rules
prim-rule) prim-rule)
@ -102,21 +103,32 @@
(begin (begin
(define-values (inferred-rules new-sub-pats) (define-values (inferred-rules new-sub-pats)
(lift-nonprimitive-pattern #'sub-pat)) (lift-nonprimitive-pattern #'sub-pat))
(with-syntax ([(sub-pat ...) new-sub-pats]) (report/file (syntax-e #'min))
(with-syntax ([(sub-pat ...) new-sub-pats]
[MIN-REPEAT-SUB-PATS (apply append (make-list (syntax-e #'min) new-sub-pats))])
(cons (cond [(= (syntax-e #'min) 0) (cons (cond [(= (syntax-e #'min) 0)
#`(head origin name #`(head origin name
[(inferred-id name repeat) sub-pat ...] [(inferred-id name repeat) sub-pat ...]
[])] [])]
[(= (syntax-e #'min) 1) [(= (syntax-e #'min) 1)
#`(head origin name #`(head origin name
[(inferred-id name repeat) sub-pat ...] [(inferred-id name repeat) sub-pat ...]
[sub-pat ...])]) [sub-pat ...])]
[(= (syntax-e #'min) 2)
#`(head origin name
[(inferred-id name repeat) sub-pat ...]
[sub-pat ... sub-pat ...])])
inferred-rules)
#;(cons (report/file #`(head origin name
[(inferred-id name repeat) sub-pat ...]
MIN-REPEAT-SUB-PATS))
inferred-rules)))] inferred-rules)))]
[(maybe sub-pat) [(maybe sub-pat)
(begin (begin
(define-values (inferred-rules new-sub-pats) (define-values (inferred-rules new-sub-pats)
(lift-nonprimitive-pattern #'sub-pat)) (lift-nonprimitive-pattern #'sub-pat))
(report*/file #'pat #'sub-pat)
(with-syntax ([(sub-pat ...) new-sub-pats]) (with-syntax ([(sub-pat ...) new-sub-pats])
(cons #'(head origin name (cons #'(head origin name
[sub-pat ...] [sub-pat ...]

@ -65,10 +65,8 @@
["|" ["|"
(token-PIPE lexeme)] (token-PIPE lexeme)]
[(:or "+" "*" [(:or "+" "*"
;; todo: consolidate next two patterns? (:: "{" (:* digit) (:? (:: "," (:* digit))) "}"))
(:: "{" (:* digit) "}") (token-REPEAT lexeme)]
(:: "{" (:* digit) "," (:* digit) "}"))
(token-REPEAT (let () (println lexeme) lexeme))]
[whitespace [whitespace
;; Skip whitespace ;; Skip whitespace
(return-without-pos (lex/1 input-port))] (return-without-pos (lex/1 input-port))]

@ -4,6 +4,7 @@
racket/list racket/list
racket/match racket/match
"rule-structs.rkt") "rule-structs.rkt")
(require sugar/debug)
;; A parser for grammars. ;; A parser for grammars.
@ -162,12 +163,16 @@
(pattern-repeat (position->pos $1-start-pos) (pattern-repeat (position->pos $1-start-pos)
(position->pos $2-end-pos) (position->pos $2-end-pos)
1 $1)] 1 $1)]
[(regexp-match #px"^\\{(\\d+)?,?(\\d+)?\\}$" $2) [(regexp-match #px"^\\{(\\d+)?,?(\\d+)?\\}$" $2) ; "{min,max}" with both min & max optional
=> (λ (m) => (λ (m)
(define pr (match m (match-define (cons min-repeat max-repeat)
[(list _ min max) (cons (and min (string->number min)) (match m
(and max (string->number max)))])) [(list _ min max) (cons (if min (string->number min) 0)
(error (format "~a" pr)))] (and max (string->number max)))]))
(report/file min-repeat)
(pattern-repeat (position->pos $1-start-pos)
(position->pos $2-end-pos)
min-repeat $1))]
[else [else
(error 'grammar-parse "unknown repetition operator ~e" $2)])] (error 'grammar-parse "unknown repetition operator ~e" $2)])]
[(atomic-pattern) [(atomic-pattern)

Loading…
Cancel
Save