code: def _balance_braces(tokens, filename=None):
      
      depth = 0
  
      for token, line, quoted in tokens:
          if token == '}' and not quoted:
              depth -= 1
          elif token == '{' and not quoted:
              depth += 1
  
          # raise error if we ever have more right braces than left
          if depth < 0:
              reason = 'unexpected "}"'
              raise NgxParserSyntaxError(reason, filename, line)
          else:
              yield (token, line, quoted)
  
      # raise error if we have less right braces than left at EOF
      if depth > 0:
          reason = 'unexpected end of file, expecting "}"'
          raise NgxParserSyntaxError(reason, filename, line)
docs: Raises syntax errors if braces aren't balanced