28 enum class Type : uint8_t {
64 Token(
const Token &other) : _type(other._type), _value(other._value), _location(other._location) {}
65 Token(
Token &&other) noexcept : _type(other._type), _value(std::move(other._value)), _location(other._location) {}
70 _value = other._value;
71 _location = other._location;
78 _value = std::move(other._value);
79 _location = other._location;
95 return std::format(
"Token(type: {}, value: '{}', line: {}, column: {})",
typeToString(), _value, _location.line(),
100 const std::string &
value()
const {
return _value; }
101 size_t line()
const {
return _location.line(); }
102 size_t column()
const {
return _location.column(); }
192 bool isIdentifier(
char ch)
const;
198 bool isSymbol(
char ch)
const;
219 Result<Token> readIdentifierOrKeywordOrType(std::pair<size_t, size_t> start);
243 std::string unescapeString(std::string &&
string)
const;
Lexer()
Constructs a Lexer.
Definition lexer.cpp:73
Result< Token > peekToken()
Returns the next token without advancing the input.
Definition lexer.cpp:137
Result< Token > nextToken()
Returns the next token from the input.
Definition lexer.cpp:101
void skipWhitespaceAndComment()
Skips whitespace characters and comment lines in the input.
Definition lexer.cpp:148
Result< std::vector< Token > > getTokens()
Tokenizes the entire input.
Definition lexer.cpp:159
Result< void > setSource(const std::string &filepath)
Sets the source file for the lexer.
Definition lexer.cpp:85
void setSourceString(std::string_view source)
Sets the source string for the lexer.
Definition lexer.cpp:80
Struct to hold location information.
Definition error.h:10
std::expected< T, CompileError > Result
A type alias for the result of a compilation operation.
Definition error.h:59
std::ostream & operator<<(std::ostream &os, const Token &token)
Outputs a token to the given output stream.
Definition lexer.cpp:385
Represents a token produced by the lexer.
Definition lexer.h:26
std::string typeToString() const
Converts the token type to a string representation.
Definition lexer.cpp:10
Token & operator=(const Token &other)
Definition lexer.h:67
friend constexpr bool operator==(const Token &lhs, const Token::Type &rhs)
Definition lexer.h:110
const std::string & value() const
Definition lexer.h:100
Token(Token &&other) noexcept
Definition lexer.h:65
std::string toString() const
Converts the token to a string representation.
Definition lexer.h:94
size_t line() const
Definition lexer.h:101
Type type() const
Definition lexer.h:99
Token(const Token &other)
Definition lexer.h:64
Location location() const
Definition lexer.h:103
Token(Type type, std::string value, Location location)
Definition lexer.h:62
size_t column() const
Definition lexer.h:102
Type
The type of the token.
Definition lexer.h:28
@ L_PAREN
Definition lexer.h:36
@ MULTIPLY
Definition lexer.h:33
@ L_BRACE
Definition lexer.h:40
@ R_BRACE
Definition lexer.h:41
@ DIVIDE
Definition lexer.h:34
@ EQ
Definition lexer.h:46
@ NUMBER
Definition lexer.h:30
@ KW_RETURN
Definition lexer.h:54
@ KW_DEF
Definition lexer.h:53
@ R_BRACKET
Definition lexer.h:39
@ COMMA
Definition lexer.h:42
@ R_PAREN
Definition lexer.h:37
@ STRING
Definition lexer.h:35
@ UNKNOWN
Definition lexer.h:57
@ IDENTIFIER
Definition lexer.h:29
@ EOF_TOKEN
Definition lexer.h:56
@ PLUS
Definition lexer.h:31
@ TYPE
Definition lexer.h:52
@ L_BRACKET
Definition lexer.h:38
@ LT
Definition lexer.h:48
@ ARROW
Definition lexer.h:44
@ GT
Definition lexer.h:49
@ LE
Definition lexer.h:50
@ NE
Definition lexer.h:47
@ COLON
Definition lexer.h:43
@ GE
Definition lexer.h:51
@ KW_IF
Definition lexer.h:55
@ MINUS
Definition lexer.h:32
@ ASSIGN
Definition lexer.h:45
Token()
Definition lexer.h:60
Token & operator=(Token &&other) noexcept
Definition lexer.h:75