Porytiles
Loading...
Searching...
No Matches
token.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <string>
5
8
9namespace porytiles {
10
19enum class TokenType : std::uint8_t {
20 // End of input
22
23 // Preprocessor directives
24 hash, // #
25 kw_define, // define
26 kw_undef, // undef (reserved for future)
27 kw_include, // include (reserved for future)
28 kw_ifdef, // ifdef (reserved for future)
29 kw_ifndef, // ifndef (reserved for future)
30 kw_if, // if (reserved for future)
31 kw_else, // else (reserved for future)
32 kw_elif, // elif (reserved for future)
33 kw_endif, // endif (reserved for future)
34 kw_defined, // defined (reserved for future)
35 kw_pragma, // pragma (reserved for future)
36 kw_enum, // enum (for enum declarations)
37
38 // Literals
39 identifier, // variable/macro names
40 integer_literal, // decimal, hex, octal, binary integers
41 string_literal, // "..." strings
42 char_literal, // '...' character literals (reserved for future)
43
44 // Operators
45 plus, // +
46 minus, // -
47 star, // *
48 slash, // /
49 percent, // %
50 ampersand, // &
51 pipe, // |
52 caret, // ^
53 tilde, // ~
54 exclaim, // !
55 less, // <
56 greater, // >
57 equal, // =
58 question, // ? (reserved for future)
59 colon, // : (reserved for future)
60
61 // Compound operators
62 less_less, // <<
63 greater_greater, // >>
64 ampersand_ampersand, // && (reserved for future)
65 pipe_pipe, // || (reserved for future)
66 equal_equal, // == (reserved for future)
67 exclaim_equal, // != (reserved for future)
68 less_equal, // <= (reserved for future)
69 greater_equal, // >= (reserved for future)
70
71 // Delimiters
72 left_paren, // (
73 right_paren, // )
74 left_brace, // { (reserved for future)
75 right_brace, // } (reserved for future)
76 left_bracket, // [ (reserved for future)
77 right_bracket, // ] (reserved for future)
78 comma, // ,
79 semicolon, // ; (reserved for future)
80 period, // . (for designated initializers)
81
82 // Special
83 newline, // Significant for preprocessor directives
84 unknown, // Unrecognized character
85};
86
93[[nodiscard]] std::string token_type_name(TokenType type);
94
106class Token {
107 public:
116 : type_{type}, text_{std::move(text)}, position_{position}
117 {
118 }
119
127 Token(std::string text, std::int64_t int_value, SourcePosition position)
128 : type_{TokenType::integer_literal}, text_{std::move(text)}, position_{position}, int_value_{int_value}
129 {
130 }
131
137 [[nodiscard]] TokenType type() const
138 {
139 return type_;
140 }
141
147 [[nodiscard]] const std::string &text() const
148 {
149 return text_;
150 }
151
157 [[nodiscard]] const SourcePosition &position() const
158 {
159 return position_;
160 }
161
168 [[nodiscard]] std::int64_t int_value() const
169 {
170 assert_or_panic(type_ == TokenType::integer_literal, "int_value() called on non-integer token");
171 return int_value_;
172 }
173
180 [[nodiscard]] bool is(TokenType type) const
181 {
182 return type_ == type;
183 }
184
192 template <typename... Types>
193 [[nodiscard]] bool is_any_of(Types... types) const
194 {
195 return ((type_ == types) || ...);
196 }
197
198 private:
199 TokenType type_;
200 std::string text_;
201 SourcePosition position_;
202 std::int64_t int_value_{0}; // Only valid for integer_literal
203};
204
205} // namespace porytiles
Represents a lexical token from C source code.
Definition token.hpp:106
std::int64_t int_value() const
Returns the integer value for integer_literal tokens.
Definition token.hpp:168
bool is(TokenType type) const
Checks if this token is of the specified type.
Definition token.hpp:180
Token(TokenType type, std::string text, SourcePosition position)
Constructs a token with the given type, text, and position.
Definition token.hpp:115
bool is_any_of(Types... types) const
Checks if this token is any of the specified types.
Definition token.hpp:193
const SourcePosition & position() const
Returns the source position where the token starts.
Definition token.hpp:157
Token(std::string text, std::int64_t int_value, SourcePosition position)
Constructs an integer literal token with a parsed value.
Definition token.hpp:127
TokenType type() const
Returns the token type.
Definition token.hpp:137
const std::string & text() const
Returns the raw text of the token.
Definition token.hpp:147
void assert_or_panic(bool condition, const StringViewSourceLoc &s)
Conditionally panics if the given condition is false.
Definition panic.cpp:53
std::string token_type_name(TokenType type)
Returns a human-readable name for a token type.
Definition lexer.cpp:61
TokenType
Enumeration of token types recognized by the C parser lexer.
Definition token.hpp:19
Represents a position within source content.