Writing Mode based on Parser, not only Lexer

I am writing a CodeMirror mode for the language I am designing; I would like to do syntax-highlighting not only based on the lexer, but on the parsed syntax tree. I can make it work quite nicely by registering a handler on content change, parsing upon content change, and then let the mode access the parsing result when lexing.

Now, when a user enters something in line, say, 20, this may actually influence how line 10 should be coloured. Is there a way to make CodeMirror start lexing from an earlier line on, not just from the currently edited line?

Thanks,

Steven Obua

The best way to handle this is to write your parser with a stream interface and some way to save its state, that way you can straightforwardly (and efficiently) wrap it in a CodeMirror mode without any code duplication.

If that’s not an option, yes, you can pass a syntax tree to a CodeMirror mode and have it use that, but this is fragile, clunky, and not very efficient. You do, for example, have to re-highlight the whole file every time it changes. I’d recommend just writing a superficial tokenizer as a CodeMirror mode instead.

Thanks for your answer!

A simple tokenizer will unfortunately not work sufficiently well enough, so I will have to go with the re-highlight option. Is there a way to tell CodeMirror only to re-highlight a certain range of lines (I would guess not, as that is outside of what normal lexers need / can provide)? How do I invoke the re-highlighting best, would I register a new overlay each time I register a change, or is there a better way?

Cheers,

Steven

Just to add a final remark how my problem can be solved: to rehighlight, I just call CodeMirror.setOption(“mode”, “mymode”). This apparently triggers a rehighlighting, and so I call this after every document change.