lexan
v1.0.1
Published
A lexical analyzer for nodejs.
Downloads
1
Maintainers
Readme
Lexan
Lexical Analyzer
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