harec

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit f79ba06cb4142991dcf12ed6fccdf5c1485457c9
parent a540ab1d3ae435ae22ab1acade5b22648c836db7
Author: Drew DeVault <sir@cmpwn.com>
Date:   Sun, 22 Nov 2020 11:30:29 -0500

Add depth to traces

Diffstat:
Minclude/trace.h | 10+++++++++-
Msrc/lex.c | 4++--
Msrc/parse.c | 13++++++++-----
Msrc/trace.c | 57+++++++++++++++++++++++++++++++++++++++++++++++++++------
4 files changed, 70 insertions(+), 14 deletions(-)

diff --git a/include/trace.h b/include/trace.h @@ -1,6 +1,14 @@ #ifndef HARE_TRACE_H #define HARE_TRACE_H -void trace(const char *sys, const char *fmt, ...); +enum trace_sys { + TR_LEX, + TR_PARSE, + TR_MAX, +}; + +void trace(enum trace_sys sys, const char *fmt, ...); +void trenter(enum trace_sys sys, const char *fmt, ...); +void trleave(enum trace_sys sys, const char *fmt, ...); #endif diff --git a/src/lex.c b/src/lex.c @@ -795,7 +795,7 @@ enum lexical_token lex(struct lexer *lexer, struct token *out) { enum lexical_token l = _lex(lexer, out); - trace("lex", "%s", token_str(out)); + trace(TR_LEX, "%s", token_str(out)); return l; } @@ -857,6 +857,6 @@ void unlex(struct lexer *lexer, struct token *in) { assert(lexer->un.token == T_ERROR && "Only one unlex is supported"); - trace("lex", "(unlex) %s", token_str(in)); + trace(TR_LEX, "(unlex) %s", token_str(in)); lexer->un = *in; } diff --git a/src/parse.c b/src/parse.c @@ -58,7 +58,7 @@ parse_identifier(struct parser *par, struct identifier *ident) { struct token tok = {0}; struct identifier *i = ident; - trace("parse", "identifier"); + trenter(TR_PARSE, "identifier"); while (!i->name) { want(par, T_NAME, &tok); @@ -81,13 +81,13 @@ parse_identifier(struct parser *par, struct identifier *ident) char buf[1024]; identifier_unparse_static(ident, buf, sizeof(buf)); - trace("parse", "-> %s", buf); + trleave(TR_PARSE, "%s", buf); } static void parse_import(struct parser *par, struct ast_imports *imports) { - trace("parse", "import"); + trenter(TR_PARSE, "import"); struct identifier ident = {0}; parse_identifier(par, &ident); @@ -105,12 +105,14 @@ parse_import(struct parser *par, struct ast_imports *imports) synassert(false, &tok, T_EQUAL, T_LBRACE, T_SEMICOLON, T_EOF); break; } + + trleave(TR_PARSE, NULL); } static void parse_imports(struct parser *par, struct ast_subunit *subunit) { - trace("parse", "imports"); + trenter(TR_PARSE, "imports"); struct token tok = {0}; struct ast_imports **next = &subunit->imports; @@ -134,8 +136,9 @@ parse_imports(struct parser *par, struct ast_subunit *subunit) for (struct ast_imports *i = subunit->imports; i; i = i->next) { char buf[1024]; identifier_unparse_static(&i->ident, buf, sizeof(buf)); - trace("parse", "-> use %s", buf); + trace(TR_PARSE, "use %s", buf); } + trleave(TR_PARSE, NULL); } void diff --git a/src/trace.c b/src/trace.c @@ -1,20 +1,65 @@ +#include <assert.h> #include <stdarg.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include "trace.h" -void -trace(const char *sys, const char *fmt, ...) +static const char *sysname[] = { + [TR_LEX] = "lex", + [TR_PARSE] = "parse", +}; + +static int depth[] = { + [TR_LEX] = 0, + [TR_PARSE] = 0, +}; + +static void +va_trace(enum trace_sys sys, const char *fmt, va_list ap) { + assert(sys < TR_MAX); char *t = getenv("HA_TRACE"); - if (!t || !strstr(t, sys)) { + if (!t || !strstr(t, sysname[sys])) { return; } - va_list ap; - va_start(ap, fmt); - fprintf(stderr, "[%10s] ", sys); + fprintf(stderr, "[%10s] ", sysname[sys]); + for (int i = 0; i < depth[sys]; ++i) { + fprintf(stderr, " "); + } vfprintf(stderr, fmt, ap); fprintf(stderr, "\n"); +} + +void +trace(enum trace_sys sys, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + va_trace(sys, fmt, ap); + va_end(ap); +} + +void +trenter(enum trace_sys sys, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + va_trace(sys, fmt, ap); + va_end(ap); + ++depth[sys]; +} + +void +trleave(enum trace_sys sys, const char *fmt, ...) +{ + --depth[sys]; + if (fmt == NULL) { + return; + } + + va_list ap; + va_start(ap, fmt); + va_trace(sys, fmt, ap); va_end(ap); }