I have a simple "language" (similar to template languages and simple markup languages such as BBcode — basically it's just normal text with some variables, tags and similar features) and I want to highlight its syntax.
Here is the thing I am stuck with.
There are variables, they are enclosed with $ sign ($var1$). I highlight them with this rule:
<RuleSet name="VariableSet">
<Rule color="Variable">
\$\S+?\$
</Rule>
</RuleSet>
Some area around a variable can be surrounded with { } characters.
In other words: some variables can have its "region", it starts from the first { before the variable and ends at the first } after the variable. Multiple variables can't be in one region, so in cases like { $var1$ $var2$ }
there is no any regions, { } are treated like normal characters and ignored. It is not a scope like function and local scopes in C-style languages.
Here is an example:
[b]lorem ipsum[/b] $var0$ dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.
{ // <— not nighlighted
{ // <— highlighted
ut enim ad minim veniam, quis nostrud
$var1$
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
} // <— highlighted
} // <— not nighlighted
// all brackets below also should be not highlighted
duis aute { $50, 25$} irure { dolor } excepteur $var2$ sint occaecat cupidatat non
proident, sunt in culpa qui mollit anim id { est $var2$ laborum.
{ $var3$ $var4$ }
First I tried to solve this using two Rule
regexps (for { and }, of course with this approach it is impossible to skip cases with unclosed brackets like { $var$
or $var$ }
but that's not a big problem). However I found out that Rule
works only within one line.
Then I tried Span
like this:
<Span color="VariableAreaDelimiter" multiline="true">
<Begin>
\{(?!\{.*?\$\S+?\$)(?=.*?\$\S+?\$)
</Begin>
<End>
\}
</End>
<RuleSet>
<Import ruleSet="VariableSet"/>
<Rule foreground="Black" fontWeight="normal">
.
</Rule>
</RuleSet>
</Span>
Some of the problems:
Although the
multiline
istrue
regexps inBegin
andEnd
don't work for multiple lines. So it doesn't match this:{ $var$
If there is no closing bracket the span takes everything until end of document. That's why I added the
.
rule.