Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
kawoou committed Oct 13, 2018
2 parents 0e1108a + db60814 commit 3e336d1
Show file tree
Hide file tree
Showing 7 changed files with 388 additions and 473 deletions.
21 changes: 21 additions & 0 deletions Binary/Sources/Deli/Extension/String+Range.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@
// Deli
//

extension String {
subscript (i: Int) -> Character {
return self[index(startIndex, offsetBy: i)]
}
subscript (i: Int) -> String {
return String(self[i])
}
subscript (r: CountableRange<Int>) -> String {
return String(self[r.lowerBound...(r.upperBound - 1)])
}
subscript (r: CountableClosedRange<Int>) -> String {
let start = index(startIndex, offsetBy: r.lowerBound)
let end = index(startIndex, offsetBy: r.upperBound)
return String(self[start...end])
}
subscript (r: CountablePartialRangeFrom<Int>) -> String {
let start = index(startIndex, offsetBy: r.lowerBound)
return String(self[start...])
}
}

extension String.UTF8View {
subscript (i: Int) -> Character? {
return self[i]?.first
Expand Down
2 changes: 1 addition & 1 deletion Binary/Sources/Deli/Model/Version.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
struct Version {
let value: String

static let current = Version(value: "0.6.0")
static let current = Version(value: "0.6.1")
}
62 changes: 31 additions & 31 deletions Binary/Sources/Deli/Runner/Parser/InjectParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ final class InjectParser: Parsable {

static let typeRefererSuffix = ".self"
static let injectFuncRegex = "Inject\\(([^\\(]*(\\([^\\)]*\\))*[^\\)]*)\\)".r!
static let argumentRegex = ",[\\s]*([^:]+:[\\s]*\\([^\\)]*\\))|[\\s]*([^,]+)".r!


static let qualifierName = "qualifier"
static let qualifierPrefix = "\(qualifierName):"
static let qualifierRegex = "\(qualifierName):[\\s]*\"([^\"]*)\"".r!
Expand All @@ -37,45 +36,47 @@ final class InjectParser: Parsable {
guard name == Constant.functionName || name.hasSuffix(".\(Constant.functionName)") else { return nil }
guard source.kind == Constant.functionCallKey else { return nil }

let callExpr: String = fileContent
.utf8[Int(source.offset)..<Int(source.offset + source.length)]?
.replacingOccurrences(of: Constant.typeRefererSuffix, with: "")
.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""

guard let callExprMatch = Constant.injectFuncRegex.findFirst(in: callExpr)?.group(at: 1) else {
Logger.log(.assert("Mismatched usage of `\(Constant.functionName)` method on SourceKitten result. \(callExpr)"))
Logger.log(.error("Unknown error.", source.getSourceLine(with: fileContent)))
throw ParserError.unknown
}

let arguments = try Constant.argumentRegex
.findAll(in: callExprMatch.trimmingCharacters(in: .whitespacesAndNewlines))
.map { match -> String in
guard let result = match.group(at: 1) ?? match.group(at: 2) else {
Logger.log(.error("Failed to parse argument `\(match.source)`.", source.getSourceLine(with: fileContent)))
throw ParserError.parseErrorArguments
let arguments: [String] = {
let list = source.substructures
.map { source in
return fileContent.utf8[Int(source.offset)..<Int(source.offset + source.length)] ?? ""
}
return result
}
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
.filter { !$0.isEmpty }
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }

guard list.isEmpty else { return list }

/// Bug fixed in the empty substructure of SourceKittenFramework.
guard let sourceData = fileContent.utf8[Int(source.offset)..<Int(source.offset + source.length)] else { return [] }
guard !sourceData.contains(",") else { return [] }
guard let type = Constant.injectFuncRegex.findFirst(in: sourceData)?.group(at: 1) else { return [] }
return [type]
}()


guard let firstArgument = arguments.first else {
Logger.log(.error("The `\(Constant.functionName)` method in `\(name)` required arguments.", source.getSourceLine(with: fileContent)))
throw ParserError.emptyArguments
}

let typeName: String = {
if firstArgument.hasSuffix(Constant.typeRefererSuffix) {
return firstArgument[0..<(firstArgument.count - Constant.typeRefererSuffix.count)]
} else {
return firstArgument
}
}()

let qualifier = arguments
.first { $0.contains(Constant.qualifierPrefix) }
.first { $0.hasPrefix(Constant.qualifierPrefix) }
.flatMap { result -> String? in
guard let match = Constant.qualifierRegex.findFirst(in: result) else { return nil }
return match.group(at: 1)
}?
.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""

let isPayload = arguments.contains { $0.contains(Constant.payloadPrefix) }
let isPayload = arguments.contains { $0.hasPrefix(Constant.payloadPrefix) }

if let arrayMatch = Constant.arrayRegex.findFirst(in: firstArgument), let arrayType = arrayMatch.group(at: 1) {
if let arrayMatch = Constant.arrayRegex.findFirst(in: typeName), let arrayType = arrayMatch.group(at: 1) {
return Dependency(
parent: rootName,
target: source,
Expand All @@ -87,7 +88,7 @@ final class InjectParser: Parsable {
return Dependency(
parent: rootName,
target: source,
name: arguments[0],
name: typeName,
rule: isPayload ? .payload : .default,
qualifier: qualifier
)
Expand All @@ -98,11 +99,10 @@ final class InjectParser: Parsable {

var queue = source.substructures
while let item = queue.popLast() {
guard let dependency = try found(item, root: source, fileContent: fileContent) else {
queue.append(contentsOf: item.substructures)
continue
queue.append(contentsOf: item.substructures)
if let dependency = try found(item, root: source, fileContent: fileContent) {
dependencyList.append(dependency)
}
dependencyList.append(dependency)
}

return dependencyList
Expand Down
Loading

0 comments on commit 3e336d1

Please sign in to comment.