Sublime Forum

How to write a scope syntax with balanced `'

#1

I am trying to improve on StataEditor’s scope syntax for variable.parameter.stata. In Stata, this scope is called a local, starts with ` and ends with '. There can be an unlimited number of locals inside locals. A simple example would be:

`local' not_local `this is a long local `inside local' still part of local'

Below is part of the package’s original tmLanguage file.

<dict>
	<key>begin</key>
	<string>`(\S(?!'))*`(\S(?!'))*`</string>
	<key>end</key>
	<string>'\S*'\S*'</string>
	<key>name</key>
	<string>variable.parameter.stata</string>
</dict>
<dict>
	<key>begin</key>
	<string>`(\S(?!'))*`</string>
	<key>end</key>
	<string>'\S*'</string>
	<key>name</key>
	<string>variable.parameter.stata</string>
</dict>
<dict>
	<key>begin</key>
	<string>`</string>
	<key>end</key>
	<string>'</string>
	<key>name</key>
	<string>variable.parameter.stata</string>
</dict>

Any help is appreciated.

0 Likes

#2

With the regex below, I am able to capture up to four levels of locals inside locals. Is a regex able to capture an unlimited number of inside locals?

<dict>
	<key>match</key>
	<string>`[^`']*?((`[^`']*?((`[^`']*?((`[^`']*?'[^`']*?)*?)'[^`']*?)*?)'[^`']*?)*?)[^`']*?'</string>
	<key>name</key>
	<string>variable.parameter.stata</string>
</dict>
0 Likes

#3

It’s not possible to do that with a single regex. I am pretty sure there is context-like feature in .tmLanguage too but I am not familiar with it.

If the plugin is for ST 3 and not for other editor, I would suggest the author (or maintainer) convert it into .sublime-syntax format and check https://www.sublimetext.com/docs/syntax.html#selected-examples

1 Like

#4

@jfcherng, thank you for your reply. Unfortunately the package has not been maintained for over three years now. I will need to learn more about YAML if I go that route.

Interstly, the following regex matches the locals correctly when I use ST’s Find field: `([^`’]|(?0))*’. However, when I use it in the .tmLanguage file, ST gives me an error when I try to save the file. Am I forgetting to scape something?

<dict>
	<key>match</key>
	<string>`([^`']|(?0))*'</string>
	<key>name</key>
	<string>variable.parameter.stata</string>
</dict>
0 Likes

#5

ST’s find uses Boost regex lib. The syntax engine for .tmLanguage uses Oniguruma regex. Each implementation has their supported feature, although most common features are supported for sure.

0 Likes

#6

That makes sense. Thank you for clarifying that.

0 Likes

#7

Here’s a naive modified StataEditor.sublime-syntax: https://gist.github.com/jfcherng/16688763368a4b96c3d087dee2cacd2c

The part you interest is

  balanced-backtick-quote:
    - match: '`'
      push:
        - meta_scope: variable.parameter.stata
        - include: balanced-backtick-quote
        - match: "'"
          pop: true
0 Likes