Skip to content

Commit bff32bc

Browse files
committed
try to generate the cycle check bad error message in a canonical order
1 parent 4c161ed commit bff32bc

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

redex-lib/redex/private/cycle-check.rkt

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,41 @@
7777
(define path (loop neighbor))
7878
(and path (cons node path)))]))))
7979

80-
(define full-path (cons cycle bad-path))
81-
(define all/backwards (for/list ([nt (in-list (reverse bad-path))])
82-
(define stx-lst (hash-ref nt-identifiers nt))
83-
(define lst (if (syntax? stx-lst) (syntax->list stx-lst) stx-lst))
84-
(define stx (car lst))
85-
(datum->syntax stx nt stx)))
80+
(define bad-path/stx-objects
81+
(for/list ([nt (in-list bad-path)])
82+
(define stx-lst (hash-ref nt-identifiers nt))
83+
(define lst (if (syntax? stx-lst) (syntax->list stx-lst) stx-lst))
84+
(define stx (car lst))
85+
(datum->syntax stx nt stx)))
86+
87+
(define bad-path-starting-point
88+
(for/fold ([smallest (car bad-path/stx-objects)])
89+
([point (in-list (cdr bad-path/stx-objects))])
90+
(define smaller?
91+
(< (or (syntax-position smallest) +inf.0)
92+
(or (syntax-position point) +inf.0)))
93+
(if smaller?
94+
smallest
95+
point)))
96+
(define bad-path-in-canonical-order
97+
(let loop ([bad-path bad-path/stx-objects])
98+
(cond
99+
[(equal? bad-path-starting-point (car bad-path))
100+
bad-path]
101+
[else
102+
(loop (append (cdr bad-path) (list (car bad-path))))])))
103+
(define full-path (cons (car (reverse bad-path-in-canonical-order))
104+
bad-path-in-canonical-order))
86105
(raise-syntax-error 'define-language
87-
(if (= 1 (length bad-path))
106+
(if (= 1 (length bad-path-in-canonical-order))
88107
(format "the non-terminal ~a is defined in terms of itself"
89-
(car bad-path))
108+
(syntax-e (car bad-path-in-canonical-order)))
90109
(format
91110
"found a cycle of non-terminals that doesn't consume input:~a"
92111
(apply
93112
string-append
94-
(for/list ([node (in-list full-path)])
95-
(format " ~a" node)))))
113+
(for/list ([node (in-list bad-path-in-canonical-order)])
114+
(format " ~a" (syntax-e node))))))
96115
stx
97-
(car all/backwards)
98-
(cdr all/backwards))))
116+
(car bad-path-in-canonical-order)
117+
(cdr bad-path-in-canonical-order))))

0 commit comments

Comments
 (0)