(import scheme (scheme base) ; make-parameter (chicken base) (chicken condition) (chicken module) (chicken pathname) (chicken process-context) srfi-1 srfi-69) (export get-env-var to-absolute-pathname comment-form? strip-header-comments list-split list-join recons current-source-file set-form-source! form-source form-file form-line form-end-line copy-form-source! 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 (syntax-rules () ((prog1 form . forms) (let ((res form)) (begin . forms) res)))) (define-syntax with-directory (syntax-rules () ((with-directory path form . forms) (let ((current-dir (current-directory))) (set-working-directory path) (prog1 (begin form . forms) (change-directory current-dir)))))) (define (get-env-var name) (get-environment-variable name)) (define (set-working-directory file) (change-directory (normalize-pathname (if (absolute-pathname? file) (pathname-directory file) (make-absolute-pathname (current-directory) (pathname-directory file)))))) (define (to-absolute-pathname pathname) (if (absolute-pathname? pathname) pathname (make-absolute-pathname (current-directory) pathname))) (define (comment-form? form) (and (pair? form) (eq? (car form) 'comment))) ;;; Remove the comment forms from the first COUNT elements of FORM -- ;;; its header -- so that the positional accessors reading it are not ;;; shifted by one (define (strip-header-comments form count) ;; This always rebuilds the list, so the location has to be carried ;; over explicitly -- otherwise every form loses it (copy-form-source! form (let loop ((rest form) (kept 0) (acc (list))) (cond ((null? rest) (reverse acc)) ((= kept count) (append (reverse acc) rest)) ((comment-form? (car rest)) (loop (cdr rest) kept acc)) (else (loop (cdr rest) (+ kept 1) (cons (car rest) acc))))))) (define (list-split src-list split-elt) ;; split '(1 2 / 3 4 / 5 6) by '/ -> '((1 2) (3 4) (5 6)) (fold (lambda (elt acc) (if (eq? elt split-elt) (append acc (list (list))) (append (drop-right acc 1) (list (append (last acc) (list elt)))))) (list (list)) src-list)) (define (list-join lists join-by) (drop-right (fold (lambda (elt acc) (append acc (list elt) (list join-by))) (list) lists) 1)) ;;; Reconstruct form (define (recons old-cons new-car new-cdr) (if (and (eq? new-car (car old-cons)) (eq? new-cdr (cdr old-cons))) old-cons ;; A rebuilt cell is still the same source form, so it keeps the ;; same location (copy-form-source! old-cons (cons new-car new-cdr)))) ;;; Source-location map. ;;; Our hand-written reader records the source location of each form ;;; here, keyed by the form's cons cell (eq?). This replaces CHICKEN's ;;; read-with-source-info / get-line-number, which only works when forms ;;; are produced by the built-in `read'. ;;; ;;; A location is (file line end-line): where the form starts and the ;;; line its closing delimiter is on. The file matters because imported ;;; modules paste their public forms into the current unit: those forms ;;; originate in another file and must be reported as such. The end line ;;; tells the C writer whether two toplevel forms were written with a ;;; blank line between them (define +form-sources+ (make-hash-table eq?)) ;;; The file `parse-all' is currently reading. Bound by the reader (define current-source-file (make-parameter "")) (define (set-form-source! form file line #!optional (end-line line)) (hash-table-set! +form-sources+ form (list file line end-line))) (define (form-source form) (hash-table-ref/default +form-sources+ form #f)) (define (form-file form) (let ((src (form-source form))) (and src (car src)))) (define (form-line form) (let ((src (form-source form))) (and src (cadr src)))) (define (form-end-line form) (let ((src (form-source form))) (and src (caddr src)))) (define (copy-form-source! from to) "Give TO the location of FROM, if FROM has one. Returns TO, so it can wrap a form-building expression." (let ((src (form-source from))) (when (and src (pair? to)) (hash-table-set! +form-sources+ to src))) to) ;;; Diagnostics ;;; ;;; Every form carries a location now, so an error can say where the ;;; wrong code was written (define (form-location form) "\"file:line: \" for FORM, or \"\" when it has none" (let ((src (form-source form))) (if src (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. 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. 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 macro expansions, which inherit the location of the call site the way a cpp macro does. Forms that already have a location keep it." (when (and src (pair? form)) (unless (form-source form) (hash-table-set! +form-sources+ form src)) (stamp-form-source! (car form) src) (stamp-form-source! (cdr form) src)) form)