class Haml::Parser

def balance_tokens(buf, start, finish, count: 0)

Unlike #balance, this balances Ripper tokens to balance something like `{ a: "}" }` correctly.
def balance_tokens(buf, start, finish, count: 0)
  text = ''.dup
  Ripper.lex(buf).each do |_, token, str|
    text << str
    case token
    when start
      count += 1
    when finish
      count -= 1
    end
    if count == 0
      return text, buf.sub(text, '')
    end
  end
  raise SyntaxError.new(Error.message(:unbalanced_brackets))
end