@@ -37,6 +37,12 @@ using ::testing::VariantWith;
37
37
38
38
namespace quick_lint_js {
39
39
namespace {
40
+ // Contains pointers to objects inside a Lexer.
41
+ struct Tokens_And_Diags {
42
+ std::vector<Token> tokens;
43
+ Diag_List* diags;
44
+ };
45
+
40
46
class Test_Lex : public ::testing::Test {
41
47
protected:
42
48
// NOTE(strager): These functions take callbacks as function pointers to
@@ -78,7 +84,7 @@ class Test_Lex : public ::testing::Test {
78
84
void check_tokens_with_errors (
79
85
String8_View input, Span<const Diagnostic_Assertion>,
80
86
std::initializer_list<Token_Type> expected_token_types, Source_Location);
81
- std::vector<Token> lex_to_eof (Padded_String_View, Diag_Reporter& );
87
+ Tokens_And_Diags lex_to_eof_returning_diags (Padded_String_View);
82
88
std::vector<Token> lex_to_eof (Padded_String_View,
83
89
Source_Location = Source_Location::current());
84
90
std::vector<Token> lex_to_eof (String8_View,
@@ -2931,25 +2937,24 @@ void Test_Lex::check_single_token_with_errors(
2931
2937
void Test_Lex::check_single_token_with_errors (
2932
2938
String8_View input, Span<const Diagnostic_Assertion> diagnostic_assertions,
2933
2939
String8_View expected_identifier_name, Source_Location caller) {
2934
- Diag_List_Diag_Reporter errors (&this ->memory_ );
2935
2940
Padded_String code (input);
2936
- std::vector<Token> lexed_tokens = this ->lex_to_eof (&code, errors );
2941
+ Tokens_And_Diags lexed = this ->lex_to_eof_returning_diags (&code);
2937
2942
2938
2943
EXPECT_THAT_AT_CALLER (
2939
- lexed_tokens ,
2944
+ lexed. tokens ,
2940
2945
ElementsAreArray ({
2941
2946
::testing::Field (" type" , &Token::type,
2942
2947
::testing::AnyOf (Token_Type::identifier,
2943
2948
Token_Type::private_identifier)),
2944
2949
}));
2945
- if (lexed_tokens .size () == 1 &&
2946
- (lexed_tokens [0 ].type == Token_Type::identifier ||
2947
- lexed_tokens [0 ].type == Token_Type::private_identifier)) {
2948
- EXPECT_EQ_AT_CALLER (lexed_tokens [0 ].identifier_name ().normalized_name (),
2950
+ if (lexed. tokens .size () == 1 &&
2951
+ (lexed. tokens [0 ].type == Token_Type::identifier ||
2952
+ lexed. tokens [0 ].type == Token_Type::private_identifier)) {
2953
+ EXPECT_EQ_AT_CALLER (lexed. tokens [0 ].identifier_name ().normalized_name (),
2949
2954
expected_identifier_name);
2950
2955
}
2951
2956
2952
- assert_diagnostics (&code, errors .diags () , diagnostic_assertions, caller);
2957
+ assert_diagnostics (&code, *lexed .diags , diagnostic_assertions, caller);
2953
2958
}
2954
2959
2955
2960
void Test_Lex::check_tokens (
@@ -3003,31 +3008,29 @@ void Test_Lex::check_tokens_with_errors(
3003
3008
String8_View input, Span<const Diagnostic_Assertion> diag_assertions,
3004
3009
std::initializer_list<Token_Type> expected_token_types,
3005
3010
Source_Location caller) {
3006
- Diag_List_Diag_Reporter errors (&this ->memory_ );
3007
3011
Padded_String code (input);
3008
- std::vector<Token> lexed_tokens = this ->lex_to_eof (&code, errors );
3012
+ Tokens_And_Diags lexed = this ->lex_to_eof_returning_diags (&code);
3009
3013
3010
3014
std::vector<Token_Type> lexed_token_types;
3011
- for (const Token& t : lexed_tokens ) {
3015
+ for (const Token& t : lexed. tokens ) {
3012
3016
lexed_token_types.push_back (t.type );
3013
3017
}
3014
3018
3015
3019
EXPECT_THAT_AT_CALLER (lexed_token_types,
3016
3020
::testing::ElementsAreArray (expected_token_types));
3017
3021
3018
- assert_diagnostics (&code, errors .diags () , diag_assertions, caller);
3022
+ assert_diagnostics (&code, *lexed .diags , diag_assertions, caller);
3019
3023
}
3020
3024
3021
3025
std::vector<Token> Test_Lex::lex_to_eof (Padded_String_View input,
3022
3026
Source_Location caller) {
3023
- Diag_List_Diag_Reporter diags (&this ->memory_ );
3024
- std::vector<Token> tokens = this ->lex_to_eof (input, diags);
3025
- assert_diagnostics (input, diags.diags (), {}, caller);
3026
- return tokens;
3027
+ Tokens_And_Diags lexed = this ->lex_to_eof_returning_diags (input);
3028
+ assert_diagnostics (input, *lexed.diags , {}, caller);
3029
+ return lexed.tokens ;
3027
3030
}
3028
3031
3029
- std::vector<Token> Test_Lex::lex_to_eof (Padded_String_View input,
3030
- Diag_Reporter& errors ) {
3032
+ Tokens_And_Diags Test_Lex::lex_to_eof_returning_diags (
3033
+ Padded_String_View input ) {
3031
3034
Lexer& l = this ->make_lexer (input);
3032
3035
std::vector<Token> tokens;
3033
3036
while (l.peek ().type != Token_Type::end_of_file) {
@@ -3038,11 +3041,10 @@ std::vector<Token> Test_Lex::lex_to_eof(Padded_String_View input,
3038
3041
l.skip ();
3039
3042
}
3040
3043
}
3041
- // TODO(#1154): Remove or deduplicate with quick_lint_js::parse_and_lint.
3042
- l.diags ().for_each ([&](Diag_Type diag_type, void * diag_data) {
3043
- errors.report_impl (diag_type, diag_data);
3044
- });
3045
- return tokens;
3044
+ return Tokens_And_Diags{
3045
+ .tokens = std::move (tokens),
3046
+ .diags = &l.diags (),
3047
+ };
3046
3048
}
3047
3049
3048
3050
std::vector<Token> Test_Lex::lex_to_eof (String8_View input,
0 commit comments