-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.swift
45 lines (34 loc) · 1.13 KB
/
main.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
//
// main.swift
// Author: Alexey Komnin
//
// Another bit about Lexer
import Foundation
func printUsage() {
print("Usage: swiftl [FILENAME]")
}
let arguments = CommandLine.arguments
guard arguments.count == 2 else {
printUsage()
exit(EXIT_FAILURE)
}
let filePath = URL(fileURLWithPath: arguments[1])
guard FileManager.default.fileExists(atPath: filePath.path) else {
print("Unable to read a file")
exit(EXIT_FAILURE)
}
// with that simple set of token we can check if the Lexer works as expected. Let's write a simple code containing only braces, parenthesis and whitespaces and check if it parser the input correctly.
// First, we define input and output buffer.
var output = ""
let input = try! Data(contentsOf: filePath)
// Next, we create Lexer and adjust initial state
let lexer = Lexer(content: input)
var token = Token.NUM_TOKENS
// And define the main parsing loop
while token != .EOF {
lexer.lex(Result: &token)
print("\(token)", terminator: " ", to: &output)
}
// And, finally, we get what we expected:
// Lbrace("{") AtSign("@") Comma(",") Colon(":") Rbrace("}") Lparen("(") Rparen(")") EOF
print(output)