elenpi

0.5.1 • Public • Published

elenpi

Travis branch bitHound Overall Score Coverage Status npm-downloads dependecies dev-dependencies Conventional Commits npm licence

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

Allow to describe parser rules with structured sentences based on Method Chaining.

  • really short, clear, clean and maintenable parser rules
  • less than 2ko gzip/minified
  • easy DSL prototyping
  • quite fast

Take a look at examples in ./examples.

Esdoc references

Rule's Api

Create a rule instance :

var rule = new Rule();

or using shortcut :

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

The Atom :

The fundamental atom of elenpi : a simple handler that take an object (env) containing the rest of the string to parse and the errors report, and the current descriptor where to store catched information.

.done(function(env, descriptor){
    // place information in current descriptor
    descriptor.foo = env.string...;
    // when information catched : your responsible to replace THE REST of the string in env.
    env.string = env.string.substring(...);
}) : Rule

Recognize a terminal (aka try to match a regexp at beginning of current string) :

.terminal(RegExp, ?String || ?function(env, descriptor, captured){
    descriptor.something = captured[1]; // example
}: Rule

Second argument :

  • could be the name of the property in descriptor where elenpi will store first value of the regexp matching output ("captured" below, which is an array with captured parts)
  • could be a function that receive the "env" object, the current descriptor, and the "captured" regexp output. You're free to do what you want. You should not manage the rest of the string (it will be done by elenpi).

Recognize a single char, that will not be stored in descriptor :

.char( String ) : Rule

Need the end of string there :

.end() : Rule

Use a rule, either by name (will be found in parser's rules), or by providing a rule instance (that will be "inserted" there) :

.use(ruleName:String||rule:Rule) : Rule

Skip an element : Place a skip:true in current descriptor and so elenpi will ignored it :

.skip() : Rule

Recognized an optional space (/^\s+/ : one or more) :

.space(needed = false) : Rule

Force error if parser execute it (Only aimed to be used in .oneOf() as last rule - so if no other rule have been matched -> force error) :

.error(msg): Rule

Match elements in strings through one rule (maybe optional) :

.one(rule || { 
    rule:rule, 
    ?as:function(){ return Instance }, 
    ?set:'name' || function(env, parent, descriptor){ ... } 
}) : Rule
.maybeOne(rule || { 
    rule:rule, 
    ?as:function(){ return Instance }, 
    ?set:'name' || function(env, parent, descriptor){ ... } 
}) : Rule
  • rule : the rule to apply (ruleName:String||rule:Rule)
  • as : optional, a function that return a descriptor instance to use with the provided rule
  • set : optional,
    • either a string that give the property's name where to store descriptor (the one created by the "as" function) in parent descriptor
    • either a function that receive "env", the parent descriptor, and the descriptor.

Match elements in strings through one of provided rules :

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

Match x or more elements 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, descriptor){ ... },
    ?separator:rule,
    ?maximum:int = Infinity
}) : Rule
  • rule : the rule to apply (ruleName:String||rule:Rule)
  • minimum: the minimum number of elements to recognize (default 0)
  • as : optional, a function that return a descriptor instance to use with the provided rule
  • pushTo : optional,
    • either a string that give the property's name where to PUSH descriptor (the one created by the "as" function) in parent descriptor
    • either a function that receive "env", the parent descriptor, and the descriptor.
  • separator: optional, a rule that express the possible separator between recognized elements. (by example a coma between items in an array)
  • maximum: optional, the maximum number of elements to recognize (default Infinity)

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 ./examples

Licence

The MIT License

Copyright (c) 2015-2017 Gilles Coomans gilles.coomans@gmail.com

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.

Readme

Keywords

Package Sidebar

Install

npm i elenpi

Weekly Downloads

3

Version

0.5.1

License

MIT

Last publish

Collaborators

  • nomocas