From b2bf70eacebbf4ce11fae0bb1fa680c22be2c69e Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Wed, 4 Feb 2026 18:52:20 -0600 Subject: [PATCH] Move peg debug output to stderr in line with other internal debug tools. Also allow disabling color in the debug output. --- src/core/capi.c | 2 +- src/core/peg.c | 10 ++++++++-- test/suite-peg.janet | 34 ++++++++++++++++++++-------------- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/core/capi.c b/src/core/capi.c index 526e58a8..7981a6eb 100644 --- a/src/core/capi.c +++ b/src/core/capi.c @@ -460,7 +460,7 @@ Janet janet_dyn(const char *name) { return janet_table_get(janet_vm.top_dyns, janet_ckeywordv(name)); } if (janet_vm.fiber->env) { - return janet_table_get(janet_vm.fiber->env, janet_ckeywordv(name)); + return janet_table_get_keyword(janet_vm.fiber->env, name); } else { return janet_wrap_nil(); } diff --git a/src/core/peg.c b/src/core/peg.c index 1912dcea..bddb87a4 100644 --- a/src/core/peg.c +++ b/src/core/peg.c @@ -198,10 +198,16 @@ tail: char buffer[32] = {0}; size_t len = (size_t)(s->outer_text_end - text); memcpy(buffer, text, (len > 31 ? 31 : len)); - janet_printf("\n?? at [%s]\nstack [%d]:\n", buffer, s->captures->count); + janet_eprintf("\n?? at [%s]\nstack [%d]:\n", buffer, s->captures->count); + int has_color = janet_truthy(janet_dyn("err-color")); for (int32_t i = 0; i < s->captures->count; i++) { - janet_printf(" [%d]: %M\n", i, s->captures->data[i]); + if (has_color) { + janet_eprintf(" [%d]: %M\n", i, s->captures->data[i]); + } else { + janet_eprintf(" [%d]: %m\n", i, s->captures->data[i]); + } } + janet_eprintf("\n"); return text; } diff --git a/test/suite-peg.janet b/test/suite-peg.janet index ae3d0dbf..9684d63b 100644 --- a/test/suite-peg.janet +++ b/test/suite-peg.janet @@ -266,6 +266,12 @@ (marshpeg '(sub "abcdf" "abc")) (marshpeg '(* (sub 1 1))) (marshpeg '(split "," (+ "a" "b" "c"))) +(marshpeg "") +(marshpeg 1) +(marshpeg 0) +(marshpeg -1) +(marshpeg '(drop 1)) +(marshpeg '(accumulate 1)) # Peg swallowing errors # 159651117 @@ -846,49 +852,49 @@ @[["b" "b" "b"]]) # Debug and ?? tests. -(defn test-stdout [name peg input expected-matches expected-stdout] +(defn test-stderr [name peg input expected-matches expected-stdout] (def actual @"") - (with-dyns [:out actual] + (with-dyns [:err actual] (test name peg input expected-matches)) (assert (deep= (string actual) expected-stdout))) -(test-stdout "?? long form" +(test-stderr "?? long form" '(* (debug) "abc") "abc" @[] - "\n?? at [abc]\nstack [0]:\n") + "\n?? at [abc]\nstack [0]:\n\n") -(test-stdout "?? short form" +(test-stderr "?? short form" '(* (??) "abc") "abc" @[] - "\n?? at [abc]\nstack [0]:\n") + "\n?? at [abc]\nstack [0]:\n\n") -(test-stdout "?? end of text" +(test-stderr "?? end of text" '(* "abc" (??)) "abc" @[] - "\n?? at []\nstack [0]:\n") + "\n?? at []\nstack [0]:\n\n") -(test-stdout "?? between rules" +(test-stderr "?? between rules" '(* "a" (??) "bc") "abc" @[] - "\n?? at [bc]\nstack [0]:\n") + "\n?? at [bc]\nstack [0]:\n\n") -(test-stdout +(test-stderr "?? stack display, string" '(* (<- "a") (??) "bc") "abc" @["a"] - (string/format "\n?? at [bc]\nstack [1]:\n [0]: %M\n" "a")) + (string/format "\n?? at [bc]\nstack [1]:\n [0]: %M\n\n" "a")) -(test-stdout +(test-stderr "?? stack display, multiple types" '(* (<- "a") (number :d) (constant true) (constant {}) (constant @[]) (??) "bc") "a1bc" @["a" 1 true {} @[]] - (string/format "\n?? at [bc]\nstack [5]:\n [0]: %M\n [1]: %M\n [2]: %M\n [3]: %M\n [4]: %M\n" "a" 1 true {} @[])) + (string/format "\n?? at [bc]\nstack [5]:\n [0]: %M\n [1]: %M\n [2]: %M\n [3]: %M\n [4]: %M\n\n" "a" 1 true {} @[])) (marshpeg '(* (??) "abc"))