I’m trying to write a set of syntax rules for matching a nestable syntax. Basically, at the simplest level, everything is either a token or a bracket. Here’s an example of such a syntax:
[
[a b c]
[1 2 3]
[foo
[bar baz]]
]
I want the first element of each “list” to be colored, so in this case, a, 1, foo, and bar. I also want the second element of each list to be colored, but in a different way. In this case, b, 2, and baz. I don’t care about anything else. There are no delimiters between items besides whitespace.
How can I do this with the vim pattern system? I’ve managed to get a simple system working, but it handles nested lists poorly. The trouble I’m having is reliably matching the first and second items of each list separately, without screwing with the other ones.
Any help?
The following set of rules highlights your syntax:
The
FirstTupleregion matches tuples, of either tokens or of other tuples, that are the first in their parent tuple;OtherTupleis used for all other tuples. This is done via mutually exclusive lookbehinds, and proper nesting is handled automatically.FirstTokenuses a positive lookbehind to only match at the beginning of a region.FirstTokenandFirstTupleuse thenextgroupattribute to attempt to matchSecondTokenimmediately after themselves, ignoring whitespace, newlines, and empty lines.SecondTokenuses thecontainedattribute to avoid matching everywhere by itself.