Skip to content

Commit c4f2f47

Browse files
committed
WIP: Get the first potential token of a node recursively
1 parent 559c79a commit c4f2f47

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

CodeGeneration/Sources/SyntaxSupport/Node.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,55 @@ public class Node {
8484
return CollectionNode(node: self)
8585
}
8686
}
87+
88+
/// Get the first potential token of a node recursively
89+
/// This information is used by incremental parse
90+
public var firstPotentialToken: [TokenChoice] {
91+
if let children = self.layoutNode?.children,
92+
let firstChild = children.first(where: {!$0.isUnexpectedNodes}) {
93+
94+
switch firstChild.kind {
95+
case .node(kind: let nodeKind):
96+
guard let nodeSpec = SYNTAX_NODES.first(where: {$0.kind == nodeKind}) else {
97+
return []
98+
}
99+
return nodeSpec.firstPotentialToken
100+
101+
case .nodeChoices(choices: let children):
102+
return children.compactMap { child in
103+
if let nodeSpec = SYNTAX_NODES.first(where: {$0.kind == child.syntaxNodeKind}) {
104+
return nodeSpec.firstPotentialToken
105+
}
106+
return nil
107+
}.flatMap {$0}
108+
case .collection(kind: let kind, _):
109+
guard let nodeSpec = SYNTAX_NODES.first(where: {$0.kind == kind}),
110+
let collection = nodeSpec.collectionNode else {
111+
return []
112+
}
113+
return collection.elementChoices.compactMap { choiceKind in
114+
if let nodeSpec = SYNTAX_NODES.first(where: {$0.kind == choiceKind}) {
115+
return nodeSpec.firstPotentialToken
116+
}
117+
return nil
118+
}.flatMap {$0}
119+
120+
case .token(choices: let choices, _, _):
121+
return choices
122+
}
123+
}
124+
125+
if let collection = self.collectionNode {
126+
return collection.elementChoices.compactMap { choiceKind in
127+
if let nodeSpec = SYNTAX_NODES.first(where: {$0.kind == choiceKind}) {
128+
return nodeSpec.firstPotentialToken
129+
}
130+
return nil
131+
}.flatMap {$0}
132+
}
133+
134+
return []
135+
}
87136

88137
/// Construct the specification for a layout syntax node.
89138
init(

0 commit comments

Comments
 (0)