Skip to content

Commit 682f3a7

Browse files
authored
Merge pull request #36249 from ahoppen/pr/range-in-token-data
[libSyntax] Store range in token_data in C lib parse actions
2 parents 2159908 + 1e64023 commit 682f3a7

File tree

5 files changed

+96
-49
lines changed

5 files changed

+96
-49
lines changed

include/swift-c/SyntaxParser/SwiftSyntaxParser.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ typedef struct {
103103
uint16_t leading_trivia_count;
104104
uint16_t trailing_trivia_count;
105105
swiftparse_token_kind_t kind;
106+
/// Represents the range for the node, including trivia.
107+
swiftparse_range_t range;
106108
} swiftparse_token_data_t;
107109

108110
typedef struct {
@@ -115,9 +117,6 @@ typedef struct {
115117
swiftparse_token_data_t token_data;
116118
swiftparse_layout_data_t layout_data;
117119
};
118-
/// Represents the range for the node. For a token node the range includes
119-
/// the trivia associated with it.
120-
swiftparse_range_t range;
121120
/// The syntax kind. A value of '0' means this is a token node.
122121
swiftparse_syntax_kind_t kind;
123122
bool present;

tools/libSwiftSyntaxParser/libSwiftSyntaxParser.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ class CLibParseActions : public SyntaxParseActions {
150150
node.token_data.trailing_trivia_count = trailingTrivia.size();
151151
assert(node.token_data.trailing_trivia_count == trailingTrivia.size() &&
152152
"trailing trivia count value is too large");
153-
makeCRange(node.range, range);
153+
makeCRange(node.token_data.range, range);
154154
node.present = true;
155155
}
156156

@@ -186,7 +186,6 @@ class CLibParseActions : public SyntaxParseActions {
186186
node.layout_data.nodes =
187187
const_cast<const swiftparse_client_node_t *>(elements.data());
188188
node.layout_data.nodes_count = elements.size();
189-
makeCRange(node.range, range);
190189
node.present = true;
191190
return getNodeHandler()(&node);
192191
}

tools/swift-syntax-parser-test/swift-syntax-parser-test.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ NumParses("n", cl::desc("number of invocations"), cl::init(1));
5858
namespace {
5959
struct SPNode {
6060
swiftparse_syntax_kind_t kind;
61-
StringRef nodeText;
6261

6362
Optional<swiftparse_token_kind_t> tokKind;
6463
StringRef leadingTriviaText;
@@ -111,21 +110,20 @@ static swiftparse_client_node_t
111110
makeNode(const swiftparse_syntax_node_t *raw_node, StringRef source) {
112111
SPNode *node = new SPNode();
113112
node->kind = raw_node->kind;
114-
auto range = raw_node->range;
115-
node->nodeText = source.substr(range.offset, range.length);
116113
if (raw_node->kind == 0) {
114+
auto range = raw_node->token_data.range;
115+
auto nodeText = source.substr(range.offset, range.length);
117116
node->tokKind = raw_node->token_data.kind;
118117
size_t leadingTriviaLen =
119118
trivialLen(makeArrayRef(raw_node->token_data.leading_trivia,
120119
raw_node->token_data.leading_trivia_count));
121120
size_t trailingTriviaLen =
122121
trivialLen(makeArrayRef(raw_node->token_data.trailing_trivia,
123122
raw_node->token_data.trailing_trivia_count));
124-
node->leadingTriviaText = node->nodeText.take_front(leadingTriviaLen);
125-
node->tokenText =
126-
node->nodeText.substr(leadingTriviaLen,
127-
range.length-leadingTriviaLen-trailingTriviaLen);
128-
node->trailingTriviaText = node->nodeText.take_back(trailingTriviaLen);
123+
node->leadingTriviaText = nodeText.take_front(leadingTriviaLen);
124+
node->tokenText = nodeText.substr(
125+
leadingTriviaLen, range.length - leadingTriviaLen - trailingTriviaLen);
126+
node->trailingTriviaText = nodeText.take_back(trailingTriviaLen);
129127
} else {
130128
for (unsigned i = 0, e = raw_node->layout_data.nodes_count; i != e; ++i) {
131129
auto subnode = convertClientNode(raw_node->layout_data.nodes[i]);

unittests/SyntaxParser/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ endif()
1010

1111
target_link_libraries(SwiftSyntaxParserTests
1212
PRIVATE
13-
libSwiftSyntaxParser)
13+
libSwiftSyntaxParser
14+
swiftSyntax)
1415

1516
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
1617
set_target_properties(SwiftSyntaxParserTests PROPERTIES

unittests/SyntaxParser/SyntaxParserTests.cpp

Lines changed: 85 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313
#include "swift-c/SyntaxParser/SwiftSyntaxParser.h"
1414
#include "swift/Basic/LLVM.h"
1515
#include "llvm/ADT/StringRef.h"
16+
#include "swift/Syntax/Serialization/SyntaxSerialization.h"
1617
#include <vector>
1718
#include "gtest/gtest.h"
1819

1920
using namespace swift;
21+
using namespace swift::syntax;
22+
using namespace serialization;
2023

2124
static swiftparse_client_node_t
2225
parse(StringRef source, swiftparse_node_handler_t node_handler,
@@ -29,63 +32,110 @@ parse(StringRef source, swiftparse_node_handler_t node_handler,
2932
return top;
3033
}
3134

35+
static bool containsChild(swiftparse_layout_data_t layout_data, void *child) {
36+
for (size_t i = 0; i < layout_data.nodes_count; i++) {
37+
if (layout_data.nodes[i] == child) {
38+
return true;
39+
}
40+
}
41+
return false;
42+
}
43+
3244
TEST(SwiftSyntaxParserTests, IncrementalParsing) {
3345
StringRef source1 =
3446
"func t1() { }\n"
35-
"func t2() { }\n";
47+
"func t2() { }\n"
48+
"func t3() { }\n";
49+
3650
StringRef source2 =
3751
"func t1renamed() { }\n"
38-
"func t2() { }\n";
52+
"func t2() { }\n"
53+
"func t3() { }\n";
54+
55+
swiftparse_syntax_kind_t token = getNumericValue(SyntaxKind::Token);
56+
swiftparse_syntax_kind_t functionDecl = getNumericValue(SyntaxKind::FunctionDecl);
57+
swiftparse_syntax_kind_t codeBlockItem = getNumericValue(SyntaxKind::CodeBlockItem);
58+
swiftparse_syntax_kind_t codeBlockItemList = getNumericValue(SyntaxKind::CodeBlockItemList);
59+
60+
// Set up a bunch of node ids that we can later use.
61+
void *t1Token = &t1Token;
62+
void *t1Func = &t1Func;
63+
void *t1CodeBlockItem = &t1CodeBlockItem;
64+
void *t2Token = &t2Token;
65+
void *t2Func = &t2Func;
66+
void *t2CodeBlockItem = &t2CodeBlockItem;
67+
void *t3Token = &t3Token;
68+
void *t3Func = &t3Func;
69+
void *t3CodeBlockItem = &t3CodeBlockItem;
3970

40-
// FIXME: Use the syntax kind directly instead of the serialization number.
41-
swiftparse_syntax_kind_t codeBlockItemList = 163;
42-
swiftparse_syntax_kind_t codeBlockItem = 92;
71+
// Find the t1/t2/t3 tokens in the source
72+
size_t t1TokenOffset = StringRef(source1).find("t1");
73+
size_t t2TokenOffset = StringRef(source1).find("t2");
74+
size_t t3TokenOffset = StringRef(source1).find("t3");
4375

44-
// Assign id numbers to codeBlockItem nodes and collect the ids that are
45-
// listed as members of a codeBlockItemList node into a vector.
46-
// When we reparse, check that we got the parser to resuse the node id from
47-
// the previous parse.
76+
// The length of the t2/t3 code block items
77+
size_t t2CodeBlockItemLength = 14;
78+
size_t t3CodeBlockItemLength = 14;
79+
80+
// Collect the node ids of the code block items in this list and verify that
81+
// t2 and t3 get reused after the edit from source1 to source2.
82+
__block std::vector<void *> codeBlockItemIds;
4883

49-
__block std::vector<int> nodeids;
50-
__block int idcounter = 0;
51-
size_t t2Offset = StringRef(source1).find("\nfunc t2");
52-
__block int t2NodeId = 0;
53-
__block size_t t2NodeLength = 0;
5484
swiftparse_node_handler_t nodeHandler =
5585
^swiftparse_client_node_t(const swiftparse_syntax_node_t *raw_node) {
56-
if (raw_node->kind == codeBlockItem) {
57-
int nodeid = ++idcounter;
58-
if (raw_node->range.offset == t2Offset) {
59-
t2NodeId = nodeid;
60-
t2NodeLength = raw_node->range.length;
86+
if (raw_node->kind == token) {
87+
if (raw_node->token_data.range.offset == t1TokenOffset) {
88+
return t1Token;
89+
} else if (raw_node->token_data.range.offset == t2TokenOffset) {
90+
return t2Token;
91+
} else if (raw_node->token_data.range.offset == t3TokenOffset) {
92+
return t3Token;
6193
}
62-
return (void*)(intptr_t)nodeid;
63-
}
64-
if (raw_node->kind == codeBlockItemList) {
94+
} else if (raw_node->kind == functionDecl) {
95+
if (containsChild(raw_node->layout_data, t1Token)) {
96+
return t1Func;
97+
} else if (containsChild(raw_node->layout_data, t2Token)) {
98+
return t2Func;
99+
} else if (containsChild(raw_node->layout_data, t3Token)) {
100+
return t3Func;
101+
}
102+
} else if (raw_node->kind == codeBlockItem) {
103+
if (containsChild(raw_node->layout_data, t1Func)) {
104+
return t1CodeBlockItem;
105+
} else if (containsChild(raw_node->layout_data, t2Func)) {
106+
return t2CodeBlockItem;
107+
} else if (containsChild(raw_node->layout_data, t3Func)) {
108+
return t3CodeBlockItem;
109+
}
110+
} else if (raw_node->kind == codeBlockItemList) {
65111
for (unsigned i = 0, e = raw_node->layout_data.nodes_count;
66112
i != e; ++i) {
67-
nodeids.push_back((int)(intptr_t)raw_node->layout_data.nodes[i]);
113+
codeBlockItemIds.push_back(raw_node->layout_data.nodes[i]);
68114
}
69115
}
70116
return nullptr;
71117
};
72-
parse(source1, nodeHandler, nullptr);
73-
EXPECT_EQ(t2NodeId, 2);
74-
ASSERT_NE(t2NodeLength, size_t(0));
75-
EXPECT_EQ(nodeids, (std::vector<int>{1, 2}));
118+
parse(source1, nodeHandler, /*node_lookup=*/nullptr);
119+
ASSERT_NE(t2CodeBlockItemLength, size_t(0));
120+
EXPECT_EQ(codeBlockItemIds, (std::vector<void *>{t1CodeBlockItem, t2CodeBlockItem, t3CodeBlockItem}));
76121

77-
nodeids.clear();
78-
idcounter = 1000;
79-
t2Offset = StringRef(source2).find("\nfunc t2");
122+
codeBlockItemIds.clear();
123+
size_t t2CodeBlockItemOffset = StringRef(source2).find("\nfunc t2");
124+
size_t t3CodeBlockItemOffset = StringRef(source2).find("\nfunc t3");
80125
swiftparse_node_lookup_t nodeLookup =
81126
^swiftparse_lookup_result_t(size_t offset, swiftparse_syntax_kind_t kind) {
82-
if (offset == t2Offset && kind == codeBlockItem) {
83-
return { t2NodeLength, (void*)(intptr_t)t2NodeId };
84-
} else {
85-
return {0, nullptr};
127+
if (kind == codeBlockItem) {
128+
if (offset == t2CodeBlockItemOffset) {
129+
return { t2CodeBlockItemLength, t2CodeBlockItem };
130+
} else if (offset == t3CodeBlockItemOffset) {
131+
return { t3CodeBlockItemLength, t3CodeBlockItem };
132+
}
86133
}
134+
return {0, nullptr};
87135
};
88136

89137
parse(source2, nodeHandler, nodeLookup);
90-
EXPECT_EQ(nodeids, (std::vector<int>{1001, 2}));
138+
// Assert that t2 and t3 get reused.
139+
EXPECT_EQ(codeBlockItemIds[1], t2CodeBlockItem);
140+
EXPECT_EQ(codeBlockItemIds[2], t3CodeBlockItem);
91141
}

0 commit comments

Comments
 (0)