From e446fc0435eb21c4a931db161c9c3a25dc8edb2b Mon Sep 17 00:00:00 2001 From: nankanull <534129087@qq.com> Date: Tue, 20 Dec 2022 10:08:34 +0800 Subject: [PATCH] fix: fix bracket match --- src/utils/index.ts | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/src/utils/index.ts b/src/utils/index.ts index d9fa6dd..9256073 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -35,25 +35,32 @@ function lexer(input: string): Token[] { /** * 过滤函数内容 */ - const matchFunction = (currentChar: string, validator: RegExp) => { - let value = currentChar; - const start = current; - do { + const matchFunction = () => { + const bracketNum = [current]; + for (let i = current + 1; i < input.length; i++) { + const currentChar = input[i]; if (currentChar === '\n') { line++; } - - currentChar = input[++current]; - value += currentChar; - } while (!validator.test(currentChar)); // 处理转义字符 - tokens.push({ - type: TokenType.FunctionArguments, - value, - start: start, - lineNumber: line, - end: current, - }); - ++current; + if (TokenReg.LeftSmallBracket.test(currentChar)) { + bracketNum.push(i); + } + if (TokenReg.RightSmallBracket.test(currentChar)) { + const start = bracketNum.pop(); + const end = i + 1; + if (bracketNum.length === 0) { + current = end; + tokens.push({ + type: TokenType.FunctionArguments, + value: input.slice(start, end), + start, + lineNumber: line, + end, + }); + return; + } + } + } }; /** @@ -83,7 +90,7 @@ function lexer(input: string): Token[] { } if (TokenReg.LeftSmallBracket.test(char)) { - matchFunction(char, TokenReg.RightSmallBracket); + matchFunction(); continue; }