bluesocks

0.7.5 • Public • Published

bluesocks

Bluesocks is a lightweight lexer in Javascript

let { lexer, rule } = require('bluesocks');
 
let rules = {
    default: [
        rule("PERIOD1", /\./, "othercontext"), //push othercontext
        rule("NOTPERIOD1", /[^\.]+/)
    ],
    othercontext: [
        rule("PERIOD2", /\./, "<"), //pop othercontext
        rule("NOTPERIOD2", /[^\.]+/)
    ]
}
 
 
lex = lexer("This is a test. This is a test. This is a test.", "test", rules);
 
let token = lex.next();
while(!token.done) {
    switch (token.value.type) {
        case "PERIOD1":
            console.log("We switched to default context");
            break;
        case "NOTPERIOD1":
            console.log(`We found a non-period token: "${token.value.data}"`);
            break;
        case "PERIOD2":
            console.log("We switched to othercontext");
            break;
        case "NOTPERIOD2":
            console.log(`We found a non-period token: "${token.value.data}"`);
            break;
        }
    token = lex.next();
}

Package Sidebar

Install

npm i bluesocks

Weekly Downloads

1

Version

0.7.5

License

MIT

Last publish

Collaborators

  • cottonflop