Class: AsciiChem::Linter::BracketBalanceCheck

Inherits:
Base
  • Object
show all
Defined in:
lib/asciichem/linter/bracket_balance_check.rb

Overview

Verifies that group brackets are matched. The grammar already enforces this at parse time, but the linter adds defence in depth in case future grammar changes or direct model construction produce malformed trees.

Instance Method Summary collapse

Methods inherited from Base

register

Instance Method Details

#run(formula) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/asciichem/linter/bracket_balance_check.rb', line 12

def run(formula)
  diagnostics = []
  walk(formula) do |node|
    next unless node.is_a?(AsciiChem::Model::Group)

    case node.bracket
    when :paren  then check_pair(node, "(", ")", diagnostics)
    when :square then check_pair(node, "[", "]", diagnostics)
    when :brace  then check_pair(node, "{", "}", diagnostics)
    end
  end
  diagnostics
end