Line data Source code
1 : #pragma once
2 :
3 : #include <string>
4 :
5 : #include "stax/utils/result.hpp"
6 : #include "tokens.hpp"
7 :
8 : namespace stax::lexer {
9 :
10 : #undef EOF
11 :
12 4 : DEFINE_ERROR("lexer", Error, EOF, Syntax, IllegalCharacter)
13 :
14 : const std::unordered_map<std::string_view, token::Type> kKeywords = {
15 : {"return", token::Type::kReturn},
16 : };
17 :
18 : class Lexer {
19 : public:
20 : Lexer(std::string input);
21 :
22 : auto NextToken() -> result_t<token::Token>;
23 :
24 : private:
25 216 : static auto IsWhitespace(const char c) {
26 216 : constexpr std::array kWhitespaceChars{' ', '\t', '\n'};
27 216 : return std::ranges::any_of(kWhitespaceChars, [c](const char whitespace) {
28 422 : return c == whitespace;
29 432 : });
30 : }
31 :
32 131 : static auto IsLetter(const char c) {
33 131 : return std::isalpha(c) || c == '_';
34 : }
35 :
36 30 : static auto IsDigit(const char c) {
37 30 : return std::isdigit(c) || c == '.';
38 : }
39 :
40 : auto NextChar() -> result_t<char>;
41 :
42 : void Advance();
43 :
44 : auto Identifier() -> result_t<std::string_view>;
45 : auto Number() -> result_t<std::string_view>;
46 : void ConsumeWhitespace();
47 :
48 : std::string input_;
49 : bool started = false;
50 : std::string::iterator curr_;
51 : std::string::iterator peek_;
52 : };
53 :
54 : } // namespace stax::lexer
|