Module: Ibex::Codegen::Dot

Defined in:
lib/ibex/codegen/dot.rb,
sig/ibex/codegen/dot.rbs

Overview

Renders Graphviz DOT from Automaton IR.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.escape(value) ⇒ Object



37
38
39
# File 'lib/ibex/codegen/dot.rb', line 37

def escape(value)
  value.gsub("\\", "\\\\").gsub('"', '\\"')
end

.render(automaton) ⇒ Object

RBS:

  • (IR::Automaton automaton) -> String



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ibex/codegen/dot.rb', line 16

def render(automaton)
  labels = SymbolLabels.build(automaton.grammar)
  lines = ["digraph ibex_automaton {", "  rankdir=LR;", "  node [shape=box];"]
  automaton.states.each do |state|
    attributes = ["label=\"State #{state.id}\""]
    attributes << "color=red" unless state.conflicts.empty?
    lines << "  state_#{state.id} [#{attributes.join(', ')}];"
    state.transitions.each do |symbol_id, target|
      label = escape(symbol_name(labels, symbol_id))
      lines << "  state_#{state.id} -> state_#{target} [label=\"#{label}\"];"
    end
  end
  lines << "}"
  "#{lines.join("\n")}\n"
end

.symbol_name(labels, id) ⇒ Object



42
43
44
# File 'lib/ibex/codegen/dot.rb', line 42

def symbol_name(labels, id)
  labels.fetch(id) { raise Ibex::Error, "missing grammar symbol id #{id}" }
end

Instance Method Details

#self?.renderString

RBS:

  • (IR::Automaton automaton) -> String

Parameters:

Returns:

  • (String)


16
# File 'sig/ibex/codegen/dot.rbs', line 16

def self?.render: (IR::Automaton automaton) -> String