|
|
@ -2,7 +2,6 @@
|
|
|
|
(require brag/rules/stx-types
|
|
|
|
(require brag/rules/stx-types
|
|
|
|
racket/list
|
|
|
|
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)
|
|
|
@ -69,78 +68,73 @@
|
|
|
|
(values (apply append (reverse rules))
|
|
|
|
(values (apply append (reverse rules))
|
|
|
|
(apply append (reverse patterns))))
|
|
|
|
(apply append (reverse patterns))))
|
|
|
|
|
|
|
|
|
|
|
|
(with-syntax ([head (if inferred? #'inferred-prim-rule #'prim-rule)]
|
|
|
|
(with-syntax ([HEAD (if inferred? #'inferred-prim-rule #'prim-rule)]
|
|
|
|
[origin (syntax-case a-rule (rule) [(rule name (pat-head rest ...)) #'pat-head])])
|
|
|
|
[ORIGIN (syntax-case a-rule (rule) [(rule name (pat-head rest ...)) #'pat-head])])
|
|
|
|
(syntax-case a-rule (rule)
|
|
|
|
(syntax-case a-rule (rule)
|
|
|
|
[(rule name pat)
|
|
|
|
[(rule NAME PAT)
|
|
|
|
(syntax-case #'pat (id inferred-id lit token choice repeat maybe seq)
|
|
|
|
(syntax-case #'PAT (id inferred-id lit token choice repeat maybe seq)
|
|
|
|
|
|
|
|
|
|
|
|
;; The primitive types stay as they are:
|
|
|
|
;; The primitive types stay as they are:
|
|
|
|
[(id val)
|
|
|
|
[(id val)
|
|
|
|
(list #'(head origin name [pat]))]
|
|
|
|
(list #'(HEAD ORIGIN NAME [PAT]))]
|
|
|
|
[(inferred-id val reason)
|
|
|
|
[(inferred-id val reason)
|
|
|
|
(list #'(head origin name [pat]))]
|
|
|
|
(list #'(HEAD ORIGIN NAME [PAT]))]
|
|
|
|
[(lit val)
|
|
|
|
[(lit val)
|
|
|
|
(list #'(head origin name [pat]))]
|
|
|
|
(list #'(HEAD ORIGIN NAME [PAT]))]
|
|
|
|
[(token val)
|
|
|
|
[(token val)
|
|
|
|
(list #'(head origin name [pat]))]
|
|
|
|
(list #'(HEAD ORIGIN NAME [PAT]))]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; Everything else might need lifting:
|
|
|
|
;; Everything else might need lifting:
|
|
|
|
[(choice sub-pat ...)
|
|
|
|
[(choice SUB-PAT ...)
|
|
|
|
(begin
|
|
|
|
(begin
|
|
|
|
(define-values (inferred-ruless/rev new-sub-patss/rev)
|
|
|
|
(define-values (inferred-ruless/rev new-sub-patss/rev)
|
|
|
|
(for/fold ([rs '()] [ps '()])
|
|
|
|
(for/fold ([rs '()] [ps '()])
|
|
|
|
([p (syntax->list #'(sub-pat ...))])
|
|
|
|
([p (syntax->list #'(SUB-PAT ...))])
|
|
|
|
(let-values ([(new-r new-p)
|
|
|
|
(let-values ([(new-r new-p)
|
|
|
|
(lift-nonprimitive-pattern p)])
|
|
|
|
(lift-nonprimitive-pattern p)])
|
|
|
|
(values (cons new-r rs) (cons new-p ps)))))
|
|
|
|
(values (cons new-r rs) (cons new-p ps)))))
|
|
|
|
(with-syntax ([((sub-pat ...) ...) (reverse new-sub-patss/rev)])
|
|
|
|
(with-syntax ([((SUB-PAT ...) ...) (reverse new-sub-patss/rev)])
|
|
|
|
(append (list #'(head origin name [sub-pat ...] ...))
|
|
|
|
(append (list #'(HEAD ORIGIN NAME [SUB-PAT ...] ...))
|
|
|
|
(apply append (reverse inferred-ruless/rev)))))]
|
|
|
|
(apply append (reverse inferred-ruless/rev)))))]
|
|
|
|
|
|
|
|
|
|
|
|
[(repeat min sub-pat)
|
|
|
|
[(repeat MIN #f SUB-PAT)
|
|
|
|
|
|
|
|
;; indefinite repeat
|
|
|
|
(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 (syntax-e #'min))
|
|
|
|
(with-syntax ([(SUB-PAT ...) new-sub-pats]
|
|
|
|
(with-syntax ([(sub-pat ...) new-sub-pats]
|
|
|
|
[MIN-REPEAT-SUB-PATS (apply append (make-list (syntax-e #'MIN) new-sub-pats))])
|
|
|
|
[MIN-REPEAT-SUB-PATS (apply append (make-list (syntax-e #'min) new-sub-pats))])
|
|
|
|
(cons #`(HEAD ORIGIN NAME
|
|
|
|
(cons (cond [(= (syntax-e #'min) 0)
|
|
|
|
[(inferred-id NAME repeat) SUB-PAT ...]
|
|
|
|
#`(head origin name
|
|
|
|
MIN-REPEAT-SUB-PATS)
|
|
|
|
[(inferred-id name repeat) sub-pat ...]
|
|
|
|
|
|
|
|
[])]
|
|
|
|
|
|
|
|
[(= (syntax-e #'min) 1)
|
|
|
|
|
|
|
|
#`(head origin name
|
|
|
|
|
|
|
|
[(inferred-id name repeat) 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)))]
|
|
|
|
|
|
|
|
[(repeat MIN MAX SUB-PAT)
|
|
|
|
[(maybe sub-pat)
|
|
|
|
;; finite repeat
|
|
|
|
|
|
|
|
(let ([min (syntax-e #'MIN)]
|
|
|
|
|
|
|
|
[max (syntax-e #'MAX)])
|
|
|
|
|
|
|
|
(recur
|
|
|
|
|
|
|
|
(with-syntax ([(MIN-SUBPAT ...) (make-list min #'SUB-PAT)]
|
|
|
|
|
|
|
|
[(EXTRA-SUBPAT ...) (make-list (- max min) #'SUB-PAT)])
|
|
|
|
|
|
|
|
#'(rule NAME (seq MIN-SUBPAT ... (maybe EXTRA-SUBPAT) ...)))
|
|
|
|
|
|
|
|
#f))]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[(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 ...]
|
|
|
|
|
|
|
|
[])
|
|
|
|
[])
|
|
|
|
inferred-rules)))]
|
|
|
|
inferred-rules)))]
|
|
|
|
|
|
|
|
|
|
|
|
[(seq sub-pat ...)
|
|
|
|
[(seq SUB-PAT ...)
|
|
|
|
(begin
|
|
|
|
(begin
|
|
|
|
(define-values (inferred-rules new-sub-pats)
|
|
|
|
(define-values (inferred-rules new-sub-pats)
|
|
|
|
(lift-nonprimitive-patterns (syntax->list #'(sub-pat ...))))
|
|
|
|
(lift-nonprimitive-patterns (syntax->list #'(SUB-PAT ...))))
|
|
|
|
(with-syntax ([(sub-pat ...) new-sub-pats])
|
|
|
|
(with-syntax ([(SUB-PAT ...) new-sub-pats])
|
|
|
|
(cons #'(head origin name [sub-pat ...])
|
|
|
|
(cons #'(HEAD ORIGIN NAME [SUB-PAT ...])
|
|
|
|
inferred-rules)))])]))))
|
|
|
|
inferred-rules)))])]))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -174,7 +168,7 @@
|
|
|
|
#t]
|
|
|
|
#t]
|
|
|
|
[(choice sub-pat ...)
|
|
|
|
[(choice sub-pat ...)
|
|
|
|
#f]
|
|
|
|
#f]
|
|
|
|
[(repeat min val)
|
|
|
|
[(repeat min max val)
|
|
|
|
#f]
|
|
|
|
#f]
|
|
|
|
[(maybe sub-pat)
|
|
|
|
[(maybe sub-pat)
|
|
|
|
#f]
|
|
|
|
#f]
|
|
|
|