commit d02a9ce93f18ea55cf07cd988822bec2f42b94b6
parent 31c7f1381d12aba90f690945d4c4f1cd87782e88
Author: Drew DeVault <sir@cmpwn.com>
Date: Sun, 22 Nov 2020 09:17:58 -0500
add trace subsystem
Diffstat:
5 files changed, 43 insertions(+), 14 deletions(-)
diff --git a/configure b/configure
@@ -7,6 +7,7 @@ harec() {
src/lex.c \
src/main.c \
src/parse.c \
+ src/trace.c \
src/types.c \
src/utf8.c
}
diff --git a/include/trace.h b/include/trace.h
@@ -0,0 +1,6 @@
+#ifndef HARE_TRACE_H
+#define HARE_TRACE_H
+
+void trace(const char *sys, const char *fmt, ...);
+
+#endif
diff --git a/src/lex.c b/src/lex.c
@@ -8,6 +8,7 @@
#include <stdlib.h>
#include <string.h>
#include "lex.h"
+#include "trace.h"
#include "utf8.h"
static const char *tokens[] = {
@@ -709,8 +710,8 @@ lex2(struct lexer *lexer, struct token *out, uint32_t c)
return out->token;
}
-enum lexical_token
-lex(struct lexer *lexer, struct token *out)
+static enum lexical_token
+_lex(struct lexer *lexer, struct token *out)
{
if (lexer->un.token != T_ERROR) {
*out = lexer->un;
@@ -790,6 +791,14 @@ lex(struct lexer *lexer, struct token *out)
return out->token;
}
+enum lexical_token
+lex(struct lexer *lexer, struct token *out)
+{
+ enum lexical_token l = _lex(lexer, out);
+ trace("lex", "%s", token_str(out));
+ return l;
+}
+
void
token_finish(struct token *tok)
{
@@ -848,5 +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));
lexer->un = *in;
}
diff --git a/src/parse.c b/src/parse.c
@@ -8,6 +8,7 @@
#include "identifier.h"
#include "lex.h"
#include "parse.h"
+#include "trace.h"
#include "types.h"
#include "utf8.h"
@@ -16,15 +17,6 @@ struct parser {
};
static void
-trace(struct parser *par, const char *name)
-{
- if (getenv("HAREC_TRACE") == NULL) {
- return;
- }
- fprintf(stderr, "%s\n", name);
-}
-
-static void
synassert(bool cond, struct token *tok, ...)
{
if (!cond) {
@@ -66,7 +58,7 @@ parse_identifier(struct parser *par, struct identifier *ident)
{
struct token tok = {0};
struct identifier *i = ident;
- trace(par, "identifier");
+ trace("parse", "identifier");
while (true) {
want(par, T_NAME, &tok);
@@ -91,7 +83,7 @@ parse_identifier(struct parser *par, struct identifier *ident)
static void
parse_import(struct parser *par, struct ast_imports *imports)
{
- trace(par, "import");
+ trace("parse", "import");
struct identifier ident = {0};
parse_identifier(par, &ident);
@@ -114,7 +106,7 @@ parse_import(struct parser *par, struct ast_imports *imports)
static void
parse_imports(struct parser *par, struct ast_subunit *subunit)
{
- trace(par, "imports");
+ trace("parse", "imports");
struct token tok = {0};
struct ast_imports **next = &subunit->imports;
diff --git a/src/trace.c b/src/trace.c
@@ -0,0 +1,20 @@
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "trace.h"
+
+void
+trace(const char *sys, const char *fmt, ...)
+{
+ char *t = getenv("HA_TRACE");
+ if (!t || !strstr(t, sys)) {
+ return;
+ }
+ va_list ap;
+ va_start(ap, fmt);
+ fprintf(stderr, "[%10s] ", sys);
+ vfprintf(stderr, fmt, ap);
+ fprintf(stderr, "\n");
+ va_end(ap);
+}