gtksourceview keyword escaping

448 Views Asked by At

In my language a keyword is the literal #list-empty? so I would like to match against this. I am using gtksourceview to provide syntax highlighting, and using the following definition

<keyword>list-empty?</keyword>

However, that matches list-empt and list-empty (but not the literal list-empty?). Which to me would indicate gktsourceview treats what's in <keyword> as a regular expression. So I would like to escape the ? and match it literally. However something like: <keyword>list-empty\?<keyword> doesn't work at all.

https://github.com/espringe/wisp/blob/bf1aed13/resources/wisp.lang#L67

So how do i escape the ? and match against it

2

There are 2 best solutions below

3
On BEST ANSWER

Add this to your keyword context:

<suffix>(?!\w)</suffix>

...then go ahead and escape the question mark:

<keyword>list-empty\?</keyword>

The default suffix is \b, on the assumption that every keyword ends with a word character. Most of yours do, and (?!\w) acts just like \b in those cases, but it also matches after the question mark while still disallowing the next character to be a word character.

3
On

? matches a single character before it

/list-empty?/ would match both list-empt and list-empty.

You might want to use look ahead:

<keyword>(?=list\-empty).+ to match the lines which have < keyword > followed by list-empty.