Vim语法文件 - 在函数名末尾引用[dg编程语言]

时间:2022-08-30 15:06:36

I'm trying to write a syntax file for the dg programming language. It's not very complex, and it's derived from Python, so I copied from the Python syntax file.

我正在尝试为dg编程语言编写语法文件。它不是很复杂,而且它是从Python派生的,所以我从Python语法文件中复制了。

The problem is that function names can have a single quote at the end:

问题是函数名称最后可以有一个引号:

a = list' 1 2 3  # [1, 2, 3]

Obviously from the quote onwards, the line is highlighted as a string. The string definition, which I have taken from the Python syntax file, is this:

显然,从引用开始,该行突出显示为字符串。我从Python语法文件中获取的字符串定义是这样的:

syn region dgString start=+[bB]\='+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=dgEscape,dgEscapeError
syn region dgString start=+[bB]\="+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=dgEscape,dgEscapeError
syn region dgString start=+[bB]\="""+ end=+"""+ keepend contains=dgEscape,dgEscapeError
syn region dgString start=+[bB]\='''+ end=+'''+ keepend contains=dgEscape,dgEscapeError

syn match  dgEscape +\\[abfnrtv'"\\]+ display contained
syn match  dgEscape "\\\o\o\=\o\=" display contained
syn match  dgEscapeError "\\\o\{,2}[89]" display contained
syn match  dgEscape "\\x\x\{2}" display contained
syn match  dgEscapeError "\\x\x\=\X" display contained
syn match  dgEscape "\\$"

I'd like to have the quote at the end of function names not highlighted (normal text). How can I accomplish this?

我希望功能名称末尾的引号不突出显示(普通文本)。我怎么能做到这一点?

For reference:

以供参考:

1 个解决方案

#1


1  

In the default python syntax highlighting, it looks as though list is part of the pythonBuiltin syntax group. (See the example under :help synID() for how to check.) So I tried

在默认的python语法高亮显示中,看起来list是pythonBuiltin语法组的一部分。 (请参阅下面的示例:help synID()以了解如何检查。)所以我试过了

:syn keyword pythonBuiltin issubclass iter len list locals map max nextgroup=pythonFoo
:syn match pythonFoo /'/ contained

and then your sample text

然后是你的示例文本

a = list' 1 2 3  # [1, 2, 3]
b = list 'a' 'b' 'c'

is highlighted correctly: list' is recognized as a Builtin followed by a Foo, and 'a' is recognized as a String.

正确突出显示:list'被识别为Builtin后跟Foo,'a'被识别为String。

The idea is that my newly created syntax item is given priority after vim has finished processing one of the listed keywords, so the ' is gobbled up by the new group and is not available as the start of a string.

我的想法是,在vim完成处理列出的一个关键字之后,我新创建的语法项被赋予优先级,因此'被新组吞噬并且不能作为字符串的开头。

In principle, you can do the same thing with user-defined functions. The problem is coming up with a syntax definition that just matches functions. The default python syntax file seems to highlight function definitions but not usage. (More precisely, a function name preceded by def or class or @.)

原则上,您可以使用用户定义的函数执行相同的操作。问题出现了一个只匹配函数的语法定义。默认的python语法文件似乎突出显示了函数定义,但没有使用。 (更确切地说,函数名称前面带有def或class或@。)

Another approach is to modify the definition of a string by inserting a zero-width start-of-word atom before the optional [bB] or similar character, or insisting that the string not be preceded by a word character. Dealing with just the single-quote version of a string (the first line of your example) that would be

另一种方法是通过在可选的[bB]或类似字符之前插入零宽度的字首字母原子来修改字符串的定义,或者坚持字符串不以字符开头。只处理字符串的单引号版本(示例的第一行)

:syn region dgString start=+\%(\w\@<!\|\<[bB]\)'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=dgEscape,dgEscapeError

#1


1  

In the default python syntax highlighting, it looks as though list is part of the pythonBuiltin syntax group. (See the example under :help synID() for how to check.) So I tried

在默认的python语法高亮显示中,看起来list是pythonBuiltin语法组的一部分。 (请参阅下面的示例:help synID()以了解如何检查。)所以我试过了

:syn keyword pythonBuiltin issubclass iter len list locals map max nextgroup=pythonFoo
:syn match pythonFoo /'/ contained

and then your sample text

然后是你的示例文本

a = list' 1 2 3  # [1, 2, 3]
b = list 'a' 'b' 'c'

is highlighted correctly: list' is recognized as a Builtin followed by a Foo, and 'a' is recognized as a String.

正确突出显示:list'被识别为Builtin后跟Foo,'a'被识别为String。

The idea is that my newly created syntax item is given priority after vim has finished processing one of the listed keywords, so the ' is gobbled up by the new group and is not available as the start of a string.

我的想法是,在vim完成处理列出的一个关键字之后,我新创建的语法项被赋予优先级,因此'被新组吞噬并且不能作为字符串的开头。

In principle, you can do the same thing with user-defined functions. The problem is coming up with a syntax definition that just matches functions. The default python syntax file seems to highlight function definitions but not usage. (More precisely, a function name preceded by def or class or @.)

原则上,您可以使用用户定义的函数执行相同的操作。问题出现了一个只匹配函数的语法定义。默认的python语法文件似乎突出显示了函数定义,但没有使用。 (更确切地说,函数名称前面带有def或class或@。)

Another approach is to modify the definition of a string by inserting a zero-width start-of-word atom before the optional [bB] or similar character, or insisting that the string not be preceded by a word character. Dealing with just the single-quote version of a string (the first line of your example) that would be

另一种方法是通过在可选的[bB]或类似字符之前插入零宽度的字首字母原子来修改字符串的定义,或者坚持字符串不以字符开头。只处理字符串的单引号版本(示例的第一行)

:syn region dgString start=+\%(\w\@<!\|\<[bB]\)'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=dgEscape,dgEscapeError