-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathDSLTree.swift
793 lines (675 loc) · 21.8 KB
/
DSLTree.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2021-2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
@_implementationOnly import _RegexParser
@_spi(RegexBuilder)
public struct DSLTree {
var root: Node
init(_ r: Node) {
self.root = r
}
}
extension DSLTree {
@_spi(RegexBuilder)
public indirect enum Node {
/// Matches each node in order.
///
/// ... | ... | ...
case orderedChoice([Node])
/// Match each node in sequence.
///
/// ... ...
case concatenation([Node])
/// Captures the result of a subpattern.
///
/// (...), (?<name>...)
case capture(
name: String? = nil, reference: ReferenceID? = nil, Node,
CaptureTransform? = nil)
/// Matches a noncapturing subpattern.
case nonCapturingGroup(_AST.GroupKind, Node)
// TODO: Consider splitting off grouped conditions, or have
// our own kind
/// Matches a choice of two nodes, based on a condition.
///
/// (?(cond) true-branch | false-branch)
///
case conditional(
_AST.ConditionKind, Node, Node)
case quantification(
_AST.QuantificationAmount,
QuantificationKind,
Node)
case customCharacterClass(CustomCharacterClass)
case atom(Atom)
/// Comments, non-semantic whitespace, and so on.
// TODO: Do we want this? Could be interesting
case trivia(String)
// TODO: Probably some atoms, built-ins, etc.
case empty
case quotedLiteral(String)
/// An embedded literal.
case regexLiteral(_AST.ASTNode)
// TODO: What should we do here?
///
/// TODO: Consider splitting off expression functions, or have our own kind
case absentFunction(_AST.AbsentFunction)
// MARK: - Tree conversions
/// The target of AST conversion.
///
/// Keeps original AST around for rich syntactic and source information
case convertedRegexLiteral(Node, _AST.ASTNode)
// MARK: - Extensibility points
case consumer(_ConsumerInterface)
case matcher(Any.Type, _MatcherInterface)
// MARK: - Type erasure
case typeErase(Node)
// TODO: Would this just boil down to a consumer?
case characterPredicate(_CharacterPredicateInterface)
}
}
extension DSLTree {
@_spi(RegexBuilder)
public enum QuantificationKind {
/// The default quantification kind, as set by options.
case `default`
/// An explicitly chosen kind, overriding any options.
case explicit(_AST.QuantificationKind)
/// A kind set via syntax, which can be affected by options.
case syntax(_AST.QuantificationKind)
var ast: AST.Quantification.Kind? {
switch self {
case .default: return nil
case .explicit(let kind), .syntax(let kind):
return kind.ast
}
}
}
@_spi(RegexBuilder)
public struct CustomCharacterClass {
var members: [Member]
var isInverted: Bool
var containsAny: Bool {
members.contains { member in
switch member {
case .atom(.any): return true
case .custom(let ccc): return ccc.containsAny
default:
return false
}
}
}
public init(members: [DSLTree.CustomCharacterClass.Member], isInverted: Bool = false) {
self.members = members
self.isInverted = isInverted
}
public static func generalCategory(_ category: Unicode.GeneralCategory) -> Self {
let property = AST.Atom.CharacterProperty(.generalCategory(category.extendedGeneralCategory!), isInverted: false, isPOSIX: false)
let astAtom = AST.Atom(.property(property), .fake)
return .init(members: [.atom(.unconverted(.init(ast: astAtom)))])
}
public var inverted: CustomCharacterClass {
var result = self
result.isInverted.toggle()
return result
}
@_spi(RegexBuilder)
public enum Member {
case atom(Atom)
case range(Atom, Atom)
case custom(CustomCharacterClass)
case quotedLiteral(String)
case trivia(String)
indirect case intersection(CustomCharacterClass, CustomCharacterClass)
indirect case subtraction(CustomCharacterClass, CustomCharacterClass)
indirect case symmetricDifference(CustomCharacterClass, CustomCharacterClass)
}
}
@_spi(RegexBuilder)
public enum Atom {
case char(Character)
case scalar(Unicode.Scalar)
case any
case assertion(_AST.AssertionKind)
case backreference(_AST.Reference)
case symbolicReference(ReferenceID)
case changeMatchingOptions(_AST.MatchingOptionSequence)
case unconverted(_AST.Atom)
}
}
extension Unicode.GeneralCategory {
var extendedGeneralCategory: Unicode.ExtendedGeneralCategory? {
switch self {
case .uppercaseLetter: return .uppercaseLetter
case .lowercaseLetter: return .lowercaseLetter
case .titlecaseLetter: return .titlecaseLetter
case .modifierLetter: return .modifierLetter
case .otherLetter: return .otherLetter
case .nonspacingMark: return .nonspacingMark
case .spacingMark: return .spacingMark
case .enclosingMark: return .enclosingMark
case .decimalNumber: return .decimalNumber
case .letterNumber: return .letterNumber
case .otherNumber: return .otherNumber
case .connectorPunctuation: return .connectorPunctuation
case .dashPunctuation: return .dashPunctuation
case .openPunctuation: return .openPunctuation
case .closePunctuation: return .closePunctuation
case .initialPunctuation: return .initialPunctuation
case .finalPunctuation: return .finalPunctuation
case .otherPunctuation: return .otherPunctuation
case .mathSymbol: return .mathSymbol
case .currencySymbol: return .currencySymbol
case .modifierSymbol: return .modifierSymbol
case .otherSymbol: return .otherSymbol
case .spaceSeparator: return .spaceSeparator
case .lineSeparator: return .lineSeparator
case .paragraphSeparator: return .paragraphSeparator
case .control: return .control
case .format: return .format
case .surrogate: return .surrogate
case .privateUse: return .privateUse
case .unassigned: return .unassigned
@unknown default: return nil
}
}
}
// CollectionConsumer
@_spi(RegexBuilder)
public typealias _ConsumerInterface = (
String, Range<String.Index>
) throws -> String.Index?
// Type producing consume
// TODO: better name
@_spi(RegexBuilder)
public typealias _MatcherInterface = (
String, String.Index, Range<String.Index>
) throws -> (String.Index, Any)?
// Character-set (post grapheme segmentation)
@_spi(RegexBuilder)
public typealias _CharacterPredicateInterface = (
(Character) -> Bool
)
/*
TODO: Use of syntactic types, like group kinds, is a
little suspect. We may want to figure out a model here.
TODO: Do capturing groups need explicit numbers?
TODO: Are storing closures better/worse than existentials?
*/
extension DSLTree.Node {
@_spi(RegexBuilder)
public var children: [DSLTree.Node] {
switch self {
case let .orderedChoice(v): return v
case let .concatenation(v): return v
case let .convertedRegexLiteral(n, _):
// Treat this transparently
return n.children
case let .capture(_, _, n, _): return [n]
case let .nonCapturingGroup(_, n): return [n]
case let .quantification(_, _, n): return [n]
case let .typeErase(n): return [n]
case let .conditional(_, t, f): return [t,f]
case .trivia, .empty, .quotedLiteral, .regexLiteral,
.consumer, .matcher, .characterPredicate,
.customCharacterClass, .atom:
return []
case let .absentFunction(abs):
return abs.ast.children.map(\.dslTreeNode)
}
}
}
extension DSLTree.Node {
var astNode: AST.Node? {
switch self {
case let .regexLiteral(literal): return literal.ast
case let .convertedRegexLiteral(_, literal): return literal.ast
default: return nil
}
}
}
extension DSLTree.Atom {
// Return the Character or promote a scalar to a Character
var literalCharacterValue: Character? {
switch self {
case let .char(c): return c
case let .scalar(s): return Character(s)
default: return nil
}
}
}
extension DSLTree {
struct Options {
// TBD
}
}
extension DSLTree {
var ast: AST? {
guard let root = root.astNode else {
return nil
}
// TODO: Options mapping
return AST(root, globalOptions: nil)
}
}
extension DSLTree {
var hasCapture: Bool {
root.hasCapture
}
}
extension DSLTree.Node {
var hasCapture: Bool {
switch self {
case .capture:
return true
case let .regexLiteral(re):
return re.ast.hasCapture
case let .convertedRegexLiteral(n, re):
assert(n.hasCapture == re.ast.hasCapture)
return n.hasCapture
default:
return self.children.any(\.hasCapture)
}
}
}
extension DSLTree.Node {
@_spi(RegexBuilder)
public func appending(_ newNode: DSLTree.Node) -> DSLTree.Node {
if case .concatenation(let components) = self {
return .concatenation(components + [newNode])
}
return .concatenation([self, newNode])
}
@_spi(RegexBuilder)
public func appendingAlternationCase(
_ newNode: DSLTree.Node
) -> DSLTree.Node {
if case .orderedChoice(let components) = self {
return .orderedChoice(components + [newNode])
}
return .orderedChoice([self, newNode])
}
}
@_spi(RegexBuilder)
public struct ReferenceID: Hashable {
private static var counter: Int = 0
var base: Int
public init() {
base = Self.counter
Self.counter += 1
}
}
@_spi(RegexBuilder)
public struct CaptureTransform: Hashable, CustomStringConvertible {
enum Closure {
/// A failable transform.
case failable((Any) throws -> Any?)
/// Specialized case of `failable` for performance.
case substringFailable((Substring) throws -> Any?)
/// A non-failable transform.
case nonfailable((Any) throws -> Any)
/// Specialized case of `failable` for performance.
case substringNonfailable((Substring) throws -> Any?)
}
let argumentType: Any.Type
let resultType: Any.Type
let closure: Closure
init(argumentType: Any.Type, resultType: Any.Type, closure: Closure) {
self.argumentType = argumentType
self.resultType = resultType
self.closure = closure
}
public init<Argument, Result>(
_ userSpecifiedTransform: @escaping (Argument) throws -> Result
) {
let closure: Closure
if let substringTransform = userSpecifiedTransform
as? (Substring) throws -> Result {
closure = .substringNonfailable(substringTransform)
} else {
closure = .nonfailable {
try userSpecifiedTransform($0 as! Argument) as Any
}
}
self.init(
argumentType: Argument.self,
resultType: Result.self,
closure: closure)
}
public init<Argument, Result>(
_ userSpecifiedTransform: @escaping (Argument) throws -> Result?
) {
let closure: Closure
if let substringTransform = userSpecifiedTransform
as? (Substring) throws -> Result? {
closure = .substringFailable(substringTransform)
} else {
closure = .failable {
try userSpecifiedTransform($0 as! Argument) as Any?
}
}
self.init(
argumentType: Argument.self,
resultType: Result.self,
closure: closure)
}
func callAsFunction(_ input: Any) throws -> Any? {
switch closure {
case .nonfailable(let transform):
let result = try transform(input)
assert(type(of: result) == resultType)
return result
case .substringNonfailable(let transform):
let result = try transform(input as! Substring)
assert(type(of: result) == resultType)
return result
case .failable(let transform):
guard let result = try transform(input) else {
return nil
}
assert(type(of: result) == resultType)
return result
case .substringFailable(let transform):
guard let result = try transform(input as! Substring) else {
return nil
}
assert(type(of: result) == resultType)
return result
}
}
func callAsFunction(_ input: Substring) throws -> Any? {
switch closure {
case .substringFailable(let transform):
return try transform(input)
case .substringNonfailable(let transform):
return try transform(input)
case .failable(let transform):
return try transform(input)
case .nonfailable(let transform):
return try transform(input)
}
}
public static func == (lhs: CaptureTransform, rhs: CaptureTransform) -> Bool {
unsafeBitCast(lhs.closure, to: (Int, Int).self) ==
unsafeBitCast(rhs.closure, to: (Int, Int).self)
}
public func hash(into hasher: inout Hasher) {
let (fn, ctx) = unsafeBitCast(closure, to: (Int, Int).self)
hasher.combine(fn)
hasher.combine(ctx)
}
public var description: String {
"<transform argument_type=\(argumentType) result_type=\(resultType)>"
}
}
// MARK: AST wrapper types
//
// These wrapper types are required because even @_spi-marked public APIs can't
// include symbols from implementation-only dependencies.
@available(SwiftStdlib 5.7, *)
extension DSLTree.Node {
func _addCaptures(
to list: inout CaptureList,
optionalNesting nesting: Int
) {
let addOptional = nesting+1
switch self {
case let .orderedChoice(children):
for child in children {
child._addCaptures(to: &list, optionalNesting: addOptional)
}
case let .concatenation(children):
for child in children {
child._addCaptures(to: &list, optionalNesting: nesting)
}
case let .capture(name, _, child, transform):
list.append(.init(
name: name,
type: transform?.resultType ?? child.wholeMatchType,
optionalDepth: nesting, .fake))
child._addCaptures(to: &list, optionalNesting: nesting)
case let .nonCapturingGroup(kind, child):
assert(!kind.ast.isCapturing)
child._addCaptures(to: &list, optionalNesting: nesting)
case let .conditional(cond, trueBranch, falseBranch):
switch cond.ast {
case .group(let g):
AST.Node.group(g)._addCaptures(to: &list, optionalNesting: nesting)
default:
break
}
trueBranch._addCaptures(to: &list, optionalNesting: addOptional)
falseBranch._addCaptures(to: &list, optionalNesting: addOptional)
case let .quantification(amount, _, child):
var optNesting = nesting
if amount.ast.bounds.atLeast == 0 {
optNesting += 1
}
child._addCaptures(to: &list, optionalNesting: optNesting)
case let .regexLiteral(re):
return re.ast._addCaptures(to: &list, optionalNesting: nesting)
case let .absentFunction(abs):
switch abs.ast.kind {
case .expression(_, _, let child):
child._addCaptures(to: &list, optionalNesting: nesting)
case .clearer, .repeater, .stopper:
break
}
case let .convertedRegexLiteral(n, _):
return n._addCaptures(to: &list, optionalNesting: nesting)
case .matcher:
break
case .customCharacterClass, .atom, .trivia, .empty,
.quotedLiteral, .consumer, .characterPredicate, .typeErase:
break
}
}
/// Returns true if the node is output-forwarding, i.e. not defining its own
/// output but forwarding its only child's output.
var isOutputForwarding: Bool {
switch self {
case .nonCapturingGroup:
return true
case .orderedChoice, .concatenation, .capture,
.conditional, .quantification, .customCharacterClass, .atom,
.trivia, .empty, .quotedLiteral, .regexLiteral, .absentFunction,
.convertedRegexLiteral, .consumer,
.characterPredicate, .matcher, .typeErase:
return false
}
}
/// Returns the output-defining node, peering through any output-forwarding
/// nodes.
var outputDefiningNode: Self {
if isOutputForwarding {
assert(children.count == 1)
return children[0].outputDefiningNode
}
return self
}
/// Returns the type of the whole match, i.e. `.0` element type of the output.
var wholeMatchType: Any.Type {
switch outputDefiningNode {
case .matcher(let type, _):
return type
case .typeErase:
return AnyRegexOutput.self
default:
return Substring.self
}
}
}
extension DSLTree {
@available(SwiftStdlib 5.7, *)
var captureList: CaptureList {
var list = CaptureList()
// FIXME: This is peering through any top-level `.typeErase`. Once type
// erasure was handled in the engine, this can be simplified to using `root`
// directly.
var root = root
while case let .typeErase(child) = root {
root = child
}
list.append(.init(type: root.wholeMatchType, optionalDepth: 0, .fake))
root._addCaptures(to: &list, optionalNesting: 0)
return list
}
/// Presents a wrapped version of `DSLTree.Node` that can provide an internal
/// `_TreeNode` conformance.
struct _Tree: _TreeNode {
var node: DSLTree.Node
init(_ node: DSLTree.Node) {
self.node = node
}
var children: [_Tree]? {
switch node {
case let .orderedChoice(v): return v.map(_Tree.init)
case let .concatenation(v): return v.map(_Tree.init)
case let .convertedRegexLiteral(n, _):
// Treat this transparently
return _Tree(n).children
case let .capture(_, _, n, _): return [_Tree(n)]
case let .nonCapturingGroup(_, n): return [_Tree(n)]
case let .quantification(_, _, n): return [_Tree(n)]
case let .typeErase(n): return [_Tree(n)]
case let .conditional(_, t, f): return [_Tree(t), _Tree(f)]
case .trivia, .empty, .quotedLiteral, .regexLiteral,
.consumer, .matcher, .characterPredicate,
.customCharacterClass, .atom:
return []
case let .absentFunction(abs):
return abs.ast.children.map(\.dslTreeNode).map(_Tree.init)
}
}
}
@_spi(RegexBuilder)
public enum _AST {
@_spi(RegexBuilder)
public struct GroupKind {
internal var ast: AST.Group.Kind
public static var atomicNonCapturing: Self {
.init(ast: .atomicNonCapturing)
}
public static var lookahead: Self {
.init(ast: .lookahead)
}
public static var negativeLookahead: Self {
.init(ast: .negativeLookahead)
}
}
@_spi(RegexBuilder)
public struct ConditionKind {
internal var ast: AST.Conditional.Condition.Kind
}
@_spi(RegexBuilder)
public struct QuantificationKind {
internal var ast: AST.Quantification.Kind
public static var eager: Self {
.init(ast: .eager)
}
public static var reluctant: Self {
.init(ast: .reluctant)
}
public static var possessive: Self {
.init(ast: .possessive)
}
}
@_spi(RegexBuilder)
public struct QuantificationAmount {
internal var ast: AST.Quantification.Amount
public static var zeroOrMore: Self {
.init(ast: .zeroOrMore)
}
public static var oneOrMore: Self {
.init(ast: .oneOrMore)
}
public static var zeroOrOne: Self {
.init(ast: .zeroOrOne)
}
public static func exactly(_ n: Int) -> Self {
.init(ast: .exactly(.init(faking: n)))
}
public static func nOrMore(_ n: Int) -> Self {
.init(ast: .nOrMore(.init(faking: n)))
}
public static func upToN(_ n: Int) -> Self {
.init(ast: .upToN(.init(faking: n)))
}
public static func range(_ lower: Int, _ upper: Int) -> Self {
.init(ast: .range(.init(faking: lower), .init(faking: upper)))
}
}
@_spi(RegexBuilder)
public struct ASTNode {
internal var ast: AST.Node
}
@_spi(RegexBuilder)
public struct AbsentFunction {
internal var ast: AST.AbsentFunction
}
@_spi(RegexBuilder)
public struct AssertionKind {
internal var ast: AST.Atom.AssertionKind
public static func startOfSubject(_ inverted: Bool = false) -> Self {
.init(ast: .startOfSubject)
}
public static func endOfSubjectBeforeNewline(_ inverted: Bool = false) -> Self {
.init(ast: .endOfSubjectBeforeNewline)
}
public static func endOfSubject(_ inverted: Bool = false) -> Self {
.init(ast: .endOfSubject)
}
public static func firstMatchingPositionInSubject(_ inverted: Bool = false) -> Self {
.init(ast: .firstMatchingPositionInSubject)
}
public static func textSegmentBoundary(_ inverted: Bool = false) -> Self {
inverted
? .init(ast: .notTextSegment)
: .init(ast: .textSegment)
}
public static func startOfLine(_ inverted: Bool = false) -> Self {
.init(ast: .startOfLine)
}
public static func endOfLine(_ inverted: Bool = false) -> Self {
.init(ast: .endOfLine)
}
public static func wordBoundary(_ inverted: Bool = false) -> Self {
inverted
? .init(ast: .notWordBoundary)
: .init(ast: .wordBoundary)
}
}
@_spi(RegexBuilder)
public struct Reference {
internal var ast: AST.Reference
}
@_spi(RegexBuilder)
public struct MatchingOptionSequence {
internal var ast: AST.MatchingOptionSequence
}
@_spi(RegexBuilder)
public struct Atom {
internal var ast: AST.Atom
}
}
}
extension DSLTree.Atom {
/// Returns a Boolean indicating whether the atom represents a pattern that's
/// matchable, e.g. a character or a scalar, not representing a change of
/// matching options or an assertion.
var isMatchable: Bool {
switch self {
case .changeMatchingOptions, .assertion:
return false
case .char, .scalar, .any, .backreference, .symbolicReference, .unconverted:
return true
}
}
}