AT&T汇编语言(似乎)没有专门的语法高亮插件,于是我想自己写一个插件来玩玩。在这里以ATT语言为例,记录一下开发的过程。
首先,通过yo创建插件的框架(这个步骤可以看其他教程)
创建之后的文件大致是这样的(这是我写完以后按印象恢复的,所以可能有一点不同):
方框中的两个文件是主要需要编辑的。
解释一下文件名。att.tmLanguage.json包含了可以匹配的关键字,文件名中的att是语言的名字。language-configuration.json包括了语言的标点符号。
更改language-configuration.json
"comments": {
// symbol used for single line comment. Remove this entry if your language does not support line comments
"lineComment": "//",
// symbols used for start and end a block comment. Remove this entry if your language does not support block comments
"blockComment": [ "/*", "*/" ]
},
对于AT&T语言,单行注释是以’;'开头的,把lineComment中的“//”改为‘;’就可以了。同时,我不想使用块注释的功能,所以把blockComment的部分注释掉了,注意注释以后要把上一项lineComment末尾的‘,’删除。
"comments": {
// symbol used for single line comment. Remove this entry if your language does not support line comments
"lineComment": ";"
// symbols used for start and end a block comment. Remove this entry if your language does not support block comments
// "blockComment": [ "/*", "*/" ]
},
更改att.tmLanguage.json
主要代码有:
"patterns": [
{
"include": "#keywords"
},
{
"include": "#strings"
}
],
"repository": {
"keywords": {
"patterns": [{
"name": "keyword.control.att",
"match": "\\b(if|while|for|return)\\b"
}]
},
"strings": {
"name": "string.quoted.double.att",
"begin": "\"",
"end": "\"",
"patterns": [
{
"name": "constant.character.escape.att",
"match": "\\\\."
}
]
}
},
这段代码我并不懂,但是会改就可以了。在repository下,例如keywords的patterns中,name是语法规则匹配的代码的种类,规定了keywords类型的代码的颜色,比如代码中为keyword.control的颜色。这个颜色名是有规定的。颜色的后缀为语言的名字,在这个例子中为att。name后的match是匹配字符的正则表达式。对正则表达式不了解或者不熟悉(比如我)也没关系,可以参考教程。
首先,在原有的模板下进行更改。把repository-keywords-patterns-match下的"\b(if|while|for|return)\b"改为movb,这时候就可以匹配movb的字符串并将其改为keyword.control的颜色了。
除此之外,还可以加入新的字符类型。在string的代码块后加上’,’,在后面加上类似keywords的代码块(最后不用加‘,’)
"strings": {
"name": "string.quoted.double.att",
"begin": "\"",
"end": "\"",
"patterns": [
{
"name": "constant.character.escape.att",
"match": "\\\\."
}
]
},
"registers": {
"patterns": [{
"name": "variable.att",
"match": "%rax"
}]
}
这个registers的名字不固定,可以随便取。
然而这样是没有作用的,还要在和repository并排的上面的那个patterns里加上{“include”: “#registers”}(注意registers前要加‘#’,前一代码块后之前要加‘,’)
"patterns": [
{
"include": "#keywords"
},
{
"include": "#strings"
},
{
"include": "#registers"
}
],
"repository": {
这时再按F5调试。打开指定后缀名的文件,输入%rax,就可以看到高亮的效果了。