user1709175
user1709175

Reputation:

Vi isn't handling curly braces in my perl script correctly

Vi gets messed up with my perl script! While { at the end of the code matches none of the closing curly brackets, {s at the end of the lines 27 and 28 match the same } at 30.

Here is the screen video of how Vi treats my script behavior.

mini-screencast demonstrating the issue
(source: abbasinasab.com)

Here is also my troublemaker piece of code:

#CODE                                                                                                               
while ($data =~ m{                                                                                                      
    ^foo_\s+ $X \s* \{                                                                                                  
        ( (?: [^{}]+ | \{(?1)\} )* )                                                                                    
    \}                                                                                                                  
}mgx)                                                                                                                   
{                                                                                                                       
    my $Y = $1;                                                                                                         
    next if $Y !~ m{                                                                                                    
        bar_$Z \s* \{                                                                                                   
            ( (?: [^{}]+ | \{(?1)\} )*? )                                                                               
        \}                                                                                                              
    }mx;                                                                                                                

    my $DO = $1;                                                                                                        
    #CODE                                                                                                               
} 

My questions are:

  1. How and Why couldn't Vi handle the curly braces in this situation.
  2. How can I rewrite my ugly-written piece of code to avoid this confusion for Vi.

Upvotes: 3

Views: 296

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172600

There are two pieces of functionality in Vim that can be fooled by complex syntaxes:

  • The 'matchpairs' highlighting and jumping uses an internal heuristic. This can be slightly influenced (cp. :help cpo-M):
:set cpo+=M

With this, the % correctly jumps to the expected closing brace (unless you have a plugin like matchpairs.vim override the % command). Also note that the 'cpoptions' setting is global, so this may adversely affect other filetypes.

  • The syntax highlighting is regexp-based. As such, it will fail with corner cases and complex syntaxes (like C++ and Perl) which cannot be faithfully modeled with regular expressions (but require a custom parser).

In general, I would avoid rewriting the code just so that the editor will be happy. Other people may use different editors, and soon the code gets hugely distorted with various such "workarounds". The only exception is when the code is obviously complex and cumbersome, and general readability would improve from a restructuring.

Upvotes: 4

Related Questions