regexgtkpcregtksourceview

How can I match a number with an optional letter suffix in a GtkSourceView language specification?


I'm writing a new GtkSourceView language specification; following the information found in the tutorial and on other internet resources. The language that I'm defining can take a digit as:

  1. One or more digit characters
  2. Optionally an appened (e.g. at the end) letter L, l, N, or n.

Examples:

1000
1234L
987654321n

Please note that this is just one of a few possible definitions.

So far, I've written (approximately (Other parts of the definition ommitted)):

<context id="number" style-ref="decimal" end-at-line-end="true">
    <start>(\d+)([NnlL]?)</start>
    <end>\D</end>
</context>

I interpret that as: _A number starts with one or more digit characters, optionally followed by any of the follwing: N n l L, and ends when a non digit number is encountered (e.g. space, letter.)

Unfortunately this doesn't quite work, since appending any of the specified characters (or any character for that matter) causes it to fail (in this context, that means not highlight.)

Any ideas on how to match this pattern? Thanks in advance! :)


Solution

  • You don't need a Container Context; that's for things like string literals and comments, where you identify it by beginning and ending patterns, and it may contain other contexts. (For example, a string literal might contain escape sequences like \n, which you might want to highlight differently, as in this screenshot.) Your "number" tokens can be matched completely with a single, simple regex, which means you want a Simple Context:

    <context id="number" style-ref="decimal">
        <match>\b\d+[NnlL]?\b</match>
    </context>
    

    Disclaimer: I've never used GtkSourceView, but I've worked with other regex-based syntax highlighters, including EditPad Pro's and one I wrote myself, in Java.