Skip to content

Commit a487137

Browse files
committed
[libSyntax] Store range in token_data in C lib parse actions
We don't actually need the range for layout nodes, so just store it for token nodes. This will also make deferred node handling easier later on, because we don't need to keep track of layout node ranges.
1 parent 107add7 commit a487137

File tree

3 files changed

+9
-13
lines changed

3 files changed

+9
-13
lines changed

Diff for: include/swift-c/SyntaxParser/SwiftSyntaxParser.h

+2-3
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;

Diff for: tools/libSwiftSyntaxParser/libSwiftSyntaxParser.cpp

+1-2
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
}

Diff for: tools/swift-syntax-parser-test/swift-syntax-parser-test.cpp

+6-8
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]);

0 commit comments

Comments
 (0)