@@ -27,13 +27,15 @@ ExtractTokenData(llvm::StringRef input_expr) {
2727 if (lexer.NumLexedTokens () == 0 )
2828 return llvm::createStringError (" No lexed tokens" );
2929
30- lexer.ResetTokenIdx (UINT_MAX );
30+ lexer.ResetTokenIdx (0 );
3131 std::vector<std::pair<Token::Kind, std::string>> data;
3232 do {
33- lexer.Advance ();
3433 Token tok = lexer.GetCurrentToken ();
3534 data.push_back (std::make_pair (tok.GetKind (), tok.GetSpelling ()));
35+ lexer.Advance ();
3636 } while (data.back ().first != Token::eof);
37+ // Don't return the eof token.
38+ data.pop_back ();
3739 return data;
3840}
3941
@@ -42,11 +44,8 @@ TEST(DILLexerTests, SimpleTest) {
4244 llvm::Expected<DILLexer> maybe_lexer = DILLexer::Create (input_expr);
4345 ASSERT_THAT_EXPECTED (maybe_lexer, llvm::Succeeded ());
4446 DILLexer lexer (*maybe_lexer);
45- Token token = Token (Token::unknown, " " , 0 );
46- EXPECT_EQ (token.GetKind (), Token::unknown);
47+ Token token = lexer.GetCurrentToken ();
4748
48- lexer.Advance ();
49- token = lexer.GetCurrentToken ();
5049 EXPECT_EQ (token.GetKind (), Token::identifier);
5150 EXPECT_EQ (token.GetSpelling (), " simple_var" );
5251 lexer.Advance ();
@@ -69,9 +68,7 @@ TEST(DILLexerTests, LookAheadTest) {
6968 llvm::Expected<DILLexer> maybe_lexer = DILLexer::Create (input_expr);
7069 ASSERT_THAT_EXPECTED (maybe_lexer, llvm::Succeeded ());
7170 DILLexer lexer (*maybe_lexer);
72- Token token = Token (Token::unknown, " " , 0 );
73- lexer.Advance ();
74- token = lexer.GetCurrentToken ();
71+ Token token = lexer.GetCurrentToken ();
7572
7673 // Current token is '('; check the next 4 tokens, to make
7774 // sure they are the identifier 'anonymous', the identifier 'namespace'
@@ -110,49 +107,54 @@ TEST(DILLexerTests, LookAheadTest) {
110107TEST (DILLexerTests, MultiTokenLexTest) {
111108 EXPECT_THAT_EXPECTED (
112109 ExtractTokenData (" This string has (several ) ::identifiers" ),
113- llvm::HasValue (
114- testing::ElementsAre (testing::Pair (Token::identifier, " This" ),
115- testing::Pair (Token::identifier, " string" ),
116- testing::Pair (Token::identifier, " has" ),
117- testing::Pair (Token::l_paren, " (" ),
118- testing::Pair (Token::identifier, " several" ),
119- testing::Pair (Token::r_paren, " )" ),
120- testing::Pair (Token::coloncolon, " ::" ),
121- testing::Pair (Token::identifier, " identifiers" ),
122- testing::Pair (Token::eof, " " ))));
110+ llvm::HasValue (testing::ElementsAre (
111+ testing::Pair (Token::identifier, " This" ),
112+ testing::Pair (Token::identifier, " string" ),
113+ testing::Pair (Token::identifier, " has" ),
114+ testing::Pair (Token::l_paren, " (" ),
115+ testing::Pair (Token::identifier, " several" ),
116+ testing::Pair (Token::r_paren, " )" ),
117+ testing::Pair (Token::coloncolon, " ::" ),
118+ testing::Pair (Token::identifier, " identifiers" ))));
123119}
124120
125121TEST (DILLexerTests, IdentifiersTest) {
122+ // These strings should lex into identifier tokens.
126123 std::vector<std::string> valid_identifiers = {
127- " $My_name1" , " $pc" , " abcd" , " _" , " _a" , " _a_" ,
124+ " $My_name1" , " $pc" , " abcd" , " _" , " _a" , " _a_" , " $ " ,
128125 " a_b" , " this" , " self" , " a" , " MyName" , " namespace" };
129- std::vector<std::string> invalid_identifiers = {" 234" , " 2a" , " 2" ,
130- " $" , " 1MyName" , " " };
126+
127+ // The lexer can lex these strings, but they should not be identifiers.
128+ std::vector<std::string> invalid_identifiers = {" " , " ::" , " (" , " )" };
129+
130+ // The lexer is expected to fail attempting to lex these strings (it cannot
131+ // create valid tokens out of them).
132+ std::vector<std::string> invalid_tok_strings = {" 234" , " 2a" , " 2" , " 1MyName" };
131133
132134 // Verify that all of the valid identifiers come out as identifier tokens.
133135 for (auto &str : valid_identifiers) {
134136 SCOPED_TRACE (str);
135137 EXPECT_THAT_EXPECTED (ExtractTokenData (str),
136138 llvm::HasValue (testing::ElementsAre (
137- testing::Pair (Token::identifier, str),
138- testing::Pair (Token::eof, " " ))));
139+ testing::Pair (Token::identifier, str))));
140+ }
141+
142+ // Verify that the lexer fails on invalid token strings.
143+ for (auto &str : invalid_tok_strings) {
144+ SCOPED_TRACE (str);
145+ auto maybe_lexer = DILLexer::Create (str);
146+ EXPECT_THAT_EXPECTED (maybe_lexer, llvm::Failed ());
139147 }
140148
141149 // Verify that none of the invalid identifiers come out as identifier tokens.
142150 for (auto &str : invalid_identifiers) {
143151 SCOPED_TRACE (str);
144152 llvm::Expected<DILLexer> maybe_lexer = DILLexer::Create (str);
145- if (!maybe_lexer) {
146- llvm::consumeError (maybe_lexer.takeError ());
147- // In this case, it's ok for lexing to return an error.
148- } else {
149- DILLexer lexer (*maybe_lexer);
150- Token token = Token (Token::unknown, " " , 0 );
151- // We didn't get an error; make sure we did not get an identifier token.
152- lexer.Advance ();
153- token = lexer.GetCurrentToken ();
154- EXPECT_TRUE (token.IsNot (Token::identifier));
155- EXPECT_TRUE (token.IsOneOf (Token::unknown, Token::eof));
156- }
153+ EXPECT_THAT_EXPECTED (maybe_lexer, llvm::Succeeded ());
154+ DILLexer lexer (*maybe_lexer);
155+ Token token = lexer.GetCurrentToken ();
156+ EXPECT_TRUE (token.IsNot (Token::identifier));
157+ EXPECT_TRUE (token.IsOneOf (Token::eof, Token::coloncolon, Token::l_paren,
158+ Token::r_paren));
157159 }
158160}
0 commit comments