diff options
| author | Stefan Weigl-Bosker <stefan@s00.xyz> | 2026-02-23 22:18:22 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-02-23 22:18:22 -0500 |
| commit | 8f98dc579af1993ec85bd849656c4835b4039dd6 (patch) | |
| tree | 3ee45620d83b209c1c11248afc9ab83ffcf39691 /willow/tools/willowc/include/tokenizer.hpp | |
| parent | c2d4209f85f46cc91163bc47cc43db252c94acf6 (diff) | |
| download | compiler-8f98dc579af1993ec85bd849656c4835b4039dd6.tar.gz | |
[willow]: frontend plumbing (#13)
...
Diffstat (limited to 'willow/tools/willowc/include/tokenizer.hpp')
| -rw-r--r-- | willow/tools/willowc/include/tokenizer.hpp | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/willow/tools/willowc/include/tokenizer.hpp b/willow/tools/willowc/include/tokenizer.hpp index 3de9d32..47577ab 100644 --- a/willow/tools/willowc/include/tokenizer.hpp +++ b/willow/tools/willowc/include/tokenizer.hpp @@ -1,6 +1,7 @@ #ifndef WILLOWC_INCLUDE_TOKENIZER_HPP #define WILLOWC_INCLUDE_TOKENIZER_HPP +#include <utility> #include <willow/IR/Location.h> namespace willowc { @@ -14,6 +15,7 @@ enum class TokenKind { Inst, Comma, + Colon, Semicolon, LParen, RParen, @@ -37,6 +39,8 @@ class Tokenizer { std::string_view buf; std::size_t offset; + friend class Parser; + void skip(std::size_t idx = 1) { offset += idx; } char eat(std::size_t num = 1) { @@ -55,6 +59,8 @@ class Tokenizer { return buf[offset + idx]; } + void recover(); + bool scan_id(bool accept_digits); bool scan_dec(); bool scan_hex(); @@ -67,6 +73,58 @@ public: void seek(uint64_t offset); }; +constexpr std::string_view TokenKindName(TokenKind t) { + switch (t) { + case TokenKind::Function: + return "Function"; + case TokenKind::Variable: + return "Variable"; + case TokenKind::Constant: + return "Constant"; + case TokenKind::Type: + return "Type"; + case TokenKind::Label: + return "Label"; + case TokenKind::Inst: + return "Inst"; + case TokenKind::Comma: + return "Comma"; + case TokenKind::Colon: + return "Colon"; + case TokenKind::Semicolon: + return "Semicolon"; + case TokenKind::LParen: + return "LParen"; + case TokenKind::RParen: + return "RParen"; + case TokenKind::LCurly: + return "LCurly"; + case TokenKind::RCurly: + return "RCurly"; + case TokenKind::Equals: + return "Equals"; + case TokenKind::RArrow: + return "RArrow"; + case TokenKind::Comment: + return "Comment"; + case TokenKind::FuncKW: + return "FuncKW"; + case TokenKind::Eof: + return "Eof"; + case TokenKind::Invalid: + return "Invalid"; + } + std::unreachable(); +} + } // namespace willowc +template <> +struct std::formatter<willowc::TokenKind> { + constexpr auto parse(std::format_parse_context &ctx) { return ctx.begin(); } + constexpr auto format(const willowc::TokenKind t, std::format_context &ctx) const { + return std::format_to(ctx.out(), "{}", TokenKindName(t)); + } +}; + #endif // WILLOWC_INCLUDE_TOKENIZER_HPP |