func-formula-parser
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

func-formula-parser

a AST parser for formula & formula function

build status codecov.io node version

关于 About

这是一个严格验证数学公式的分析器.它帮助你进行词法分析,转化一个数学公式为令牌(tokens)流,还可以把令牌流进行语法分析,生成一个节点树.更多请参看用法

This is a strict parser for formula. It will help you to parse a formula with Lexical Analysis & Syntactic Analysis. It can translate a formula string to tokens, and translate tokens to node tree. More infomation please check Usage.

安装 Install

npm i func-formula-parser --save-dev

用法 Usage

import FuncFormulaParser from 'func-formula-parser';
// or const FuncFormulaParser = require('func-formula-parser').default;

const formulaParser = new FuncFormulaParser({}); // option See API

const tokenList = formulaParser.setFormula('1+1'); // tokenList === formulaParser.getTokens();

console.log(tokenList);

const nodeTree = formulaParser.parseNode(); // nodeTree === formulaParser.getNodeTree();

console.log(nodeTree);

// get Error
try {
    formulaParser.setFormula('1+');
}catch(e) {
    console.error(e);
    // Print:
    // SyntaxError: Token parse Error(2:0): ""
    //    Illegal end symbol
}

API

FuncFormulaParser(option?):instance

生成一个分析器实例 Create a parse instance

  • option: object
    • autoParseNode: boolean 是否自动解析语法树节点

instance.setFormula(formula):ITokenItem[]

设置公式

  • formula: string 公式支持多行形式,Formula support mulit line.
// Single line
formulaParser.setFormula('1+1');
// Mulit line
formulaParser.setFormula(`
IF(a>b,
    a,
    b
)
`);

instance.parseNode():INodeItem

解析节点,如果autoParseNodetrue时,则会在setFormula时自动执行.

If autoParseNode is true, it will autorun in setFormula

instance.getTokens():ITokenItem[]

返回令牌列表 Return current tokens

instance.getNodeTree():INodeItem

返回节点树 Return current node tree

构建 Build

npm run build

测试 Test

npm run test:unit
npm run test

基准测试 Benchmarks

npm run test:benchmark

Readme

Keywords

Package Sidebar

Install

npm i func-formula-parser

Weekly Downloads

0

Version

1.1.0

License

MIT

Unpacked Size

42.4 kB

Total Files

22

Last publish

Collaborators

  • cqggff