forked from alex-eg/sex
index a unit past its first bad form
Record plain declarations, with location and docstring, and keep going when one toplevel form is wrong. sexc --check and --symbols print that. Specs, realizations, and closure helpers stay out of the index for future rework (@alex-eg).
This commit is contained in:
119
utils.scm
119
utils.scm
@@ -2,6 +2,7 @@
|
||||
scheme
|
||||
(scheme base) ; make-parameter
|
||||
(chicken base)
|
||||
(chicken condition)
|
||||
(chicken module)
|
||||
(chicken pathname)
|
||||
(chicken process-context)
|
||||
@@ -26,6 +27,19 @@
|
||||
stamp-form-source!
|
||||
sex-error
|
||||
sex-warning
|
||||
sex-condition?
|
||||
sex-diagnostics
|
||||
sex-symbols
|
||||
sex-scope-range
|
||||
sex-index-suppressed?
|
||||
make-note-box
|
||||
note-box-items
|
||||
record-diagnostic!
|
||||
record-diagnostic-at!
|
||||
record-exception!
|
||||
record-symbol!
|
||||
form-line-range
|
||||
reset-form-sources!
|
||||
(with-directory set-working-directory))
|
||||
|
||||
(define-syntax prog1
|
||||
@@ -162,20 +176,107 @@ wrap a form-building expression."
|
||||
(string-append (car src) ":" (number->string (cadr src)) ": ")
|
||||
"")))
|
||||
|
||||
;;; A long-running query collects into these instead of aborting the
|
||||
;;; process or writing warnings out. Each is #f, or a box whose car is
|
||||
;;; the newest-first list. `sex-scope-range' is the line span of the
|
||||
;;; block a local is being declared in, (start . end). Suppressing the
|
||||
;;; index drops forms the compiler invented, such as a sum's payloads.
|
||||
(define sex-diagnostics (make-parameter #f))
|
||||
(define sex-symbols (make-parameter #f))
|
||||
(define sex-scope-range (make-parameter #f))
|
||||
(define sex-index-suppressed? (make-parameter #f))
|
||||
|
||||
(define (make-note-box) (list '()))
|
||||
(define (note-box-items box) (car box))
|
||||
|
||||
(define (note-box-push! box item)
|
||||
(set-car! box (cons item (car box))))
|
||||
|
||||
(define (form-line-range form)
|
||||
"The (start . end) lines of FORM, or #f when it was not read from a file."
|
||||
(let ((src (and (pair? form) (form-source form))))
|
||||
(and src (cons (cadr src) (caddr src)))))
|
||||
|
||||
(define (record-diagnostic-at! severity file line end message args)
|
||||
(let ((box (sex-diagnostics)))
|
||||
(when box
|
||||
(note-box-push! box (list severity file line end message args)))))
|
||||
|
||||
(define (record-diagnostic! severity form message args)
|
||||
(let ((src (and (pair? form) (form-source form))))
|
||||
(record-diagnostic-at! severity
|
||||
(if src (car src) (current-source-file))
|
||||
(if src (cadr src) 1)
|
||||
(if src (caddr src) 1)
|
||||
message
|
||||
args)))
|
||||
|
||||
;;; Names the compiler minted: closure helpers, sum tags, variant payloads.
|
||||
(define (generated-name? name)
|
||||
(let ((s (symbol->string name)))
|
||||
(let loop ((i 0))
|
||||
(and (< i (string-length s))
|
||||
(or (memv (string-ref s i) '(#\ƛ #\ǂ #\ː))
|
||||
(loop (+ i 1)))))))
|
||||
|
||||
;;; SCOPED? asks for the current block. A local met outside one is dropped
|
||||
;;; rather than published as a global. A form the reader never stamped,
|
||||
;;; such as the prelude, is not a name the unit declared.
|
||||
(define (record-symbol! kind name form signature doc scoped?)
|
||||
(let ((box (sex-symbols))
|
||||
(scope (and scoped? (sex-scope-range))))
|
||||
(when (and box (not (sex-index-suppressed?))
|
||||
(symbol? name) (not (generated-name? name))
|
||||
(or (not scoped?) scope))
|
||||
(let ((src (and (pair? form) (form-source form))))
|
||||
(when src
|
||||
(note-box-push! box
|
||||
(list kind name (car src) (cadr src) (caddr src)
|
||||
signature doc
|
||||
(and scope (car scope))
|
||||
(and scope (cdr scope)))))))))
|
||||
|
||||
(define (sex-error form message . args)
|
||||
"Signal an error about FORM, prefixed with where it was written."
|
||||
(apply error (string-append (form-location form) message) args))
|
||||
"Signal an error about FORM, prefixed with where it was written.
|
||||
When diagnostics are being collected the same error is recorded first,
|
||||
so a caller that continues still knows about it."
|
||||
(record-diagnostic! 'error form message args)
|
||||
(abort
|
||||
(make-composite-condition
|
||||
(make-property-condition 'exn
|
||||
'message (string-append (form-location form) message)
|
||||
'arguments args)
|
||||
(make-property-condition 'sex 'form form))))
|
||||
|
||||
(define (sex-condition? exn)
|
||||
(and (condition? exn) ((condition-predicate 'sex) exn)))
|
||||
|
||||
;;; A `sex-error' has already been recorded. Anything else raised while
|
||||
;;; collecting -- a macro's own `error', an assertion -- is recorded
|
||||
;;; against FORM so the walk can continue.
|
||||
(define (record-exception! form exn)
|
||||
(unless (sex-condition? exn)
|
||||
(let ((message ((condition-property-accessor 'exn 'message #f) exn))
|
||||
(args ((condition-property-accessor 'exn 'arguments (list)) exn)))
|
||||
(record-diagnostic! 'error form (or message "error")
|
||||
(if message args (list exn))))))
|
||||
|
||||
(define (sex-warning form message . args)
|
||||
"Report something about FORM that does not stop the compilation.
|
||||
Goes to stderr, prefixed with where the form was written, so a warning
|
||||
reads like an error and sorts alongside one in a build log."
|
||||
(let ((port (current-error-port)))
|
||||
(display (form-location form) port)
|
||||
(display "warning: " port)
|
||||
(display message port)
|
||||
(for-each (lambda (arg) (display " " port) (display arg port)) args)
|
||||
(newline port)))
|
||||
reads like an error and sorts alongside one in a build log. Collected
|
||||
instead when diagnostics are being gathered."
|
||||
(if (sex-diagnostics)
|
||||
(record-diagnostic! 'warning form message args)
|
||||
(let ((port (current-error-port)))
|
||||
(display (form-location form) port)
|
||||
(display "warning: " port)
|
||||
(display message port)
|
||||
(for-each (lambda (arg) (display " " port) (display arg port)) args)
|
||||
(newline port))))
|
||||
|
||||
(define (reset-form-sources!)
|
||||
(set! +form-sources+ (make-hash-table eq?)))
|
||||
|
||||
(define (stamp-form-source! form src)
|
||||
"Give FORM and every subform that has none the location SRC. Used for
|
||||
|
||||
Reference in New Issue
Block a user