regexescapinggeditgtksourceview

gtksourceview keyword escaping


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


Solution

  • 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.