nixd
Loading...
Searching...
No Matches
Token.h
Go to the documentation of this file.
1#pragma once
2
3#include "nixf/Basic/Range.h"
5
6#include <cassert>
7#include <string_view>
8
9namespace nixf {
10
11namespace tok {
12
13constexpr std::string_view spelling(TokenKind Kind) {
14 switch (Kind) {
15#define TOK_KEYWORD(NAME) \
16 case tok_kw_##NAME: \
17 return #NAME;
19#undef TOK_KEYWORD
20 case tok_dquote:
21 return "\"";
22 case tok_quote2:
23 return "''";
24 case tok_dollar_curly:
25 return "${";
26 case tok_l_curly:
27 return "{";
28 case tok_r_curly:
29 return "}";
30 case tok_l_paren:
31 return "(";
32 case tok_r_paren:
33 return ")";
34 case tok_eq:
35 return "=";
36 case tok_semi_colon:
37 return ";";
38 case tok_l_bracket:
39 return "[";
40 case tok_r_bracket:
41 return "]";
42 case tok_colon:
43 return ":";
44 default:
45 assert(false && "Not yet implemented!");
46 }
47 __builtin_unreachable();
48}
49
50} // namespace tok
51
52/// \brief A token. With it's kind, and the range in source code.
53///
54/// This class is trivially copyable.
55class Token {
56 tok::TokenKind Kind;
57 LexerCursorRange Range;
58 std::string_view View;
59
60public:
61 Token(tok::TokenKind Kind, LexerCursorRange Range, std::string_view View)
62 : Kind(Kind), Range(Range), View(View) {}
63 [[nodiscard]] LexerCursor lCur() const { return Range.lCur(); }
64 [[nodiscard]] LexerCursor rCur() const { return Range.rCur(); }
65 [[nodiscard]] tok::TokenKind kind() const { return Kind; }
66 [[nodiscard]] LexerCursorRange range() const { return Range; }
67 [[nodiscard]] std::string_view view() const { return View; }
68};
69
70} // namespace nixf
LexerCursor lCur() const
Definition Range.h:116
LexerCursor rCur() const
Definition Range.h:117
A point in the source file.
Definition Range.h:57
A token. With it's kind, and the range in source code.
Definition Token.h:55
LexerCursor lCur() const
Definition Token.h:63
tok::TokenKind kind() const
Definition Token.h:65
LexerCursorRange range() const
Definition Token.h:66
Token(tok::TokenKind Kind, LexerCursorRange Range, std::string_view View)
Definition Token.h:61
LexerCursor rCur() const
Definition Token.h:64
std::string_view view() const
Definition Token.h:67
constexpr std::string_view spelling(TokenKind Kind)
Definition Token.h:13