summaryrefslogtreecommitdiff
path: root/src/token.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/token.cc')
-rw-r--r--src/token.cc30
1 files changed, 27 insertions, 3 deletions
diff --git a/src/token.cc b/src/token.cc
index 7f38f71..ddc0a68 100644
--- a/src/token.cc
+++ b/src/token.cc
@@ -1,8 +1,9 @@
#ifndef TOKEN_CC
#define TOKEN_CC
-#include "source.cc"
+#include <cstdio>
#include "common.cc"
+#include "source.cc"
#define TOKEN_KINDS_NOLEX \
X(Eof) \
@@ -13,7 +14,7 @@
#define TOKEN_KIND(name) Token_Kind_##name
-enum Token_Kind {
+enum Token_Kind: u8 {
#define X(name) TOKEN_KIND(name),
TOKEN_KINDS_NOLEX
#undef X
@@ -29,7 +30,30 @@ struct Token {
Span span;
Token() : kind(Token_Kind_Eof), text(), span() {}
- Token(Token_Kind kind, String text, Span span) : kind(kind), text(text), span(span) {}
+ Token(Token_Kind kind, String text, Span span)
+ : kind(kind), text(text), span(span) {}
};
+bool token_write(const Token* token, FILE* stream) {
+ assert_neq(token, nullptr);
+ assert_neq(stream, nullptr);
+
+ i32 rc = fprintf(stream, "Token { kind: %d, text: ", token->kind);
+ if(unlikely(rc < 0)) return false;
+
+ bool status = string_write(&token->text, stream);
+ if(unlikely(!status)) return false;
+
+ rc = fprintf(stream, ", span: ");
+ if(unlikely(rc < 0)) return false;
+
+ status = span_write(&token->span, stream);
+ if(unlikely(!status)) return false;
+
+ rc = fprintf(stream, " }");
+ if(unlikely(rc < 0)) return false;
+
+ return true;
+}
+
#endif