take a docstring on a sum

record, struct and enum take one; sum read it where its variants go.
This commit is contained in:
2026-10-19 14:48:43 +03:00
parent 1dd1cf0ae9
commit 2fa1ed21d7
4 changed files with 43 additions and 26 deletions

View File

@@ -248,10 +248,10 @@
;; between name and fields are not skipped, they already confuse the ;; between name and fields are not skipped, they already confuse the
;; writer ;; writer
(match form (match form
(('pub (and kind (or 'struct 'union 'enum)) (('pub (and kind (or 'struct 'union 'enum 'sum))
(? symbol? name) (? string? doc) . rest) (? symbol? name) (? string? doc) . rest)
(values doc (copy-form-source! form `(pub ,kind ,name ,@rest)))) (values doc (copy-form-source! form `(pub ,kind ,name ,@rest))))
(((and kind (or 'struct 'union 'enum)) (((and kind (or 'struct 'union 'enum 'sum))
(? symbol? name) (? string? doc) . rest) (? symbol? name) (? string? doc) . rest)
(values doc (copy-form-source! form `(,kind ,name ,@rest)))) (values doc (copy-form-source! form `(,kind ,name ,@rest))))
(_ (values #f form)))) (_ (values #f form))))
@@ -2045,29 +2045,35 @@
;;; purpose: a record would typedef shapeːcircle into the ordinary ;;; purpose: a record would typedef shapeːcircle into the ordinary
;;; namespace, where the constructor of that name already lives. As a ;;; namespace, where the constructor of that name already lives. As a
;;; tag it shares nothing. ;;; tag it shares nothing.
(define (process-sum form acc) (define (process-sum written acc)
(let* ((core (if (eq? (car form) 'pub) (cdr form) form)) (let-values (((doc form) (extract-aggregate-docstring written)))
(name (and (pair? (cdr core)) (cadr core))) (let* ((core (if (eq? (car form) 'pub) (cdr form) form))
(declared (and (pair? (cddr core)) (caddr core)))) (name (and (pair? (cdr core)) (cadr core)))
(unless (and (symbol? name) (list? declared) (pair? declared)) (declared (and (pair? (cddr core)) (caddr core))))
(sex-error form "sum needs a name and a list of variants" form)) (unless (and (symbol? name) (list? declared) (pair? declared))
(let ((variants (map (lambda (v) (parse-variant form v)) declared))) (sex-error form "sum needs a name and a list of variants" form))
(for-each (lambda (v) (let ((variants (map (lambda (v) (parse-variant form v)) declared)))
(for-each (lambda (field) (check-variant-field form name field)) (for-each (lambda (v)
(cdr v))) (for-each (lambda (field)
variants) (check-variant-field form name field))
(let* ((names (map car variants)) (cdr v)))
(twice (find (lambda (v) (> (count (cut eq? <> v) names) 1)) names))) variants)
(when twice (let* ((names (map car variants))
(sex-error form "variant is declared twice" twice))) (twice (find (lambda (v) (> (count (cut eq? <> v) names) 1))
(when (get-sum name) names)))
(sex-error form "sum is already defined" name)) (when twice
(add-sum! name variants) (sex-error form "variant is declared twice" twice)))
(for-each (lambda (v) (add-constructor! (qualify-op name (car v)) name (car v))) (when (get-sum name)
variants) (sex-error form "sum is already defined" name))
(fold match-sex-form acc (add-sum! name variants)
(map (lambda (f) (copy-form-source! form f)) (for-each (lambda (v)
(sum-forms name variants)))))) (add-constructor! (qualify-op name (car v)) name (car v)))
variants)
;; the docstring goes ahead of everything the sum is made of
(fold match-sex-form
(if doc (cons (copy-form-source! form (list 'comment doc)) acc) acc)
(map (lambda (f) (copy-form-source! form f))
(sum-forms name variants)))))))
(define (parse-variant form v) (define (parse-variant form v)
(unless (and (list? v) (= 2 (length v)) (symbol? (car v)) (list? (cadr v))) (unless (and (list? v) (= 2 (length v)) (symbol? (car v)) (list? (cadr v)))

View File

@@ -332,7 +332,8 @@
(('pub 'define name . body) body) (('pub 'define name . body) body)
(('pub (or 'record 'struct 'union) name . rest) (('pub (or 'record 'struct 'union) name . rest)
(field-types (find list? (remove comment-form? rest)))) (field-types (find list? (remove comment-form? rest))))
(('pub 'sum name (variants ...)) ((or ('pub 'sum name (variants ...))
('pub 'sum name (? string?) (variants ...)))
(append-map (lambda (v) (append-map (lambda (v)
(if (and (pair? v) (pair? (cdr v))) (field-types (second v)) (list))) (if (and (pair? v) (pair? (cdr v))) (field-types (second v)) (list)))
(remove comment-form? variants))) (remove comment-form? variants)))

View File

@@ -422,6 +422,13 @@
;; A string as the first body form (or after the name of a struct, ;; A string as the first body form (or after the name of a struct,
;; union or enum) is a docstring: it becomes a comment immediately ;; union or enum) is a docstring: it becomes a comment immediately
;; before the declaration, not a statement inside it. ;; before the declaration, not a statement inside it.
(test-group "a sum's docstring"
;; record, struct and enum took one, and sum read it as its variants
(test-assert "is a comment ahead of it"
(emits? "(sum shape \"A shape.\" ((dot ()) (box ((w int)))))" "/* A shape. */"))
(test-assert "and the sum is still one"
(emits? "(sum shape \"A shape.\" ((dot ()) (box ((w int)))))" "shapeǂbox")))
(test-group "docstrings" (test-group "docstrings"
(test-assert "appears before the function" (test-assert "appears before the function"
(emits? "(fn greet ((name (* char))) void \"Greet NAME.\" (printf \"hi\" name))" (emits? "(fn greet ((name (* char))) void \"Greet NAME.\" (printf \"hi\" name))"

View File

@@ -550,6 +550,9 @@ ahead of its pub forms."
(test "a private one is solved where it is" (test "a private one is solved where it is"
'() '()
(interface-of "(var n _ 42)")) (interface-of "(var n _ 42)"))
(test-assert "a pub sum's variants are looked at past its docstring"
(interface-reports? "(record cell ((v int))) (pub sum s \"doc\" ((a ((c cell))) (b ())))"
"held by value"))
(test-assert "held by value it is an error" (test-assert "held by value it is an error"
(interface-reports? "(record cell ((v int))) (pub record shape ((c cell)))" (interface-reports? "(record cell ((v int))) (pub record shape ((c cell)))"
"held by value")) "held by value"))