all files / lib/genlex/ tokenizer.js

100% Statements 8/8
100% Branches 0/0
100% Functions 4/4
100% Lines 8/8
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                                             
/*
 * Parsec
 * https://github.com/d-plaindoux/parsec
 *
 * Copyright (c) 2016 Didier Plaindoux
 * Licensed under the LGPL2 license.
 */
 
module.exports = (function () {
    
    'use strict';
    
    var genlex = require('./genlex.js'),
        token = require('./token.js');
    
    // [String] -> Tokenizer
    function Tokenizer(keywords) {
        this.parser = genlex.generator(keywords).tokens(token.builder);
    }
    
    // Stream char -> Try [Token]
    Tokenizer.prototype.tokenize = function (charstream) {
        return this.parser.parse(charstream,0).toTry();
    };
    
    return function (keywords) {
        return new Tokenizer(keywords);
    };
    
}());