Skip to content

Commit 89e18aa

Browse files
authored
Cleanup DSLTree a little (#206)
Rename Alternation to OrderedChoice, scrap groupTransform and capturing from group in favor of a capture node.
1 parent d65fa08 commit 89e18aa

File tree

10 files changed

+945
-986
lines changed

10 files changed

+945
-986
lines changed

Sources/VariadicsGenerator/VariadicsGenerator.swift

+67-71
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ struct VariadicsGenerator: ParsableCommand {
522522
output("""
523523
extension \(altBuilderName) {
524524
public static func buildPartialBlock<\(genericParams)>(first regex: R) -> ChoiceOf<(W, \(resultCaptures))> \(whereClause) {
525-
.init(node: .alternation([regex.regex.root]))
525+
.init(node: .orderedChoice([regex.regex.root]))
526526
}
527527
}
528528
@@ -554,27 +554,26 @@ struct VariadicsGenerator: ParsableCommand {
554554
public init<\(genericParams)>(
555555
_ component: R
556556
) \(whereClauseRaw) {
557-
self.init(node: .group(.capture, component.regex.root))
557+
self.init(node: .capture(component.regex.root))
558558
}
559559
560560
\(disfavored)\
561561
public init<\(genericParams)>(
562562
_ component: R, as reference: Reference<W>
563563
) \(whereClauseRaw) {
564-
self.init(node: .group(.capture, component.regex.root, reference.id))
564+
self.init(node: .capture(reference: reference.id, component.regex.root))
565565
}
566566
567567
\(disfavored)\
568568
public init<\(genericParams), NewCapture>(
569569
_ component: R,
570570
transform: @escaping (Substring) -> NewCapture
571571
) \(whereClauseTransformed) {
572-
self.init(node: .groupTransform(
573-
.capture,
574-
component.regex.root,
572+
self.init(node: .capture(.transform(
575573
CaptureTransform(resultType: NewCapture.self) {
576574
transform($0) as Any
577-
}))
575+
},
576+
component.regex.root)))
578577
}
579578
580579
\(disfavored)\
@@ -583,13 +582,13 @@ struct VariadicsGenerator: ParsableCommand {
583582
as reference: Reference<NewCapture>,
584583
transform: @escaping (Substring) -> NewCapture
585584
) \(whereClauseTransformed) {
586-
self.init(node: .groupTransform(
587-
.capture,
588-
component.regex.root,
589-
CaptureTransform(resultType: NewCapture.self) {
590-
transform($0) as Any
591-
},
592-
reference.id))
585+
self.init(node: .capture(
586+
reference: reference.id,
587+
.transform(
588+
CaptureTransform(resultType: NewCapture.self) {
589+
transform($0) as Any
590+
},
591+
component.regex.root)))
593592
}
594593
}
595594
@@ -599,12 +598,11 @@ struct VariadicsGenerator: ParsableCommand {
599598
_ component: R,
600599
transform: @escaping (Substring) throws -> NewCapture
601600
) \(whereClauseTransformed) {
602-
self.init(node: .groupTransform(
603-
.capture,
604-
component.regex.root,
601+
self.init(node: .capture(.transform(
605602
CaptureTransform(resultType: NewCapture.self) {
606603
try transform($0) as Any
607-
}))
604+
},
605+
component.regex.root)))
608606
}
609607
610608
\(disfavored)\
@@ -613,26 +611,25 @@ struct VariadicsGenerator: ParsableCommand {
613611
as reference: Reference<NewCapture>,
614612
transform: @escaping (Substring) throws -> NewCapture
615613
) \(whereClauseTransformed) {
616-
self.init(node: .groupTransform(
617-
.capture,
618-
component.regex.root,
619-
CaptureTransform(resultType: NewCapture.self) {
620-
try transform($0) as Any
621-
},
622-
reference.id))
614+
self.init(node: .capture(
615+
reference: reference.id,
616+
.transform(
617+
CaptureTransform(resultType: NewCapture.self) {
618+
try transform($0) as Any
619+
},
620+
component.regex.root)))
623621
}
624622
625623
\(disfavored)\
626624
public init<\(genericParams), NewCapture>(
627625
_ component: R,
628626
transform: @escaping (Substring) -> NewCapture?
629627
) \(whereClauseTransformed) {
630-
self.init(node: .groupTransform(
631-
.capture,
632-
component.regex.root,
628+
self.init(node: .capture(.transform(
633629
CaptureTransform(resultType: NewCapture.self) {
634630
transform($0) as Any?
635-
}))
631+
},
632+
component.regex.root)))
636633
}
637634
638635
\(disfavored)\
@@ -641,13 +638,13 @@ struct VariadicsGenerator: ParsableCommand {
641638
as reference: Reference<NewCapture>,
642639
transform: @escaping (Substring) -> NewCapture?
643640
) \(whereClauseTransformed) {
644-
self.init(node: .groupTransform(
645-
.capture,
646-
component.regex.root,
647-
CaptureTransform(resultType: NewCapture.self) {
648-
transform($0) as Any?
649-
},
650-
reference.id))
641+
self.init(node: .capture(
642+
reference: reference.id,
643+
.transform(
644+
CaptureTransform(resultType: NewCapture.self) {
645+
transform($0) as Any?
646+
},
647+
component.regex.root)))
651648
}
652649
}
653650
@@ -658,28 +655,29 @@ struct VariadicsGenerator: ParsableCommand {
658655
public init<\(genericParams)>(
659656
@\(concatBuilderName) _ component: () -> R
660657
) \(whereClauseRaw) {
661-
self.init(node: .group(.capture, component().regex.root))
658+
self.init(node: .capture(component().regex.root))
662659
}
663660
664661
\(disfavored)\
665662
public init<\(genericParams)>(
666663
as reference: Reference<W>,
667664
@\(concatBuilderName) _ component: () -> R
668665
) \(whereClauseRaw) {
669-
self.init(node: .group(.capture, component().regex.root, reference.id))
666+
self.init(node: .capture(
667+
reference: reference.id,
668+
component().regex.root))
670669
}
671670
672671
\(disfavored)\
673672
public init<\(genericParams), NewCapture>(
674673
@\(concatBuilderName) _ component: () -> R,
675674
transform: @escaping (Substring) -> NewCapture
676675
) \(whereClauseTransformed) {
677-
self.init(node: .groupTransform(
678-
.capture,
679-
component().regex.root,
676+
self.init(node: .capture(.transform(
680677
CaptureTransform(resultType: NewCapture.self) {
681678
transform($0) as Any
682-
}))
679+
},
680+
component().regex.root)))
683681
}
684682
685683
\(disfavored)\
@@ -688,13 +686,13 @@ struct VariadicsGenerator: ParsableCommand {
688686
@\(concatBuilderName) _ component: () -> R,
689687
transform: @escaping (Substring) -> NewCapture
690688
) \(whereClauseTransformed) {
691-
self.init(node: .groupTransform(
692-
.capture,
693-
component().regex.root,
694-
CaptureTransform(resultType: NewCapture.self) {
695-
transform($0) as Any
696-
},
697-
reference.id))
689+
self.init(node: .capture(
690+
reference: reference.id,
691+
.transform(
692+
CaptureTransform(resultType: NewCapture.self) {
693+
transform($0) as Any
694+
},
695+
component().regex.root)))
698696
}
699697
}
700698
@@ -704,12 +702,11 @@ struct VariadicsGenerator: ParsableCommand {
704702
@\(concatBuilderName) _ component: () -> R,
705703
transform: @escaping (Substring) throws -> NewCapture
706704
) \(whereClauseTransformed) {
707-
self.init(node: .groupTransform(
708-
.capture,
709-
component().regex.root,
705+
self.init(node: .capture(.transform(
710706
CaptureTransform(resultType: NewCapture.self) {
711707
try transform($0) as Any
712-
}))
708+
},
709+
component().regex.root)))
713710
}
714711
715712
\(disfavored)\
@@ -718,26 +715,25 @@ struct VariadicsGenerator: ParsableCommand {
718715
@\(concatBuilderName) _ component: () -> R,
719716
transform: @escaping (Substring) throws -> NewCapture
720717
) \(whereClauseTransformed) {
721-
self.init(node: .groupTransform(
722-
.capture,
723-
component().regex.root,
724-
CaptureTransform(resultType: NewCapture.self) {
725-
try transform($0) as Any
726-
},
727-
reference.id))
718+
self.init(node: .capture(
719+
reference: reference.id,
720+
.transform(
721+
CaptureTransform(resultType: NewCapture.self) {
722+
try transform($0) as Any
723+
},
724+
component().regex.root)))
728725
}
729726
730727
\(disfavored)\
731728
public init<\(genericParams), NewCapture>(
732729
@\(concatBuilderName) _ component: () -> R,
733730
transform: @escaping (Substring) -> NewCapture?
734731
) \(whereClauseTransformed) {
735-
self.init(node: .groupTransform(
736-
.capture,
737-
component().regex.root,
732+
self.init(node: .capture(.transform(
738733
CaptureTransform(resultType: NewCapture.self) {
739734
transform($0) as Any?
740-
}))
735+
},
736+
component().regex.root)))
741737
}
742738
743739
\(disfavored)\
@@ -746,13 +742,13 @@ struct VariadicsGenerator: ParsableCommand {
746742
@\(concatBuilderName) _ component: () -> R,
747743
transform: @escaping (Substring) -> NewCapture?
748744
) \(whereClauseTransformed) {
749-
self.init(node: .groupTransform(
750-
.capture,
751-
component().regex.root,
752-
CaptureTransform(resultType: NewCapture.self) {
753-
transform($0) as Any?
754-
},
755-
reference.id))
745+
self.init(node: .capture(
746+
reference: reference.id,
747+
.transform(
748+
CaptureTransform(resultType: NewCapture.self) {
749+
transform($0) as Any?
750+
},
751+
component().regex.root)))
756752
}
757753
}
758754

Sources/_MatchingEngine/Regex/Parse/CaptureStructure.swift

+11-36
Original file line numberDiff line numberDiff line change
@@ -59,54 +59,29 @@ extension CaptureStructure.Constructor {
5959
}
6060

6161
public mutating func grouping<T: _TreeNode>(
62-
_ child: T, as kind: AST.Group.Kind
62+
_ child: T,
63+
as kind: AST.Group.Kind
6364
) -> CaptureStructure {
64-
let innerCaptures = child._captureStructure(&self)
6565
switch kind {
6666
case .capture:
67-
return .atom() + innerCaptures
67+
return capturing(child)
6868
case .namedCapture(let name):
69-
return .atom(name: name.value) + innerCaptures
69+
return capturing(name: name.value, child)
7070
case .balancedCapture(let b):
71-
return .atom(name: b.name?.value) + innerCaptures
71+
return capturing(name: b.name?.value, child)
7272
default:
7373
precondition(!kind.isCapturing)
74-
return innerCaptures
75-
}
76-
}
77-
78-
public mutating func grouping<T: _TreeNode>(
79-
_ child: T,
80-
as kind: AST.Group.Kind,
81-
withTransform transform: CaptureTransform
82-
) -> CaptureStructure {
83-
let innerCaptures = child._captureStructure(&self)
84-
switch kind {
85-
case .capture:
86-
return .atom(type: AnyType(transform.resultType)) + innerCaptures
87-
case .namedCapture(let name):
88-
return .atom(name: name.value, type: AnyType(transform.resultType))
89-
+ innerCaptures
90-
default:
91-
return innerCaptures
74+
return child._captureStructure(&self)
9275
}
9376
}
9477

95-
public mutating func grouping<T: _TreeNode>(
78+
public mutating func capturing<T: _TreeNode>(
79+
name: String? = nil,
9680
_ child: T,
97-
as kind: AST.Group.Kind,
98-
withType type: AnyType
81+
withType type: AnyType? = nil
9982
) -> CaptureStructure {
100-
let innerCaptures = child._captureStructure(&self)
101-
switch kind {
102-
case .capture:
103-
return .atom(type: type) + innerCaptures
104-
case .namedCapture(let name):
105-
return .atom(name: name.value, type: type)
106-
+ innerCaptures
107-
default:
108-
return innerCaptures
109-
}
83+
.atom(name: name, type: type)
84+
+ child._captureStructure(&self)
11085
}
11186

11287
// TODO: We'll likely want/need a generalization of

0 commit comments

Comments
 (0)