Show accumulation buffer and tagged capture in peg debug rule.

This commit is contained in:
Calvin Rose
2026-02-04 21:17:08 -06:00
parent b2bf70eace
commit f33697d6a0
3 changed files with 81 additions and 16 deletions
+1
View File
@@ -74,6 +74,7 @@ JanetBuffer *janet_pointer_buffer_unsafe(void *memory, int32_t capacity, int32_t
void janet_buffer_deinit(JanetBuffer *buffer) {
if (!(buffer->gc.flags & JANET_BUFFER_FLAG_NO_REALLOC)) {
janet_free(buffer->data);
buffer->data = NULL;
}
}
+25 -7
View File
@@ -198,16 +198,34 @@ tail:
char buffer[32] = {0};
size_t len = (size_t)(s->outer_text_end - text);
memcpy(buffer, text, (len > 31 ? 31 : len));
janet_eprintf("\n?? at [%s]\nstack [%d]:\n", buffer, s->captures->count);
janet_eprintf("?? at [%s] (index %d)\n", buffer, (int32_t)(text - s->text_start));
int has_color = janet_truthy(janet_dyn("err-color"));
for (int32_t i = 0; i < s->captures->count; 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]);
/* Accumulate buffer */
if (s->scratch->count) {
janet_eprintf("accumulate buffer: %v\n", janet_wrap_buffer(s->scratch));
}
/* Normal captures */
if (s->captures->count) {
janet_eprintf("stack [%d]:\n", s->captures->count);
for (int32_t i = 0; i < s->captures->count; 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]);
}
}
}
/* Tagged captures */
if (s->tagged_captures->count) {
janet_eprintf("tag stack [%d]:\n", s->tagged_captures->count);
for (int32_t i = 0; i < s->tagged_captures->count; i++) {
if (has_color) {
janet_eprintf(" [%d] tag=%d: %M\n", i, s->tags->data[i], s->tagged_captures->data[i]);
} else {
janet_eprintf(" [%d] tag=%d: %m\n", i, s->tags->data[i], s->tagged_captures->data[i]);
}
}
}
janet_eprintf("\n");
return text;
}
+55 -9
View File
@@ -852,50 +852,96 @@
@[["b" "b" "b"]])
# Debug and ?? tests.
(defn test-stderr [name peg input expected-matches expected-stdout]
(defn test-stderr [name peg input expected-matches expected-stderr]
(def actual @"")
(with-dyns [:err actual]
(with-dyns [:err actual *err-color* true]
(test name peg input expected-matches))
(assert (deep= (string actual) expected-stdout)))
(assert (deep= (string actual) expected-stderr)))
(defn test-stderr-no-color [name peg input expected-matches expected-stderr]
(def actual @"")
(with-dyns [:err actual *err-color* false]
(test name peg input expected-matches))
(assert (deep= (string actual) expected-stderr)))
(test-stderr "?? long form"
'(* (debug) "abc")
"abc"
@[]
"\n?? at [abc]\nstack [0]:\n\n")
"?? at [abc] (index 0)\n")
(test-stderr "?? short form"
'(* (??) "abc")
"abc"
@[]
"\n?? at [abc]\nstack [0]:\n\n")
"?? at [abc] (index 0)\n")
(test-stderr "?? end of text"
'(* "abc" (??))
"abc"
@[]
"\n?? at []\nstack [0]:\n\n")
"?? at [] (index 3)\n")
(test-stderr "?? between rules"
'(* "a" (??) "bc")
"abc"
@[]
"\n?? at [bc]\nstack [0]:\n\n")
"?? at [bc] (index 1)\n")
(test-stderr
"?? stack display, string"
'(* (<- "a") (??) "bc")
"abc"
@["a"]
(string/format "\n?? at [bc]\nstack [1]:\n [0]: %M\n\n" "a"))
(string/format "?? at [bc] (index 1)\nstack [1]:\n [0]: %M\n" "a"))
(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\n" "a" 1 true {} @[]))
(string/format "?? at [bc] (index 2)\nstack [5]:\n [0]: %M\n [1]: %M\n [2]: %M\n [3]: %M\n [4]: %M\n" "a" 1 true {} @[]))
(marshpeg '(* (??) "abc"))
(marshpeg '(* (some (debug)) (??) "abc"))
(test-stderr
"?? displays when capture fails"
'(* '1 (??) "x")
"abc"
nil
(string/format "?? at [bc] (index 1)\nstack [1]:\n [0]: %M\n" "a"))
(test-stderr-no-color
"?? displays accumuate and tagged captures"
'(* '1 '2 (% (* '1 (??) (<- 2 :tag) '3 (backref :tag) (??))))
"aksjndkajsnd"
@["a" "ks" "jndkajnd"]
(string/replace-all # In case on windows someone messes with line endings.
"\r" ""
```
?? at [ndkajsnd] (index 4)
accumulate buffer: @"j"
stack [2]:
[0]: "a"
[1]: "ks"
tag stack [3]:
[0] tag=0: "a"
[1] tag=0: "ks"
[2] tag=0: "j"
?? at [snd] (index 9)
accumulate buffer: @"jndkajnd"
stack [2]:
[0]: "a"
[1]: "ks"
tag stack [6]:
[0] tag=0: "a"
[1] tag=0: "ks"
[2] tag=0: "j"
[3] tag=1: "nd"
[4] tag=0: "kaj"
[5] tag=0: "nd"
```))
(end-suite)