Skip to content

Commit

Permalink
parsers: repair contracts for structured data
Browse files Browse the repository at this point in the history
This commit fixes f95aebd (bestiary: simplify reader -> expander
protocol, 2024-11-05) and adjusts the pretty-printers to take it into
account.

No test would catch this because none exercise the parsers in non-syntax
mode.
  • Loading branch information
benknoble committed Nov 7, 2024
1 parent 5cd5771 commit b79ec91
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 20 deletions.
10 changes: 5 additions & 5 deletions parsers/foes.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@
(pure (list set-name name numbering specs))))

(define foes/pc
(listof (or/c (list/c 'import string?)
monster-info?
(listof monster-ability?)
foe/pc)))
(list/c (cons/c 'import (listof string?))
(cons/c 'info (listof monster-info?))
(cons/c 'ability (listof monster-ability?))
(cons/c 'foe (listof foe/pc))))

(define-flow foe-dupes
(~> sep (partition
[foe/pc (~> (>< second) collect check-duplicates)])))
[foe/pc (~> (>< second) collect check-duplicates)])))

(define big-bag-foes/p
(guard/p
Expand Down
6 changes: 3 additions & 3 deletions parsers/monster.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
(module+ test (require rackunit))

(define bestiary/c
(listof (or/c (list/c 'import string?)
monster-info?
(listof monster-ability?))))
(list/c (cons/c 'import (listof string?))
(cons/c 'info (listof monster-info?))
(cons/c 'ability (listof monster-ability?))))

#| monster syntax
- outside of <text> elements, whitespace is ignored
Expand Down
11 changes: 3 additions & 8 deletions pp/bestiary.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,13 @@
(define lang-line (text "#lang frosthaven-manager/bestiary"))

(define (pretty-bestiary bestiary #:lang-line? [lang-line? #t])
(define-values (imports monster-infos monster-abilitiess)
(~> (bestiary)
sep
(partition
[(esc (list/c 'import string?)) collect]
[monster-info? collect]
[(esc (listof monster-ability?)) collect])))
(define-values (imports monster-infos monster-abilities)
(~> (bestiary) sep (>< cdr)))
;; TODO avoid spurious empty lines if preceding group is empty
(<> (if lang-line? lang-line (<>))
(pretty-imports imports)
(pretty-monster-infos monster-infos)
(pretty-monster-abilitiess monster-abilitiess)
(pretty-monster-abilitiess (group-by monster-ability-set-name monster-abilities))
nl))

(define (pretty-imports imports)
Expand Down
2 changes: 1 addition & 1 deletion scribblings/parsers/foes.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ The result is @racket[syntax?] with source @racket[src] if @racket[syn?] is
true, and the datum it contains matches @racket[foes/pc].
}

@deftogether[(@defthing[foes/pc flat-contract? #:value (listof (or/c (list/c 'import string?) monster-info? (listof monster-ability?) foe/pc))]
@deftogether[(@defthing[foes/pc flat-contract? #:value (list/c (cons/c 'import (listof string?)) (cons/c 'info (listof monster-info?)) (cons/c 'ability (listof monster-ability?)) (cons/c 'foe (listof foe/pc)))]
@defthing[foe/pc flat-contract? #:value (list/c string? string? numbering/pc (listof spec/pc))]
@defthing[spec/pc flat-contract? #:value (hash/c num-players/c monster-type/pc #:immutable #t)]
@defthing[numbering/pc flat-contract? #:value (or/c "ordered" "random" #f)]
Expand Down
6 changes: 3 additions & 3 deletions scribblings/parsers/monster.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ true, and the datum it contains matches @racket[bestiary/c].

@defthing[bestiary/c flat-contract?
#:value
(listof (or/c (list/c 'import string?)
monster-info?
(listof monster-ability?)))]{
(list/c (cons/c 'import (listof string?))
(cons/c 'info (listof monster-info?))
(cons/c 'ability (listof monster-ability?)))]{
A contract for bestiary values.
}

Expand Down

0 comments on commit b79ec91

Please sign in to comment.