Skip to content

Commit f4a4c61

Browse files
committed
Refactor AST into a struct containing AST.Node enum
Introduce `AST` as a new root type, with `AST.Node` becoming the type used to represent a particular node in the AST. This will allow us to store top-level state on the `AST` type rather than for each `AST.Node`.
1 parent fbdc959 commit f4a4c61

File tree

2 files changed

+51
-28
lines changed

2 files changed

+51
-28
lines changed

Sources/_MatchingEngine/Regex/AST/AST.swift

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,47 +9,64 @@
99
//
1010
//===----------------------------------------------------------------------===//
1111

12-
/// A regex abstract syntax tree
13-
@frozen
14-
public indirect enum AST:
15-
Hashable/*, _ASTPrintable ASTValue, ASTAction*/
16-
{
17-
/// ... | ... | ...
18-
case alternation(Alternation)
12+
/// A regex abstract syntax tree. This is a top-level type that stores the root
13+
/// node.
14+
public struct AST: Hashable {
15+
public var root: AST.Node
16+
public init(_ root: AST.Node) {
17+
self.root = root
18+
}
19+
}
1920

20-
/// ... ...
21-
case concatenation(Concatenation)
21+
extension AST {
22+
/// Whether this AST tree has nested somewhere inside it a capture.
23+
public var hasCapture: Bool { root.hasCapture }
2224

23-
/// (...)
24-
case group(Group)
25+
/// The capture structure of this AST tree.
26+
public var captureStructure: CaptureStructure { root.captureStructure }
27+
}
2528

26-
/// (?(cond) true-branch | false-branch)
27-
case conditional(Conditional)
29+
extension AST {
30+
/// A node in the regex AST.
31+
@frozen
32+
public indirect enum Node:
33+
Hashable/*, _ASTPrintable ASTValue, ASTAction*/
34+
{
35+
/// ... | ... | ...
36+
case alternation(Alternation)
2837

29-
case quantification(Quantification)
38+
/// ... ...
39+
case concatenation(Concatenation)
3040

31-
/// \Q...\E
32-
case quote(Quote)
41+
/// (...)
42+
case group(Group)
3343

34-
/// Comments, non-semantic whitespace, etc
35-
case trivia(Trivia)
44+
/// (?(cond) true-branch | false-branch)
45+
case conditional(Conditional)
3646

37-
case atom(Atom)
47+
case quantification(Quantification)
3848

39-
case customCharacterClass(CustomCharacterClass)
49+
/// \Q...\E
50+
case quote(Quote)
4051

41-
case absentFunction(AbsentFunction)
52+
/// Comments, non-semantic whitespace, etc
53+
case trivia(Trivia)
4254

43-
case empty(Empty)
55+
case atom(Atom)
4456

45-
// FIXME: Move off the regex literal AST
46-
case groupTransform(
47-
Group, transform: CaptureTransform)
48-
}
57+
case customCharacterClass(CustomCharacterClass)
4958

50-
// TODO: Do we want something that holds the AST and stored global options?
59+
case absentFunction(AbsentFunction)
5160

52-
extension AST {
61+
case empty(Empty)
62+
63+
// FIXME: Move off the regex literal AST
64+
case groupTransform(
65+
Group, transform: CaptureTransform)
66+
}
67+
}
68+
69+
extension AST.Node {
5370
// :-(
5471
//
5572
// Existential-based programming is highly prone to silent

Sources/_MatchingEngine/Regex/Printing/DumpAST.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ extension _ASTPrintable {
5757
}
5858

5959
extension AST: _ASTPrintable {
60+
public var _dumpBase: String {
61+
root._dumpBase
62+
}
63+
}
64+
65+
extension AST.Node: _ASTPrintable {
6066
public var _dumpBase: String {
6167
_associatedValue._dumpBase
6268
}

0 commit comments

Comments
 (0)