1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
#include "swift/AST/Module.h"
#include "swift/Basic/LangOptions.h"
#include "swift/Basic/SourceManager.h"
#include "swift/Parse/Lexer.h"
#include "swift/Parse/Parser.h"
#include "swift/Subsystems.h"
#include "llvm/Support/MemoryBuffer.h"
#include "gtest/gtest.h"
using namespace swift;
using namespace llvm;
// The test fixture.
class TokenizerTest : public ::testing::Test {
public:
LangOptions LangOpts;
SourceManager SM;
unsigned makeBuffer(StringRef Source) {
return SM.addMemBufferCopy(Source);
}
static void replaceNewLines(std::string &S) {
size_t Index = 0;
while (true) {
Index = S.find("\n", Index);
if (Index == std::string::npos) break;
S.erase(Index, 1);
S.insert(Index, "\\n");
Index += 3;
}
}
static std::string tokToString(swift::tok T) {
switch (T) {
#define KEYWORD(X) \
case swift::tok::kw_##X: return "kw_" #X; break;
#define PUNCTUATOR(X, Y) \
case swift::tok::X: return #X; break;
#define POUND(X, Y) \
case swift::tok::pound_##X: return "pound_" #X; break;
#include "swift/AST/TokenKinds.def"
#define OTHER(X) \
case swift::tok::X: return #X; break;
OTHER(unknown)
OTHER(eof)
OTHER(code_complete)
OTHER(identifier)
OTHER(oper_binary_unspaced)
OTHER(oper_binary_spaced)
OTHER(oper_postfix)
OTHER(oper_prefix)
OTHER(dollarident)
OTHER(integer_literal)
OTHER(floating_literal)
OTHER(string_literal)
OTHER(sil_local_name)
OTHER(comment)
default:
return "??? (" + std::to_string(static_cast<int>(tok())) + ")";
break;
}
}
void assertTokens(std::vector<Token> Ts, StringRef Expected) {
std::string Actual;
for (auto C = Ts.begin(), E = Ts.end(); C != E; ++C) {
Actual += tokToString(C->getKind());
Actual += ": ";
std::string Txt(C->getRawText());
replaceNewLines(Txt);
Actual += Txt;
Actual += "\n";
}
EXPECT_EQ(Expected, Actual)
<< "---- Expected: \n" << Expected << "\n"
<< "---- Actual: \n" << Actual << "\n";
}
std::vector<Token> parseAndGetSplitTokens(unsigned BufID) {
swift::ParserUnit PU(SM, SourceFileKind::Main, BufID, LangOpts,
TypeCheckerOptions(), SILOptions(), "unknown");
SmallVector<ASTNode, 8> items;
PU.getParser().parseTopLevelItems(items);
return PU.getParser().getSplitTokens();
}
std::vector<Token> tokenize(unsigned BufID, const std::vector<Token> &SplitTokens = {}) {
return swift::tokenize(LangOpts,
SM,
BufID,
/* Offset = */ 0,
/* EndOffset = */ 0,
/* Diags = */nullptr,
/* KeepComments = */ true,
/* TokenizeInterpolatedString = */ true,
SplitTokens);
}
};
TEST_F(TokenizerTest, ProperlySplitTokens) {
auto BufID = makeBuffer(
"infix operator ⊕ { associativity left precedence 100 }\n"
"func ⊕<T>(t1: T, t2: T) {}\n"
);
// Tokenize w/o fixing split tokens
auto Tokens = tokenize(BufID);
assertTokens(Tokens,
"identifier: infix\n"
"kw_operator: operator\n"
"oper_binary_spaced: ⊕\n"
"l_brace: {\n"
"identifier: associativity\n"
"identifier: left\n"
"identifier: precedence\n"
"integer_literal: 100\n"
"r_brace: }\n"
"kw_func: func\n"
"oper_prefix: ⊕<\n"
"identifier: T\n"
"oper_binary_unspaced: >\n"
"l_paren: (\n"
"identifier: t1\n"
"colon: :\n"
"identifier: T\n"
"comma: ,\n"
"identifier: t2\n"
"colon: :\n"
"identifier: T\n"
"r_paren: )\n"
"l_brace: {\n"
"r_brace: }\n"
);
// Parse the input and get split tokens info
auto SplitTokens = parseAndGetSplitTokens(BufID);
// Tokenize with fixing split tokens
Tokens = tokenize(BufID, SplitTokens);
assertTokens(Tokens,
"identifier: infix\n"
"kw_operator: operator\n"
"oper_binary_spaced: ⊕\n"
"l_brace: {\n"
"identifier: associativity\n"
"identifier: left\n"
"identifier: precedence\n"
"integer_literal: 100\n"
"r_brace: }\n"
"kw_func: func\n"
"oper_binary_spaced: ⊕\n"
"oper_binary_unspaced: <\n"
"identifier: T\n"
"oper_binary_unspaced: >\n"
"l_paren: (\n"
"identifier: t1\n"
"colon: :\n"
"identifier: T\n"
"comma: ,\n"
"identifier: t2\n"
"colon: :\n"
"identifier: T\n"
"r_paren: )\n"
"l_brace: {\n"
"r_brace: }\n"
);
}
|