如何在 PEG 语法中描述函数参数
How to describe function arguments in PEG grammar
我还在为 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]
如何在这种语法中添加对不带 quotes/double 引号的嵌入式括号的支持?如何区分函数参数中的括号和函数结束参数?
有效的函数调用示例:
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]
希望这对某些人有用。
我还在为 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]
如何在这种语法中添加对不带 quotes/double 引号的嵌入式括号的支持?如何区分函数参数中的括号和函数结束参数?
有效的函数调用示例:
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]
希望这对某些人有用。