picceler
A compiled DSL for image processing, built on MLIR
Loading...
Searching...
No Matches
lexer.h
Go to the documentation of this file.
1
8
9#pragma once
10
11#include <cstdint>
12#include <fstream>
13#include <ostream>
14#include <string>
15#include <utility>
16#include <vector>
17
18#include "error.h"
19
20namespace picceler {
21
26struct Token {
28 enum class Type : uint8_t {
29 IDENTIFIER, // Represents identifiers (user defined names)
30 NUMBER, // Represents numeric literals (e.g., integers, floats)
31 PLUS, // Represents binary operation '+'
32 MINUS, // Represents binary operation '-'
33 MULTIPLY, // Represents binary operation '*'
34 DIVIDE, // Represents binary operation '/'
35 STRING, // Represents string literals
36 L_PAREN, // Represents the left parenthesis '('
37 R_PAREN, // Represents the right parenthesis ')'
38 L_BRACKET, // Represents the left bracket '['
39 R_BRACKET, // Represents the right bracket ']'
40 L_BRACE, // Represents the left brace '{'
41 R_BRACE, // Represents the right brace '}'
42 COMMA, // Represents the comma ','
43 COLON, // Represents the colon ':'
44 ARROW, // Represents the arrow '->'
45 ASSIGN, // Represents the assignment operator '='
46 EQ, // Represents the assignment operator '=='
47 NE, // Represents the assignment operator '!='
48 LT, // Represents the assignment operator '<'
49 GT, // Represents the assignment operator '>'
50 LE, // Represents the assignment operator '<='
51 GE, // Represents the assignment operator '>='
52 KW_FOR, // Represents the keyword 'for'
53 KW_STEP, // Represents the keyword 'step'
54 DOT_DOT, // Represents the operator '..'
55 TYPE, // Represents type annotations (e.g., int, float, string)
56 KW_DEF, // Represents the keyword 'def'
57 KW_RETURN, // Represents the keyword 'return'
58 KW_IF, // Represents the keyword 'if'
59 KW_ELSE, // Represents the keyword 'else'
60 EOF_TOKEN, // Represents the end of file
61 UNKNOWN // Represents unknown tokens
62 };
63
64 Token() : _type(Type::UNKNOWN), _value(""), _location() {}
65
66 Token(Type type, std::string value, Location location) : _type(type), _value(std::move(value)), _location(location) {}
67
68 Token(const Token &other) : _type(other._type), _value(other._value), _location(other._location) {}
69 Token(Token &&other) noexcept : _type(other._type), _value(std::move(other._value)), _location(other._location) {}
70 ~Token() = default;
71 Token &operator=(const Token &other) {
72 if (this != &other) {
73 _type = other._type;
74 _value = other._value;
75 _location = other._location;
76 }
77 return *this;
78 }
79 Token &operator=(Token &&other) noexcept {
80 if (this != &other) {
81 _type = other._type;
82 _value = std::move(other._value);
83 _location = other._location;
84 }
85 return *this;
86 }
87
92 std::string typeToString() const;
93
98 std::string toString() const {
99 return std::format("Token(type: {}, value: '{}', line: {}, column: {})", typeToString(), _value, _location.line(),
100 _location.column());
101 }
102
103 Type type() const { return _type; }
104 const std::string &value() const { return _value; }
105 size_t line() const { return _location.line(); }
106 size_t column() const { return _location.column(); }
107 Location location() const { return _location; }
108 /*
109 * @brief Compares a token to a token type for easy checking in parsing.
110 * @param lhs The token to compare.
111 * @param rhs The token type to compare against.
112 * @return True if the token's type matches the token type, false otherwise.
113 */
114 friend constexpr bool operator==(const Token &lhs, const Token::Type &rhs) { return lhs._type == rhs; }
115
116private:
117 Type _type;
118 std::string _value;
119 Location _location;
120};
121
126class Lexer {
127public:
131 Lexer();
132
137 void setSourceString(std::string_view source);
138
143 Result<void> setSource(const std::string &filepath);
144
150
156
161
167
168private:
173 void resetState();
174
178 bool eof() const;
179
183 char peek() const;
184
188 char get();
189
195
196 bool isIdentifier(char ch) const;
202 bool isSymbol(char ch) const;
203
209 Result<Token::Type> isKeyword(const std::string &value) const;
210
216 Result<Token::Type> isType(const std::string &value) const;
217
223 Result<Token> readIdentifierOrKeywordOrType(std::pair<size_t, size_t> start);
224
230 Result<Token> readNumber(std::pair<size_t, size_t> start);
231
237 Result<Token> readString(std::pair<size_t, size_t> start);
238
244 Result<Token> readSymbol(std::pair<size_t, size_t> start);
245
246private:
247 std::string unescapeString(std::string &&string) const;
248
249private:
250 std::ifstream _file;
251 std::string _buffer;
252 size_t _position;
253 size_t _line;
254 size_t _column;
255};
256
263std::ostream &operator<<(std::ostream &os, const Token &token);
264
265} // namespace picceler
Lexer()
Constructs a Lexer.
Definition lexer.cpp:81
Result< Token > peekToken()
Returns the next token without advancing the input.
Definition lexer.cpp:145
Result< Token > nextToken()
Returns the next token from the input.
Definition lexer.cpp:109
void skipWhitespaceAndComment()
Skips whitespace characters and comment lines in the input.
Definition lexer.cpp:156
Result< std::vector< Token > > getTokens()
Tokenizes the entire input.
Definition lexer.cpp:167
Result< void > setSource(const std::string &filepath)
Sets the source file for the lexer.
Definition lexer.cpp:93
void setSourceString(std::string_view source)
Sets the source string for the lexer.
Definition lexer.cpp:88
Struct to hold location information.
Definition error.h:10
Definition ast.h:11
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:394
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:71
friend constexpr bool operator==(const Token &lhs, const Token::Type &rhs)
Definition lexer.h:114
const std::string & value() const
Definition lexer.h:104
Token(Token &&other) noexcept
Definition lexer.h:69
std::string toString() const
Converts the token to a string representation.
Definition lexer.h:98
size_t line() const
Definition lexer.h:105
Type type() const
Definition lexer.h:103
~Token()=default
Token(const Token &other)
Definition lexer.h:68
Location location() const
Definition lexer.h:107
Token(Type type, std::string value, Location location)
Definition lexer.h:66
size_t column() const
Definition lexer.h:106
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:57
@ KW_DEF
Definition lexer.h:56
@ R_BRACKET
Definition lexer.h:39
@ COMMA
Definition lexer.h:42
@ KW_STEP
Definition lexer.h:53
@ R_PAREN
Definition lexer.h:37
@ STRING
Definition lexer.h:35
@ UNKNOWN
Definition lexer.h:61
@ IDENTIFIER
Definition lexer.h:29
@ EOF_TOKEN
Definition lexer.h:60
@ PLUS
Definition lexer.h:31
@ TYPE
Definition lexer.h:55
@ KW_FOR
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
@ DOT_DOT
Definition lexer.h:54
@ NE
Definition lexer.h:47
@ KW_ELSE
Definition lexer.h:59
@ COLON
Definition lexer.h:43
@ GE
Definition lexer.h:51
@ KW_IF
Definition lexer.h:58
@ MINUS
Definition lexer.h:32
@ ASSIGN
Definition lexer.h:45
Token()
Definition lexer.h:64
Token & operator=(Token &&other) noexcept
Definition lexer.h:79