Sublime Forum

Context Dependence in syntax highlighting

#1

So, I have this language definition that is largely working.

It does some fun things. It only highlights the language’s primitives inside of a procedure definition. It only highlights the ‘then’ keyword when it is terminating an if statement(as is customary in the language).

It handles loops similarly, but I’ve run into an issue. I’d like it to highlight things like continue or break only inside of a loop, but if those things are in an if block, they aren’t highlighted because the scope doesn’t include that.

Do any of you have any advice for this? I can break up the encapsulating scope so that the if statements inside a loop are “special”, but I’d rather not do this as it strikes me as being less elegant than somehow crawling up the context-stack and checking to see if there’s an enclosing loop.

0 Likes

#2

You’ll have to make a few compromises regarding accuracy. In this case, I would just highlight break and continue statements as regular keywords, since things would get hairy too quickly. For things like the case here people can use linters, where this is much easier to track.

0 Likes

#3

I’m assuming that we’re talking about language that allows complex nested structures, such as a break inside an if block inside a loop. If that’s the case, then there’s no reasonable way to allow break statements only within a loop body without basically making a duplicate set of all of the contexts that could be used within a loop and could contain a break.

In most cases, I would then say that this isn’t worth it. If having a break outside a loop is illegal, then don’t worry about it; just mark break statements in the same way regardless of whether or not you’re in a loop. It’s not necessary to mark invalid syntax as invalid; to do so can make the syntax definition much more complicated for little or no real benefit.

However, there could conceivably be a language where break could be a control keyword inside a loop but have some other meaning outside a loop. I am not personally aware of any such language, but if one were to exist, then it would make sense for the syntax definition to respect this.

If you must do this, then yes, you do need multiple contexts. I have some untested ideas about how this might be accomplished without having to copy-paste code (and maintain duplicate code). Is this truly necessary for the language you’re working on? What language is it?

2 Likes

#4

Well, I think that you both are right about this then.

The language in question is the one created in the early nineties/late eighties for MUCKs, Multi User Forth, or MUF.

It’s not exactly easy to lint, since, being a Forth, the grammar is rather… minimal, nearly all errors are caught at runtime, which, in game, having an editor not far above ed’s capabilities, makes it rather annoying to write normally.

So I’ll just mark break and continue as keywords that can occur anywhere.

In any case, thanks for both of your help.

0 Likes