From 606e05852f656e323a9cc8c5533045fb466cf1a1 Mon Sep 17 00:00:00 2001 From: Itamar Date: Sat, 21 Aug 2021 16:36:40 +0300 Subject: [PATCH] LibCpp: Add lex_iterable() method to the Lexer This allows us to collect the tokens iteratively instead of having to lex the whole program and then get a tokens vector. --- Userland/Libraries/LibCpp/Lexer.cpp | 17 ++++++++++++----- Userland/Libraries/LibCpp/Lexer.h | 9 +++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/Userland/Libraries/LibCpp/Lexer.cpp b/Userland/Libraries/LibCpp/Lexer.cpp index ea8367d0dc..85c61b8c6c 100644 --- a/Userland/Libraries/LibCpp/Lexer.cpp +++ b/Userland/Libraries/LibCpp/Lexer.cpp @@ -6,6 +6,7 @@ #include "Lexer.h" #include +#include #include #include #include @@ -205,15 +206,13 @@ static bool is_known_type(StringView const& string) return types.contains(string); } -Vector Lexer::lex() +void Lexer::lex_impl(Function callback) { - Vector tokens; - size_t token_start_index = 0; Position token_start_position; auto emit_single_char_token = [&](auto type) { - tokens.empend(type, m_position, m_position, m_input.substring_view(m_index, 1)); + callback(Token(type, m_position, m_position, m_input.substring_view(m_index, 1))); consume(); }; @@ -224,7 +223,7 @@ Vector Lexer::lex() auto commit_token = [&](auto type) { if (m_options.ignore_whitespace && type == Token::Type::Whitespace) return; - tokens.empend(type, token_start_position, m_previous_position, m_input.substring_view(token_start_index, m_index - token_start_index)); + callback(Token(type, token_start_position, m_previous_position, m_input.substring_view(token_start_index, m_index - token_start_index))); }; auto emit_token_equals = [&](auto type, auto equals_type) { @@ -784,6 +783,14 @@ Vector Lexer::lex() dbgln("Unimplemented token character: {}", ch); emit_single_char_token(Token::Type::Unknown); } +} + +Vector Lexer::lex() +{ + Vector tokens; + lex_impl([&](auto token) { + tokens.append(move(token)); + }); return tokens; } diff --git a/Userland/Libraries/LibCpp/Lexer.h b/Userland/Libraries/LibCpp/Lexer.h index 88cd64d4a8..e3f958ac7e 100644 --- a/Userland/Libraries/LibCpp/Lexer.h +++ b/Userland/Libraries/LibCpp/Lexer.h @@ -17,12 +17,15 @@ public: explicit Lexer(StringView const&, size_t start_line = 0); Vector lex(); + template + void lex_iterable(Callback); void set_ignore_whitespace(bool value) { m_options.ignore_whitespace = value; } private: char peek(size_t offset = 0) const; char consume(); + void lex_impl(Function); StringView m_input; size_t m_index { 0 }; @@ -34,4 +37,10 @@ private: } m_options; }; +template +void Lexer::lex_iterable(Callback callback) +{ + return lex_impl(move(callback)); +} + }