-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathTokenizer.js
More file actions
158 lines (140 loc) · 4.15 KB
/
Tokenizer.js
File metadata and controls
158 lines (140 loc) · 4.15 KB
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
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var assert = require('assert');
var Transform = require('stream').Transform;
function noop(){}
function Tokenizer (check_token_cb, options) {
if(!(this instanceof Tokenizer)) {
return new Tokenizer(check_token_cb);
}
Transform.call(this, options);
this._readableState.objectMode = true;
this._buffered = ""; // we buffer untokenized data between writes
this._regexes = []; // should contain objects
// with regex[RegExp] and type[String]
this._ignored = {}; // a hash of ignored token types
// these will be parsed but not emitted
this._checkToken = check_token_cb || noop;
}
util.inherits(Tokenizer, Transform);
Tokenizer.prototype._transform = function _transform(chunk, encoding, callback) {
chunk = chunk.toString();
var self = this;
process.nextTick(function () {
try {
var index = 0, step = 64;
while(index < chunk.length) {
self._tokenize(chunk.substr(index, step));
index += step;
}
callback();
} catch(e) {
callback(e);
}
})
};
Tokenizer.prototype._getMatchingRule = function _getMatchingRule(str) {
for (var i = 0; i < this._regexes.length; ++i) {
if(this._regexes[i].regex.test(str)) {
return this._regexes[i];
}
}
return null;
};
Tokenizer.prototype._tokenize = function _tokenize(data, nobuffer) {
var regexes = this._regexes;
// in case we buffered data on previous writes
data = this._buffered + data;
this._buffered = '';
if(!data.length) {
return;
}
var self = this;
for (var maxIndex = data.length; maxIndex > 0; maxIndex--) {
var buf = data.substr(0,maxIndex);
if (self._getMatchingRule(buf)) break;
}
if(maxIndex === 0) {
// no match found
throw new SyntaxError('could not tokenize ' + JSON.stringify(data));
}
else if (maxIndex === data.length && !nobuffer) {
// the whole string is matching
this._buffered = data;
return;
}
else {
// some substring is matching
var str = data.substring(0, maxIndex);
var rule = this._getMatchingRule(str);
if(!rule) {
throw new Error('wut ?');
}
this._gotToken(str, rule);
this._tokenize(data.substring(maxIndex), nobuffer);
}
};
Tokenizer.prototype._flush = function _flush(callback) {
var self = this;
process.nextTick(function () {
try {
self._tokenize('', true);
callback();
} catch(e) {
callback(e);
}
});
};
var Token = function String (content, type) {
this.content = content;
this.type = type;
this.toString = function () {
return this.content.toString();
}
}
util.inherits(Token, String);
Token.prototype.valueOf = function valueOf() {
return this.content;
};
Tokenizer.prototype._gotToken = function _gotToken(str, rule) {
// notify the token checker
var type = this._checkToken(str, rule) || rule.type;
if(this._ignored[type]) return;
var token = new Token(str, type);
this.push(token);
this.emit('token', token, type);
};
Tokenizer.prototype.addRule = function addRule(regex, type) {
// this is useful for built-in rules
if(!type) {
if(Array.isArray(regex)) {
return this.addRule(regex[0], regex[1]);
}
else if(regex) {
return this.addRule(Tokenizer[regex]);
}
else {
throw new Error('No parameters specified');
}
}
assert.ok((regex instanceof RegExp) || (typeof regex === 'function'));
assert.equal(typeof type, 'string');
this._regexes.push({regex:regex,type:type});
};
/**
* set some tokens to be ignored. these won't be emitted
*/
Tokenizer.prototype.ignore = function ignore(ignored) {
if(Array.isArray(ignored)) {
for (var i = 0; i < ignored.length; ++i) {
this.ignore(ignored[i]);
}
return;
}
this._ignored[ignored] = true;
};
module.exports = Tokenizer;
// built-in rules
Tokenizer.whitespace = [/^(\s)+$/, 'whitespace'];
Tokenizer.word = [/^\w+$/, 'word'];
Tokenizer.number = [/^\d+(\.\d+)?$/, 'number'];