class CodeRay::Tokens
def fix
Ensure that all begin_group tokens have a correspondent end_group.
def fix raise NotImplementedError, 'Tokens#fix needs to be rewritten.' # tokens = self.class.new # # Check token nesting using a stack of kinds. # opened = [] # for type, kind in self # case type # when :begin_group # opened.push [:begin_group, kind] # when :begin_line # opened.push [:end_line, kind] # when :end_group, :end_line # expected = opened.pop # if [type, kind] != expected # # Unexpected end; decide what to do based on the kind: # # - token was never opened: delete the end (just skip it) # next unless opened.rindex expected # # - token was opened earlier: also close tokens in between # tokens << token until (token = opened.pop) == expected # end # end # tokens << [type, kind] # end # # Close remaining opened tokens # tokens << token while token = opened.pop # tokens end