|
9 | 9 | //
|
10 | 10 | //===----------------------------------------------------------------------===//
|
11 | 11 |
|
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 | +} |
19 | 20 |
|
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 } |
22 | 24 |
|
23 |
| - /// (...) |
24 |
| - case group(Group) |
| 25 | + /// The capture structure of this AST tree. |
| 26 | + public var captureStructure: CaptureStructure { root.captureStructure } |
| 27 | +} |
25 | 28 |
|
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) |
28 | 37 |
|
29 |
| - case quantification(Quantification) |
| 38 | + /// ... ... |
| 39 | + case concatenation(Concatenation) |
30 | 40 |
|
31 |
| - /// \Q...\E |
32 |
| - case quote(Quote) |
| 41 | + /// (...) |
| 42 | + case group(Group) |
33 | 43 |
|
34 |
| - /// Comments, non-semantic whitespace, etc |
35 |
| - case trivia(Trivia) |
| 44 | + /// (?(cond) true-branch | false-branch) |
| 45 | + case conditional(Conditional) |
36 | 46 |
|
37 |
| - case atom(Atom) |
| 47 | + case quantification(Quantification) |
38 | 48 |
|
39 |
| - case customCharacterClass(CustomCharacterClass) |
| 49 | + /// \Q...\E |
| 50 | + case quote(Quote) |
40 | 51 |
|
41 |
| - case absentFunction(AbsentFunction) |
| 52 | + /// Comments, non-semantic whitespace, etc |
| 53 | + case trivia(Trivia) |
42 | 54 |
|
43 |
| - case empty(Empty) |
| 55 | + case atom(Atom) |
44 | 56 |
|
45 |
| - // FIXME: Move off the regex literal AST |
46 |
| - case groupTransform( |
47 |
| - Group, transform: CaptureTransform) |
48 |
| -} |
| 57 | + case customCharacterClass(CustomCharacterClass) |
49 | 58 |
|
50 |
| -// TODO: Do we want something that holds the AST and stored global options? |
| 59 | + case absentFunction(AbsentFunction) |
51 | 60 |
|
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 { |
53 | 70 | // :-(
|
54 | 71 | //
|
55 | 72 | // Existential-based programming is highly prone to silent
|
|
0 commit comments