-
Notifications
You must be signed in to change notification settings - Fork 61
/
CwlScalarScanner.swift
464 lines (415 loc) · 17 KB
/
CwlScalarScanner.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
//
// CwlScalarScanner.swift
// CwlWhitespace
//
// Created by Matt Gallagher on 2016/01/05.
// Copyright © 2016 Matt Gallagher ( https://www.cocoawithlove.com ). All rights reserved.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
// IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
import Swift
/// A type for representing the different possible failure conditions when using ScalarScanner
public enum ScalarScannerError: Error {
/// The scalar at the specified index doesn't match the expected grammar
case unexpected(at: Int)
/// Expected `wanted` at offset `at`
case matchFailed(wanted: String, at: Int)
/// Expected numerals at offset `at`
case expectedInt(at: Int)
/// Attempted to read `count` scalars from position `at` but hit the end of the sequence
case endedPrematurely(count: Int, at: Int)
/// Unable to find search patter `wanted` at or after `after` in the sequence
case searchFailed(wanted: String, after: Int)
}
extension UnicodeScalar {
/// Tests if the scalar is within a range
public func isInRange(_ range: ClosedRange<UnicodeScalar>) -> Bool {
return range.contains(self)
}
/// Tests if the scalar is a plain ASCII digit
public var isDigit: Bool {
return ("0"..."9").contains(self)
}
/// Tests if the scalar is a plain ASCII English alphabet lowercase letter
public var isLower: Bool {
return ("a"..."z").contains(self)
}
/// Tests if the scalar is a plain ASCII English alphabet uppercase letter
public var isUpper: Bool {
return ("A"..."Z").contains(self)
}
/// Tests if the scalar is a plain ASCII English alphabet letter
public var isLetter: Bool {
return isLower || isUpper
}
}
/// A structure for traversing a `String.UnicodeScalarView`.
///
/// **UNICODE WARNING**: this struct ignores all Unicode combining rules and parses each scalar individually. The rules for parsing must allow combined characters to be parsed separately or better yet, forbid combining characters at critical parse locations. If your data structure does not include these types of rule then you should be iterating over the `Character` elements in a `String` rather than using this struct.
public struct ScalarScanner<C: Collection> where C.Iterator.Element == UnicodeScalar {
/// The underlying storage
public let scalars: C
/// Current scanning index
public var index: C.Index
/// Number of scalars consumed up to `index` (since String.UnicodeScalarView.Index is not a RandomAccessIndex, this makes determining the position *much* easier)
public var consumed: Int
/// Construct from a String.UnicodeScalarView and a context value
public init(scalars: C) {
self.scalars = scalars
self.index = self.scalars.startIndex
self.consumed = 0
}
/// Sets the index back to the beginning and clears the consumed count
public mutating func reset() {
index = scalars.startIndex
consumed = 0
}
/// Throw if the scalars at the current `index` don't match the scalars in `scalars`. Advance the `index` to the end of the match.
/// WARNING: `string` is used purely for its `unicodeScalars` property and matching is purely based on direct scalar comparison (no decomposition or normalization is performed).
public mutating func match(string: String) throws {
let (newIndex, newConsumed) = try string.unicodeScalars.reduce((index: index, count: 0)) { (tuple: (index: C.Index, count: Int), scalar: UnicodeScalar) in
if tuple.index == self.scalars.endIndex || scalar != self.scalars[tuple.index] {
throw ScalarScannerError.matchFailed(wanted: string, at: consumed)
}
return (index: self.scalars.index(after: tuple.index), count: tuple.count + 1)
}
index = newIndex
consumed += newConsumed
}
/// Throw if the next scalar at the current `index` fails to match the next scalar in `scalars`. Advance the `index` to the end of the match.
public mutating func match(scalar: UnicodeScalar) throws {
if index == scalars.endIndex || scalars[index] != scalar {
throw ScalarScannerError.matchFailed(wanted: String(scalar), at: consumed)
}
index = self.scalars.index(after: index)
consumed += 1
}
/// Throw if the next scalar at the current `index` fails to match the next scalar in `scalars`. Advance the `index` to the end of the match.
public mutating func match(where test: (UnicodeScalar) -> Bool) throws {
if index == scalars.endIndex || !test(scalars[index]) {
try withoutActuallyEscaping(test) { t in
throw ScalarScannerError.matchFailed(wanted: String(describing: t), at: consumed)
}
}
index = self.scalars.index(after: index)
consumed += 1
}
/// Throw if the scalars at the current `index` don't match the scalars in `value`. Advance the `index` to the end of the match.
mutating func read(where test: (UnicodeScalar) -> Bool) throws -> UnicodeScalar {
if index == scalars.endIndex || !test(scalars[index]) {
try withoutActuallyEscaping(test) { t in
throw ScalarScannerError.matchFailed(wanted: String(describing: t), at: consumed)
}
}
let s = scalars[index]
index = self.scalars.index(after: index)
consumed += 1
return s
}
/// Consume scalars from the contained collection, up to but not including the first instance of `scalar` found. `index` is advanced to immediately before `scalar`. Returns all scalars consumed prior to `scalar` as a `String`. Throws if `scalar` is never found.
public mutating func readUntil(scalar: UnicodeScalar) throws -> String {
var i = index
let previousConsumed = consumed
try skipUntil(scalar: scalar)
var result = ""
result.reserveCapacity(consumed - previousConsumed)
while i != index {
result.unicodeScalars.append(scalars[i])
i = scalars.index(after: i)
}
return result
}
/// Consume scalars from the contained collection, up to but not including the first instance of `string` found. `index` is advanced to immediately before `string`. Returns all scalars consumed prior to `string` as a `String`. Throws if `string` is never found.
/// WARNING: `string` is used purely for its `unicodeScalars` property and matching is purely based on direct scalar comparison (no decomposition or normalization is performed).
public mutating func readUntil(string: String) throws -> String {
var i = index
let previousConsumed = consumed
try skipUntil(string: string)
var result = ""
result.reserveCapacity(consumed - previousConsumed)
while i != index {
result.unicodeScalars.append(scalars[i])
i = scalars.index(after: i)
}
return result
}
/// Consume scalars from the contained collection, up to but not including the first instance of any character in `set` found. `index` is advanced to immediately before `string`. Returns all scalars consumed prior to `string` as a `String`. Throws if no matching characters are ever found.
public mutating func readUntil(set inSet: Set<UnicodeScalar>) throws -> String {
var i = index
let previousConsumed = consumed
try skipUntil(set: inSet)
var result = ""
result.reserveCapacity(consumed - previousConsumed)
while i != index {
result.unicodeScalars.append(scalars[i])
i = scalars.index(after: i)
}
return result
}
/// Peeks at the scalar at the current `index`, testing it with function `f`. If `f` returns `true`, the scalar is appended to a `String` and the `index` increased. The `String` is returned at the end.
public mutating func readWhile(true test: (UnicodeScalar) -> Bool) -> String {
var string = ""
while index != scalars.endIndex {
if !test(scalars[index]) {
break
}
string.unicodeScalars.append(scalars[index])
index = self.scalars.index(after: index)
consumed += 1
}
return string
}
/// Repeatedly peeks at the scalar at the current `index`, testing it with function `f`. If `f` returns `true`, the `index` increased. If `false`, the function returns.
public mutating func skipWhile(true test: (UnicodeScalar) -> Bool) {
while index != scalars.endIndex {
if !test(scalars[index]) {
return
}
index = self.scalars.index(after: index)
consumed += 1
}
}
/// Consume scalars from the contained collection, up to but not including the first instance of `scalar` found. `index` is advanced to immediately before `scalar`. Throws if `scalar` is never found.
public mutating func skipUntil(scalar: UnicodeScalar) throws {
var i = index
var c = 0
while i != scalars.endIndex && scalars[i] != scalar {
i = self.scalars.index(after: i)
c += 1
}
if i == scalars.endIndex {
throw ScalarScannerError.searchFailed(wanted: String(scalar), after: consumed)
}
index = i
consumed += c
}
/// Consume scalars from the contained collection, up to but not including the first instance of any scalar from `set` is found. `index` is advanced to immediately before `scalar`. Throws if `scalar` is never found.
public mutating func skipUntil(set inSet: Set<UnicodeScalar>) throws {
var i = index
var c = 0
while i != scalars.endIndex && !inSet.contains(scalars[i]) {
i = self.scalars.index(after: i)
c += 1
}
if i == scalars.endIndex {
throw ScalarScannerError.searchFailed(wanted: "One of: \(inSet.sorted())", after: consumed)
}
index = i
consumed += c
}
/// Consume scalars from the contained collection, up to but not including the first instance of `string` found. `index` is advanced to immediately before `string`. Throws if `string` is never found.
/// WARNING: `string` is used purely for its `unicodeScalars` property and matching is purely based on direct scalar comparison (no decomposition or normalization is performed).
public mutating func skipUntil(string: String) throws {
let match = string.unicodeScalars
guard let first = match.first else { return }
if match.count == 1 {
return try skipUntil(scalar: first)
}
var i = index
var j = index
var c = 0
var d = 0
let remainder = match[match.index(after: match.startIndex)..<match.endIndex]
outerLoop: repeat {
while scalars[i] != first {
if i == scalars.endIndex {
throw ScalarScannerError.searchFailed(wanted: String(match), after: consumed)
}
i = self.scalars.index(after: i)
c += 1
// Track the last index and consume count before hitting the match
j = i
d = c
}
i = self.scalars.index(after: i)
c += 1
for s in remainder {
if i == self.scalars.endIndex {
throw ScalarScannerError.searchFailed(wanted: String(match), after: consumed)
}
if scalars[i] != s {
continue outerLoop
}
i = self.scalars.index(after: i)
c += 1
}
break
} while true
index = j
consumed += d
}
/// Attempt to advance the `index` by count, returning `false` and `index` unchanged if `index` would advance past the end, otherwise returns `true` and `index` is advanced.
public mutating func skip(count: Int = 1) throws {
if count == 1 && index != scalars.endIndex {
index = scalars.index(after: index)
consumed += 1
} else {
var i = index
var c = count
while c > 0 {
if i == scalars.endIndex {
throw ScalarScannerError.endedPrematurely(count: count, at: consumed)
}
i = self.scalars.index(after: i)
c -= 1
}
index = i
consumed += count
}
}
/// Attempt to advance the `index` by count, returning `false` and `index` unchanged if `index` would advance past the end, otherwise returns `true` and `index` is advanced.
public mutating func backtrack(count: Int = 1) throws {
if count <= consumed {
if count == 1 {
index = scalars.index(index, offsetBy: -1)
consumed -= 1
} else {
let limit = consumed - count
while consumed != limit {
index = scalars.index(index, offsetBy: -1)
consumed -= 1
}
}
} else {
throw ScalarScannerError.endedPrematurely(count: -count, at: consumed)
}
}
/// Returns all content after the current `index`. `index` is advanced to the end.
public mutating func remainder() -> String {
var string: String = ""
while index != scalars.endIndex {
string.unicodeScalars.append(scalars[index])
index = scalars.index(after: index)
consumed += 1
}
return string
}
/// If the next scalars after the current `index` match `scalars`, advance over them and return `true`, otherwise, leave `index` unchanged and return `false`.
/// WARNING: `string` is used purely for its `unicodeScalars` property and matching is purely based on direct scalar comparison (no decomposition or normalization is performed).
public mutating func conditional(string: String) -> Bool {
var i = index
var c = 0
for s in string.unicodeScalars {
if i == scalars.endIndex || s != scalars[i] {
return false
}
i = self.scalars.index(after: i)
c += 1
}
index = i
consumed += c
return true
}
/// If the next scalar after the current `index` match `scalars`, advance over it and return `true`, otherwise, leave `index` unchanged and return `false`.
public mutating func conditional(scalar: UnicodeScalar) -> Bool {
if index == scalars.endIndex || scalar != scalars[index] {
return false
}
index = self.scalars.index(after: index)
consumed += 1
return true
}
/// If the next scalar after the current `index` match `value`, advance over it and return `true`, otherwise, leave `index` unchanged and return `false`.
public mutating func conditional(where test: (UnicodeScalar) -> Bool) -> UnicodeScalar? {
if index == scalars.endIndex || !test(scalars[index]) {
return nil
}
let s = scalars[index]
index = self.scalars.index(after: index)
consumed += 1
return s
}
/// If the `index` is at the end, throw, otherwise, return the next scalar at the current `index` without advancing `index`.
public func requirePeek() throws -> UnicodeScalar {
if index == scalars.endIndex {
throw ScalarScannerError.endedPrematurely(count: 1, at: consumed)
}
return scalars[index]
}
/// If `index` + `ahead` is within bounds, return the scalar at that location, otherwise return `nil`. The `index` will not be changed in any case.
public func peek(skipCount: Int = 0) -> UnicodeScalar? {
var i = index
var c = skipCount
while c > 0 && i != scalars.endIndex {
i = self.scalars.index(after: i)
c -= 1
}
if i == scalars.endIndex {
return nil
}
return scalars[i]
}
/// If the `index` is at the end, throw, otherwise, return the next scalar at the current `index`, advancing `index` by one.
public mutating func readScalar() throws -> UnicodeScalar {
if index == scalars.endIndex {
throw ScalarScannerError.endedPrematurely(count: 1, at: consumed)
}
let result = scalars[index]
index = self.scalars.index(after: index)
consumed += 1
return result
}
/// Throws if scalar at the current `index` is not in the range `"0"` to `"9"`. Consume scalars `"0"` to `"9"` until a scalar outside that range is encountered. Return the integer representation of the value scanned, interpreted as a base 10 integer. `index` is advanced to the end of the number.
public mutating func readInt() throws -> Int {
let result = conditionalInt()
guard let r = result else {
throw ScalarScannerError.expectedInt(at: consumed)
}
return r
}
/// Throws if scalar at the current `index` is not in the range `"0"` to `"9"`. Consume scalars `"0"` to `"9"` until a scalar outside that range is encountered. Return the integer representation of the value scanned, interpreted as a base 10 integer. `index` is advanced to the end of the number.
public mutating func conditionalInt() -> Int? {
var result = 0
var i = index
var c = 0
while i != scalars.endIndex && scalars[i].isDigit {
let digit = Int(scalars[i].value - UnicodeScalar("0").value)
// Avoid overflow
if (Int.max - digit) / 10 < result {
return nil
}
result = result * 10 + digit
i = self.scalars.index(after: i)
c += 1
}
if i == index {
return nil
}
index = i
consumed += c
return result
}
/// Consume and return `count` scalars. `index` will be advanced by count. Throws if end of `scalars` occurs before consuming `count` scalars.
public mutating func readScalars(count: Int) throws -> String {
var result = String()
result.reserveCapacity(count)
var i = index
for _ in 0..<count {
if i == scalars.endIndex {
throw ScalarScannerError.endedPrematurely(count: count, at: consumed)
}
result.unicodeScalars.append(scalars[i])
i = self.scalars.index(after: i)
}
index = i
consumed += count
return result
}
/// Returns a throwable error capturing the current scanner progress point.
public func unexpectedError() -> ScalarScannerError {
return ScalarScannerError.unexpected(at: consumed)
}
public var isAtEnd: Bool {
return index == scalars.endIndex
}
}