0.5.8 • Published 2 years ago

ub-script-parser v0.5.8

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Ubot script parser

GitHub Workflow Status (branch) npm

Parser for UBotScript.

$ npm i ub-script-parser

Module has no dependencies.

const { go, goFromFile } = require('ubot-script-parser');

const ubotScriptStr = `
:fn fact n
    match n
        lt 0 -> concat "Error"
        0 -> 1
        _ -> n * fact (n - 1)
`;

const options = {};
const model = go(ubotScriptStr, options); // goFromFile(path, options);

Document

Module contains Shift-reduce parser implementation. And Parser Combinators implementation.

/core/grammar.js <- contains grammars
const { LexNode, Lexer } = require('ub-script-parser/core/lexer');
const { GrammarReduce } = require('ub-script-parser/core/grammar');
const { ReaderFactory } = require('ub-script-parser/core/reader');

const lexer = new Lexer("lexer name");
lexer.add((stream) => {
    stream.trimSpaces();
    const from = stream.pos;
    const number = stream.match(/^-?\d+.?\d*/);
    if(!number) return false;
    const to = stream.pos;
    return new LexNode("number", number, from, to); 
});
lexer.add((stream) => {
    stream.trimSpaces();
    const pos = stream.pos;
    if(stream.check(1) !== '+') return false;
    stream.seek(1);
    return new LexNode("plus", "+", pos, stream.pos);
});

const math = new GrammarReduce("grammar name");
math.with(lexer);

math.rule(([e]) => parseFloat(e), "LIT", "number");
math.rule(([e]) => e, "EXP", "LIT");
math.rule(([l,o,r]) => l + r, "EXP", "EXP", "plus", "LIT");

const model = math.run(new ReaderFactory("2 + 3 + 5 + 10"), {});

model.valid() // true
model.get() // 20 

Licence

MIT.

0.5.8

2 years ago

0.5.7

2 years ago

0.5.6

2 years ago

0.5.5

2 years ago

0.5.3

2 years ago

0.5.2

2 years ago

0.5.1

2 years ago