-
Notifications
You must be signed in to change notification settings - Fork 2
/
lexer.swift
216 lines (173 loc) · 4.82 KB
/
lexer.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
public protocol Tokenizer { }
public protocol TokenType { }
public protocol LexerToken {
var bytes: [UInt8] { get }
var startIndex: Int { get }
var endIndex: Int { get }
var type: TokenType { get }
var string: String { get }
func collect() -> [UInt8]
}
protocol Lexer {
var stream: ByteStream { get }
func parse(fn: (type: TokenType) throws -> Void) throws
func parseTokenStrings(
fn: (type: TokenType, string: String) throws -> Void) throws
func parseTokens(fn: (token: LexerToken) throws -> Void) throws
}
public struct CommonLexerStatus {
public var lineCount = 0
public var newLineKeyword: TokenType?
public var lineStartIndex = 0
public var tokenizer: Tokenizer?
public var spaceTokenizer: Tokenizer?
init(tokenizer: Tokenizer?) {
lineCount = 0
newLineKeyword = nil
lineStartIndex = 0
self.tokenizer = tokenizer
spaceTokenizer = nil
}
}
extension LexerToken {
public var string: String {
if let s = String.fromCharCodes(charCodes: bytes, start: startIndex,
end: endIndex - 1) {
return s
}
return ""
}
public func collect() -> [UInt8] {
return Array(bytes[startIndex..<endIndex])
}
public func collectString() -> String? {
return String.fromCharCodes(charCodes: bytes, start: startIndex,
end: endIndex - 1)
}
public var description: String {
return "CommonToken(startIndex: \(startIndex), endIndex: \(endIndex), " +
"type: \(type), string: \(inspect( string)))"
}
}
public struct CommonToken: LexerToken, CustomStringConvertible {
public var bytes: [UInt8]
public var startIndex: Int
public var endIndex: Int
public var type: TokenType
}
public class CommonLexer: Lexer, CustomStringConvertible {
public var stream: ByteStream
var status: CommonLexerStatus
init(bytes: [UInt8], status: CommonLexerStatus) {
stream = ByteStream(bytes: bytes)
self.status = status
}
// Override me.
func next(tokenizer: Tokenizer) -> TokenType? {
return nil
}
func parseLine(fn: (type: TokenType) throws -> Void) throws {
status.lineCount += 1
while !stream.isEol {
var tt: TokenType?
if let st = status.spaceTokenizer {
tt = next(tokenizer: st)
}
if tt == nil {
if let t = status.tokenizer {
tt = next(tokenizer: t)
}
}
//p([stream.currentTokenString, status.tokenizer]);
//p(status.stored);
if let t = tt {
if stream.startIndex != stream.currentIndex {
try fn(type: t)
}
} else {
throw CommonLexerError.TokenType
}
stream.startIndex = stream.currentIndex
}
//p(status.indent);
}
func parseTokenStrings(fn: (type: TokenType, string: String) throws -> Void)
throws {
try parse() { tt in
if let s = self.stream.currentTokenString {
try fn(type: tt, string: s)
} else {
throw CommonLexerError.String
}
}
}
func parse(fn: (type: TokenType) throws -> Void) throws {
let len = stream.bytes.count
var ninc = 0
var haveNewLine = false
let newLineKey = status.newLineKeyword
let haveNewLineKey = newLineKey != nil
var si = 0
repeat {
var i = stream.findIndex(c: UInt8(10), startAt: si)
haveNewLine = i >= 0
if !haveNewLine {
i = len
} else if i > 0 && stream.bytes[i - 1] == 13 {
// CR. Handle crlf newline.
i -= 1
ninc = 2
} else {
ninc = 1
}
if i - si > 0 {
stream.lineEndIndex = i
try parseLine(fn: fn)
if stream.currentIndex >= len {
break
}
if haveNewLineKey && haveNewLine {
stream.startIndex = i
stream.currentIndex = i + ninc
try fn(type: newLineKey!)
}
} else {
if haveNewLineKey && haveNewLine {
stream.startIndex = i
stream.currentIndex = i + ninc
try fn(type: newLineKey!)
}
status.lineCount += 1
}
si = i + ninc
stream.startIndex = si
stream.currentIndex = si
status.lineStartIndex = si
} while si < len
}
func parseTokens(fn: (token: LexerToken) throws -> Void) throws {
try parse() { tt in
try fn(token: CommonToken(bytes: self.stream.bytes,
startIndex: self.stream.startIndex,
endIndex: self.stream.currentIndex,
type: tt))
}
}
func parseAllTokens() throws -> [CommonToken] {
var a = [CommonToken]()
try parse() { tt in
a.append(CommonToken(bytes: self.stream.bytes,
startIndex: self.stream.startIndex,
endIndex: self.stream.currentIndex,
type: tt))
}
return a
}
public var description: String {
return "CommonLexer(status: \(status))"
}
}
enum CommonLexerError: ErrorProtocol {
case TokenType
case String
}