Home Reference Source Test Repository

elenpi

Travis branch npm npm-downloads licence dependecies dev-dependencies

Small javascript LL(1) Parser generator through simple and expressive DSL.

Allow to describe Lexer and Parser rules with structured sentences based on Method Chaining.

Take a look at examples in ./src/examples.

Rule's Api

Create a rule instance :

var rule = new Rule();

or using shortcut :

var  r = Rule.initializer;
var rule = r.oneOf(...);

Behaviour :

.done(function(env, obj){
    // ...
    obj.foo = env.string...;
    env.string = env.string.substring(...);
}) : Rule
.terminal(RegExp, ?String || ?function(env, descriptor, captured){
    descriptor.something = captured[1]; // example
}) : Rule
.char( String ) : Rule
.end() : Rule
.rule(ruleName) : Rule
.skip() : Rule
.space(?needed) : Rule

Match elements in strings through one rule :

.one(rule || { 
    rule:rule, 
    ?as:function(){ return Instance }, 
    ?set:'name' || function(env, parent, obj){ ... } 
}) : Rule
.maybeOne(rule || { 
    rule:rule, 
    ?as:function(){ return Instance }, 
    ?set:'name' || function(env, parent, obj){ ... } 
}) : Rule

Match elements in strings through one of provided rules :

.oneOf(...rules || { 
    rules:[rules], 
    ?as:function(){ return Instance }, 
    ?set:'name' || function(env, parent, obj){ ... } 
}) : Rule
.maybeOneOf(...rules || {
    rules:[rules], 
    ?as:function(){ return Instance }, 
    ?set:'name' || function(env, parent, obj){ ... } 
})
.error(msg)

Match x or more element in string with provided rule (and maybe a separator rule) :

.xOrMore({ 
    rule:rule,
    minimum:int = 0,
    ?as:function(){ return Instance }, 
    ?pushTo:'name' || function(env, parent, obj){ ... },
    ?separator:rule,
    ?maximum:int = Infinity
}) : Rule

xOrMore shortcuts :

.zeroOrMore(opt /* as above in xOrMore */) : Rule
.oneOrMore(opt /* as above in xOrMore (minimum = 1) */) : Rule

Parser's API

Constructor :

var parser = new Parser(rulesObject, 'defaultRulesName');
var  r = parser.parse('a string to parse', ?ruleToApply); // parse until the end of string
// r is false if parsing failed
// Otherwise, r is a descriptor object containing catched properties
var descriptor = {};
var  r = parser.parse('a string to parse', ruleToApply = null, objectWhereStoreTokens = null);

// r is false if parsing failed
// Otherwise, r is the string that still to be parsed

// If parsing succeed, descriptor has been decorated with catched properties

see parsers examples in ./src/examples

Licence

The MIT License

Copyright (c) 2015-2017 Gilles Coomans gilles[email protected]

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.