File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -78,17 +78,23 @@ bool Lexer::read_more_tokens()
7878{
7979 const std::size_t token_size = m_tokens_to_emit.size ();
8080 while (token_size >= m_tokens_to_emit.size ()) {
81+ const std::size_t already_filtered = m_tokens_to_emit.size ();
8182 bool read_tokens = read_more_tokens_loop ();
8283 if (read_tokens) {
83- std::erase_if (m_tokens_to_emit, [this ](Token &token) {
84- if (m_ignore_comments && token.token_type () == Token::TokenType::COMMENT ) {
85- return true ;
86- }
87- if (m_ignore_nl_token && token.token_type () == Token::TokenType::NL ) {
88- return true ;
89- }
90- return false ;
91- });
84+ const auto first = m_tokens_to_emit.begin () + already_filtered;
85+ m_tokens_to_emit.erase (
86+ std::remove_if (first,
87+ m_tokens_to_emit.end (),
88+ [this ](const Token &token) {
89+ if (m_ignore_comments && token.token_type () == Token::TokenType::COMMENT ) {
90+ return true ;
91+ }
92+ if (m_ignore_nl_token && token.token_type () == Token::TokenType::NL ) {
93+ return true ;
94+ }
95+ return false ;
96+ }),
97+ m_tokens_to_emit.end ());
9298 } else {
9399 return false ;
94100 }
@@ -974,4 +980,4 @@ Token Lexer::pop_front()
974980 auto &result = m_tokens_to_emit.front ();
975981 m_tokens_to_emit.pop_front ();
976982 return result;
977- }
983+ }
Original file line number Diff line number Diff line change @@ -609,6 +609,24 @@ TEST(Lexer, IgnoreCommentsAndNL)
609609 assert_generates_tokens_without_comment_tokens (program, expected_tokens);
610610}
611611
612+ TEST (Lexer, IgnoreCommentsAndNLLateInTheProgram)
613+ {
614+ constexpr std::size_t line_count = 200 ;
615+ std::string program;
616+ std::vector<Token::TokenType> expected_tokens;
617+ for (std::size_t i = 0 ; i < line_count; ++i) {
618+ program += std::format (" # leading comment {}\n " , i);
619+ program += " \n " ;
620+ program += std::format (" a{} = {} # trailing comment\n " , i, i);
621+ expected_tokens.push_back (Token::TokenType::NAME );
622+ expected_tokens.push_back (Token::TokenType::EQUAL );
623+ expected_tokens.push_back (Token::TokenType::NUMBER );
624+ expected_tokens.push_back (Token::TokenType::NEWLINE );
625+ }
626+ expected_tokens.push_back (Token::TokenType::ENDMARKER );
627+ assert_generates_tokens_without_comment_tokens (program, expected_tokens);
628+ }
629+
612630TEST (Lexer, Ellipsis)
613631{
614632 constexpr std::string_view program = " ...\n " ;
You can’t perform that action at this time.
0 commit comments