1.0.1 • Published 8 years ago

lexan v1.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
8 years ago

Lexan

Lexical Analyzer

Build Status Coverage Status Dependency Status License

NPM

Installation

npm install lexan

Usage

const Lexan = require('lexan');

let lexer = new Lexan({
    Repeat: /^\s*(\d+x)/i,
    Roll: /^\s*(\d+d\d+)/i,
    Subtract: /^\s*(-)/i,
    Add: /^\s*(\+)/,
    Number: /^\s*(\d+)/i,
    Target: /^\s*(>=|<=|<|>|=)/,
});

let tokens = lexer.analyze('1d20+6 (Stealth)');
lexer.addRule('Comment', /^\s*(\(.+?\))/);
let cTokens = lexer.analyze('1d20+6 (Stealth)');

tokens.forEach((token) => { 
    console.log('%s=%s', token.type, token.value); 
});
// Output: Roll=1d20
//         Add=+
//         Number=6
                                                                               
cTokens.forEach((token) => { 
    console.log('%s=%s', token.type, token.value); 
}); 
// Output: Roll=1d20
//         Add=+
//         Number=6
//         Comment=Stealth