/
lexer.js
296 lines (242 loc) · 7.46 KB
/
lexer.js
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
/*
* lexer.js: Custom regular expression based lexer for Kyuri
*
* (C) 2010 Charlie Robbins
* MIT LICENSE
*
*/
var helpers = require('./helpers'),
sys = require('sys'),
eyes = require('eyes');
var MULTI_DENT = /^(\t+)(\.)?/,
IS_EXAMPLE_ROW = /^([\|\s+\S+]+\s+\|\s*)$/,
PARSE_EXAMPLE_ROW = /\|\s*(\S+)/gi,
PYSTRING = /"""/,
TAG = /@(\w+)/i,
COMMENT = /#\s*([\S+\s+]+)/i,
SENTENCE = /([\S+\s+]+)/i,
SENTENCE_COMMENT = /([\S+\s+]+)#\s*([\S+\s+]+)/i;
var Lexer = function (language, i18n) {
// Set the i18n dictionary on the lexer for later use
this.i18n = i18n;
// Set the keyword type list on the lexer for later use
this.operators = ['but', 'and', 'when', 'then', 'given'];
this.headers = ['examples', 'background', 'feature', 'scenario_outline', 'scenario'];
this.tokens = this.operators.concat(this.headers);
// Initialize the set of keywords in the lexer
this.keywords = {};
for (var i = 0; i < this.tokens.length; i++) {
this.keywords[this.tokens[i]] = [];
}
if (typeof i18n[language] === 'undefined') {
throw new Error('Cannot lex against unknown language: ' + language);
}
// Build up the set of keywords from each language
for (var key in i18n[language]) {
if (typeof this.keywords[key] !== 'undefined') {
this.keywords[key].unshift(i18n[language][key].replace(/^\*/, '').replace(/^\|/, ''));
}
}
// Build up the set of regular expressions for matching
// the entire set of i18n keyword tokens.
this.matchers = {};
for (var index in this.headers) {
var key = this.headers[index];
var base = '^(' + this.keywords[key].join('|') + ')(\\:\\s*)';
this.matchers[key] = new RegExp(base, 'i');
}
for (var index in this.operators) {
var key = this.operators[index];
var base = '^(' + this.keywords[key].join('|') + ')';
this.matchers[key] = new RegExp(base, 'i');
}
};
Lexer.prototype = {
tokenize: function (source, options) {
if (!source) {
throw new Error('Cannot lex null or undefined.');
}
source = source.replace(/(\r|\s+$)/g, '');
this.options = options || {}; // Options passed to the lexer
this.source = source; // Source code we are attempting to lex
this.lineNum = 0; // Line number of the lexing operation
this.indents = 0; // Number of indents
this.tokens = []; // Stream of parsed tokens in the form ['TYPE', value, line]
this.lines = this.source.split('\n'); // Split the source code by \n since we have no multi-line statements
while (this.lines.length > 0) {
this.i = 0; // Current character position in this line
this.line = this.lines.shift(); // Get the next line
while (this.i < this.line.length) {
this.chunk = this.line.slice(this.i);
this.extractNextToken();
}
this.newlineToken();
this.lineNum++;
}
this.token('EOF', 'EOF');
return this.tokens;
},
extractNextToken: function () {
var tokens = ['lineToken', 'featureToken', 'scenarioToken', 'scenarioOutlineToken',
'backgroundToken', 'commentToken', 'examplesToken', 'examplesRowToken', 'operatorToken',
'pystringToken', 'tagToken', 'sentenceToken'];
for (var index in tokens) {
if (this[tokens[index]].apply(this)) {
return;
}
}
},
featureToken: function () {
return this.gherkinToken('feature');
},
scenarioToken: function () {
return this.gherkinToken('scenario');
},
scenarioOutlineToken: function () {
return this.gherkinToken('scenario_outline');
},
backgroundToken: function () {
return this.gherkinToken('background');
},
examplesToken: function () {
return this.gherkinToken('examples');
},
examplesRowToken: function () {
var match;
if (!(match = this.match(IS_EXAMPLE_ROW, 1))) {
return false;
}
this.i += match.length;
var matches = this.match(PARSE_EXAMPLE_ROW);
this.token('EXAMPLE_ROW', matches.map(function (item) { return item.replace('|','').trim() }));
return true;
},
operatorToken: function () {
var match;
for (var j = 0; j < this.operators.length; j++) {
match = this.gherkinToken(this.operators[j], 'operator');
if (match) {
return match;
}
}
return false;
},
gherkinToken: function (name, type) {
var match;
if (!(match = this.match(this.matchers[name]))) {
return false;
}
for (var j = 1; j < match.length; j++) {
this.i += match[j].length;
}
if (type) {
this.token(type.toUpperCase(), name.toUpperCase());
}
else {
this.token(name.toUpperCase(), name.toUpperCase());
}
return true;
},
pystringToken: function () {
return this.complexMatch(PYSTRING, 'PYSTRING', 'PYSTRING');
},
tagToken: function () {
return this.complexMatch(TAG, 'TAG');
},
commentToken: function () {
var match;
if (!(match = this.match(COMMENT))) {
return false;
}
// If the comment is at the beginning of the chunk
// then evaluate it normally
if(this.chunk.indexOf('#') === 0) {
this.i += match[0].length;
this.token('COMMENT', match[1].trim());
return true;
}
// Otherwise we have a comment inside of
// a SENTENCE token, so strip off the SENTENCE
// and create tokens for both.
match = this.match(SENTENCE_COMMENT);
this.i += match[0].length;
this.token('SENTENCE', match[1].trim());
this.token('COMMENT', match[2].trim());
return true;
},
sentenceToken: function () {
var sentence;
if (!(sentence = this.match(SENTENCE, 1))) {
return false;
}
this.i += sentence.length;
this.token('SENTENCE', sentence.trim());
return true;
},
lineToken: function () {
var diff, indent, size;
if (!(indent = this.match(MULTI_DENT, 1))) {
return false;
}
size = indent.length;
this.i += size;
if (size > this.indents) {
diff = size - this.indents;
this.token('INDENT', diff);
}
else if (size < this.indents) {
this.token('OUTDENT', this.indents - size);
}
this.indents = size;
return true;
},
newlineToken: function(newlines) {
if (this.tag() !== 'TERMINATOR') {
this.token('TERMINATOR', "\n");
}
return true;
},
token: function(tag, value) {
var token = [tag, value, this.lineNum];
return this.tokens.push(token);
},
prev: function(index) {
return this.tokens[this.tokens.length - (index || 1)];
},
tag: function(index, newTag) {
var tok;
if (!(tok = this.prev(index))) {
return null;
}
if (typeof newTag !== "undefined" && newTag !== null) {
return (tok[0] = newTag);
}
return tok[0];
},
complexMatch: function (regex, token, value) {
var match;
if (!(match = this.match(regex))) {
return false;
}
this.i += match[0].length;
if (value) {
this.token(token, value);
return true;
}
this.token(token, match[0].trim());
return true;
},
match: function(regex, index) {
var m;
if (!(m = this.chunk.match(regex))) {
return false;
}
if (m) {
return index ? m[index] : m;
}
else {
return false;
}
}
};
exports.Lexer = Lexer;