From c7d3a7789c6a14100ff0b4c8eaf2b0d7d111347c Mon Sep 17 00:00:00 2001 From: Itamar Date: Fri, 13 Aug 2021 12:11:26 +0300 Subject: [PATCH] LibCpp: Add lexer option to ignore whitespace tokens --- Userland/Libraries/LibCpp/Lexer.cpp | 2 ++ Userland/Libraries/LibCpp/Lexer.h | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/Userland/Libraries/LibCpp/Lexer.cpp b/Userland/Libraries/LibCpp/Lexer.cpp index 56ab1afd6c..2edb5b4863 100644 --- a/Userland/Libraries/LibCpp/Lexer.cpp +++ b/Userland/Libraries/LibCpp/Lexer.cpp @@ -222,6 +222,8 @@ Vector Lexer::lex() token_start_position = m_position; }; 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)); }; diff --git a/Userland/Libraries/LibCpp/Lexer.h b/Userland/Libraries/LibCpp/Lexer.h index bbee2bbf9f..88cd64d4a8 100644 --- a/Userland/Libraries/LibCpp/Lexer.h +++ b/Userland/Libraries/LibCpp/Lexer.h @@ -18,6 +18,8 @@ public: Vector lex(); + void set_ignore_whitespace(bool value) { m_options.ignore_whitespace = value; } + private: char peek(size_t offset = 0) const; char consume(); @@ -26,6 +28,10 @@ private: size_t m_index { 0 }; Position m_previous_position { 0, 0 }; Position m_position { 0, 0 }; + + struct Options { + bool ignore_whitespace { false }; + } m_options; }; }