[pseudo] Strip comments for TokenStream.

Add a utility function to strip comments from a "raw" tokenstream. The
derived stream will be fed to the GLR parser (for early testing).

Differential Revision: https://reviews.llvm.org/D121092
This commit is contained in:
Haojian Wu 2022-03-07 10:47:38 +01:00
parent 6d9eb7e7ec
commit 2d01ac18df
3 changed files with 31 additions and 0 deletions

View File

@ -195,6 +195,9 @@ enum class LexFlags : uint8_t {
/// (And having cooked token kinds in PP-disabled sections is useful for us).
TokenStream cook(const TokenStream &, const clang::LangOptions &);
/// Drops comment tokens.
TokenStream stripComments(const TokenStream &);
} // namespace pseudo
} // namespace syntax
} // namespace clang

View File

@ -93,6 +93,17 @@ void TokenStream::print(llvm::raw_ostream &OS) const {
OS << '\n';
}
TokenStream stripComments(const TokenStream &Input) {
TokenStream Out;
for (const Token &T : Input.tokens()) {
if (T.Kind == tok::comment)
continue;
Out.push(T);
}
Out.finalize();
return Out;
}
} // namespace pseudo
} // namespace syntax
} // namespace clang

View File

@ -172,6 +172,23 @@ no_indent \
}));
}
TEST(TokenTest, DropComments) {
LangOptions Opts;
std::string Code = R"cpp(
// comment
int /*abc*/;
)cpp";
TokenStream Raw = cook(lex(Code, Opts), Opts);
TokenStream Stripped = stripComments(Raw);
EXPECT_THAT(Raw.tokens(),
ElementsAreArray(
{token("// comment", tok::comment), token("int", tok::kw_int),
token("/*abc*/", tok::comment), token(";", tok::semi)}));
EXPECT_THAT(Stripped.tokens(), ElementsAreArray({token("int", tok::kw_int),
token(";", tok::semi)}));
}
} // namespace
} // namespace pseudo
} // namespace syntax