Skip to content

Commit e446fc0

Browse files
nankaNULLwewoor
authored andcommitted
fix: fix bracket match
1 parent d3e91e6 commit e446fc0

1 file changed

Lines changed: 24 additions & 17 deletions

File tree

src/utils/index.ts

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,32 @@ function lexer(input: string): Token[] {
3535
/**
3636
* 过滤函数内容
3737
*/
38-
const matchFunction = (currentChar: string, validator: RegExp) => {
39-
let value = currentChar;
40-
const start = current;
41-
do {
38+
const matchFunction = () => {
39+
const bracketNum = [current];
40+
for (let i = current + 1; i < input.length; i++) {
41+
const currentChar = input[i];
4242
if (currentChar === '\n') {
4343
line++;
4444
}
45-
46-
currentChar = input[++current];
47-
value += currentChar;
48-
} while (!validator.test(currentChar)); // 处理转义字符
49-
tokens.push({
50-
type: TokenType.FunctionArguments,
51-
value,
52-
start: start,
53-
lineNumber: line,
54-
end: current,
55-
});
56-
++current;
45+
if (TokenReg.LeftSmallBracket.test(currentChar)) {
46+
bracketNum.push(i);
47+
}
48+
if (TokenReg.RightSmallBracket.test(currentChar)) {
49+
const start = bracketNum.pop();
50+
const end = i + 1;
51+
if (bracketNum.length === 0) {
52+
current = end;
53+
tokens.push({
54+
type: TokenType.FunctionArguments,
55+
value: input.slice(start, end),
56+
start,
57+
lineNumber: line,
58+
end,
59+
});
60+
return;
61+
}
62+
}
63+
}
5764
};
5865

5966
/**
@@ -83,7 +90,7 @@ function lexer(input: string): Token[] {
8390
}
8491

8592
if (TokenReg.LeftSmallBracket.test(char)) {
86-
matchFunction(char, TokenReg.RightSmallBracket);
93+
matchFunction();
8794
continue;
8895
}
8996

0 commit comments

Comments
 (0)