A wide purpose tokenizer for JavaScript that tokenizes based on rules established using Regular Expressions. The interface conforms to the WriteStream from node.js.
npm i tokenizer
Requiring
var Tokenizer = require('tokenizer');Construction
var t = new Tokenizer(mycallback, options);Setting Options
Options is an object passed to the constructor function and can contain the following properties (defaults shown inline):
{
stepSize: 0, // For large streams, the maximum size that will be tokenized at a time. This must be larger than the largest expected token.
split: undefined // A regular expression. See explanation in 'Splitting into Smaller Pieces'
}
Adding Rules
t.addRule(/^my regex$/, 'type');Splitting into Smaller Pieces
By default, tokenizer attempts to find the longest match in the input stream. This can be a large performance hit for big files. If you are certain that your tokens will never cross a certain type of regular expression boundary (like /\n/) you can specify to split your input by that before tokenization which could improve performance dramatically.
// Break CSV into subportions and tokenize each subportion separately but in order of original input
t = new Tokenizer(undefined, {
split: /\,/
}); // Break file up by lines and tokenize each line separately.
t = new Tokenizer(undefined, {
split: /\r?\n/
});Writing/Piping
t.write(data);
// or
stream.pipe(t);Listen for tokens
t.on('token', function(token, type) {
// do something useful
// type is the type of the token (specified with addRule)
// token is the actual matching string
});
// alternatively you can use the tokenizer as a readable stream.Listening for completion
t.on('end', callback);the optional callback argument for the constructor is a function that will be called for each token in order to specify a different type by returning a string. The parameters passed to the function are token(the token that we found) and match, an object like this
{
regex: /whatever/ // the regex that matched the token
type: 'type' // the type of the token
}##Examples
Take a look a the examples folder.
Rules are regular expressions associated with a type name.
The tokenizer tries to find the longest string matching one or more rules. When several rules match the same string, priority is given to the rule which was added first.
Note: normally your regular expressions should use ^ and $ in order to test the whole string. If these are not used, you rule will match every string that contains what you specified, this could be the whole file!
- Continued optimisation
- Rule sharing across several tokenizers (although this can be achieved through inheritance)
- Need more hooks
- Increase test coverage
Testing is provided via the
Copyright (c) 2012 Florent Jaby
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
