匹配 Textmate Bundle 正则表达式中的多个条目
tl;dr 您可以将多个值收集到单个反向引用中吗?
我正在尝试为 Textmate 包编写语法突出显示,但遇到了问题。
方法类型声明可以出现在两个地方。第一个是在方法声明中:
method name(param1: String, param2: String) -> String { ... }
第二个是在通用类型声明中:
type Name = { name(param1: String, param2: String) -> String }
因为这两个声明(从 name
到最后一个 String
)具有完全相同的语法,所以我想要在存储库中为它们放置一个匹配项,如下所示:
"method_type": {
"match": "(\w+)((\((.*)\))?(\s*(->)\s*(\w+))?)?"
"captures": { ... }
}
我遇到的问题是如何捕获多个参数(当前使用占位符 .*
捕获)。我可以用这个替换它(如果有人有更好的解决方案来匹配逗号分隔值,那么我将不胜感激):
((\w+\s*(:\s*\w+)?\s*,\s*)*\w+\s*(:\s*\w+)?))?
但问题是第二个捕获(逗号之前的所有参数)仅与最后一个捕获匹配根据标准正则表达式行为,因此只有最后两个参数将在该子表达式中被捕获。
我不能使用 begin
和 end
标记,因为除了初始名称之外的所有声明都是可选的,因此模式没有明确定义的结束。 captures
字典不接受其中的pattern
或include
值。有没有办法单独匹配所有参数(与当前的行为相反,当前的行为只是一次匹配所有参数)?
tl;dr Can you collect multiple values into a single backreference?
I'm trying to write the syntax highlighting for a Textmate bundle but I've struck a problem.
There are two places a method type declaration can appear. The first is in a method declaration:
method name(param1: String, param2: String) -> String { ... }
The second is in a general type declaration:
type Name = { name(param1: String, param2: String) -> String }
Because the two declarations (from name
to the last String
) have exactly the same syntax, I want to place a single match for them in the repository, like so:
"method_type": {
"match": "(\w+)((\((.*)\))?(\s*(->)\s*(\w+))?)?"
"captures": { ... }
}
The problem I'm having is how to capture the multiple parameters (currently caught with the placeholder .*
). I could replace it with this (If anyone has a better solution for matching comma-separated values then I would appreciate it):
((\w+\s*(:\s*\w+)?\s*,\s*)*\w+\s*(:\s*\w+)?))?
But the problem with this is that the second capture (all of the parameters before commas) only matches the last capture as per standard regular expression behaviour, so only the last two parameters will ever be captured within that subexpression.
I can't use the begin
and end
markers instead because all of the declaration apart from the initial name is optional, so there's no well-defined end to the pattern. The captures
dictionary does not accept patterns
or include
values inside of it. Is there a way to individually match all of the parameters (as opposed to the current behaviour, which just matches all of them at once)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论