diff options
| author | Fabrice <fabrice@schaub-dev.xyz> | 2026-03-03 09:42:37 +0100 |
|---|---|---|
| committer | Fabrice <fabrice@schaub-dev.xyz> | 2026-03-03 09:42:37 +0100 |
| commit | 3a9f7e6c9e1f44385c1950edc6267af8eea56a7e (patch) | |
| tree | fbf7175fbaec5d9776d105a6488680c87a694a61 /src/token.cc | |
| parent | 7ff9d2586a666bd420b98eb9f8de130cf57cd59d (diff) | |
formatting of tokens
Diffstat (limited to 'src/token.cc')
| -rw-r--r-- | src/token.cc | 30 |
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 |
