1
0
mirror of https://github.com/janet-lang/janet synced 2024-09-29 15:30:41 +00:00

Make flycheck follow GNU standards for errors.

This commit is contained in:
Calvin Rose 2021-01-30 12:51:38 -06:00
parent 72ec89dfe9
commit c63fe6ef8a
2 changed files with 28 additions and 15 deletions

View File

@ -2442,22 +2442,25 @@
(put env :source (or src (if-not path-is-file spath path))) (put env :source (or src (if-not path-is-file spath path)))
(var exit-error nil) (var exit-error nil)
(var exit-fiber nil) (var exit-fiber nil)
(defn chunks [buf _] (file/read f 2048 buf)) (defn chunks [buf _] (file/read f 4096 buf))
(defn bp [&opt x y] (defn bp [&opt x y]
(when exit (when exit
(bad-parse x y) (bad-parse x y)
(os/exit 1)) (os/exit 1))
(put env :exit true) (put env :exit true)
(def [line col] (:where x)) (def buf @"")
(def pe (string (:error x) " in " y " around line " line ", column " col)) (with-dyns [:err buf :err-color false]
(set exit-error pe)) (bad-parse x y))
(set exit-error (string/slice buf 0 -2)))
(defn bc [&opt x y z a b] (defn bc [&opt x y z a b]
(when exit (when exit
(bad-compile x y z a b) (bad-compile x y z a b)
(os/exit 1)) (os/exit 1))
(put env :exit true) (put env :exit true)
(def ce (string x " while compiling " z)) (def buf @"")
(set exit-error ce) (with-dyns [:err buf :err-color false]
(bad-compile x nil z a b))
(set exit-error (string/slice buf 0 -2))
(set exit-fiber y)) (set exit-fiber y))
(unless f (unless f
(error (string "could not find file " path))) (error (string "could not find file " path)))
@ -2469,7 +2472,8 @@
:on-status (fn [f x] :on-status (fn [f x]
(when (not= (fiber/status f) :dead) (when (not= (fiber/status f) :dead)
(when exit (when exit
(debug/stacktrace f x) (eprint x)
(debug/stacktrace f)
(eflush) (eflush)
(os/exit 1)) (os/exit 1))
(put env :exit true) (put env :exit true)
@ -3252,7 +3256,10 @@
(defn- use-2 [evaluator args] (defn- use-2 [evaluator args]
(each a args (import* (string a) :prefix "" :evaluator evaluator))) (each a args (import* (string a) :prefix "" :evaluator evaluator)))
(defn- evaluator (defn flycheck-evaluator
``An evaluator function that is passed to `run-context` that lints (flychecks) code.
This means code will parsed and compiled, macros executed, but the code will not be run.
Used by `flycheck`.``
[thunk source env where] [thunk source env where]
(when (tuple? source) (when (tuple? source)
(def head (source 0)) (def head (source 0))
@ -3266,20 +3273,25 @@
(thunk) (thunk)
# Use # Use
(= 'use head) (= 'use head)
(use-2 evaluator (tuple/slice source 1)) (use-2 flycheck-evaluator (tuple/slice source 1))
# Import-like form # Import-like form
(importers head) (importers head)
(let [[l c] (tuple/sourcemap source) (let [[l c] (tuple/sourcemap source)
newtup (tuple/setmap (tuple ;source :evaluator evaluator) l c)] newtup (tuple/setmap (tuple ;source :evaluator flycheck-evaluator) l c)]
((compile newtup env where)))))) ((compile newtup env where))))))
(defn flycheck (defn flycheck
``Check a file for errors without running the file. Found errors will be printed to stderr ``Check a file for errors without running the file. Found errors will be printed to stderr
in the usual format. Macros will still be executed, however, so in the usual format. Macros will still be executed, however, so
arbitrary execution is possible. Other arguments are the same as dofile. `path` can also be arbitrary execution is possible. Other arguments are the same as dofile. `path` can also be
a file value such as stdin.`` a file value such as stdin. Returns nil.``
[path &keys kwargs] [path &keys kwargs]
(dofile path :evaluator evaluator ;(kvs kwargs))) (try
(dofile path :evaluator flycheck-evaluator ;(kvs kwargs))
([e f]
(eprint e)
(debug/stacktrace f)))
nil)
### ###
### ###

View File

@ -102,7 +102,9 @@ void janet_stacktrace(JanetFiber *fiber, Janet err) {
int32_t fi; int32_t fi;
const char *errstr = (const char *)janet_to_string(err); const char *errstr = (const char *)janet_to_string(err);
JanetFiber **fibers = NULL; JanetFiber **fibers = NULL;
int wrote_error = 0;
/* Don't print error line if it is nil. */
int wrote_error = janet_checktype(err, JANET_NIL);
int print_color = janet_truthy(janet_dyn("err-color")); int print_color = janet_truthy(janet_dyn("err-color"));
if (print_color) janet_eprintf("\x1b[31m"); if (print_color) janet_eprintf("\x1b[31m");
@ -302,7 +304,6 @@ static Janet cfun_debug_stacktrace(int32_t argc, Janet *argv) {
janet_arity(argc, 1, 2); janet_arity(argc, 1, 2);
JanetFiber *fiber = janet_getfiber(argv, 0); JanetFiber *fiber = janet_getfiber(argv, 0);
Janet x = argc == 1 ? janet_wrap_nil() : argv[1]; Janet x = argc == 1 ? janet_wrap_nil() : argv[1];
x = janet_checktype(x, JANET_NIL) ? fiber->last_value : x;
janet_stacktrace(fiber, x); janet_stacktrace(fiber, x);
return argv[0]; return argv[0];
} }
@ -382,7 +383,7 @@ static const JanetReg debug_cfuns[] = {
JDOC("(debug/stacktrace fiber &opt err)\n\n" JDOC("(debug/stacktrace fiber &opt err)\n\n"
"Prints a nice looking stacktrace for a fiber. Can optionally provide " "Prints a nice looking stacktrace for a fiber. Can optionally provide "
"an error value to print the stack trace with. If `err` is nil or not " "an error value to print the stack trace with. If `err` is nil or not "
"provided, will default to `(fiber/last-value fiber)`. Returns the fiber.") "provided, will skipp the error line. Returns the fiber.")
}, },
{ {
"debug/lineage", cfun_debug_lineage, "debug/lineage", cfun_debug_lineage,