-
Notifications
You must be signed in to change notification settings - Fork 82
/
ParseFilesOperation.swift
277 lines (241 loc) · 10.5 KB
/
ParseFilesOperation.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
//
// ParseFilesOperation.swift
// MockingbirdCli
//
// Created by Andrew Chang on 8/17/19.
// Copyright © 2019 Bird Rides, Inc. All rights reserved.
//
import Foundation
import PathKit
import SourceKittenFramework
import os.log
struct ParsedFile {
let file: File
let data: Data?
let path: Path
let moduleName: String
let imports: Set<String>
let importedModuleNames: Set<String>
let compilationDirectives: [CompilationDirective]
let structure: Structure
let shouldMock: Bool
init(file: File,
path: Path,
moduleName: String,
imports: Set<String>,
compilationDirectives: [CompilationDirective],
structure: Structure,
shouldMock: Bool) {
self.file = file
self.data = file.contents.data(using: .utf8, allowLossyConversion: false)
self.path = path
self.moduleName = moduleName
self.imports = imports
self.importedModuleNames = Set(imports.compactMap({
guard let importKeywordIndex = $0.range(of: #"\bimport\b"#, options: .regularExpression)
else { return nil }
let importDeclaration = $0[$0.index(after: importKeywordIndex.upperBound)...]
guard let moduleMemberIndex = importDeclaration.firstIndex(of: ".") else {
return String(importDeclaration).trimmingCharacters(in: .whitespacesAndNewlines)
}
return String(importDeclaration[..<moduleMemberIndex])
.trimmingCharacters(in: .whitespacesAndNewlines)
}))
self.compilationDirectives = compilationDirectives
self.structure = structure
self.shouldMock = shouldMock
}
func clone(shouldMock: Bool) -> ParsedFile {
return ParsedFile(file: file,
path: path,
moduleName: moduleName,
imports: imports,
compilationDirectives: compilationDirectives,
structure: structure,
shouldMock: shouldMock)
}
}
struct CompilationDirective: Comparable {
let range: Range<Int64> // Byte offset bounds of the compilation directive declaration.
let declaration: String
var condition: String {
return declaration[declaration.index(declaration.startIndex, offsetBy: 3)...]
.trimmingCharacters(in: .whitespaces)
}
static func < (lhs: CompilationDirective, rhs: CompilationDirective) -> Bool {
return lhs.range.lowerBound < rhs.range.lowerBound
}
}
public class ParseFilesOperation: BasicOperation {
let extractSourcesResult: ExtractSourcesOperationResult
let checkCacheResult: CheckCacheOperation.Result?
public class Result {
fileprivate(set) var parsedFiles = [ParsedFile]()
fileprivate(set) var imports = Set<String>()
fileprivate(set) var moduleDependencies = [String: Set<String>]()
}
public let result = Result()
class SubOperation: BasicOperation {
let sourcePath: SourcePath
let shouldMock: Bool
class Result {
fileprivate(set) var parsedFile: ParsedFile?
}
let result = Result()
init(sourcePath: SourcePath, shouldMock: Bool) {
self.sourcePath = sourcePath
self.shouldMock = shouldMock
}
private static var memoizedParsedFiles = Synchronized<[SourcePath: ParsedFile]>([:])
override func run() throws {
let sourcePath = self.sourcePath
if let memoized = ParseFilesOperation.SubOperation.memoizedParsedFiles.value[sourcePath] {
result.parsedFile = memoized.clone(shouldMock: shouldMock)
return
}
let file = try sourcePath.path.getFile()
let structure = try Structure(file: file)
let (imports, compilationDirectives) = shouldMock ? file.parse() : ([], [])
let parsedFile = ParsedFile(file: file,
path: sourcePath.path,
moduleName: sourcePath.moduleName,
imports: imports,
compilationDirectives: compilationDirectives,
structure: structure,
shouldMock: shouldMock)
ParseFilesOperation.SubOperation.memoizedParsedFiles.update { $0[sourcePath] = parsedFile }
result.parsedFile = parsedFile
if shouldMock {
log("Parsed \(imports.count) import declaration\(imports.count != 1 ? "s" : "") in source file at \(sourcePath.path)")
log("Parsed source structure for module `\(sourcePath.moduleName)` at \(sourcePath.path)")
} else {
log("Parsed dependency source structure for module `\(sourcePath.moduleName)` at \(sourcePath.path)")
}
}
}
public init(extractSourcesResult: ExtractSourcesOperationResult,
checkCacheResult: CheckCacheOperation.Result?) {
self.extractSourcesResult = extractSourcesResult
self.checkCacheResult = checkCacheResult
}
override func run() {
guard checkCacheResult?.isCached != true else { return }
time(.parseFiles) {
let operations = extractSourcesResult.targetPaths.map({
SubOperation(sourcePath: $0, shouldMock: true)
}) + extractSourcesResult.dependencyPaths.map({
SubOperation(sourcePath: $0, shouldMock: false)
})
let queue = OperationQueue.createForActiveProcessors()
queue.addOperations(operations, waitUntilFinished: true)
result.parsedFiles = operations.compactMap({ $0.result.parsedFile })
}
result.imports = Set(result.parsedFiles.flatMap({ $0.imports }))
result.moduleDependencies = extractSourcesResult.moduleDependencies
}
}
private extension Path {
func getFile() throws -> File {
let url = URL(fileURLWithPath: String(describing: absolute()), isDirectory: false)
return try File(contents: String(contentsOf: url, encoding: .utf8))
}
}
private extension File {
/// Parses a file line-by-line looking for valid import and compilation directive declarations.
func parse() -> (imports: Set<String>, compilationDirective: [CompilationDirective]) {
// All Swift files implicitly import the Swift stdlib core.
var imports: Set<String> = ["import Swift"]
var compilationDirectives = [CompilationDirective]()
var provisionalCompilationDirectives = [CompilationDirective]()
var currentOffset: Int64 = 0
// TODO: Look into using a proper tokenizer for Swift grammar here...
enum ExclusionType {
case commentBlock, multiLineStringLiteral
}
var currentExclusionType: ExclusionType?
contents
.substringComponents(separatedBy: "\n")
.forEach({ line in
let lineOffset = Int64(line.utf8.count) + (currentOffset > 0 ? 1 : 0)
defer { currentOffset += lineOffset }
let lineContents = line.trimmingCharacters(in: .whitespaces)
guard !lineContents.isEmpty else { return }
if let exclusionType = currentExclusionType {
switch exclusionType {
case .commentBlock:
guard lineContents.contains("*/") else { break }
currentExclusionType = nil
case .multiLineStringLiteral:
guard lineContents.hasPrefix("\"\"\"") else { break }
currentExclusionType = nil
}
return
}
if lineContents.contains("/*") {
currentExclusionType = .commentBlock
} else if lineContents.contains("\"\"\"") {
currentExclusionType = .multiLineStringLiteral
}
guard !lineContents.hasPrefix("//") else { return }
imports.formUnion(parseImports(from: lineContents))
let lineRange = currentOffset..<currentOffset+lineOffset
let (preprocessorMacro, provisionalMacro) =
parseCompilationDirective(from: lineContents,
range: lineRange,
provisionalDirective: provisionalCompilationDirectives.last)
if let preprocessorMacro = preprocessorMacro {
compilationDirectives.append(preprocessorMacro)
provisionalCompilationDirectives.removeLast()
}
if let provisionalMacro = provisionalMacro {
provisionalCompilationDirectives.append(provisionalMacro)
}
})
return (imports, compilationDirectives.sorted())
}
// MARK: Line processors
private func parseImports(from lineContents: String) -> [String] {
guard lineContents.hasPrefix("import ") ||
lineContents.hasPrefix("@testable import ") ||
lineContents.hasPrefix(";")
else { return [] }
return lineContents.substringComponents(separatedBy: ";")
.map({ $0.trimmingCharacters(in: .whitespacesAndNewlines) })
.filter({ $0.hasPrefix("import ") || $0.hasPrefix("@testable import ") })
.map({ String($0
.substringComponents(separatedBy: "/").first!
.trimmingCharacters(in: .whitespacesAndNewlines)) })
}
private func parseCompilationDirective(from lineContents: String,
range: Range<Int64>,
provisionalDirective: CompilationDirective?)
-> (directive: CompilationDirective?, provisionalDirective: CompilationDirective?) {
guard lineContents.hasPrefix("#") else { return (nil, nil) }
if lineContents.hasPrefix("#if") {
return (nil, CompilationDirective(range: range, declaration: lineContents))
} else if lineContents.hasPrefix("#else") {
guard let provisionalDirective = provisionalDirective else {
logWarning("Unmatched compilation directive `#else` declaration")
return (nil, nil)
}
let directiveRange = provisionalDirective.range.lowerBound..<range.lowerBound
let directive = CompilationDirective(range: directiveRange,
declaration: provisionalDirective.declaration)
let provisional = CompilationDirective(range: range,
declaration: "#if !(\(provisionalDirective.condition))")
return (directive, provisional)
} else if lineContents.hasPrefix("#endif") {
guard let provisionalDirective = provisionalDirective else {
logWarning("Unmatched compilation directive `#endif` declaration")
return (nil, nil)
}
let directiveRange = provisionalDirective.range.lowerBound..<range.lowerBound
let directive = CompilationDirective(range: directiveRange,
declaration: provisionalDirective.declaration)
return (directive, nil)
} else {
logWarning("Invalid compilation directive `\(lineContents)`")
return (nil, nil)
}
}
}