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

时间:2017-12-21 05:47:53

标签: javascript parsing grammar peg pegjs

我还在和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)?.*)

1 个答案:

答案 0 :(得分:2)

嗯,我自己找到了一个非常好的解决方案:
再看下一个陈述 这是一个使用这种方式的简化语法片段:

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]

希望这对某些人有用。