Multiple types of opening braces when matching pairs with the tokenizer/ext

Whilst I was testing DocBlox on the Solar Framework I found a bug in the part of the application where I match the curly brace pairs of structures (like classes, functions, methods, etc). Every time a variable was imported in a string using the {$var} notation my algorithm thought that the function/method had ended at the closing brace of that string.

To my surprise I found out that the issue was caused by the fact that the Tokenizer has three different tokens for recognizing an opening curly brace and only one for a closing curly brace.

Should you ever be in a position that you want to find curly brace pairs in your code you can test for the following:

  1. a single ‘{‘ (thus without token type)
  2. an array whose first element (thus token type) is T_CURLY_OPEN (representing the sequence ‘{$’) or
  3. an array whose first element (thus token type) is T_DOLLAR_OPEN_CURLY_BRACES (representing the sequence ‘${‘)
  4. a single ‘}’

It took me quite some time to find out what went wrong here and I hope this blog post will help someone to find the answer to his problem.

Thank you for reading!

Leave a Reply

Your email address will not be published. Required fields are marked *