spreadsheet parser example

node v10.23.3
version: 2.0.0
endpointsharetweet
// mygenerator.js var Parser = require("jison").Parser; // a grammar in JSON var grammar = { "comment": "JSON Math Parser", // JavaScript comments also work "lex": { "rules": [ ["\\s+", "/* skip whitespace */"], ["=", "/* skip = */"], ["[0-9]+(?:\\.[0-9]+)?\\b", "return 'NUMBER'"], ["[A-Z][0-9]+", "return 'CELL'"], ["\\*", "return '*'"], ["\\/", "return '/'"], ["-", "return '-'"], ["\\+", "return '+'"], ["\\^", "return '^'"], ["!", "return '!'"], ["%", "return '%'"], ["\\(", "return '('"], ["\\)", "return ')'"], ["PI\\b", "return 'PI'"], ["E\\b", "return 'E'"], ["quot;, "return 'EOF'"] ] }, "operators": [ ["left", "+", "-"], ["left", "*", "/"], ["left", "^"], ["right", "!"], ["right", "%"], ["left", "UMINUS"] ], "bnf": { "expressions": [["e EOF", "return $1"]], "e" :[ ["e + e", "$ = $1+$3"], ["e - e", "$ = $1-$3"], ["e * e", "$ = $1*$3"], ["e / e", "$ = $1/$3"], ["e ^ e", "$ = Math.pow($1, $3)"], ["e !", "$ = (function(n) {if(n==0) return 1; return arguments.callee(n-1) * n})($1)"], ["e %", "$ = $1/100"], ["- e", "$ = -$2", {"prec": "UMINUS"}], ["( e )", "$ = $2"], ["NUMBER", "$ = Number(yytext)"], ["CELL", "$ = Number(global.values[yytext])"], ["E", "$ = Math.E"], ["PI", "$ = Math.PI"] ] } } global.values = {"A1": 48, "A2": 24}; var parser = new Parser(grammar); parser.parse("=A1*A2");
Loading…

no comments

    sign in to comment