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 | 1
1
1
1
1
1
1
1
1
1
603
72
72
72
603
603
603
603
603
603
603
603
603
603
603
1
6983
1
5
1
15
15
26
25
14
11
14
1
26
25
25
2
25
2
25
1
2469
2469
2469
2469
2469
2469
2469
2469
2469
1
81
81
81
81
1
3098
3098
546
2552
2552
2552
2551
2551
1
4321
1
4189
4189
4189
4423
4423
4423
766
3657
6
6
6
3651
8
3643
1
2552
1
2552
2552
44
2508
603
1905
468
1437
1
2560
2560
2560
1
2559
2559
8
8
8
2559
1
3105
1
546
1
1880
1
99
99
99
78
78
78
21
21
21
99
99
99
99
99
99
1
| (function(sc) {
"use strict";
require("../compiler");
require("../marker/");
var slice = [].slice;
var strlib = sc.libs.strlib;
var charlib = sc.libs.charlib;
var Token = sc.lang.compiler.Token;
var Message = sc.lang.compiler.Message;
var Marker = sc.lang.compiler.Marker;
function Lexer(source, opts) {
/* istanbul ignore next */
if (typeof source !== "string") {
Eif (typeof source === "undefined") {
source = "";
}
source = String(source);
}
this.source = source.replace(/\r\n?/g, "\n");
this.opts = opts = opts || /* istanbul ignore next */ {};
this.length = source.length;
this.errors = null;
this.index = 0;
this.lineNumber = this.length ? 1 : 0;
this.lineStart = 0;
this.lookahead = this.advance();
this.index = 0;
this.lineNumber = this.length ? 1 : 0;
this.lineStart = 0;
}
Object.defineProperty(Lexer.prototype, "columnNumber", {
get: function() {
return this.index - this.lineStart;
}
});
Lexer.addLexMethod = function(name, method) {
Lexer.prototype["lex" + name] = method;
};
Lexer.prototype.tokenize = function() {
var tokens = [];
while (true) {
var token = this.collectToken();
if (token.type === Token.EOF) {
break;
}
tokens.push(token);
}
return tokens;
};
Lexer.prototype.collectToken = function() {
var token = this.advance();
var result = {
type: token.type,
value: token.value
};
if (this.opts.range) {
result.range = token.range;
}
if (this.opts.loc) {
result.loc = token.loc;
}
return result;
};
Lexer.prototype.lex = function() {
var token = this.lookahead;
this.index = token.range[1];
this.lineNumber = token.lineNumber;
this.lineStart = token.lineStart;
this.lookahead = this.advance();
this.index = token.range[1];
this.lineNumber = token.lineNumber;
this.lineStart = token.lineStart;
return token;
};
Lexer.prototype.unlex = function(token) {
this.lookahead = token;
this.index = token.range[0];
this.lineNumber = token.lineNumber;
this.lineStart = token.lineStart;
};
Lexer.prototype.advance = function() {
this.skipComment();
if (this.length <= this.index) {
return this.EOFToken();
}
var lineNumber = this.lineNumber;
var columnNumber = this.columnNumber;
var token = this.scan();
token.loc = {
start: { line: lineNumber, column: columnNumber },
end: { line: this.lineNumber, column: this.columnNumber }
};
return token;
};
Lexer.prototype.createMarker = function(node) {
return Marker.create(this, node);
};
Lexer.prototype.skipComment = function() {
var source = this.source;
var length = this.length;
while (this.index < length) {
var ch1 = source.charAt(this.index);
var ch2 = source.charAt(this.index + 1);
if (ch1 === " " || ch1 === "\t") {
this.index += 1;
} else if (ch1 === "\n") {
this.index += 1;
this.lineNumber += 1;
this.lineStart = this.index;
} else if (ch1 === "/" && (ch2 === "/" || ch2 === "*")) {
this.scanWithFunc(this.lexComment);
} else {
break;
}
}
};
Lexer.prototype.scan = function() {
return this.scanWithFunc(this.selectScanner());
};
Lexer.prototype.selectScanner = function() {
var ch = this.source.charAt(this.index);
if (ch === "\\" || ch === "'" || ch === '"' || ch === "$") {
return this.lexString;
}
if (ch === "_" || charlib.isAlpha(ch)) {
return this.lexIdentifier;
}
if (charlib.isNumber(ch)) {
return this.lexNumber;
}
return this.lexPunctuator;
};
Lexer.prototype.scanWithFunc = function(func) {
var start = this.index;
var token = func(this.source, this.index);
if (token.error) {
return this.throwError({}, Message.UnexpectedToken, token.value);
}
this.index += token.length;
if (token.line) {
var value = token.value;
this.lineStart = this.index - (value.length - value.lastIndexOf("\n") - 1);
this.lineNumber += token.line;
}
return this.makeToken(token.type, token.value, start);
};
Lexer.prototype.makeToken = function(type, value, start) {
return {
type: type,
value: value,
lineNumber: this.lineNumber,
lineStart: this.lineStart,
range: [ start, this.index ]
};
};
Lexer.prototype.EOFToken = function() {
return this.makeToken(Token.EOF, "<EOF>", this.index);
};
Lexer.prototype.getLocItems = function() {
return [ this.index, this.lineNumber, this.columnNumber ];
};
Lexer.prototype.throwError = function(token, messageFormat) {
var message = strlib.format(messageFormat, slice.call(arguments, 2));
var index, lineNumber, column;
if (typeof token.lineNumber === "number") {
index = token.range[0];
lineNumber = token.lineNumber;
column = token.range[0] - token.lineStart + 1;
} else {
index = this.index;
lineNumber = this.lineNumber;
column = index - this.lineStart + 1;
}
var error = new SyntaxError(
strlib.format("Line #{0}: #{1}", lineNumber, message),
null, // TODO: filename
lineNumber
);
error.index = index;
error.lineNumber = lineNumber;
error.column = column;
error.description = message;
throw error;
};
sc.lang.compiler.Lexer = Lexer;
})(sc);
|