;;; 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 ")))))) (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 ")) (test-assert "the unit's header" (string-contains probe "#include ")) (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)))