Can someone explain or link to valid explanation information on the syntax highlighting mechanism. In particular I am having having a hard time understanding how rules are applied.

Imagine the following rule

syn match paragraph "^\s*[^\n]*\%(\n[^\n]\+\)\n\n\+"

If the applied to a (UNIX) file with of 100 lines. I would expect the pattern to be applied at most 100 times. In reality it is applied much more times.

I have a Markdown syntax file that correctly detects and supports all Pandoc features (and a couple more). But I am confronted with rules being executed tens of thousands times.

Any ideas ?

  • 1
    How did you measure it was applied that many times? Anyway, I would suggest not to use multiline regexes in a syntax match. – Maxim Kim Feb 6 at 16:20
  • Using syntime to verify (cf. he syntime). Multi-line regexes are not the issue here, though I did do some testing to see how they impacted the search -- I also tried changing the regex engine (\%#). My concern is how a regex is called, and why it is called so many times. – JM. Marcastel Feb 6 at 20:33

Found a way round my problem... though I still don't fully understand the underlying logic. So all comments welcome.

By adding a rule that simply matches the start of line and making my previously top-level rules (block level as opposed to inlined in block) contained, I dramatically reduced the number of occurrences, and hence the syntime.

syn match mdStartOfLine /^/ contains=@mdBlock

My mdStartOfLine rule is effectively only matched once per line, as opposed to the paragraph rule in my original post which was tried thousands of times (the number of trials not apparently linked to the number of lines in the file to hilight).

A (supposedly) side effect of this approach being that I can now use region rather than match, and consequently don't need to check for the block separator (\n\n\+). This significantly simplifies my syntax file.

A lesson learnt for me in this exercise is that a syntax highlighting grammar is not a parser; it doesn't need to understand, simply to detect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.