简体   繁体   English

如何用PEG语法描述函数参数

[英]How to describe function arguments in PEG grammar

I'm still fighting with ambiguous grammar of Qt's qmake. 我还在和Qt的qmake模糊语法作斗争。

Now I can't find a way to describe function arguments that can contain parenthesis (eg regex): 现在我找不到一种方法来描述可以包含括号的函数参数(例如regex):

functionName(arg1, "arg2", ^(arg3)+$)

I've tried to describe function call like this: 我试图像这样描述函数调用:

FunctionCall = Identifier space* "(" space* FunctionArgumentList? space* ")" space* eol*

FunctionArgumentList = FunctionArgumentString ((space* "," space* FunctionArgumentString)* / (blank* FunctionArgumentString)*)
FunctionArgumentString = ReplaceFunctionCall / TestFunctionCall / EnquotedString / RegularFunctionArgumentString
RegularFunctionArgumentString = RegularFunctionArgumentStringChar+
RegularFunctionArgumentStringChar = !(")" / blank / "," / quote / doublequote) SourceCharacter
SourceCharacter <- [\u0000-\uFFFC]

How do I add support for embedded parenthesis WITHOUT quotes/double quotes in such grammar? 如何在这种语法中添加对嵌入式括号的支持而不使用引号/双引号? How do I distinguish the parenthesis inside function arguments and function closing one? 如何区分函数参数和函数关闭中的括号?

Valid function call example: 有效函数调用示例:

contains(CMAKE_INSTALL_LIBS_DIR, ^(/usr)?/lib(64)?.*)

Well, i found a pretty hacky solution myself: 好吧,我自己发现了一个非常糟糕的解决方案:
just look further for a next statement. 再看下一个声明。
Here is a simplified grammar fragment using this way: 这是一个使用这种方式的简化语法片段:

FunctionCall = Identifier _* "(" _* FunctionArgumentList? _* ")" _*
FunctionArgumentList = CommaSeparatedList / FunctionArgument
CommaSeparatedList = FunctionArgument (COMMA_WS FunctionArgument?)+

FunctionArgument = FunctionArgumentImpl FunctionArgumentImpl*
FunctionArgumentImpl = EnquotedString / FunctionArgumentString
FunctionArgumentString = FunctionArgumentStringChar+
FunctionArgumentStringChar = !(COMMA / QUOTE / DOUBLEQUOTE / EndOfFunction) SourceCharacter

EndOfFunction = ")" _* (
     eoi / eol
    / "=" / "+=" / "*=" / "-=" / "~="
    / "," / "." / "_"
    / "(" / ")"
    "{" / "}" / ":" / "|"
)

COMMA_WS = _ "," _
COMMA = ","
QUOTE = "'"
DOUBLEQUOTE = '"'
BACKSLASH = "\\"
_ = [ \t]

Hope this will be useful for somebody. 希望这对某些人有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM