问题描述
我正在使用VScode并创建自己的语言扩展以突出显示语法,在这里我需要使用正则表达式来查找注释.
I am using VScode and create my own language extension to highlight syntax, where I need to use regular expression to find the comments.
基本规则是,!
之后的所有内容均为注释,但是有特殊情况.当!
在eval()
命令中时,表示没有.
The basic rule is that everything after !
is a comment, however there is a special case. When !
is inside eval()
command, it means NOT.
例如,我的一些代码如下:
For example some of my code would look like:
if condition=(eval(!DB_EXIST)) ! this is a comment
(eval( !DB_UPDATED && !DB_EXIST)) !---"!" inside eval() means NOT
!this is another comment
<some commands> ! this is also a comment
第1行和第2行中的!DB_EXIST
不应被解释为注释,并且!
之后将是一个非空格.
The !DB_EXIST
in line 1 and 2 should not be interpreted as comments, and !
will be followed by a non-space.
空格在注释中无关紧要.
Whitespace doesn't matter in comments.
"comments": {
"patterns" [{
"match":"regex1",
"name":"comment"
}]
},
"operator": {
"patterns" [{
"match":"regex2",
"name":"keyword.operator.NOT"
}]
},
我应该使用哪种正则表达式1和2来显示不同的颜色,而不是注释?
What kind of regex 1 and 2 should I use to show different color for comments and NOT?
我不擅长此扩展写作,因此,如果有更好的方法来完成这项工作,我将不胜感激.谢谢!
I am not good at this extension writing, so if there is any better way to do the job I will be very appreciated to hear.Thanks!
更新
@ Gama11帮助了我,但我并未在代码示例中完全涵盖所有情况.!"之后的任何非空白也应该是注释,只要!"不在eval()内.
@Gama11 helped me but I didn't completely cover all the case in my code samples.Any non-sapce after "!" should also be comments, as long as "!" is not inside eval().
推荐答案
这里是一种方法:
{
"$schema": "https://raw.githubusercontent.com/Septh/tmlanguage/master/tmLanguage.schema.json",
"scopeName": "source.abc",
"patterns": [
{
"begin": "(eval)\\(",
"end": "\\)",
"captures": {
"1": {
"name": "entity.name.function"
}
},
"patterns": [
{
"include": "#parens"
},
{
"match": "!",
"name": "keyword"
}
]
},
{
"match": "!.*?$",
"name": "comment"
}
],
"repository": {
"parens": {
"begin": "\\(",
"end": "\\)",
"patterns": [
{
"include": "#parens"
}
]
}
}
}
我们将非注释!
的模式放在第一位,因为它更具体,应该优先于其他注释.另外,我使用了"keyword"
范围而不是更合适的"keyword.operator.NOT"
,因此它实际上在屏幕截图中显示了不同的颜色.
We put the pattern for the non-comment !
first, since it's more specific and should have priority over the other one. Also, I used the "keyword"
scope instead of the more appropriate "keyword.operator.NOT"
so it actually shows a different color in the screenshot.
第一个正则表达式是begin
-end
模式,它允许我们仅对这两个匹配项之间的文本应用模式(在本例中,在eval()
calll中).当我们讨论它时,我们不妨将eval
突出显示为具有entity.name.function
范围的函数.
The first regex is a begin
-end
pattern, which allows us to apply patterns only for the text between those two matches (in this case within an eval()
calll). While we're at it, we might as well highlight eval
as a function with the entity.name.function
scope.
如果我们在eval()
之内,则允许两种模式:
If we're within a eval()
, we allow two patterns:
- 递归
begin
-end
模式(包括其自身)以平衡括号(您可能会想到类似eval(foo() + !bar())
的内容,并且foo()
中的)
不应以eval
结尾-模式) - 首先是我们感兴趣的
!
运算符
- a recursive
begin
-end
pattern (includes itself) to balance the parentheses (you could presumably have something likeeval(foo() + !bar())
, and the)
infoo()
shouldn't end theeval
-pattern) - the
!
operator we're interested in in the first place
第二个正则表达式仅匹配!
,然后匹配任何内容(.*?
),直到行的末尾($
).
The second regex simply matches !
, and then anything (.*?
) until the end of the line ($
).
通常,我强烈建议使用 regex101.com 之类的工具来玩TM语法文件.比起在VSCode本身中进行迭代,要容易得多,因为您可以获得即时反馈.
In general, I'd highly recommend using a tool like regex101.com for playing around with the regexes of TM Grammar files. Much easier than iterating in VSCode itself since you get instant feedback.
这篇关于如何使用“!"作为注释指示符,还不是语言语法突出显示的运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!