1
0
mirror of https://github.com/janet-lang/janet synced 2026-03-05 15:29:52 +00:00

Move peg debug output to stderr in line with other internal debug tools.

Also allow disabling color in the debug output.
This commit is contained in:
Calvin Rose
2026-02-04 18:52:20 -06:00
parent 855d1f2940
commit b2bf70eace
3 changed files with 29 additions and 17 deletions

View File

@@ -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();
}

View File

@@ -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;
}

View File

@@ -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"))