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:
@@ -21,6 +21,7 @@
|
||||
(chicken module)
|
||||
(chicken pathname)
|
||||
(chicken platform) ; software-version, machine-type
|
||||
(chicken port) ; open-input-string
|
||||
(only (chicken string) substring-index)
|
||||
(only srfi-1 every any) ; srfi-1 also has an append-reverse
|
||||
utils)
|
||||
@@ -28,6 +29,7 @@
|
||||
(export
|
||||
read-from-file
|
||||
read-raw-forms
|
||||
read-sex-string
|
||||
|
||||
current-features
|
||||
platform-features
|
||||
@@ -613,3 +615,9 @@
|
||||
(parameterize ((current-source-file "stdin"))
|
||||
(parse-all (current-input-port)))
|
||||
(read-from-file input-source)))
|
||||
|
||||
;;; TEXT is a buffer the editor holds, named SOURCE-FILE so locations
|
||||
;;; and imports agree with the file it will be saved as.
|
||||
(define (read-sex-string text source-file)
|
||||
(parameterize ((current-source-file source-file))
|
||||
(parse-all (open-input-string text))))
|
||||
|
||||
85
semen.scm
85
semen.scm
@@ -4,6 +4,7 @@
|
||||
scheme
|
||||
(only (scheme base) make-parameter)
|
||||
(chicken base)
|
||||
(chicken condition)
|
||||
(chicken keyword)
|
||||
(chicken string)
|
||||
(chicken module)
|
||||
@@ -47,18 +48,27 @@
|
||||
(string-append "called at " (form-file call) ":"
|
||||
(number->string (form-line call)))))))
|
||||
|
||||
(define (process-one forms acc)
|
||||
(if (macro? (car forms))
|
||||
(process-rec (macroexpand (car forms) (cdr forms)) acc)
|
||||
(let* ((processed (match-sex-form (car forms) acc))
|
||||
(new (take-until processed acc)))
|
||||
(process-rec (cdr forms)
|
||||
(append new acc)))))
|
||||
|
||||
;;; One bad toplevel form is one diagnostic. The rest of the unit is
|
||||
;;; still worth indexing. A compile that is not collecting still stops
|
||||
;;; at the first error, as it always has.
|
||||
(define (process-rec forms acc)
|
||||
(cond
|
||||
((null? forms) (reverse acc))
|
||||
((macro? (car forms))
|
||||
(process-rec
|
||||
(macroexpand (car forms) (cdr forms))
|
||||
acc))
|
||||
(else
|
||||
(let* ((processed (match-sex-form (car forms) acc))
|
||||
(new (take-until processed acc)))
|
||||
(process-rec (cdr forms)
|
||||
(append new acc))))))
|
||||
((sex-diagnostics)
|
||||
(handle-exceptions exn
|
||||
(begin
|
||||
(record-exception! (car forms) exn)
|
||||
(process-rec (cdr forms) acc))
|
||||
(process-one forms acc)))
|
||||
(else (process-one forms acc))))
|
||||
|
||||
(define (take-until forms tail)
|
||||
(if (eq? forms tail)
|
||||
@@ -207,6 +217,7 @@
|
||||
;;; Typdef
|
||||
|
||||
(define (process-typedef form new-type target acc)
|
||||
(record-symbol! 'typedef new-type form target #f #f)
|
||||
(cons (copy-form-source! form `(typedef ,target ,new-type)) acc))
|
||||
|
||||
;;; Fn processing
|
||||
@@ -297,10 +308,22 @@
|
||||
"a function's types are written out; `_' is a variable's"
|
||||
(sex-fn-name expanded)))
|
||||
(add-name-type! (sex-fn-name expanded) (fn-type-of expanded))
|
||||
(record-symbol! 'fn (sex-fn-name expanded) sex-fn-raw
|
||||
`(fn ,(sex-fn-name expanded)
|
||||
,(sex-fn-arglist expanded)
|
||||
,(sex-fn-return-type expanded))
|
||||
doc #f)
|
||||
(let* ((env (make-fn-env expanded))
|
||||
;; the parameters are the body's outermost scope
|
||||
(header (take expanded boundary))
|
||||
(body (parameterize ((operand-position? #f))
|
||||
(body (parameterize ((sex-scope-range (form-line-range sex-fn-raw))
|
||||
(operand-position? #f))
|
||||
(for-each
|
||||
(lambda (param)
|
||||
(when (named-arg? param)
|
||||
(record-symbol! 'var (first param) sex-fn-raw
|
||||
(named-arg-type param) #f #t)))
|
||||
(sex-fn-arglist expanded))
|
||||
(walk-body (drop expanded boundary) env)))
|
||||
;; A function is header and body stitched together, so the
|
||||
;; result is fresh structure: without this the definition
|
||||
@@ -424,10 +447,11 @@
|
||||
(or (hash-table-ref/default (car scopes) name #f)
|
||||
(search (cdr scopes))))))
|
||||
|
||||
(define (with-scope env body)
|
||||
(let ((enclosing (hash-table-ref env :scopes)))
|
||||
(define (with-scope env form body)
|
||||
(let ((enclosing (hash-table-ref env :scopes))
|
||||
(range (or (form-line-range form) (sex-scope-range))))
|
||||
(set! (hash-table-ref env :scopes) (cons (make-hash-table) enclosing))
|
||||
(let ((walked (body)))
|
||||
(let ((walked (parameterize ((sex-scope-range range)) (body))))
|
||||
(set! (hash-table-ref env :scopes) enclosing)
|
||||
walked)))
|
||||
|
||||
@@ -496,10 +520,10 @@
|
||||
(case (car form)
|
||||
((do default)
|
||||
(parameterize ((operand-position? #f))
|
||||
(with-scope env (lambda () (walk-parts form env)))))
|
||||
(with-scope env form (lambda () (walk-parts form env)))))
|
||||
|
||||
((for while if switch case case/fallthrough)
|
||||
(with-scope env (lambda () (walk-control form env))))
|
||||
(with-scope env form (lambda () (walk-control form env))))
|
||||
|
||||
((lambda)
|
||||
(let ((name (aux-name! env make-lambda-name)))
|
||||
@@ -525,7 +549,8 @@
|
||||
(list))))
|
||||
env)))
|
||||
(when (>= (length walked) 3)
|
||||
(declare-name! env (second walked) (third walked)))
|
||||
(declare-name! env (second walked) (third walked))
|
||||
(record-symbol! 'var (second walked) walked (third walked) #f #t))
|
||||
walked))
|
||||
|
||||
((return)
|
||||
@@ -1436,6 +1461,9 @@
|
||||
;;; Record the named structs, unions and enums in the type database
|
||||
(define (process-struct sex-struct acc)
|
||||
(let-values (((doc form) (extract-aggregate-docstring sex-struct)))
|
||||
(let ((core (if (eq? (car form) 'pub) (cdr form) form)))
|
||||
(when (and (pair? (cdr core)) (symbol? (cadr core)))
|
||||
(record-symbol! (car core) (cadr core) sex-struct #f doc #f)))
|
||||
(register-aggregate! form)
|
||||
(with-docstring doc form acc)))
|
||||
|
||||
@@ -1473,11 +1501,20 @@
|
||||
(for-each (lambda (v)
|
||||
(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)))))))
|
||||
(record-symbol! 'sum name written `(struct ,name) doc #f)
|
||||
(for-each (lambda (v)
|
||||
(record-symbol! 'fn (qualify-op name (car v)) written
|
||||
`(fn ,(map second (cdr v)) ,name)
|
||||
#f #f))
|
||||
variants)
|
||||
;; the docstring goes ahead of everything the sum is made of.
|
||||
;; The generated structs and typedefs are the compiler's, and
|
||||
;; the sum itself is already indexed.
|
||||
(parameterize ((sex-index-suppressed? #t))
|
||||
(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)
|
||||
(unless (and (list? v) (= 2 (length v)) (symbol? (car v)) (list? (cadr v)))
|
||||
@@ -1602,7 +1639,8 @@
|
||||
(for-each (lambda (value)
|
||||
(let ((constant (if (pair? value) (car value) value)))
|
||||
(when (symbol? constant)
|
||||
(add-name-type! constant `(enum ,name)))))
|
||||
(add-name-type! constant `(enum ,name))
|
||||
(record-symbol! 'const constant form `(enum ,name) #f #f))))
|
||||
(remove comment-form? (caddr f)))))))))
|
||||
|
||||
;;; No function around a toplevel form, so no scope chain: what a
|
||||
@@ -1650,7 +1688,8 @@
|
||||
(copy-form-source! sex-var (cons qualifier core))
|
||||
core)))
|
||||
(when (and (pair? (cdr core)) (pair? (cddr core)) (symbol? (second core)))
|
||||
(add-name-type! (second core) (third core)))
|
||||
(add-name-type! (second core) (third core))
|
||||
(record-symbol! 'var (second core) sex-var (third core) #f #f))
|
||||
(cons form (append (hash-table-ref env :lambda-aux-code) acc))))
|
||||
|
||||
;;; A compound literal is not a constant, though cc lets one pass at file
|
||||
|
||||
@@ -16,7 +16,8 @@
|
||||
apply-macro
|
||||
defmacro
|
||||
macro-definition
|
||||
restore-macro!)
|
||||
restore-macro!
|
||||
reset-macros!)
|
||||
|
||||
(define (cat-syms s-1 s-2)
|
||||
(fmt #f s-1 s-2))
|
||||
@@ -30,7 +31,15 @@
|
||||
(define (comment . _)
|
||||
(void))
|
||||
|
||||
;;; Every name given a macro, so a later unit can take them all off.
|
||||
(define +macro-names+ (list))
|
||||
|
||||
(define (note-macro-name! name)
|
||||
(unless (memq name +macro-names+)
|
||||
(set! +macro-names+ (cons name +macro-names+))))
|
||||
|
||||
(define (register-macro name arglist body)
|
||||
(note-macro-name! name)
|
||||
(put! name 'sex-macro
|
||||
`(lambda ,arglist
|
||||
;; A macro body is ordinary Scheme, evaluated at compile
|
||||
@@ -96,9 +105,14 @@
|
||||
|
||||
(define (restore-macro! name definition)
|
||||
(if definition
|
||||
(put! name 'sex-macro definition)
|
||||
(begin (note-macro-name! name)
|
||||
(put! name 'sex-macro definition))
|
||||
(remprop! name 'sex-macro)))
|
||||
|
||||
(define (reset-macros!)
|
||||
(for-each (lambda (name) (remprop! name 'sex-macro)) +macro-names+)
|
||||
(set! +macro-names+ (list)))
|
||||
|
||||
(define (defmacro form)
|
||||
(let ((arglist (car form))
|
||||
(body (cdr form)))
|
||||
|
||||
@@ -21,7 +21,8 @@
|
||||
load-persistent-module-paths
|
||||
check-public-interface
|
||||
module-dependencies
|
||||
read-public-interface)
|
||||
read-public-interface
|
||||
reset-modules!)
|
||||
|
||||
(define +persistent-module-paths+ (list))
|
||||
|
||||
@@ -35,6 +36,14 @@
|
||||
(define (mark-imported! path)
|
||||
(set! +imported-modules+ (cons (normalize-pathname path) +imported-modules+)))
|
||||
|
||||
;;; Drop what the previous unit imported, read, and exported. The search
|
||||
;;; path stays: it is the process's, not the unit's.
|
||||
(define (reset-modules!)
|
||||
(set! +imported-modules+ (list))
|
||||
(set! +modules-read+ (list))
|
||||
(set! +modules-expanded+ (list))
|
||||
(set! +module-exports+ (list)))
|
||||
|
||||
(define (get-modules-public-forms import-form)
|
||||
;; How Sex handles modules:
|
||||
;; For each module IMPORT-FORM names, construct path, find module by
|
||||
|
||||
426
sex-query.scm
Normal file
426
sex-query.scm
Normal file
@@ -0,0 +1,426 @@
|
||||
;;; Queries over one Sex unit: diagnostics, the symbol table, and a
|
||||
;;; slice of the C that unit compiles to. `sexc --check` and the
|
||||
;;; language server both call this. It is not a second compiler.
|
||||
;;;
|
||||
;;; Specs, realizations, and closures are not in the language, and
|
||||
;;; nothing here records them.
|
||||
|
||||
(import
|
||||
scheme
|
||||
(chicken base)
|
||||
(chicken condition)
|
||||
(chicken io)
|
||||
(chicken irregex)
|
||||
(chicken module)
|
||||
(chicken port)
|
||||
(chicken pretty-print)
|
||||
(chicken string)
|
||||
fmt-c-writer
|
||||
matchable
|
||||
reader
|
||||
semen
|
||||
sex-macros
|
||||
sex-modules
|
||||
srfi-1
|
||||
srfi-13
|
||||
types
|
||||
utils)
|
||||
|
||||
(export
|
||||
compiler-prelude
|
||||
analyze-text
|
||||
analyze-file
|
||||
analyze-port
|
||||
analysis-read?
|
||||
analysis-path
|
||||
analysis-diagnostics
|
||||
analysis-symbols
|
||||
analysis-includes
|
||||
analysis-forms
|
||||
analysis-lookup
|
||||
analysis-completions
|
||||
analysis-document-symbols
|
||||
analysis-expand
|
||||
symbol-summary
|
||||
symbol-hover
|
||||
sym-kind
|
||||
sym-name
|
||||
sym-file
|
||||
sym-line
|
||||
sym-end
|
||||
sym-signature
|
||||
sym-doc
|
||||
sym-scope-start
|
||||
sym-scope-end
|
||||
diag-severity
|
||||
diag-file
|
||||
diag-line
|
||||
diag-end
|
||||
diag-message
|
||||
diag-args
|
||||
write-diagnostics
|
||||
write-symbols
|
||||
parse-expand-lines
|
||||
sex-name->c
|
||||
c-name->sex
|
||||
probe-text)
|
||||
|
||||
;;; The same prelude `sexc` compiles in front of every unit. Kept here
|
||||
;;; so a query and a build cannot drift apart.
|
||||
(define (compiler-prelude)
|
||||
'((include inttypes.h)
|
||||
(include stdbool.h)
|
||||
(include stddef.h)
|
||||
(typedef u8 uint8-t)
|
||||
(typedef i8 int8-t)
|
||||
(typedef u16 uint16-t)
|
||||
(typedef i16 int16-t)
|
||||
(typedef u32 uint32-t)
|
||||
(typedef i32 int32-t)
|
||||
(typedef u64 uint64-t)
|
||||
(typedef i64 int64-t)))
|
||||
|
||||
(define (reset-compiler-state!)
|
||||
(reset-form-sources!)
|
||||
(reset-name-tables!)
|
||||
(reset-macros!)
|
||||
(reset-modules!))
|
||||
|
||||
;;; read? is #f only when the reader itself failed. Semantic errors are
|
||||
;;; diagnostics and the unit is otherwise analyzed.
|
||||
;;;
|
||||
;;; symbols: (kind name file line end signature doc scope-start scope-end)
|
||||
;;; scope-start is #f for a name visible through the whole unit.
|
||||
;;; diagnostics: (severity file line end message args)
|
||||
(define (make-analysis read? path diagnostics symbols includes forms)
|
||||
(vector read? path diagnostics symbols includes forms))
|
||||
|
||||
(define (analysis-read? a) (vector-ref a 0))
|
||||
(define (analysis-path a) (vector-ref a 1))
|
||||
(define (analysis-diagnostics a) (vector-ref a 2))
|
||||
(define (analysis-symbols a) (vector-ref a 3))
|
||||
(define (analysis-includes a) (vector-ref a 4))
|
||||
(define (analysis-forms a) (vector-ref a 5))
|
||||
|
||||
(define (sym-kind s) (list-ref s 0))
|
||||
(define (sym-name s) (list-ref s 1))
|
||||
(define (sym-file s) (list-ref s 2))
|
||||
(define (sym-line s) (list-ref s 3))
|
||||
(define (sym-end s) (list-ref s 4))
|
||||
(define (sym-signature s) (list-ref s 5))
|
||||
(define (sym-doc s) (list-ref s 6))
|
||||
(define (sym-scope-start s) (list-ref s 7))
|
||||
(define (sym-scope-end s) (list-ref s 8))
|
||||
|
||||
(define (diag-severity d) (list-ref d 0))
|
||||
(define (diag-file d) (list-ref d 1))
|
||||
(define (diag-line d) (list-ref d 2))
|
||||
(define (diag-end d) (list-ref d 3))
|
||||
(define (diag-message d) (list-ref d 4))
|
||||
(define (diag-args d) (list-ref d 5))
|
||||
|
||||
;;; "file:line: text" is how the reader reports a bad buffer. Pull the
|
||||
;;; line back out so the diagnostic points at it.
|
||||
(define location-prefix (irregex "^(.+):([0-9]+): (.*)$"))
|
||||
|
||||
(define (note-read-error! exn)
|
||||
(let ((message ((condition-property-accessor 'exn 'message #f) exn))
|
||||
(args ((condition-property-accessor 'exn 'arguments (list)) exn)))
|
||||
(let ((m (and message (irregex-match location-prefix message))))
|
||||
(if m
|
||||
(let ((line (string->number (irregex-match-substring m 2))))
|
||||
(record-diagnostic-at! 'error
|
||||
(irregex-match-substring m 1)
|
||||
line line
|
||||
(irregex-match-substring m 3)
|
||||
args))
|
||||
(record-exception! #f exn)))))
|
||||
|
||||
(define (include-names form)
|
||||
(match form
|
||||
(('include . names) names)
|
||||
(('pub 'include . names) names)
|
||||
(_ (list))))
|
||||
|
||||
(define (gather-includes raw processed)
|
||||
(delete-duplicates
|
||||
(append (if (pair? raw) (append-map include-names raw) (list))
|
||||
(if (pair? processed) (append-map include-names processed) (list)))
|
||||
equal?))
|
||||
|
||||
(define (mark-unit! path)
|
||||
(when (and (string? path)
|
||||
(not (equal? path "stdin"))
|
||||
(not (equal? path "<unknown>")))
|
||||
(mark-imported! (to-absolute-pathname path))))
|
||||
|
||||
(define (process-unit raw path)
|
||||
(mark-unit! path)
|
||||
(handle-exceptions exn
|
||||
(record-exception! #f exn)
|
||||
(check-public-interface raw))
|
||||
(handle-exceptions exn
|
||||
(begin (record-exception! #f exn) #f)
|
||||
(semen-process (append (compiler-prelude) raw))))
|
||||
|
||||
;;; Newest-first, and a prototype recorded before its body loses to the
|
||||
;;; body, which is where the docstring is.
|
||||
(define (unique-symbols syms)
|
||||
(let loop ((syms syms) (seen (list)) (acc (list)))
|
||||
(if (null? syms)
|
||||
(reverse acc)
|
||||
(let ((key (list (sym-kind (car syms)) (sym-name (car syms))
|
||||
(sym-file (car syms)) (sym-line (car syms)))))
|
||||
(if (member key seen)
|
||||
(loop (cdr syms) seen acc)
|
||||
(loop (cdr syms) (cons key seen) (cons (car syms) acc)))))))
|
||||
|
||||
(define (finish read? path raw processed diags syms)
|
||||
(make-analysis read? path
|
||||
(reverse (note-box-items diags))
|
||||
(unique-symbols (note-box-items syms))
|
||||
(gather-includes raw processed)
|
||||
processed))
|
||||
|
||||
(define (analyze-text text path)
|
||||
(reset-compiler-state!)
|
||||
(let ((diags (make-note-box))
|
||||
(syms (make-note-box)))
|
||||
(parameterize ((sex-diagnostics diags)
|
||||
(sex-symbols syms)
|
||||
(current-source-file path))
|
||||
(let ((raw (handle-exceptions exn
|
||||
(begin (note-read-error! exn) #f)
|
||||
(read-sex-string text path))))
|
||||
(if raw
|
||||
(finish #t path raw (process-unit raw path) diags syms)
|
||||
(finish #f path #f #f diags syms))))))
|
||||
|
||||
(define (analyze-port port path)
|
||||
(analyze-text (read-string #f port) path))
|
||||
|
||||
(define (analyze-file path)
|
||||
(let ((abs (to-absolute-pathname path)))
|
||||
(reset-compiler-state!)
|
||||
(let ((diags (make-note-box))
|
||||
(syms (make-note-box)))
|
||||
(parameterize ((sex-diagnostics diags)
|
||||
(sex-symbols syms)
|
||||
(current-source-file abs))
|
||||
(let ((raw (handle-exceptions exn
|
||||
(begin (note-read-error! exn) #f)
|
||||
(read-from-file path))))
|
||||
(if raw
|
||||
(finish #t abs raw (process-unit raw abs) diags syms)
|
||||
(finish #f abs #f #f diags syms)))))))
|
||||
|
||||
(define (kind-rank kind)
|
||||
(case kind
|
||||
((fn) 0) ((sum) 1) ((struct) 2) ((union) 3) ((enum) 4)
|
||||
((typedef) 5) ((const) 6) ((var) 7)
|
||||
(else 9)))
|
||||
|
||||
(define (symbol-width sym)
|
||||
(if (sym-scope-start sym)
|
||||
(- (sym-scope-end sym) (sym-scope-start sym))
|
||||
1000000000))
|
||||
|
||||
;;; A tighter block beats a wider one. At the same span the later
|
||||
;;; declaration wins, and a struct beats the typedef of the same name.
|
||||
(define (symbol-better? a b)
|
||||
(let ((wa (symbol-width a))
|
||||
(wb (symbol-width b)))
|
||||
(cond ((< wa wb) #t)
|
||||
((> wa wb) #f)
|
||||
((> (sym-line a) (sym-line b)) #t)
|
||||
((< (sym-line a) (sym-line b)) #f)
|
||||
(else (< (kind-rank (sym-kind a)) (kind-rank (sym-kind b)))))))
|
||||
|
||||
(define (symbol-visible? sym line)
|
||||
(let ((start (sym-scope-start sym)))
|
||||
(or (not start)
|
||||
(and (>= line (sym-line sym))
|
||||
(<= line (sym-scope-end sym))))))
|
||||
|
||||
(define (analysis-lookup analysis name line)
|
||||
(let loop ((syms (analysis-symbols analysis)) (best #f))
|
||||
(if (null? syms)
|
||||
best
|
||||
(let ((s (car syms)))
|
||||
(loop (cdr syms)
|
||||
(if (and (eq? (sym-name s) name)
|
||||
(symbol-visible? s line)
|
||||
(or (not best) (symbol-better? s best)))
|
||||
s
|
||||
best))))))
|
||||
|
||||
(define (analysis-completions analysis prefix line)
|
||||
(let loop ((syms (analysis-symbols analysis)) (seen (list)) (acc (list)))
|
||||
(if (null? syms)
|
||||
(reverse acc)
|
||||
(let ((name (sym-name (car syms))))
|
||||
(if (or (memq name seen)
|
||||
(not (string-prefix? prefix (symbol->string name))))
|
||||
(loop (cdr syms) (if (memq name seen) seen (cons name seen)) acc)
|
||||
(let ((hit (analysis-lookup analysis name line)))
|
||||
(loop (cdr syms) (cons name seen)
|
||||
(if hit (cons hit acc) acc))))))))
|
||||
|
||||
(define (analysis-document-symbols analysis)
|
||||
(filter (lambda (s) (equal? (sym-file s) (analysis-path analysis)))
|
||||
(analysis-symbols analysis)))
|
||||
|
||||
(define (sexp->string form)
|
||||
(with-output-to-string (lambda () (write form))))
|
||||
|
||||
(define (symbol-summary sym)
|
||||
(let ((sig (sym-signature sym)))
|
||||
(sexp->string
|
||||
(case (sym-kind sym)
|
||||
((fn) sig)
|
||||
((var const) `(var ,(sym-name sym) ,sig))
|
||||
((typedef) `(typedef ,(sym-name sym) ,sig))
|
||||
(else `(,(sym-kind sym) ,(sym-name sym)))))))
|
||||
|
||||
(define (symbol-hover sym)
|
||||
(let ((doc (sym-doc sym)))
|
||||
(if doc
|
||||
(string-append (symbol-summary sym) "\n" doc)
|
||||
(symbol-summary sym))))
|
||||
|
||||
;;; #line N "file" starts a chunk. A chunk is kept when N lies in the
|
||||
;;; inclusive range. Text before the first directive -- the prelude --
|
||||
;;; belongs to no Sex line and is left out of a slice.
|
||||
(define line-directive (irregex "^#line ([0-9]+) "))
|
||||
|
||||
(define (split-lines text)
|
||||
(let loop ((i 0) (start 0) (acc (list)))
|
||||
(cond ((>= i (string-length text))
|
||||
(reverse (cons (substring text start i) acc)))
|
||||
((char=? (string-ref text i) #\newline)
|
||||
(loop (+ i 1) (+ i 1) (cons (substring text start i) acc)))
|
||||
((char=? (string-ref text i) #\return)
|
||||
(let ((next (if (and (< (+ i 1) (string-length text))
|
||||
(char=? (string-ref text (+ i 1)) #\newline))
|
||||
(+ i 2)
|
||||
(+ i 1))))
|
||||
(loop next next (cons (substring text start i) acc))))
|
||||
(else (loop (+ i 1) start acc)))))
|
||||
|
||||
(define (keep-chunk? sex-line start end)
|
||||
(and sex-line start end (<= start sex-line end)))
|
||||
|
||||
(define (slice-c-text text start end)
|
||||
(let loop ((lines (split-lines text))
|
||||
(sex-line #f)
|
||||
(chunk (list))
|
||||
(acc (list)))
|
||||
(define (flush line chunk acc)
|
||||
(if (keep-chunk? line start end) (append chunk acc) acc))
|
||||
(if (null? lines)
|
||||
(let ((kept (reverse (flush sex-line chunk acc))))
|
||||
(if (null? kept) "" (string-append (string-intersperse kept "\n") "\n")))
|
||||
(let ((m (irregex-search line-directive (car lines))))
|
||||
(if m
|
||||
(loop (cdr lines)
|
||||
(string->number (irregex-match-substring m 1))
|
||||
(list (car lines))
|
||||
(flush sex-line chunk acc))
|
||||
(loop (cdr lines) sex-line (cons (car lines) chunk) acc))))))
|
||||
|
||||
;;; END #f is the whole buffer, directives and all. Otherwise the C
|
||||
;;; whose Sex line falls in START..END.
|
||||
(define (analysis-expand analysis start end)
|
||||
(let ((forms (analysis-forms analysis)))
|
||||
(and forms
|
||||
(parameterize ((sex-line-directives 'statement))
|
||||
(let ((text (with-output-to-string (lambda () (emit-c forms)))))
|
||||
(if end (slice-c-text text start end) text))))))
|
||||
|
||||
(define (diagnostic->sexp d)
|
||||
(append (list (diag-severity d) (diag-file d) (diag-line d) (diag-end d)
|
||||
(diag-message d))
|
||||
(diag-args d)))
|
||||
|
||||
(define (symbol->sexp s)
|
||||
(list (sym-kind s) (sym-name s) (sym-file s) (sym-line s) (sym-end s)
|
||||
(sym-signature s) (sym-doc s) (sym-scope-start s) (sym-scope-end s)))
|
||||
|
||||
(define (write-diagnostics analysis port)
|
||||
(pretty-print `(diagnostics ,@(map diagnostic->sexp (analysis-diagnostics analysis)))
|
||||
port))
|
||||
|
||||
(define (write-symbols analysis port)
|
||||
(pretty-print `(symbols ,@(map symbol->sexp (analysis-symbols analysis)))
|
||||
port))
|
||||
|
||||
;;; "START:END", or START with the end left off, which is the whole
|
||||
;;; buffer. ":" is the whole buffer too.
|
||||
(define (parse-expand-lines spec)
|
||||
(let ((colon (substring-index ":" spec)))
|
||||
(cond
|
||||
((not colon)
|
||||
(let ((n (string->number spec)))
|
||||
(or (and n (cons n #f))
|
||||
(error "--expand-lines wants START or START:END, got" spec))))
|
||||
(else
|
||||
(let ((start-text (substring spec 0 colon))
|
||||
(end-text (substring spec (+ colon 1))))
|
||||
(cond
|
||||
((and (string=? start-text "") (string=? end-text ""))
|
||||
(cons #f #f))
|
||||
((string=? end-text "")
|
||||
(let ((n (string->number start-text)))
|
||||
(if n
|
||||
(cons n #f)
|
||||
(error "--expand-lines wants START or START:END, got" spec))))
|
||||
(else
|
||||
(let ((start (string->number start-text))
|
||||
(end (string->number end-text)))
|
||||
(if (and start end)
|
||||
(cons start end)
|
||||
(error "--expand-lines wants START or START:END, got" spec))))))))))
|
||||
|
||||
;;; Hyphens are underscores in C, except the arrow, which is one token.
|
||||
(define (sex-name->c name)
|
||||
(let ((s (if (symbol? name) (symbol->string name) name)))
|
||||
(let loop ((i 0) (chars (list)))
|
||||
(if (>= i (string-length s))
|
||||
(list->string (reverse chars))
|
||||
(let ((c (string-ref s i)))
|
||||
(if (and (char=? c #\-)
|
||||
(not (and (< (+ i 1) (string-length s))
|
||||
(char=? (string-ref s (+ i 1)) #\>))))
|
||||
(loop (+ i 1) (cons #\_ chars))
|
||||
(loop (+ i 1) (cons c chars))))))))
|
||||
|
||||
(define (c-name->sex name)
|
||||
(string->symbol
|
||||
(string-translate (if (symbol? name) (symbol->string name) name) "_" "-")))
|
||||
|
||||
(define prelude-headers '(stddef.h stdbool.h inttypes.h))
|
||||
|
||||
(define (include-spelling name)
|
||||
(if (string? name)
|
||||
(with-output-to-string (lambda () (write name)))
|
||||
(string-append "<" (symbol->string name) ">")))
|
||||
|
||||
;;; A C translation unit of just the headers, then PREFIX in C spelling,
|
||||
;;; which is where clangd is asked to complete or hover.
|
||||
(define (probe-text includes prefix)
|
||||
(with-output-to-string
|
||||
(lambda ()
|
||||
(for-each (lambda (name)
|
||||
(display "#include ")
|
||||
(display (include-spelling name))
|
||||
(newline))
|
||||
(delete-duplicates (append prelude-headers includes) equal?))
|
||||
;; A bare name is not a hoverable token. A call is, and completion
|
||||
;; still lands on the name, which is written before the argument.
|
||||
(display "void sex_probe (void) {")
|
||||
(newline)
|
||||
(display (sex-name->c (or prefix "")))
|
||||
(display "(0);")
|
||||
(newline)
|
||||
(display "}")
|
||||
(newline))))
|
||||
73
sexc.scm
73
sexc.scm
@@ -17,6 +17,7 @@
|
||||
getopt-long
|
||||
sex-macros
|
||||
sex-modules
|
||||
sex-query
|
||||
reader
|
||||
semen
|
||||
srfi-1 ; list routines
|
||||
@@ -80,6 +81,19 @@
|
||||
(pad padding) "land on the right source line; `none' is for reading -C" nl
|
||||
(pad padding) "output by eye")
|
||||
(required #f)
|
||||
(value #t))
|
||||
(check ,(fmt #f "Print semantic diagnostics as an s-expression and" nl
|
||||
(pad padding) "do not compile. Exit status is non-zero only" nl
|
||||
(pad padding) "when the file cannot be read")
|
||||
(required #f)
|
||||
(value #f))
|
||||
(symbols "Print the symbol table as an s-expression and do not compile"
|
||||
(required #f)
|
||||
(value #f))
|
||||
(expand-lines
|
||||
,(fmt #f "Print the C for Sex lines START:END. START with no end," nl
|
||||
(pad padding) "or `:`, is the whole buffer. Does not compile")
|
||||
(required #f)
|
||||
(value #t)))))
|
||||
|
||||
(define (print-help)
|
||||
@@ -169,7 +183,7 @@
|
||||
;;; executable or an object goes to cc's -o, where `-' is a file name
|
||||
(define (text-output? args)
|
||||
(any (lambda (option) (get-arg args option #f))
|
||||
'(emit-c macro-expand public-interface)))
|
||||
'(emit-c macro-expand public-interface check symbols expand-lines)))
|
||||
|
||||
(define (write-to-file-or-stdout output what)
|
||||
(if (stdout-output? output)
|
||||
@@ -251,23 +265,43 @@ status, which is ours to pass on."
|
||||
(mark-imported! (to-absolute-pathname input-source)))
|
||||
(let ((process (lambda ()
|
||||
(check-public-interface unit-forms)
|
||||
(semen-process (append prelude unit-forms)))))
|
||||
(semen-process (append (compiler-prelude) unit-forms)))))
|
||||
(if (eq? input-source 'stdin)
|
||||
(process)
|
||||
(with-directory input-source (process)))))
|
||||
|
||||
(define prelude
|
||||
'((include inttypes.h)
|
||||
(include stdbool.h)
|
||||
(include stddef.h)
|
||||
(typedef u8 uint8-t)
|
||||
(typedef i8 int8-t)
|
||||
(typedef u16 uint16-t)
|
||||
(typedef i16 int16-t)
|
||||
(typedef u32 uint32-t)
|
||||
(typedef i32 int32-t)
|
||||
(typedef u64 uint64-t)
|
||||
(typedef i64 int64-t)))
|
||||
;;; Query output. C from --expand-lines owns stdout; diagnostics and the
|
||||
;;; symbol table go beside it on stderr so a slice stays valid text.
|
||||
;;; On their own they are the stdout of the command.
|
||||
(define (report-query analysis args output)
|
||||
(let* ((expanding? (get-arg args 'expand-lines #f))
|
||||
(want-check? (get-arg args 'check #f))
|
||||
(want-symbols? (get-arg args 'symbols #f))
|
||||
(range (and expanding? (parse-expand-lines expanding?))))
|
||||
;; A reader failure is the one non-zero status. Say what it was,
|
||||
;; on stdout for --check and on stderr when stdout is C.
|
||||
(unless (analysis-read? analysis)
|
||||
(let ((where (if (or expanding? (not want-check?))
|
||||
(current-error-port)
|
||||
#f)))
|
||||
(if where
|
||||
(write-diagnostics analysis where)
|
||||
(write-to-file-or-stdout
|
||||
output
|
||||
(lambda () (write-diagnostics analysis (current-output-port))))))
|
||||
(exit 70))
|
||||
(when (or want-check? want-symbols?)
|
||||
(let ((port (if expanding? (current-error-port) #f)))
|
||||
(define (write-reports)
|
||||
(when want-check? (write-diagnostics analysis (current-output-port)))
|
||||
(when want-symbols? (write-symbols analysis (current-output-port))))
|
||||
(if port
|
||||
(with-output-to-port port write-reports)
|
||||
(write-to-file-or-stdout output write-reports))))
|
||||
(when expanding?
|
||||
(let ((text (analysis-expand analysis (car range) (cdr range))))
|
||||
(unless text (exit 70))
|
||||
(write-to-file-or-stdout output (lambda () (display text)))))))
|
||||
|
||||
;;; An error is told by its message, file:line first where it has one.
|
||||
;;; CHICKEN's call history after it is for working on sexc itself, and
|
||||
@@ -307,7 +341,7 @@ status, which is ours to pass on."
|
||||
(print-help)
|
||||
(return #f))
|
||||
(when (and (equal? output "-") (not (text-output? args)))
|
||||
(error "-o - is stdout, which takes only -C, -m or --public-interface"))
|
||||
(error "-o - is stdout, which takes only -C, -m, --check, --symbols, --expand-lines or --public-interface"))
|
||||
(when (same-file? input output)
|
||||
(error "-o names the input, and would overwrite its source" input))
|
||||
(when (and (string? input) (not (file-exists? input)))
|
||||
@@ -324,6 +358,15 @@ status, which is ours to pass on."
|
||||
;; what types they export
|
||||
(load-persistent-module-paths)
|
||||
|
||||
(when (or (get-arg args 'check #f)
|
||||
(get-arg args 'symbols #f)
|
||||
(get-arg args 'expand-lines #f))
|
||||
(report-query (if (eq? input 'stdin)
|
||||
(analyze-port (current-input-port) "stdin")
|
||||
(analyze-file input))
|
||||
args output)
|
||||
(return #f))
|
||||
|
||||
(when (get-arg args 'public-interface #f)
|
||||
(assert (not (eq? input 'stdin)) "Error: --public-interface requires file argument")
|
||||
|
||||
|
||||
177
tests/query.scm
Normal file
177
tests/query.scm
Normal file
@@ -0,0 +1,177 @@
|
||||
;;; Symbol table, diagnostics that survive a bad form, and C sliced
|
||||
;;; back to the Sex lines it came from.
|
||||
|
||||
(import scheme
|
||||
(scheme base) ; open-input-string
|
||||
(chicken file)
|
||||
(chicken io)
|
||||
(chicken pathname)
|
||||
(chicken port)
|
||||
(chicken process)
|
||||
(chicken string)
|
||||
srfi-1
|
||||
srfi-13
|
||||
test
|
||||
sex-query)
|
||||
|
||||
(define (quietly thunk)
|
||||
(with-error-output-to-string (lambda () (thunk))))
|
||||
|
||||
(define (analyze source)
|
||||
(analyze-text source "query.sex"))
|
||||
|
||||
(define (lookup source name line)
|
||||
(analysis-lookup (analyze source) name line))
|
||||
|
||||
(define (has-char? sym ch)
|
||||
(let ((s (symbol->string (sym-name sym))))
|
||||
(let loop ((i 0))
|
||||
(and (< i (string-length s))
|
||||
(or (char=? (string-ref s i) ch)
|
||||
(loop (+ i 1)))))))
|
||||
|
||||
(test-group "symbols"
|
||||
(let ((a (analyze "(pub fn bar ((a int)) void\n \"Add once.\"\n (return))\n")))
|
||||
(test-assert "a readable unit" (analysis-read? a))
|
||||
(let ((bar (analysis-lookup a 'bar 1)))
|
||||
(test 'fn (sym-kind bar))
|
||||
(test 1 (sym-line bar))
|
||||
(test 3 (sym-end bar))
|
||||
(test '(fn bar ((a int)) void) (sym-signature bar))
|
||||
(test "Add once." (sym-doc bar))
|
||||
(test-assert "the prototype and the docstring"
|
||||
(and (string-contains (symbol-hover bar) "(fn bar ((a int)) void)")
|
||||
(string-contains (symbol-hover bar) "Add once."))))
|
||||
(let ((param (analysis-lookup a 'a 2)))
|
||||
(test 'var (sym-kind param))
|
||||
(test 'int (sym-signature param))
|
||||
(test 1 (sym-scope-start param))))
|
||||
(test-assert "the prelude's names are not the unit's"
|
||||
(not (analysis-lookup (analyze "(fn f () void (return))") 'u8 1)))
|
||||
(let ((a (analyze "(struct point \"A point.\" ((x int) (y int)))\n(typedef point-t (struct point))\n(enum mood (cheerful grumpy))\n")))
|
||||
(test 'struct (sym-kind (analysis-lookup a 'point 1)))
|
||||
(test "A point." (sym-doc (analysis-lookup a 'point 1)))
|
||||
(test 'typedef (sym-kind (analysis-lookup a 'point-t 2)))
|
||||
(test 'const (sym-kind (analysis-lookup a 'cheerful 3)))
|
||||
(test '(enum mood) (sym-signature (analysis-lookup a 'grumpy 3))))
|
||||
(test-assert "a spec method is not a name"
|
||||
(not (analysis-lookup
|
||||
(analyze "(spec (show T)\n (fn show ((self (* const T))) void))\n")
|
||||
'show 1)))
|
||||
(test-assert "a closure's minted helper is not a name"
|
||||
(not (any (lambda (s) (has-char? s #\ƛ))
|
||||
(analysis-symbols
|
||||
(analyze "(fn make-adder ((n int)) (closure ((int)) int)\n (return (closure ((b int)) int (n)\n (return (+ n b)))))\n"))))))
|
||||
|
||||
(test-group "lookup"
|
||||
(let ((src "(fn outer ((a int)) int\n (return a)\n (do\n (var a char 1)\n (return a)))\n"))
|
||||
(test 'int (sym-signature (lookup src 'a 2)))
|
||||
(test 'char (sym-signature (lookup src 'a 5)))
|
||||
(test 'fn (sym-kind (lookup src 'outer 5)))))
|
||||
|
||||
(define *recovered* #f)
|
||||
|
||||
(test-group "diagnostics"
|
||||
(let ((err (with-error-output-to-string
|
||||
(lambda ()
|
||||
(set! *recovered* (analyze "(fn good () int\n (return 1))\n(nope)\n(fn also () int\n (return 2))\n"))))))
|
||||
(test "" err)
|
||||
(test-assert "the good function is still there"
|
||||
(analysis-lookup *recovered* 'good 1))
|
||||
(test-assert "and so is the one after the bad form"
|
||||
(analysis-lookup *recovered* 'also 4))
|
||||
(test-assert "the bad form is an error"
|
||||
(any (lambda (d)
|
||||
(and (eq? (diag-severity d) 'error)
|
||||
(string-contains (diag-message d) "unknown top level form")))
|
||||
(analysis-diagnostics *recovered*))))
|
||||
(let ((a (analyze "(fn f () void (var t (* struct tm) 0) (= (. t a b) 1))")))
|
||||
(test-assert "a warning is a diagnostic, and the function remains"
|
||||
(and (analysis-lookup a 'f 1)
|
||||
(any (lambda (d)
|
||||
(and (eq? (diag-severity d) 'warning)
|
||||
(string-contains (diag-message d) "cannot type this")))
|
||||
(analysis-diagnostics a))))))
|
||||
|
||||
(test-group "expand"
|
||||
(let ((a (analyze "(fn alpha () int\n (return 11111))\n(fn beta () int\n (return 22222))\n")))
|
||||
(let ((alpha (analysis-expand a 1 2)))
|
||||
(test-assert "the form's C" (string-contains alpha "11111"))
|
||||
(test-assert "not the other form" (not (string-contains alpha "22222"))))
|
||||
(let ((whole (analysis-expand a 1 #f)))
|
||||
(test-assert "the whole buffer keeps both"
|
||||
(and (string-contains whole "11111")
|
||||
(string-contains whole "22222")
|
||||
(string-contains whole "#include <stddef.h>"))))))
|
||||
|
||||
(test-group "includes and C names"
|
||||
(let ((a (analyze "(include stdio.h)\n(include \"local.h\")\n")))
|
||||
(test-assert "a header named by a symbol" (member 'stdio.h (analysis-includes a)))
|
||||
(test-assert "and one named by a string" (member "local.h" (analysis-includes a))))
|
||||
(test "SDL_Init" (sex-name->c "SDL-Init"))
|
||||
(test "foo->bar_baz" (sex-name->c "foo->bar-baz"))
|
||||
(test 'SDL-Init (c-name->sex "SDL_Init"))
|
||||
(let ((probe (probe-text '(stdio.h) "SDL-In")))
|
||||
(test-assert "prelude headers" (string-contains probe "#include <stddef.h>"))
|
||||
(test-assert "the unit's header" (string-contains probe "#include <stdio.h>"))
|
||||
(test-assert "the prefix in C spelling" (string-contains probe "SDL_In"))))
|
||||
|
||||
(test-group "import"
|
||||
(let* ((dir (create-temporary-directory))
|
||||
(greet (make-pathname dir "greet.sex"))
|
||||
(app (make-pathname dir "app.sex")))
|
||||
(with-output-to-file greet
|
||||
(lambda ()
|
||||
(display "(pub fn greet ((name int)) int\n \"Hi.\"\n (return name))\n")))
|
||||
(with-output-to-file app
|
||||
(lambda ()
|
||||
(display "(import greet)\n(fn main () int (return (greet 1)))\n")))
|
||||
(let* ((a (analyze-file app))
|
||||
(g (analysis-lookup a 'greet 2)))
|
||||
(test-assert "the import was read" (analysis-read? a))
|
||||
(test 'fn (sym-kind g))
|
||||
(test "Hi." (sym-doc g))
|
||||
(test "greet.sex" (pathname-strip-directory (sym-file g)))
|
||||
(test-assert "document symbols stay in this file"
|
||||
(and (any (lambda (s) (eq? (sym-name s) 'main))
|
||||
(analysis-document-symbols a))
|
||||
(not (any (lambda (s) (eq? (sym-name s) 'greet))
|
||||
(analysis-document-symbols a))))))
|
||||
(delete-directory dir #t)))
|
||||
|
||||
(define (run-sexc . args)
|
||||
(let ((proc (process "./sexc" args)))
|
||||
(let ((out (read-string #f (process-output-port proc))))
|
||||
(close-input-port (process-output-port proc))
|
||||
(call-with-values (lambda () (process-wait proc))
|
||||
(lambda (pid normal? status) status))
|
||||
(cons (process-exit-status proc) out))))
|
||||
|
||||
(define (read-output text)
|
||||
(read (open-input-string text)))
|
||||
|
||||
(test-group "sexc query flags"
|
||||
(let* ((dir (create-temporary-directory))
|
||||
(file (make-pathname dir "q.sex")))
|
||||
(with-output-to-file file
|
||||
(lambda ()
|
||||
(display "(fn alpha () int\n (return 11111))\n(nope)\n")))
|
||||
(let ((checked (run-sexc "--check" file)))
|
||||
(test 0 (car checked))
|
||||
(test-assert "an error sexp"
|
||||
(let ((form (read-output (cdr checked))))
|
||||
(and (eq? (car form) 'diagnostics)
|
||||
(any (lambda (d) (eq? (car d) 'error)) (cdr form))))))
|
||||
(let ((symbols (run-sexc "--symbols" file)))
|
||||
(test 0 (car symbols))
|
||||
(test-assert "alpha survives nope"
|
||||
(let ((form (read-output (cdr symbols))))
|
||||
(and (eq? (car form) 'symbols)
|
||||
(any (lambda (s) (eq? (cadr s) 'alpha)) (cdr form))))))
|
||||
(let ((expanded (run-sexc "--expand-lines=1:2" file)))
|
||||
(test 0 (car expanded))
|
||||
(test-assert "the slice" (string-contains (cdr expanded) "11111")))
|
||||
(let ((broken (make-pathname dir "broken.sex")))
|
||||
(with-output-to-file broken (lambda () (display "(fn")))
|
||||
(test 70 (car (run-sexc "--check" broken))))
|
||||
(delete-directory dir #t)))
|
||||
@@ -12,6 +12,7 @@
|
||||
(include-relative "module-path.scm")
|
||||
(include-relative "types.scm")
|
||||
(include-relative "infer.scm")
|
||||
(include-relative "query.scm")
|
||||
|
||||
;;; Should be the last in the test suite
|
||||
(test-exit)
|
||||
|
||||
@@ -33,6 +33,7 @@
|
||||
get-constructor
|
||||
variant-fields
|
||||
reset-declarations!
|
||||
reset-name-tables!
|
||||
|
||||
normalize-fields
|
||||
|
||||
@@ -327,6 +328,13 @@
|
||||
(set! +constructor-db+ (make-hash-table))
|
||||
(set! +ops-db+ (make-hash-table)))
|
||||
|
||||
;;; What `reset-declarations!' leaves, and what a second unit must not see.
|
||||
(define (reset-name-tables!)
|
||||
(set! +type-db+ (make-hash-table))
|
||||
(set! +tag-db+ (make-hash-table))
|
||||
(set! +name-db+ (make-hash-table))
|
||||
(reset-declarations!))
|
||||
|
||||
;;; Type matcher macro
|
||||
;;; (type-match type
|
||||
;;; (int ...)
|
||||
|
||||
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