|
| 1 | +#include "Lexer.hpp" |
| 2 | +#include "utils.hpp" |
| 3 | + |
| 4 | +Lexer::Lexer(std::string_view src, bool keep_comments) : |
| 5 | + src_(src), keep_comments_(keep_comments), start_(0), current_(0), line_(1), col_(1), token_col_(1) { |
| 6 | + register_defaults(); |
| 7 | +} |
| 8 | + |
| 9 | +bool Lexer::is_at_end() const noexcept { |
| 10 | + return current_ >= src_.size(); |
| 11 | +} |
| 12 | + |
| 13 | +char Lexer::peek(size_t offset) const noexcept { |
| 14 | + size_t idx = current_ + offset; |
| 15 | + if (idx >= src_.size()) |
| 16 | + return '\0'; |
| 17 | + return src_[idx]; |
| 18 | +} |
| 19 | + |
| 20 | +char Lexer::current_char() const noexcept { |
| 21 | + if (current_ == 0) |
| 22 | + return '\0'; |
| 23 | + return src_[current_ - 1]; |
| 24 | +} |
| 25 | + |
| 26 | +char Lexer::advance() { |
| 27 | + if (is_at_end()) |
| 28 | + return '\0'; |
| 29 | + char c = src_[current_++]; |
| 30 | + if (c == '\n') { |
| 31 | + ++line_; |
| 32 | + col_ = 1; |
| 33 | + } else |
| 34 | + ++col_; |
| 35 | + return c; |
| 36 | +} |
| 37 | + |
| 38 | +void Lexer::retreat_one() { |
| 39 | + if (current_ == 0) |
| 40 | + return; |
| 41 | + --current_; |
| 42 | + int l = 1; |
| 43 | + for (size_t i = 0; i < current_; ++i) |
| 44 | + if (src_[i] == '\n') |
| 45 | + ++l; |
| 46 | + line_ = l; |
| 47 | + int col = 1; |
| 48 | + for (size_t i = current_; i > 0; --i) { |
| 49 | + if (src_[i - 1] == '\n') { |
| 50 | + col = static_cast<int>(current_ - i + 1); |
| 51 | + break; |
| 52 | + } |
| 53 | + if (i == 1) |
| 54 | + col = static_cast<int>(current_); |
| 55 | + } |
| 56 | + col_ = col; |
| 57 | +} |
| 58 | + |
| 59 | +void Lexer::consume_while(std::string &out, const std::function<bool(char)> &pred) { |
| 60 | + while (!is_at_end() && pred(peek())) { |
| 61 | + out.push_back(advance()); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +std::string Lexer::raw_lexeme() const { |
| 66 | + if (current_ >= start_) |
| 67 | + return std::string(src_.substr(start_, current_ - start_)); |
| 68 | + return {}; |
| 69 | +} |
| 70 | + |
| 71 | +bool Lexer::is_keyword(std::string_view s) const { |
| 72 | + return keyword_set().contains(std::string(s)); |
| 73 | +} |
| 74 | + |
| 75 | +bool Lexer::is_multiop(std::string_view s) const { |
| 76 | + return multi_ops_set().contains(std::string(s)); |
| 77 | +} |
| 78 | + |
| 79 | +std::vector<TokenPtr> Lexer::tokenize() { |
| 80 | + std::vector<TokenPtr> tokens; |
| 81 | + while (!is_at_end()) { |
| 82 | + start_ = current_; |
| 83 | + token_col_ = col_; |
| 84 | + char c = advance(); |
| 85 | + Handler *h = handlers_[static_cast<unsigned char>(c)].get(); |
| 86 | + if (!h) |
| 87 | + h = default_handler_.get(); |
| 88 | + OptToken maybe = h->scan(*this); |
| 89 | + if (maybe && *maybe) |
| 90 | + tokens.push_back(std::move(*maybe)); |
| 91 | + } |
| 92 | + tokens.push_back(TokenFactory::make_eof(line_, col_)); |
| 93 | + return tokens; |
| 94 | +} |
| 95 | + |
| 96 | +void Lexer::register_defaults() { |
| 97 | + for (auto &p : handlers_) |
| 98 | + p.reset(); |
| 99 | + default_handler_.reset(); |
| 100 | + |
| 101 | + set_handler(' ', std::make_unique<WhitespaceHandler>()); |
| 102 | + set_handler('\t', std::make_unique<WhitespaceHandler>()); |
| 103 | + set_handler('\r', std::make_unique<WhitespaceHandler>()); |
| 104 | + |
| 105 | + set_handler('\n', std::make_unique<NewlineHandler>()); |
| 106 | + |
| 107 | + for (unsigned char c = 'a'; c <= 'z'; ++c) |
| 108 | + set_handler(c, std::make_unique<IdentifierHandler>()); |
| 109 | + for (unsigned char c = 'A'; c <= 'Z'; ++c) |
| 110 | + set_handler(c, std::make_unique<IdentifierHandler>()); |
| 111 | + set_handler((unsigned char) '_', std::make_unique<IdentifierHandler>()); |
| 112 | + |
| 113 | + for (unsigned char d = '0'; d <= '9'; ++d) |
| 114 | + set_handler(d, std::make_unique<NumberHandler>()); |
| 115 | + set_handler((unsigned char) '.', std::make_unique<NumberHandler>()); |
| 116 | + |
| 117 | + set_handler((unsigned char) '"', std::make_unique<StringHandler>()); |
| 118 | + set_handler((unsigned char) '\'', std::make_unique<CharHandler>()); |
| 119 | + |
| 120 | + set_handler((unsigned char) '/', std::make_unique<SlashHandler>()); |
| 121 | + |
| 122 | + const std::string opchars = "+-*/%<>=!&|^~?:."; |
| 123 | + for (unsigned char c : opchars) |
| 124 | + set_handler(c, std::make_unique<OperatorHandler>()); |
| 125 | + |
| 126 | + const std::string puncts = ",;:(){}[]"; |
| 127 | + for (unsigned char c : puncts) |
| 128 | + set_handler(c, std::make_unique<PunctHandler>()); |
| 129 | + |
| 130 | + set_default_handler(std::make_unique<DefaultHandler>()); |
| 131 | +} |
0 commit comments