Sublime Forum

Changing syntax regex?

#1

Hi,

Short: I’m trying to change a syntax definition so as to stop capturing string.quoted.single.c if the ’ is preceded by a #

Description:
I am switching from Textmate to Sublime Text 2 for LPC programming. This uses mostly C syntax, with a notable exception: we have a ‘closure’ which has the format of:

#'func_name

The problem I’m having is that the ’ in #’ gets captured by the string.quoted.single.c bit of syntax highlighting.

I’d avoided this in Textmate using the following change in the syntax definition:

{ name = 'string.quoted.single.c'; begin = "(?<!#)'"; end = "'"; beginCaptures = { 0 = { name = 'punctuation.definition.string.begin.c'; }; }; endCaptures = { 0 = { name = 'punctuation.definition.string.end.c'; }; }; patterns = ( { include = '#string_escaped_char'; } ); },

I tried altering both the C.tmlanguage file with the (?<!#) addition, but this ends in a parsing failure. I’ve tried editing the user specific syntax with a snippet to override string.quoted.single.c.

{ "name": "LPC Single-Quote", "scopeName": "source.c", "fileTypes": "c""h"], "patterns": {"name": "string.quoted.single.c", "begin": "(?<!#)'", "beginCaptures": { "0" : { "name" : "punctuation.definition.string.begin.c" } }, "endCaptures" : { "0" : { "name" : "punctuation.definition.string.end.c" } }, "patterns" : { "include" : "#string_escaped_char"} ], "end" : "'" } ], "uuid": "2aeaa0d9-69e2-46a5-b631-792d8e0d8182" }

I’ve tried editing a new snippet with that as well. I’ve also tried creating a new snippet called string.closure.c which tried to match #’ and capture that over the string.quoted.single.c and format it a different colour. I’ve tried placing this snippet in C.sublime-settings. None of these have been successful – and I’m sure it’s because I’m doing something wrong.

Can someone lend a hand? I just want sublime text “C” parser to ignore #’ rather than thinking it’s the beginning of a string.

Thanks

Zyzzyr

0 Likes

#2

Ok, well after much hacking I finally got it to work.

I replaced the line

			<string>'</string>

with

			<string>(?&lt;!#)'</string>

which is essentially what I was doing in textmate, but I replaced the < for < due to the use of xml.

I could still use some help if anyone has ideas, so that I can capture #’ and highlight that differently.

Cheers

Zyzzyr

0 Likes

#3

You might be able to use AAAPackageDev to convert your custom Textmate syntax definition from JSON to XML.

If that doesn’t work, you can still translate the change you made:

<dict> <key>name</key> <string>string.quoted.single</string> <key>begin</key> <string>(?&lt;!#)'</string> <key>end</key> <string>'</string> <key>beginCaptures</key> <dict> <key>0</key> <dict> <key>name</key> <string>punctuation.definition.string.begin.c</string> </dict> </dict> <key>endCaptures</key> <dict> <key>0</key> <dict> <key>name</key> <string>punctuation.definition.string.end.c</string> </dict> </dict> <key>patterns</key> <array> <dict> <key>include</key> <string>#string_escaped_char</string> </dict> </array> </dict>

0 Likes

#4

Another LDMud dev curious if you got this sorted already, Zyzzyr. Any luck?

0 Likes

#5

Sorry for the thread necro, but:
@Zyzzyr As of today I have a syntax definition package (“LPC”) available on package control, for ST3 builds >= 3084.

0 Likes