gpt4 book ai didi

javascript - PegJS - 匹配所有字符,包括 ),除非 ) 是最后一个字符

转载 作者:行者123 更新时间:2023-12-03 01:20:55 24 4
gpt4 key购买 nike

我正在编写一个 PegJS 语法来解析 SQL 语句。我正在努力将函数拆分为 function_id(function_args)。对于函数参数,我想匹配所有字符,包括 () 除了最后一个 ),这对于嵌套函数是必需的。

如何编写规则来匹配包含 ) 的所有字符,除非 ) 是字符串中的最后一个字符。

语法如下

 Function 
= func_name open_p args close_p

func_name
= name:[A-Z]+ {return name.join('');}

open_p
= "("

close_p
= ")"

args
= ar:(.*[^)]) {return ar.join('');}

测试字符串是

AVG(A + AVG(B + C))

最佳答案

正确处理参数的规则会有所帮助。此外,您可以在规则中使用 $() 表示法来组合已解析的字符串,而不是使用 {return name.join('');}

args 可以是 functionnonfunction 重复。 nonfunction 通过前瞻捕获非函数的所有内容。

function 
= func_name open_p (args+ / "") close_p

func_name
= $([A-Z]+)

open_p
= "("

close_p
= ")"

args
= function / nonfunction

nonfunction
= $((!(function / close_p) .)+)

关于javascript - PegJS - 匹配所有字符,包括 ),除非 ) 是最后一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51768347/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com