Help with creating a grammar with escaping brackets


#1

For example, I create an inline match for latex which includes the build-in latex grammar.

	"latex-inline":
		name: "markup.latex.inline"
		patterns: [{include: "text.tex#math"}]
		begin: "(\\$)"
		end: "(\\$)"
		beginCaptures:
			1: name: "punctuation.boundary.latex"
		endCaptures:
			1: name: "punctuation.boundary.latex"

However, this does not work well as it cannot correctly highly the following,

Capture

All string inside the outer pair of $ should be colored green. This is a valid latex with nested $ … {$…$} … $, where the brackets ‘{’ ‘}’ help escape the inside ‘$’ pair. However, my grammar above obviously cannot capture this and wrongly recognize it as two pieces of latex code. How do I make a correct highlight with such escaping brackets?


#2

You would also need to capture the {} in patterns. This hides the contents from the outer rules. I think just { include: "text.tex.latex" } might work.

On a different note, making a LaTeX grammar is quite difficult. I found making one was a long process of trial and error. I can help more if you give more context about what this is / how it’s supposed to work.


#3

I figured this out. Just include another capture in the “patterns” with the right regex to escape the brackets

"latex-inline":
	name: "markup.latex.inline.restructuredtext"
	patterns: [{
		name: "meta.latex.inline.restructuredtext"
		match: "(([^\\$]*(\\{.+\\})+[^\\$]*)|([^\\$]+))"
	},{include: "text.tex#math"}]
	begin: "(\\$)"
	end: "(\\$)"
	beginCaptures:
		1: name: "punctuation.definition.latex.restructuredtext"
	endCaptures:
		1: name: "punctuation.definition.latex.restructuredtext"