任意顺序的 ANTLR 命名函数参数/参数
我一直在寻找一种方法让命名函数参数/参数以任意顺序出现在 ANTLR 中。有谁知道 ANTLR 解析器表达式中是否存在忽略顺序的语法?
假设该语言中有一个函数 foo
,它可以采用两个命名参数:x
和 y
。由于它们是命名参数,我希望它们能够以任何顺序传递到函数中:
foo(x=1, y=2)
并且
foo(y=2, x=1)
两者都应该是合法的。
我可以只列出 ANTLR 中的所有参数排列,但我希望有一个更优雅的解决方案,特别是因为我有一些可以接受 5 个参数的函数。
任何帮助将不胜感激!
I'm been looking for a way to have named function arguments / parameters appear in any order in ANTLR. Does anyone know if there is syntax to ignore order in an ANTLR parser expression?
Say there is a function foo
in the language that can take two named parameters: x
and y
. Since they are named parameters, I'd like them to be able to be passed into the function in any order:
foo(x=1, y=2)
as well as
foo(y=2, x=1)
should both be legal.
I could just list all the parameter permutations in ANTLR, but I was hoping there would be a more elegant solution, especially since I have some functions that can take 5 parameters.
Any help would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我很确定 ANTLR 中没有内置任何东西来处理这个问题。但是您可以简单地在语法中加入一些常规编程逻辑来重新组织参数。
这是一个小演示语法:
以及一个用于测试它的小类:
它会产生输出:
I'm pretty sure there is nothing built in ANTLR to handle this. But you can simple sprinkle some regular programming logic in your grammar to reorganize the parameters.
Here's a little demo grammar:
And a little class to test it:
which produces the output:
明白了...
如果您想在语法中硬连线“foo”,“x”和“y”,请执行以下操作(未编译/测试,但应该给出想法)
如果您想要更大的灵活性(以支持其他功能) ,按照巴特的建议做一些事情。
Gotcha...
If you want to hardwire "foo", "x" and "y" in your grammar, do the following (not compiled/tested, but should give the idea)
If you want more flexibility (to support other functions), do something like Bart's suggestion.