Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix: fix priority checking (#5220)

Evan0972446d316c
diff --git a/packages/engine-formula/src/basics/token.ts b/packages/engine-formula/src/basics/token.ts
index 62f1574625..10809bb70a 100644
--- a/packages/engine-formula/src/basics/token.ts
+++ b/packages/engine-formula/src/basics/token.ts
@@ -50,7 +50,7 @@ export const OPERATOR_TOKEN_PRIORITY = new Map([
[operatorToken.MINUS as string, 2],
[operatorToken.DIVIDED as string, 1],
[operatorToken.MULTIPLY as string, 1],
- [operatorToken.POWER as string, 0],
+ [operatorToken.POWER as string, 0], // ^
]);
export const OPERATOR_TOKEN_SET = new Set(OPERATOR_TOKEN_PRIORITY.keys());
diff --git a/packages/engine-formula/src/engine/analysis/__tests__/lexer-tree-builder.spec.ts b/packages/engine-formula/src/engine/analysis/__tests__/lexer-tree-builder.spec.ts
index 75fce94b7f..13bc81112c 100644
--- a/packages/engine-formula/src/engine/analysis/__tests__/lexer-tree-builder.spec.ts
+++ b/packages/engine-formula/src/engine/analysis/__tests__/lexer-tree-builder.spec.ts
@@ -749,11 +749,36 @@ describe('lexer nodeMaker test', () => {
// The space before [workbook4] will lead to error.
const node = lexerTreeBuilder.treeBuilder("'[workbook1]Sheet 01'!A5* [workbook4]aaa!A5") as LexerNode;
expect(node.serialize()).toStrictEqual(
- { token: 'R_1', st: -1, ed: -1, children: [
- "'[workbook1]Sheet 01'!A5",
- '[workbook4]aaa!A5',
- '*',
- ] }
+ {
+ token: 'R_1',
+ st: -1,
+ ed: -1,
+ children: [
+ "'[workbook1]Sheet 01'!A5",
+ '[workbook4]aaa!A5',
+ '*',
+ ],
+ }
+ );
+ });
+
+ it('#5169', () => {
+ const node = lexerTreeBuilder.treeBuilder('(A1/A2)^0.5-1') as LexerNode;
+ expect(node.serialize()).toStrictEqual(
+ {
+ token: 'R_1',
+ st: -1,
+ ed: -1,
+ children: [
+ 'A1',
+ 'A2',
+ '/',
+ '0.5',
+ '^',
+ '1',
+ '-',
+ ],
+ }
);
});
});
diff --git a/packages/engine-formula/src/engine/analysis/lexer-tree-builder.ts b/packages/engine-formula/src/engine/analysis/lexer-tree-builder.ts
index 7793b0f23b..948e86e92f 100644
--- a/packages/engine-formula/src/engine/analysis/lexer-tree-builder.ts
+++ b/packages/engine-formula/src/engine/analysis/lexer-tree-builder.ts
@@ -729,7 +729,7 @@ export class LexerTreeBuilder extends Disposable {
const lastSymbolPriority = OPERATOR_TOKEN_PRIORITY.get(lastSymbol);
const charPriority = OPERATOR_TOKEN_PRIORITY.get(char);
- if (!lastSymbolPriority || !charPriority) {
+ if (lastSymbolPriority === undefined || charPriority === undefined) {
break;
}