Skip to content

Commit

Permalink
[*]Fix empty function
Browse files Browse the repository at this point in the history
  • Loading branch information
baka397 committed Aug 15, 2018
1 parent c74a22c commit d620489
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 5 deletions.
1 change: 1 addition & 0 deletions dist/lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ function getExpectTypeByToken(prevTokenItem) {
switch (curSubType) { // 查看起始和结束
case token_1.TokenSubType.SUBTYPE_START:
expectTypeList = [
[token_1.TokenType.TYPE_SUBEXPR, token_1.TokenSubType.SUBTYPE_STOP],
[token_1.TokenType.TYPE_SUBEXPR, token_1.TokenSubType.SUBTYPE_START],
[token_1.TokenType.TYPE_SET, token_1.TokenSubType.SUBTYPE_START],
[token_1.TokenType.TYPE_FUNCTION, null],
Expand Down
14 changes: 12 additions & 2 deletions dist/lib/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ function parseLineToken(formulaStr, line, lastTokenItem, lastParentTokenTypeList
* @return {ITokenItem} 令牌对象
*/
function parseType(formula, prevTokenItem) {
var curParentType = prevTokenItem.parentType;
var expectTypeList = common_1.getExpectTypeByToken(prevTokenItem);
var tokenItem = parseFormulaStr(formula, expectTypeList);
// 如果是结束符时,设置当前类型为父类型
if (tokenItem && tokenItem.subType === token_1.TokenSubType.SUBTYPE_STOP) {
tokenItem.type = curParentType;
// 检测前一个类型是否为闭合直接设置父类型或
tokenItem.type = isParentToken(prevTokenItem) ? prevTokenItem.type : prevTokenItem.parentType;
}
return {
expectTypeList: expectTypeList,
Expand Down Expand Up @@ -212,6 +212,16 @@ function isClosedToken(tokenItem) {
}
return false;
}
// 是否为父级元素
function isParentToken(tokenItem) {
switch (tokenItem.type) {
// 以下类型需要特殊判断
case token_1.TokenType.TYPE_FUNCTION:
case token_1.TokenType.TYPE_SUBEXPR:
return true;
}
return false;
}
/**
* 以下为各个匹配数据类型函数
*/
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "func-formula-parser",
"version": "0.1.3",
"version": "0.1.4",
"description": "a AST parser for formula & formula function",
"main": "dist/index.js",
"types": "src/index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions src/lib/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export function getExpectTypeByToken(prevTokenItem: ITokenItem): ExpectType[] {
switch (curSubType) { // 查看起始和结束
case TokenSubType.SUBTYPE_START:
expectTypeList = [
[TokenType.TYPE_SUBEXPR, TokenSubType.SUBTYPE_STOP], // 函数结束符,空函数
[TokenType.TYPE_SUBEXPR, TokenSubType.SUBTYPE_START], // 子表达式
[TokenType.TYPE_SET, TokenSubType.SUBTYPE_START], // 集合起始
[TokenType.TYPE_FUNCTION, null],
Expand Down
15 changes: 13 additions & 2 deletions src/lib/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ function parseType(formula: string, prevTokenItem: ITokenItem): {
expectTypeList: ExpectType[],
item: ITokenItem
} {
const curParentType = prevTokenItem.parentType;
const expectTypeList: ExpectType[] = getExpectTypeByToken(prevTokenItem);
const tokenItem = parseFormulaStr(formula, expectTypeList);
// 如果是结束符时,设置当前类型为父类型
if (tokenItem && tokenItem.subType === TokenSubType.SUBTYPE_STOP) {
tokenItem.type = curParentType;
// 检测前一个类型是否为闭合直接设置父类型或
tokenItem.type = isParentToken(prevTokenItem) ? prevTokenItem.type : prevTokenItem.parentType;
}
return {
expectTypeList,
Expand Down Expand Up @@ -226,6 +226,17 @@ function isClosedToken(tokenItem: ITokenItem): boolean {
return false;
}

// 是否为父级元素
function isParentToken(tokenItem: ITokenItem): boolean {
switch (tokenItem.type) {
// 以下类型需要特殊判断
case TokenType.TYPE_FUNCTION:
case TokenType.TYPE_SUBEXPR:
return true;
}
return false;
}

/**
* 以下为各个匹配数据类型函数
*/
Expand Down
39 changes: 39 additions & 0 deletions test/lib/node.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,45 @@ module.exports = function() {
});
});
describe('Function', function() {
it('IF(Empty(),-count(3,4,5),5%)', function() {
formulaParser.setFormula('IF(Empty(),-count(3,4,5),5%)');
const assertNodeTree = {
token: 'IF',
children: [
{
token: 'Empty'
},
{
token: '-',
children: [
{
token: 'count',
children: [
{
token: '3'
},
{
token: '4'
},
{
token: '5'
}
]
}
]
},
{
token: '%',
children: [
{
token: '5'
}
]
}
]
};
validTokenReulst(formulaParser.getNodeTree(), assertNodeTree);
});
it('IF(a>=0,-count(3,4,5),5%)', function() {
formulaParser.setFormula('IF(a>=0,-count(3,4,5),5%)');
const assertNodeTree = {
Expand Down
28 changes: 28 additions & 0 deletions test/lib/token.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,34 @@ module.exports = function() {
});
});
describe('Function', function() {
it('Empty()', function() {
const tokenList = formulaParser.setFormula('Empty()');
const assertResult = [
{
parentType: null,
type: TOKEN.TokenType.TYPE_FUNCTION,
subType: TOKEN.TokenSubType.SUBTYPE_START,
token: 'Empty',
row: 1,
start: 0,
end: 6,
sourceStart: 0,
sourceEnd: 6
},
{
parentType: null,
type: TOKEN.TokenType.TYPE_FUNCTION,
subType: TOKEN.TokenSubType.SUBTYPE_STOP,
token: '',
row: 1,
start: 6,
end: 7,
sourceStart: 6,
sourceEnd: 7
}
];
validTokenReulst(tokenList, assertResult);
});
it('Round(-1, -5%)', function() {
const tokenList = formulaParser.setFormula('Round(-1, -5%)');
const assertResult = [
Expand Down

0 comments on commit d620489

Please sign in to comment.