0

I am trying to create a custom syntax that has a structure similar to the following

Title String:
{
   ...,
   ...,
   ...
}

Title String2:
{
   ...,
   {
      ...,
      ...,
      ...
   }
   ...,
   ...,
   ...
}

I have been able to write syn match and syn region statements that detect everything within the { ... } regions, however I have not been able to come up with one that will match the Title Strings.

Here is my region statement:

syn region dbgMessage start="{" end="}" contains=ALLBUT,dbgMessageHeader

I attempted to add something like this to detect the Title Strings, which I want to be everything/anything up to but not including the opening bracket.

syn match dbgMessageHeader "\v.\{-}\ze(\{)"

My reasoning:

  • .\{-} should consume every character and be non greedy
  • \ze(\{) should look ahead for an opening bracket and stop when it finds one

A bonus challenge is that it would be great if this syntax could correctly detect everything if I get the code in a flattened state, ex:

Title String: { ..., ..., ... }
Title String2: { ..., { ..., ..., ... } ..., ..., ... }

Again, my current implementation can correctly match everything inside the brackets in both flat and formatted states, so it would be great if I could figure something out that would also match the title strings in both cases.

See something that I'm missing?

3
  • This syntax looks suspiciously like JSON. Have you tried using that? Commented Aug 5, 2016 at 16:56
  • How deep can it nest? Commented Aug 5, 2016 at 17:17
  • @Laurel Theoretically as deep as it needs to. In practice I've only seen 4 levels max. Commented Aug 5, 2016 at 17:31

1 Answer 1

1

I'm not very familiar with Vim's regex syntax, but this regex will work for most Perl-based flavors (with the s modifier):

\s([^\s]+):.*?\{

To translate that into Vim, it should be (I think):

\s\([^\s]\+\):\_.\{-}{

Note that I am using \_. instead of . to make it act like the s modifier.

Also note that "very magic mode" (whatever that is) will probably mess this up.


It's not easy (if possible) to get the "flat version", since Vim only supports DFA matching.

Sign up to request clarification or add additional context in comments.

5 Comments

Is this still valid even though the TitleString isnt recursive? It only appears at the beginning of a top-level { ... } block
@wKavey It might be possible... Does it happen before every {?
It happens before every top level one (as shown in the question)
@wKavey OK, edited. See if this works; I'm not familiar with Vim regexes.
Thanks, .*? didn't work in vim, I tried it before googling, .\{-} did work, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.