@@ -13,15 +13,20 @@ import _MatchingEngine
13
13
14
14
func compile(
15
15
_ ast: AST , options: REOptions = . none
16
+ ) throws -> RECode {
17
+ try compile ( ast. dslTree, options: options)
18
+ }
19
+
20
+ func compile(
21
+ _ ast: DSLTree , options: REOptions = . none
16
22
) throws -> RECode {
17
23
var currentLabel = 0
18
24
func createLabel( ) -> RECode . Instruction {
19
25
defer { currentLabel += 1 }
20
26
return . label( currentLabel)
21
27
}
22
28
var instructions = RECode . InstructionList ( )
23
- func compileNode( _ ast: AST . Node ) throws {
24
-
29
+ func compileNode( _ ast: DSLTree . Node ) throws {
25
30
if let cc = ast. characterClass {
26
31
instructions. append ( . characterClass( cc) )
27
32
return
@@ -30,12 +35,12 @@ func compile(
30
35
switch ast {
31
36
case . trivia, . empty: return
32
37
33
- case . quote ( let s) :
34
- s. literal . forEach { instructions. append ( . character( $0) ) }
38
+ case let . quotedLiteral ( s) :
39
+ s. forEach { instructions. append ( . character( $0) ) }
35
40
return
36
41
37
- case . atom( let a) :
38
- switch a. kind {
42
+ case let . atom( a) :
43
+ switch a {
39
44
case . char( let c) :
40
45
instructions. append ( . character( c) )
41
46
return
@@ -46,37 +51,36 @@ func compile(
46
51
instructions. append ( . any)
47
52
return
48
53
default :
49
- throw unsupported ( " Unsupported: \( a. _dumpBase ) " )
54
+ throw unsupported ( " Unsupported: \( a) " )
50
55
}
51
56
52
- case . group( let g ) :
53
- switch g . kind. value {
57
+ case let . group( kind , child ) :
58
+ switch kind {
54
59
case . nonCapture:
55
60
instructions. append ( . beginGroup)
56
- try compileNode ( g . child)
61
+ try compileNode ( child)
57
62
instructions. append ( . endGroup)
58
63
return
59
64
case . capture:
60
65
instructions. append ( . beginCapture)
61
- try compileNode ( g . child)
66
+ try compileNode ( child)
62
67
instructions. append ( . endCapture( ) )
63
68
return
64
69
65
70
default :
66
- throw unsupported ( " Unsupported group \( g . kind. value ) \( g ) " )
71
+ throw unsupported ( " Unsupported group \( kind) " )
67
72
}
68
73
69
- case let . groupTransform( g , transform : t ) where g . kind. value == . capture:
74
+ case let . groupTransform( kind , child , transform ) where kind == . capture:
70
75
instructions. append ( . beginCapture)
71
- try compileNode ( g . child)
72
- instructions. append ( . endCapture( transform: t ) )
76
+ try compileNode ( child)
77
+ instructions. append ( . endCapture( transform: transform ) )
73
78
return
74
79
75
- case . groupTransform( let g , _) :
76
- throw unsupported ( " Unsupported group \( g ) " )
80
+ case let . groupTransform( kind , _ , _) :
81
+ throw unsupported ( " Unsupported group transform \( kind ) " )
77
82
78
- case . concatenation( let concat) :
79
- let children = concat. children
83
+ case let . concatenation( children) :
80
84
let childrenHaveCaptures = children. any ( \. hasCapture)
81
85
if childrenHaveCaptures {
82
86
instructions. append ( . beginGroup)
@@ -87,9 +91,8 @@ func compile(
87
91
}
88
92
return
89
93
90
- case . quantification( let quant) :
91
- let child = quant. child
92
- switch ( quant. amount. value, quant. kind. value) {
94
+ case let . quantification( amount, kind, child) :
95
+ switch ( amount, kind) {
93
96
case ( . zeroOrMore, . eager) :
94
97
// a* ==> L_START, <split L_DONE>, a, goto L_START, L_DONE
95
98
let childHasCaptures = child. hasCapture
@@ -221,10 +224,10 @@ func compile(
221
224
}
222
225
return
223
226
default :
224
- throw unsupported ( " Unsupported: \( quant . _dumpBase ) " )
227
+ throw unsupported ( " Unsupported: \( ( amount , kind ) ) " )
225
228
}
226
229
227
- case . alternation( let alt ) :
230
+ case let . alternation( children ) :
228
231
// a|b ==> <split L_B>, a, goto L_DONE, L_B, b, L_DONE
229
232
// a|b|c ==> <split L_B>, a, goto L_DONE,
230
233
// L_B, <split L_C>, b, goto L_DONE, L_C, c, L_DONE
@@ -237,7 +240,6 @@ func compile(
237
240
// E.g. `a` falls-through to the rest of the program and the
238
241
// other cases branch back.
239
242
//
240
- let children = alt. children
241
243
assert ( !children. isEmpty)
242
244
guard children. count > 1 else {
243
245
return try compileNode ( children [ 0 ] )
@@ -258,16 +260,25 @@ func compile(
258
260
return
259
261
260
262
case . conditional:
261
- throw unsupported ( ast . renderAsCanonical ( ) )
263
+ throw unsupported ( " Conditionals " )
262
264
263
265
case . absentFunction:
264
- throw unsupported ( ast . renderAsCanonical ( ) )
266
+ throw unsupported ( " Absent functions " )
265
267
266
268
case . customCharacterClass:
267
269
fatalError ( " unreachable " )
268
270
269
- case . atom( let a) where a. characterClass != nil :
271
+ case let . atom( a) where a. characterClass != nil :
270
272
fatalError ( " unreachable " )
273
+
274
+ case let . convertedRegexLiteral( node, _) :
275
+ try compileNode ( node)
276
+
277
+ case . characterPredicate, . consumer, . consumerValidator:
278
+ throw unsupported ( " DSL extensions " )
279
+
280
+ case let . regexLiteral( re) :
281
+ try compileNode ( re. dslTreeNode)
271
282
}
272
283
}
273
284
0 commit comments