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
17enum class TokenType : std::uint8_t {
18 // End of input
20
21 // Preprocessor directives
22 hash, // #
23 kw_define, // define
24 kw_undef, // undef (reserved for future)
25 kw_include, // include (reserved for future)
26 kw_ifdef, // ifdef (reserved for future)
27 kw_ifndef, // ifndef (reserved for future)
28 kw_if, // if (reserved for future)
29 kw_else, // else (reserved for future)
30 kw_elif, // elif (reserved for future)
31 kw_endif, // endif (reserved for future)
32 kw_defined, // defined (reserved for future)
33 kw_pragma, // pragma (reserved for future)
34 kw_enum, // enum (for enum declarations)
35
36 // Literals
37 identifier, // variable/macro names
38 integer_literal, // decimal, hex, octal, binary integers
39 string_literal, // "..." strings
40 char_literal, // '...' character literals (reserved for future)
41
42 // Operators
43 plus, // +
44 minus, // -
45 star, // *
46 slash, // /
47 percent, // %
48 ampersand, // &
49 pipe, // |
50 caret, // ^
51 tilde, // ~
52 exclaim, // !
53 less, // <
54 greater, // >
55 equal, // =
56 question, // ? (reserved for future)
57 colon, // : (reserved for future)
58
59 // Compound operators
60 less_less, // <<
61 greater_greater, // >>
62 ampersand_ampersand, // && (reserved for future)
63 pipe_pipe, // || (reserved for future)
64 equal_equal, // == (reserved for future)
65 exclaim_equal, // != (reserved for future)
66 less_equal, // <= (reserved for future)
67 greater_equal, // >= (reserved for future)
68
69 // Delimiters
70 left_paren, // (
71 right_paren, // )
72 left_brace, // { (reserved for future)
73 right_brace, // } (reserved for future)
74 left_bracket, // [ (reserved for future)
75 right_bracket, // ] (reserved for future)
76 comma, // ,
77 semicolon, // ; (reserved for future)
78 period, // . (for designated initializers)
79
80 // Special
81 newline, // Significant for preprocessor directives
82 unknown, // Unrecognized character
83};
84
89[[nodiscard]] std::string token_type_name(TokenType type);
90
100class Token {
101 public:
108 : type_{type}, text_{std::move(text)}, position_{position}
109 {
110 }
111
117 Token(std::string text, std::int64_t int_value, SourcePosition position)
118 : type_{TokenType::integer_literal}, text_{std::move(text)}, position_{position}, int_value_{int_value}
119 {
120 }
121
125 [[nodiscard]] TokenType type() const
126 {
127 return type_;
128 }
129
133 [[nodiscard]] const std::string &text() const
134 {
135 return text_;
136 }
137
141 [[nodiscard]] const SourcePosition &position() const
142 {
143 return position_;
144 }
145
150 [[nodiscard]] std::int64_t int_value() const
151 {
152 assert_or_panic(type_ == TokenType::integer_literal, "int_value() called on non-integer token");
153 return int_value_;
154 }
155
160 [[nodiscard]] bool is(TokenType type) const
161 {
162 return type_ == type;
163 }
164
170 template <typename... Types>
171 [[nodiscard]] bool is_any_of(Types... types) const
172 {
173 return ((type_ == types) || ...);
174 }
175
176 private:
177 TokenType type_;
178 std::string text_;
179 SourcePosition position_;
180 std::int64_t int_value_{0}; // Only valid for integer_literal
181};
182
183} // namespace porytiles
Represents a lexical token from C source code.
Definition token.hpp:100
std::int64_t int_value() const
Returns the integer value for integer_literal tokens.
Definition token.hpp:150
bool is(TokenType type) const
Checks if this token is of the specified type.
Definition token.hpp:160
Token(TokenType type, std::string text, SourcePosition position)
Constructs a token with the given type, text, and position.
Definition token.hpp:107
bool is_any_of(Types... types) const
Checks if this token is any of the specified types.
Definition token.hpp:171
const SourcePosition & position() const
Returns the source position where the token starts.
Definition token.hpp:141
Token(std::string text, std::int64_t int_value, SourcePosition position)
Constructs an integer literal token with a parsed value.
Definition token.hpp:117
TokenType type() const
Returns the token type.
Definition token.hpp:125
const std::string & text() const
Returns the raw text of the token.
Definition token.hpp:133
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:17
Represents a position within source content.