Skip to content

Commit 2a19722

Browse files
committed
WIP
1 parent 2637c38 commit 2a19722

File tree

4 files changed

+98
-97
lines changed

4 files changed

+98
-97
lines changed

Sources/Regex/AST/ASTBuilder.swift

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
2+
// For now, we expose a bunch of AST-construction heleprs as
3+
// global functions for test cases and the regex DSL. We'll
4+
// want to sink this out of here somehow long-term
5+
6+
extension Token: ExpressibleByExtendedGraphemeClusterLiteral {
7+
public typealias ExtendedGraphemeClusterLiteralType = Character
8+
public init(extendedGraphemeClusterLiteral value: Character) {
9+
self = .character(value, isEscaped: false)
10+
}
11+
}
12+
extension AST: ExpressibleByExtendedGraphemeClusterLiteral {
13+
public typealias ExtendedGraphemeClusterLiteralType = Character
14+
public init(extendedGraphemeClusterLiteral value: Character) {
15+
self = .atom(.char(value))
16+
}
17+
}
18+
extension Atom: ExpressibleByExtendedGraphemeClusterLiteral {
19+
public typealias ExtendedGraphemeClusterLiteralType = Character
20+
public init(extendedGraphemeClusterLiteral value: Character) {
21+
self = .char(value)
22+
}
23+
}
24+
extension CharacterClass.CharacterSetComponent: ExpressibleByExtendedGraphemeClusterLiteral {
25+
public typealias ExtendedGraphemeClusterLiteralType = Character
26+
public init(extendedGraphemeClusterLiteral value: Character) {
27+
self = .character(value)
28+
}
29+
}
30+
extension CustomCharacterClass.Member: ExpressibleByExtendedGraphemeClusterLiteral {
31+
public typealias ExtendedGraphemeClusterLiteralType = Character
32+
public init(extendedGraphemeClusterLiteral value: Character) {
33+
self = .atom(.char(value))
34+
}
35+
}
36+
37+
public let _fakeLoc = "".startIndex
38+
public let _fakeRange = _fakeLoc ..< _fakeLoc
39+
public func _fake<T: Hashable>(_ t: T) -> AST.Loc<T> {
40+
.init(t, _fakeRange)
41+
}
42+
43+
public func alt(_ asts: [AST]) -> AST {
44+
.alternation(.init(asts))
45+
}
46+
public func alt(_ asts: AST...) -> AST {
47+
alt(asts)
48+
}
49+
public func concat(_ asts: [AST]) -> AST {
50+
.concatenation(.init(asts))
51+
}
52+
public func concat(_ asts: AST...) -> AST {
53+
concat(asts)
54+
}
55+
public func group(
56+
_ kind: AST.Group.Kind, _ child: AST
57+
) -> AST {
58+
.group(.init(_fake(kind), child, _fakeRange))
59+
}
60+
public func quant(
61+
_ amount: AST.Quantification.Amount,
62+
_ kind: AST.Quantification.Kind = .greedy,
63+
_ child: AST
64+
) -> AST {
65+
.quantification(.init(
66+
_fake(amount), _fake(kind), child, _fakeRange))
67+
}
68+
public func charClass(
69+
_ members: CustomCharacterClass.Member...,
70+
inverted: Bool = false
71+
) -> AST {
72+
let cc = CustomCharacterClass(
73+
start: inverted ? .inverted : .normal, members: members
74+
)
75+
return .customCharacterClass(cc)
76+
}
77+
public func charClass(
78+
_ members: CustomCharacterClass.Member...,
79+
inverted: Bool = false
80+
) -> CustomCharacterClass.Member {
81+
let cc = CustomCharacterClass(
82+
start: inverted ? .inverted : .normal, members: members
83+
)
84+
return .custom(cc)
85+
}
86+
public func posixSet(
87+
_ set: Unicode.POSIXCharacterSet, inverted: Bool = false
88+
) -> Atom {
89+
return .named(.init(inverted: inverted, set: set))
90+
}

Sources/RegexDSL/DSL.swift

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,12 @@ import Regex
22

33
// MARK: - Primitives
44

5-
// FIXME: we will want to map #file/#line to source locations
6-
// somehow...
7-
private let fakeLoc = "".startIndex
8-
private let fakeRange = fakeLoc ..< fakeLoc
9-
internal func _fake<T: Hashable>(_ t: T) -> AST.Loc<T> {
10-
.init(t, fakeRange)
11-
}
12-
135
extension String: RegexProtocol {
146
public typealias Capture = Empty
157

168
public var regex: Regex<Capture> {
179
let atoms = self.map { AST.atom(.char($0)) }
18-
return .init(ast: .concatenation(
19-
AST.Concatenation(atoms)))
10+
return .init(ast: concat(atoms))
2011
}
2112
}
2213

@@ -62,11 +53,7 @@ public struct OneOrMore<Component: RegexProtocol>: RegexProtocol {
6253

6354
public init(_ component: Component) {
6455
self.regex = .init(
65-
.ast(.quantification(.init(
66-
_fake(.oneOrMore),
67-
_fake(.greedy),
68-
component.regex.ast!,
69-
fakeRange))))
56+
.ast(quant(.oneOrMore, .greedy, component.regex.ast!)))
7057
}
7158

7259
public init(@RegexBuilder _ content: () -> Component) {
@@ -86,11 +73,8 @@ public struct Repeat<Component: RegexProtocol>: RegexProtocol {
8673
public let regex: Regex<Capture>
8774

8875
public init(_ component: Component) {
89-
self.regex = .init(ast: .quantification(.init(
90-
_fake(.zeroOrMore),
91-
_fake(.greedy),
92-
component.regex.ast!,
93-
fakeRange)))
76+
self.regex = .init(ast: quant(
77+
.zeroOrMore, .greedy, component.regex.ast!))
9478
}
9579

9680
public init(@RegexBuilder _ content: () -> Component) {
@@ -110,11 +94,8 @@ public struct Optionally<Component: RegexProtocol>: RegexProtocol {
11094
public let regex: Regex<Capture>
11195

11296
public init(_ component: Component) {
113-
self.regex = .init(ast: .quantification(.init(
114-
_fake(.zeroOrOne),
115-
_fake(.greedy),
116-
component.regex.ast!,
117-
fakeRange)))
97+
self.regex = .init(ast: quant(
98+
.zeroOrOne, .greedy, component.regex.ast!))
11899
}
119100

120101
public init(@RegexBuilder _ content: () -> Component) {
@@ -165,10 +146,7 @@ public struct CapturingGroup<Capture>: RegexProtocol {
165146
_ component: Component
166147
) {
167148
self.regex = .init(
168-
ast: .group(.init(
169-
_fake(.capture),
170-
component.regex.ast!, // FIXME
171-
fakeRange)))
149+
ast: group(.capture, component.regex.ast!))
172150
}
173151

174152
init<NewCapture, Component: RegexProtocol>(

Tests/RegexTests/SyntaxOptionsTests.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
@testable import Regex
22
import XCTest
33

4-
private let fakeLoc = "".startIndex
5-
private let fakeRange = fakeLoc ..< fakeLoc
6-
internal func _fake<T: Hashable>(_ t: T) -> AST.Loc<T> {
7-
.init(t, fakeRange)
8-
}
9-
104
private func esc(_ c: Character) -> Token {
115
.character(c, isEscaped: true)
126
}
@@ -15,7 +9,7 @@ private let dplus = AST.quantification(.init(
159
_fake(.oneOrMore),
1610
_fake(.greedy),
1711
.atom(.escaped(.decimalDigit)),
18-
fakeRange))
12+
_fakeRange))
1913
private let dotAST = AST.concatenation(
2014
AST.Concatenation([
2115
dplus, ".", dplus, ".", dplus, ".", dplus]))

Tests/RegexTests/Util.swift

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,6 @@
11
@testable import Regex
22
import XCTest
33

4-
extension Token: ExpressibleByExtendedGraphemeClusterLiteral {
5-
public typealias ExtendedGraphemeClusterLiteralType = Character
6-
public init(extendedGraphemeClusterLiteral value: Character) {
7-
self = .character(value, isEscaped: false)
8-
}
9-
}
10-
extension AST: ExpressibleByExtendedGraphemeClusterLiteral {
11-
public typealias ExtendedGraphemeClusterLiteralType = Character
12-
public init(extendedGraphemeClusterLiteral value: Character) {
13-
self = .atom(.char(value))
14-
}
15-
}
16-
extension Atom: ExpressibleByExtendedGraphemeClusterLiteral {
17-
public typealias ExtendedGraphemeClusterLiteralType = Character
18-
public init(extendedGraphemeClusterLiteral value: Character) {
19-
self = .char(value)
20-
}
21-
}
22-
extension CharacterClass.CharacterSetComponent: ExpressibleByExtendedGraphemeClusterLiteral {
23-
public typealias ExtendedGraphemeClusterLiteralType = Character
24-
public init(extendedGraphemeClusterLiteral value: Character) {
25-
self = .character(value)
26-
}
27-
}
28-
extension CustomCharacterClass.Member: ExpressibleByExtendedGraphemeClusterLiteral {
29-
public typealias ExtendedGraphemeClusterLiteralType = Character
30-
public init(extendedGraphemeClusterLiteral value: Character) {
31-
self = .atom(.char(value))
32-
}
33-
}
34-
35-
func alt(_ asts: AST...) -> AST {
36-
.alternation(AST.Alternation(asts))
37-
}
38-
func concat(_ asts: AST...) -> AST {
39-
.concatenation(AST.Concatenation(asts))
40-
}
41-
func charClass(
42-
_ members: CustomCharacterClass.Member...,
43-
inverted: Bool = false
44-
) -> AST {
45-
let cc = CustomCharacterClass(
46-
start: inverted ? .inverted : .normal, members: members
47-
)
48-
return .customCharacterClass(cc)
49-
}
50-
func charClass(
51-
_ members: CustomCharacterClass.Member...,
52-
inverted: Bool = false
53-
) -> CustomCharacterClass.Member {
54-
let cc = CustomCharacterClass(
55-
start: inverted ? .inverted : .normal, members: members
56-
)
57-
return .custom(cc)
58-
}
59-
func posixSet(
60-
_ set: Unicode.POSIXCharacterSet, inverted: Bool = false
61-
) -> Atom {
62-
return .named(.init(inverted: inverted, set: set))
63-
}
64-
654
// MARK: - Test runners
665

676
// TODO: plumb #file and #line through...

0 commit comments

Comments
 (0)