Custom printers for user token types
I did this in a hurry, but it appears to work locally. I tested it with a couple boring custom token type printers.
This commit is contained in:
parent
bfb07bbbf6
commit
2b6b300f91
4 changed files with 88 additions and 42 deletions
65
src/pprint.c
65
src/pprint.c
|
|
@ -41,10 +41,10 @@ void h_pprint(FILE* stream, const HParsedToken* tok, int indent, int delta) {
|
|||
else {
|
||||
fprintf(stream, "%*s", indent, "");
|
||||
for (size_t i = 0; i < tok->bytes.len; i++) {
|
||||
fprintf(stream,
|
||||
"%c%02hhx",
|
||||
(i == 0) ? '<' : '.',
|
||||
tok->bytes.token[i]);
|
||||
fprintf(stream,
|
||||
"%c%02hhx",
|
||||
(i == 0) ? '<' : '.',
|
||||
tok->bytes.token[i]);
|
||||
}
|
||||
fprintf(stream, ">\n");
|
||||
}
|
||||
|
|
@ -54,7 +54,7 @@ void h_pprint(FILE* stream, const HParsedToken* tok, int indent, int delta) {
|
|||
fprintf(stream, "%*ss -%#" PRIx64 "\n", indent, "", -tok->sint);
|
||||
else
|
||||
fprintf(stream, "%*ss %#" PRIx64 "\n", indent, "", tok->sint);
|
||||
|
||||
|
||||
break;
|
||||
case TT_UINT:
|
||||
fprintf(stream, "%*su %#" PRIx64 "\n", indent, "", tok->uint);
|
||||
|
|
@ -96,7 +96,7 @@ static inline bool ensure_capacity(struct result_buf *buf, int amt) {
|
|||
return true;
|
||||
}
|
||||
|
||||
static inline bool append_buf(struct result_buf *buf, const char* input, int len) {
|
||||
bool h_append_buf(struct result_buf *buf, const char* input, int len) {
|
||||
if (ensure_capacity(buf, len)) {
|
||||
memcpy(buf->output + buf->len, input, len);
|
||||
buf->len += len;
|
||||
|
|
@ -106,7 +106,7 @@ static inline bool append_buf(struct result_buf *buf, const char* input, int len
|
|||
}
|
||||
}
|
||||
|
||||
static inline bool append_buf_c(struct result_buf *buf, char v) {
|
||||
bool h_append_buf_c(struct result_buf *buf, char v) {
|
||||
if (ensure_capacity(buf, 1)) {
|
||||
buf->output[buf->len++] = v;
|
||||
return true;
|
||||
|
|
@ -116,7 +116,7 @@ static inline bool append_buf_c(struct result_buf *buf, char v) {
|
|||
}
|
||||
|
||||
/** append a formatted string to the result buffer */
|
||||
static inline bool append_buf_formatted(struct result_buf *buf, char* format, ...)
|
||||
bool h_append_buf_formatted(struct result_buf *buf, char* format, ...)
|
||||
{
|
||||
char* tmpbuf;
|
||||
int len;
|
||||
|
|
@ -125,7 +125,7 @@ static inline bool append_buf_formatted(struct result_buf *buf, char* format, ..
|
|||
|
||||
va_start(ap, format);
|
||||
len = h_platform_vasprintf(&tmpbuf, format, ap);
|
||||
result = append_buf(buf, tmpbuf, len);
|
||||
result = h_append_buf(buf, tmpbuf, len);
|
||||
free(tmpbuf);
|
||||
va_end(ap);
|
||||
|
||||
|
|
@ -134,52 +134,59 @@ static inline bool append_buf_formatted(struct result_buf *buf, char* format, ..
|
|||
|
||||
static void unamb_sub(const HParsedToken* tok, struct result_buf *buf) {
|
||||
if (!tok) {
|
||||
append_buf(buf, "NULL", 4);
|
||||
h_append_buf(buf, "NULL", 4);
|
||||
return;
|
||||
}
|
||||
switch (tok->token_type) {
|
||||
case TT_NONE:
|
||||
append_buf(buf, "null", 4);
|
||||
h_append_buf(buf, "null", 4);
|
||||
break;
|
||||
case TT_BYTES:
|
||||
if (tok->bytes.len == 0)
|
||||
append_buf(buf, "<>", 2);
|
||||
h_append_buf(buf, "<>", 2);
|
||||
else {
|
||||
for (size_t i = 0; i < tok->bytes.len; i++) {
|
||||
const char *HEX = "0123456789abcdef";
|
||||
append_buf_c(buf, (i == 0) ? '<': '.');
|
||||
char c = tok->bytes.token[i];
|
||||
append_buf_c(buf, HEX[(c >> 4) & 0xf]);
|
||||
append_buf_c(buf, HEX[(c >> 0) & 0xf]);
|
||||
const char *HEX = "0123456789abcdef";
|
||||
h_append_buf_c(buf, (i == 0) ? '<': '.');
|
||||
char c = tok->bytes.token[i];
|
||||
h_append_buf_c(buf, HEX[(c >> 4) & 0xf]);
|
||||
h_append_buf_c(buf, HEX[(c >> 0) & 0xf]);
|
||||
}
|
||||
append_buf_c(buf, '>');
|
||||
h_append_buf_c(buf, '>');
|
||||
}
|
||||
break;
|
||||
case TT_SINT:
|
||||
if (tok->sint < 0)
|
||||
append_buf_formatted(buf, "s-%#" PRIx64, -tok->sint);
|
||||
h_append_buf_formatted(buf, "s-%#" PRIx64, -tok->sint);
|
||||
else
|
||||
append_buf_formatted(buf, "s%#" PRIx64, tok->sint);
|
||||
h_append_buf_formatted(buf, "s%#" PRIx64, tok->sint);
|
||||
break;
|
||||
case TT_UINT:
|
||||
append_buf_formatted(buf, "u%#" PRIx64, tok->uint);
|
||||
h_append_buf_formatted(buf, "u%#" PRIx64, tok->uint);
|
||||
break;
|
||||
case TT_ERR:
|
||||
append_buf(buf, "ERR", 3);
|
||||
h_append_buf(buf, "ERR", 3);
|
||||
break;
|
||||
case TT_SEQUENCE: {
|
||||
append_buf_c(buf, '(');
|
||||
h_append_buf_c(buf, '(');
|
||||
for (size_t i = 0; i < tok->seq->used; i++) {
|
||||
if (i > 0)
|
||||
append_buf_c(buf, ' ');
|
||||
h_append_buf_c(buf, ' ');
|
||||
unamb_sub(tok->seq->elements[i], buf);
|
||||
}
|
||||
append_buf_c(buf, ')');
|
||||
h_append_buf_c(buf, ')');
|
||||
}
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "Unexpected token type %d\n", tok->token_type);
|
||||
assert_message(0, "Should not reach here.");
|
||||
default: {
|
||||
const HTTEntry *e = h_get_token_type_entry(tok->token_type);
|
||||
if (e) {
|
||||
h_append_buf_c(buf, '{');
|
||||
e->unamb_sub(tok, buf);
|
||||
h_append_buf_c(buf, '}');
|
||||
} else {
|
||||
assert_message(0, "Bogus token type.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -192,7 +199,7 @@ char* h_write_result_unamb(const HParsedToken* tok) {
|
|||
};
|
||||
assert(buf.output != NULL);
|
||||
unamb_sub(tok, &buf);
|
||||
append_buf_c(&buf, 0);
|
||||
h_append_buf_c(&buf, 0);
|
||||
return buf.output;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue