2

I have matched parentheses, but highlighting failed as it transcends up the recursion stack.

Example named.conf simplified syntax

I just need to know if there is a solution to tracking pairs of delimiters during Vim highlight syntax of nested curly braces.

Details

Was adapting ISC Bind9 named configuration file using their documentation full of BNF syntax into Vim syntax.

One is the address_match_list whose BNF is:

address_match_list :=  ( name | number | address_match_list ) ;

It's easy to make that Vim syntax for above BNF for 1st time.

Now, the actual usage of address_match_list in a recursive manner, it lost the tracking of closing curly braces and no longer highlights on the way back up the recursion stack (if there is any).

syn match xyz_Semicolon contained /;/ skipwhite skipempty
\ containedin=xyz_AML

syn match xyz_Number contained "\<\d\{1,10}\>" skipwhite skipempty
\ nextgroup=xyz_Semicolon
\ containedin=xyz_AML

" { ... }  ;
syn region xyz_AML contained start=+{+ end=+}+ skipwhite skipempty
\ nextgroup=
\    xyz_Number,
\    xyz_Semicolon

" <acl_name> ( <number> | { ... } ) ;
syn match xyz_ACLIdent contained /\<[0-9a-zA-Z\-_]\{1,63}\>/ skipwhite
\ nextgroup=
\    xyz_AML,
\    xyz_Number

syn match xyz_Keywords /\_^\s*\<acl\>/ skipempty skipwhite
\ nextgroup=xyz_ACLIdent

For the condensed (but working) config file:

acl e 127;
acl internal-net {
    127; 10; 192;
};
acl an_internal_vps_ntwk {
    192;
    {
        172;
        {
            5;
        };
        6;
    };
    112;
};  

All files can be found in PasteBin

2

After reading the syn-extend and the use of keepend and extend keywords, I used Error for my first region syntax and Todo for that last character after the region (semicolon). My recursive region is foreground white-colored.

With a bit of a bit of trial and error, got it to work:

Working 2-tiered recursion of Vim syntaxes

Then started to pare-down all non-essential keywords until it just works.

syn match xyz_Semicolon contained /;/

syn match xyz_AML_Nested_Semicolon contained /;/ skipwhite skipempty
\ nextgroup=
\    xyz_AML_Placeholder

syn match xyz_Number "\<\d\{1,10}\>" skipwhite skipempty
\ nextgroup=
\    xyz_AML_Nested_Semicolon

syn region xyz_AML_Placeholder contained start=+{+ end=+}+ keepend extend
\ contains=
\    xyz_Number,
\    xyz_AML_Nested_Semicolon
\ nextgroup=
\    xyz_AML_Nested_Semicolon

" acl <acl_name> { ... } ;
syn region xyz_AML contained start=+{+ end=+}+  skipwhite skipempty
\ contains=
\    xyz_Number,
\    xyz_AML_Nested_Semicolon
\ nextgroup=
\    xyz_Semicolon

" acl <acl_name> { ... } 
syn match xyz_ACLIdent contained /\<[0-9a-zA-Z\-_]\{1,63}\>/ skipwhite skipempty
\ nextgroup=
\    xyz_AML

" acl <acl_name> ...
syn match xyz_Keywords /acl/ skipwhite skipempty
\ nextgroup=xyz_ACLIdent

syn match xyz_Keywords /test/ skipwhite skipempty
\ nextgroup=xyz_Number

Analysis:

The key thing during your Vim syntax debugging effort (other than the constant reloading) is to make use of:

  • Error highlight to color your region as red and
  • a bright color (Todo) for its last character after the region (semicolon).

While you debug (or randomly try) new syntax, and you get that last character to stay properly colorized as xyz_Semicolon, you will then be able to rock the rest of the syntaxes until you're happy, knowingly that you preserved your multiple nesting (recursive syntax).

In my case, I had two cases of near-duplication of the same region syntax. Most of you wouldn't be facing this (unless you're a NeoVim or a Python Vim syntax maintainer). Reason is I need two similar region syntaxes, is to perform a "tee-off" of limited capability before going into full-recursion syntax.

Full recursion is the following BNF:

address_match_list :== ( number | address_match_list ) ;

I had to wrap the address_match_list with:

acl_keyword :== 'acl'
acl_identifier :== [a-zA-Z0-9_\.\-]{1,63}
acl_AML :== '{' + address_match_list + '}' + ';'

Working test files are:

| improve this answer | |

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.