-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
359 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import Foundation | ||
|
||
public enum PatternRegisterError: LocalizedError { | ||
case empty | ||
case missingPrefixSlash | ||
case invalidGlobbing(globbing: String, after: String) | ||
case invalidSymbol(symbol: String, after: String) | ||
case unbalanceParenthesis | ||
case unexpectToken(after: String) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,68 @@ | ||
import Foundation | ||
|
||
internal struct RoutingPatternScanner { | ||
private static let stopWordsSet: Set<Character> = ["(", ")", "/"] | ||
|
||
let expression: String | ||
|
||
private(set) var position: String.Index | ||
|
||
private var unScannedFragment: String { | ||
return String(expression[position..<expression.endIndex]) | ||
} | ||
|
||
var isEOF: Bool { | ||
return self.position == self.expression.endIndex | ||
} | ||
|
||
init(expression: String) { | ||
self.expression = expression | ||
self.position = self.expression.startIndex | ||
private enum _PatternScanTerminator: Character { | ||
case lParen = "(" | ||
case rParen = ")" | ||
case slash = "/" | ||
case dot = "." | ||
case star = "*" | ||
|
||
var jointFragment: (token: RoutingPatternToken?, fragment: String) { | ||
switch self { | ||
case .lParen: | ||
return (token: .lParen, fragment: "") | ||
case .rParen: | ||
return (token: .rParen, fragment: "") | ||
case .slash: | ||
return (token: .slash, fragment: "") | ||
case .dot: | ||
return (token: .dot, fragment: "") | ||
case .star: | ||
return (token: nil, fragment: "*") | ||
} | ||
} | ||
} | ||
|
||
mutating func nextToken() -> RoutingPatternToken? { | ||
guard !isEOF else { return nil } | ||
|
||
guard let firstChar = unScannedFragment.first else { return nil } | ||
internal struct RoutingPatternScanner { | ||
|
||
self.position = expression.index(position, offsetBy: 1) | ||
static func tokenize(_ pattern: PatternIdentifier) -> [RoutingPatternToken] { | ||
guard !pattern.isEmpty else { return [] } | ||
|
||
switch firstChar { | ||
case "/": | ||
return .slash | ||
case ".": | ||
return .dot | ||
case "(": | ||
return .lParen | ||
case ")": | ||
return .rParen | ||
default: | ||
break | ||
} | ||
var appending = "" | ||
var result: [RoutingPatternToken] = pattern.reduce(into: []) { box, char in | ||
guard let terminator = _PatternScanTerminator(rawValue: char) else { | ||
appending.append(char) | ||
return | ||
} | ||
|
||
var fragment = "" | ||
var stepPosition = 0 | ||
for char in self.unScannedFragment { | ||
if RoutingPatternScanner.stopWordsSet.contains(char) { | ||
break | ||
let jointFragment = terminator.jointFragment | ||
defer { | ||
if let token = jointFragment.token { | ||
box.append(token) | ||
} | ||
appending = jointFragment.fragment | ||
} | ||
|
||
fragment.append(char) | ||
stepPosition += 1 | ||
guard let jointToken = _generateToken(expression: appending) else { return } | ||
box.append(jointToken) | ||
} | ||
|
||
self.position = expression.index(self.position, offsetBy: stepPosition) | ||
if let tailToken = _generateToken(expression: appending) { | ||
result.append(tailToken) | ||
} | ||
return result | ||
} | ||
|
||
static private func _generateToken(expression: String) -> RoutingPatternToken? { | ||
guard let firstChar = expression.first else { return nil } | ||
let fragments = String(expression.dropFirst()) | ||
switch firstChar { | ||
case ":": | ||
return .symbol(fragment) | ||
return .symbol(fragments) | ||
case "*": | ||
return .star(fragment) | ||
return .star(fragments) | ||
default: | ||
return .literal("\(firstChar)\(fragment)") | ||
return .literal(expression) | ||
} | ||
} | ||
|
||
static func tokenize(_ expression: String) -> [RoutingPatternToken] { | ||
var scanner = RoutingPatternScanner(expression: expression) | ||
|
||
var tokens: [RoutingPatternToken] = [] | ||
while let token = scanner.nextToken() { | ||
tokens.append(token) | ||
} | ||
|
||
return tokens | ||
} | ||
} |
Oops, something went wrong.