Sublime Forum

[syntax] How to match block when the C preprocessor is unbalancing brackets?

#1

[syntax] How to match block when the C preprocessor is unbalancing brackets?

This is my syntax: https://github.com/evandrocoan/SublimeAmxxPawn/blob/d69028089ce379aea51a520e74eb5a54b518bc03/AmxxPawn.sublime-syntax#L204-L216

It is working fine, if the C preprocessor do not mess with it. Example, this will unbalance the brackets, from the line 359 up it will show one extra scope meta.block which does not popped, as on the preprocessor define there is one opening unclosed bracket at the line 352.

337: public own_type:on_damage( id )
338: {
339:     new attacker = own_type: get_user_attacker( id )
340: 
341: #if defined DAMAGE_RECIEVED
342:     // id should be connected if this message is sent, but lets check anyway
343:     if( is_user_connected( id )
344:         && is_user_connected( attacker ) )
345:         if( get_user_flags( attacker ) & ADMIN_LEVEL_H )
346:         {
347:             new damage = read_data( 2 )
348:             ShowSyncHudMsg( id, g_MsgSync2, "%i^n", damage )
349: #else
350:         if( is_user_connected( attacker )
351:             && ( get_user_flags( attacker ) & ADMIN_LEVEL_H ) )
352:         {
353:             new damage = read_data( 2 )
354: #endif
355:             ShowSyncHudMsg( attacker, g_MsgSync, "%i^n", damage )
356:         }
357:     hi_girl()
358: }
359: 
360: public on_damage( id )
361: {
362:     get_user_attacker( id )
363: }

How to drop the scope for the unclosed bracket?

I am capturing blocks like this:

  # "PAWN Function"
  pawn_function:
    - include: function_definition
    - match: '{'
      scope: keyword.brackets.AmxxPawn
      push:
        - meta_scope: meta.block.AmxxPawn
        - include: function_call
        - match: '}'
          scope: keyword.brackets.AmxxPawn
          pop: true
        - include: pawn_function
    - include: pawn_function_includes
0 Likes

#2

It’s not possible for the parser to detect this sort of macro programming with pre-processors. You’ll need to refactor this section in order to achieve correct highlighting.

0 Likes

#3

I wonder if it could be done by self-embedding. In other words, use the external includes mechanism to include Pawn (from within the Pawn sublime-syntax) and use the with_prototype directive to enforce scope popping once the closing preprocessor directive is hit.

0 Likes

#4

Are you saying I need to change the way the code is to fix the highlighting?
When I set to the built-in C syntax, this problem does not happens. So, what is C doing I am not?
I Iooked into the C syntax, could not find too much.

0 Likes

#5

The C syntax has a number of contexts to handle funky preprocessor usage. It moves in and out of special contexts for if/else branches of the preprocessor. I’ll link to them later when I am back at a computer.

2 Likes