nano-parser
Parser combinators library for parsing strings and arrays of strings (developed for parsing ecmascript tagged template strings). Read in russian
Installing
npm install --save 'nano-parser';
Usage
const any defer end find next optional repeat required sequence } = ; const parser = ; parser; // abcd parser; // ab parser; // undefined
Examples of using:
Overview
With the help of nano-parser, you can create complex parsers, combining simple. It's possible to transform results using the method then, as well as to use the cache to improve performance.
Elementary parsers
find, next and end are most simple parsers. find parser designed to search for strings or regular expressions. next and end used when parsing an array of strings (useful for es6 template strings).
Combinators
any, conditional, sequence and repeat designed to combine other parsers with each other.
Subsidiary parsers
optional, required and defer are subsidiary. optional makes parser optional, required makes parser strictly necessary and defer provides the possibility to call the parser from itself.
Class Parser
All parsers return a instance of class Parser. Therefore, they have the same interface. The exec method is internal and is used to call from other parsers. Instead, use the parse method. The parse method takes the first argument string or array of strings, optional second parameter values are values that will be available as the second parameter of the method then. To cache the results use useCache method.
Documentation
find
find is used to search for a regular expression or a direct match with a string. Examples:
const parser = ; ; ;
const parser = ; ; ; ;
const parser = ; ; ;
The symbol "^" is recommended to optimize the regular expression.
any
any makes it possible to find one of the matches.
const parser = ; ; ; ;
const parser = ; ; ; ;
sequence
With sequence you can build sequences of parsers.
const parser = ; ; ;
const parser = ; ; ;
In the previous example you can see in action a useful method then, which is in any parser. And also, any parser has methods not and useCache. then and not return a new parser, so you can easily form a new entity, without affecting the existing:
const parser1 = parser2 = parser1 parser3 = parser1 parserCached = parser1; not; not; ;
repeat
repeat is used to find the repeated sequences.
const name = assignment = whiteSpace = parser = ; ; ; ;
conditional
conditional is useful when you want to look forward.
const condition = pattern1 = pattern2 = pattern1 parser = ; ; ;
optional
optional makes parsers optional.
const whiteSpace = optionalWhiteSpace = parser1 = parser2 = ; ; ; ; ;
required
required will generate an error that the expected characters are not found:
const whiteSpace = requeiredWhiteSpace = parser1 = parser2 = ; ; ; ; ; // error of parsing with position
next and end
next and end needed when parsing arrays of strings. next correspond to the spacing between strings, end - end of the array. These methods are useful when parsing es6 tagged template strings.
let lang; const i18Table = 'привет': 'hello' anything = parser = { return parser; }; lang = 'en'; ; lang = 'ru'; ;
defer
defer useful for creating recursive parsers.
const xml = ; ;