Stax
Programming language
Loading...
Searching...
No Matches
lexer.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4
6#include "tokens.hpp"
7
8namespace stax::lexer {
9
10#undef EOF
11
12DEFINE_ERROR("lexer", Error, EOF, Syntax, IllegalCharacter)
13
14const std::unordered_map<std::string_view, token::Type> kKeywords = {
15 {"let", token::Type::kLet},
16 {"fn", token::Type::kFn},
17};
18
19class Lexer {
20 public:
21 Lexer(std::string input);
22
24
25 private:
26 static auto IsWhitespace(const char c) {
27 constexpr std::array kWhitespaceChars{' ', '\t', '\n'};
28 return std::ranges::any_of(kWhitespaceChars, [c](const char whitespace) {
29 return c == whitespace;
30 });
31 }
32
33 static auto IsLetter(const char c) {
34 return std::isalpha(c) || c == '_';
35 }
36
37 static auto IsDigit(const char c) {
38 return std::isdigit(c) || c == '.';
39 }
40
41 auto NextChar() -> result_t<char>;
42
43 void Advance();
44
45 auto Identifier() -> result_t<std::string_view>;
46 auto Number() -> result_t<std::string_view>;
47 void ConsumeWhitespace();
48
49 std::string input_;
50 bool started = false;
51 std::string::iterator curr_;
52 std::string::iterator peek_;
53};
54
55} // namespace stax::lexer
Definition lexer.hpp:19
auto NextToken() -> result_t< token::Token >
Definition lexer.cpp:16
Lexer(std::string input)
Definition lexer.cpp:10
Definition lexer.cpp:8
const std::unordered_map< std::string_view, token::Type > kKeywords
Definition lexer.hpp:14
boost::outcome_v2::result< T, result::Err, boost::outcome_v2::policy::terminate > result_t
Definition result.hpp:66
#define DEFINE_ERROR(module_name, name,...)
Definition result.hpp:91