Wednesday, February 29, 2012

How to limit syntax rule match to the outside of a region?

Hello,

I'm trying to write a syntax file for a language that looks like this:

var1 = foo;
block name {
var2 = bar;
var3 = true;
}

The problem I have is that some variables can occur only at the global
scope, i.e. outside of any block, while others can only occur inside a
block. Ideally I'd like to highlight occurrences of the variables of the
first kind inside a block and that of the second kind at global scope as
errors but I'd also settle for not highlighting them at all. Unfortunately
I don't see how to do it. Currently I have (simplified):

syn keyword GlobalVar var1;
syn keyword BlockVar var2 contained;
syn region Block start="{" end="}" contains=ALLBUT,GlobalVar transparent

which more or less works for "var2" but "var1" is highlighted even if it
occurs inside the block. I'd like to understand why does this happen and
how to avoid it -- as well as implementing highlighting of the variables in
the wrong places as errors. Does anybody know how could this be done?


Also, on a similar topic, what is the best way to highlight the values of
the variables? I.e. for some of them only a limited set of values is valid,
for example there are boolean variables which can only be "true" or "false".
Currently I do

syn keyword BoolValue false true contained
syn match BlockVar "var3 *=.*$"he=s+4 contains BoolValue

but this is quite inconvenient because I need to compute the highlighting
index (4 above) for each variable and just doesn't look right. I'm sure
there must be a better way of doing what I want but what could it be?

Thanks in advance for your help!
VZ

No comments: