You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -17,6 +17,10 @@ For general information about PEGs, see [the original paper](https://dl.acm.org/
17
17
18
18
To try out Patterns in a playground, open Playground/Playground.xcworkspace in Xcode.
19
19
20
+
### Note
21
+
22
+
Patterns requires Swift 5.3, which is currently in beta. If using Xcode, version 12 (also in beta) is required.
23
+
20
24
## Example
21
25
22
26
```swift
@@ -183,6 +187,30 @@ let pointsAsSubstrings = parser.matches(in: text).map { match in
183
187
184
188
You can also use `match[multiple: name]` to get an array if captures with that name may be matched multiple times. `match[one: name]` only returns the first capture of that name.
185
189
190
+
### Inputs
191
+
192
+
By default, patterns have `String` as their input type. But you can use any `BidirectionalCollection` with `Hashable` elements for input. Just explicitly specify the input type of the first pattern, and the rest should get it automatically:
193
+
194
+
```swift
195
+
let text ="This is a point: (43,7), so is (0, 5). But my final point is (3,-1).".utf8
196
+
197
+
let digit = OneOf<String.UTF8View>(UInt8(ascii: "0")...UInt8(ascii: "9"))
198
+
let number = ("+"/"-"/"") • digit+
199
+
let point ="("•Capture(name: "x", number)
200
+
•","•""¿•Capture(name: "y", number) •")"
201
+
202
+
structPoint: Codable {
203
+
let x, y:Int
204
+
}
205
+
206
+
let parser =tryParser(search: point)
207
+
let pointsAsSubstrings = parser.matches(in: text).map { match in
208
+
(text[match[one: "x"]!], text[match[one: "y"]!])
209
+
}
210
+
```
211
+
212
+
`Parser.decode` can (currently) only take String as input, but `.matches` handles all types.
0 commit comments