-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathPreprocess.swift
367 lines (328 loc) · 12.7 KB
/
Preprocess.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
/// Preprocesses a source file
///
/// The preprocessor takes a source file and interprets
/// the following directives:
///
/// - `/* #if <condition> */`
/// - `/* #else */`
/// - `/* #endif */`
/// - `@<VARIABLE>@`
/// - `import.meta.<VARIABLE>`
///
/// The condition is a boolean expression that can use the variables
/// defined in the `options`. Variable names must be `[a-zA-Z0-9_]+`.
/// Contents between `if-else-endif` blocks will be included or excluded
/// based on the condition like C's `#if` directive.
///
/// `@<VARIABLE>@` and `import.meta.<VARIABLE>` will be substituted with
/// the value of the variable.
///
/// The preprocessor will return the preprocessed source code.
func preprocess(source: String, file: String? = nil, options: PreprocessOptions) throws -> String {
let preprocessor = Preprocessor(source: source, file: file, options: options)
let tokens = try preprocessor.tokenize()
let parsed = try preprocessor.parse(tokens: tokens)
return try preprocessor.preprocess(parsed: parsed)
}
struct PreprocessOptions {
/// The conditions to evaluate in the source code
var conditions: [String: Bool] = [:]
/// The variables to substitute in the source code
var substitutions: [String: String] = [:]
}
private struct Preprocessor {
enum Token: Equatable {
case `if`(condition: String)
case `else`
case `endif`
case block(String)
}
struct TokenInfo {
let token: Token
let position: String.Index
}
struct PreprocessorError: Error, CustomStringConvertible {
let file: String?
let message: String
let source: String
let line: Int
let column: Int
init(file: String?, message: String, source: String, line: Int, column: Int) {
self.file = file
self.message = message
self.source = source
self.line = line
self.column = column
}
init(file: String?, message: String, source: String, index: String.Index) {
let (line, column) = Self.computeLineAndColumn(from: index, in: source)
self.init(file: file, message: message, source: source, line: line, column: column)
}
/// Get the 1-indexed line and column
private static func computeLineAndColumn(from index: String.Index, in source: String) -> (line: Int, column: Int) {
var line = 1
var column = 1
for char in source[..<index] {
if char == "\n" {
line += 1
column = 1
} else {
column += 1
}
}
return (line, column)
}
var description: String {
let lines = source.split(separator: "\n", omittingEmptySubsequences: false)
let lineIndex = line - 1
let lineNumberWidth = "\(line + 1)".count
var description = ""
if let file = file {
description += "\(file):"
}
description += "\(line):\(column): \(message)\n"
// Show context lines
if lineIndex > 0 {
description += formatLine(number: line - 1, content: lines[lineIndex - 1], width: lineNumberWidth)
}
description += formatLine(number: line, content: lines[lineIndex], width: lineNumberWidth)
description += formatPointer(column: column, width: lineNumberWidth)
if lineIndex + 1 < lines.count {
description += formatLine(number: line + 1, content: lines[lineIndex + 1], width: lineNumberWidth)
}
return description
}
private func formatLine(number: Int, content: String.SubSequence, width: Int) -> String {
return "\(number)".padding(toLength: width, withPad: " ", startingAt: 0) + " | \(content)\n"
}
private func formatPointer(column: Int, width: Int) -> String {
let padding = String(repeating: " ", count: width) + " | " + String(repeating: " ", count: column - 1)
return padding + "^\n"
}
}
let source: String
let file: String?
let options: PreprocessOptions
init(source: String, file: String?, options: PreprocessOptions) {
self.source = source
self.file = file
self.options = options
}
func unexpectedTokenError(expected: Token?, token: Token, at index: String.Index) -> PreprocessorError {
let message = expected.map { "Expected \($0) but got \(token)" } ?? "Unexpected token \(token)"
return PreprocessorError(
file: file,
message: message, source: source, index: index)
}
func unexpectedCharacterError(expected: CustomStringConvertible, character: Character, at index: String.Index) -> PreprocessorError {
return PreprocessorError(
file: file,
message: "Expected \(expected) but got \(character)", source: source, index: index)
}
func unexpectedDirectiveError(at index: String.Index) -> PreprocessorError {
return PreprocessorError(
file: file,
message: "Unexpected directive", source: source, index: index)
}
func eofError(at index: String.Index) -> PreprocessorError {
return PreprocessorError(
file: file,
message: "Unexpected end of input", source: source, index: index)
}
func undefinedVariableError(name: String, at index: String.Index) -> PreprocessorError {
return PreprocessorError(
file: file,
message: "Undefined variable \(name)", source: source, index: index)
}
func tokenize() throws -> [TokenInfo] {
var cursor = source.startIndex
var tokens: [TokenInfo] = []
var bufferStart = cursor
func consume(_ count: Int = 1) {
cursor = source.index(cursor, offsetBy: count)
}
func takeIdentifier() throws -> String {
var identifier = ""
var char = try peek()
while ["a"..."z", "A"..."Z", "0"..."9"].contains(where: { $0.contains(char) })
|| char == "_"
{
identifier.append(char)
consume()
char = try peek()
}
return identifier
}
func expect(_ expected: Character) throws {
guard try peek() == expected else {
throw unexpectedCharacterError(expected: expected, character: try peek(), at: cursor)
}
consume()
}
func expect(_ expected: String) throws {
guard
let endIndex = source.index(
cursor, offsetBy: expected.count, limitedBy: source.endIndex)
else {
throw eofError(at: cursor)
}
guard source[cursor..<endIndex] == expected else {
throw unexpectedCharacterError(expected: expected, character: try peek(), at: cursor)
}
consume(expected.count)
}
func peek() throws -> Character {
guard cursor < source.endIndex else {
throw eofError(at: cursor)
}
return source[cursor]
}
func peek2() throws -> (Character, Character) {
guard cursor < source.endIndex, source.index(after: cursor) < source.endIndex else {
throw eofError(at: cursor)
}
let char1 = source[cursor]
let char2 = source[source.index(after: cursor)]
return (char1, char2)
}
func addToken(_ token: Token, at position: String.Index) {
tokens.append(.init(token: token, position: position))
}
func flushBufferToken() {
guard bufferStart < cursor else { return }
addToken(.block(String(source[bufferStart..<cursor])), at: bufferStart)
bufferStart = cursor
}
while cursor < source.endIndex {
guard case .some(("/", "*")) = try? peek2() else {
consume()
continue
}
let directiveStart = cursor
// Push the current buffer to the tokens
flushBufferToken()
consume(2)
// Start of a block comment
guard try peek2() == (" ", "#") else {
continue
}
consume(2)
// Start of a directive
let directiveSource = source[cursor...]
let directives: [String: () throws -> Token] = [
"if": {
try expect(" ")
let condition = try takeIdentifier()
return .if(condition: condition)
},
"else": {
return .else
},
"endif": {
return .endif
},
]
var token: Token?
for (keyword, factory) in directives {
guard directiveSource.hasPrefix(keyword) else {
continue
}
consume(keyword.count)
token = try factory()
try expect(" */")
break
}
guard let token = token else {
throw unexpectedDirectiveError(at: directiveStart)
}
// Skip a trailing newline
if (try? peek()) == "\n" {
consume()
}
addToken(token, at: directiveStart)
bufferStart = cursor
}
flushBufferToken()
return tokens
}
enum ParseResult {
case block(String)
indirect case `if`(
condition: String, then: [ParseResult], else: [ParseResult], position: String.Index)
}
func parse(tokens: [TokenInfo]) throws -> [ParseResult] {
var cursor = tokens.startIndex
func consume() {
cursor = tokens.index(after: cursor)
}
func parse() throws -> ParseResult {
switch tokens[cursor].token {
case .block(let content):
consume()
return .block(content)
case .if(let condition):
let ifPosition = tokens[cursor].position
consume()
var then: [ParseResult] = []
var `else`: [ParseResult] = []
while cursor < tokens.endIndex && tokens[cursor].token != .else
&& tokens[cursor].token != .endif
{
then.append(try parse())
}
if case .else = tokens[cursor].token {
consume()
while cursor < tokens.endIndex && tokens[cursor].token != .endif {
`else`.append(try parse())
}
}
guard case .endif = tokens[cursor].token else {
throw unexpectedTokenError(
expected: .endif, token: tokens[cursor].token, at: tokens[cursor].position)
}
consume()
return .if(condition: condition, then: then, else: `else`, position: ifPosition)
case .else, .endif:
throw unexpectedTokenError(
expected: nil, token: tokens[cursor].token, at: tokens[cursor].position)
}
}
var results: [ParseResult] = []
while cursor < tokens.endIndex {
results.append(try parse())
}
return results
}
func preprocess(parsed: [ParseResult]) throws -> String {
var result = ""
func appendBlock(content: String) {
// Apply substitutions
var substitutedContent = content
for (key, value) in options.substitutions {
substitutedContent = substitutedContent.replacingOccurrences(
of: "@" + key + "@", with: value)
substitutedContent = substitutedContent.replacingOccurrences(
of: "import.meta." + key, with: value)
}
result.append(substitutedContent)
}
func evaluate(parsed: ParseResult) throws {
switch parsed {
case .block(let content):
appendBlock(content: content)
case .if(let condition, let then, let `else`, let position):
guard let condition = options.conditions[condition] else {
throw undefinedVariableError(name: condition, at: position)
}
let blocks = condition ? then : `else`
for block in blocks {
try evaluate(parsed: block)
}
}
}
for parsed in parsed {
try evaluate(parsed: parsed)
}
return result
}
}