我仍然在与 Qt 的 qmake 的模棱两可的语法作斗争。
现在我找不到一种方法来描述可以包含括号的函数参数(例如正则表达式):
functionName(arg1, "arg2", ^(arg3)+$)
我试过这样描述函数调用:
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]
如何在此类语法中添加对不带引号/双引号的嵌入式括号的支持?如何区分函数参数中的括号和函数结束参数?
有效的函数调用示例:
contains(CMAKE_INSTALL_LIBS_DIR, ^(/usr)?/lib(64)?.*)
最佳答案
好吧,我自己找到了一个非常 hacky 的解决方案:
请进一步查看下一个声明。
这是使用这种方式的简化语法片段:
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]
希望这对某些人有用。
关于javascript - 如何在 PEG 语法中描述函数参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47918673/