parexgram.js
🔠 Parsing Expression Grammars for JS
Platform | Build Status |
---|---|
Linux | |
Windows |
Install
NPM
npm i parexgram-js
CDN
Usage
Loading the module
CommonJS
const Parexgram = ;
Browser
The Browser version provides an object named 'Parexgram' which you can use to access the provided classes.
Using the classes
Feed
Feed serves as the consumable string for the Parexgram. Matchers relies on a Feed for extracting/consuming strings. If a Matcher successfully consumes the Feed prefix, the Feed value changes.
const feed = 'some expression';
CharSet
CharSet is a Matcher class which extracts an exact sequence of a string from the Feed prefix.
If a CharSet attempts to consume the Feed, it will return the exact match if the match was successful, otherwise, the CharSet will return an undefined result.
const feed = 'some expression';console; // some
Range
Range is a Matcher class which extracts a single character from the Feed prefix if the character lies within the Character range.
If a Range attempts to consume the Feed, it will return the character that matches, otherwise, undefined is returned.
const feed = 'some expression';console; // s
Sequence
Sequence is a Matcher class which extracts an exact sequence of matches given a sequence of matchers from a Feed prefix.
If a CharSet attempts to consume the Feed, it will return the exact matches inside of an array if the match was successful, otherwise, the CharSet will return an undefined result.
const feed = 'some expression';const alpha = 'a' 'z';console; // ['s', 'o'];
Alternation
Alternation is a Matcher class which extracts an exact match given a sequence of matchers from a Feed prefix. Unlike Sequence, Alternation will only return one of the matchers result.
const feed = 'some expression';const alpha = 'a' 'z';console; // 's';
Quantifier
Quantifier is a Matcher class which extracts a series of matches by a given matcher from a Feed prefix. Given a range, if a Quantifier collects an amount of matches between the given range, the Quantifier returns an array of those matches, whereas if the Quantifier collects an amount less than the minimum boundary of the range, the Quantifier returns undefined.
const feed = 'some expression';const alpha = 'a' 'z';console; // ['s', 'o', 'm', 'e']
Pattern
Pattern is a Matcher class which extracts the Feed prefix if it matches the RegExp provided.
const feed = 'some expression';console; // ['s', 'o', 'm', 'e']
Matcher
Matcher is an interface used for the above classes. This signifies that the object can consume a Feed prefix. You can extend the Matcher to create your own custom Matcher.
Documentation
Online Documentation can be found at the official Doc Site.
Examples
Build
Clone the repo then run:
npm install
To build the docs, coverages, commonjs and browser module:
npm run build
Changelogs
0.2.0
- Feed.peek(undefined) returns the rest of the Feed string.
- Pattern class which allows the use of RegExp for matching Feeds.