Module: Ibex::Codegen::Mermaid

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

Overview

Renders an Automaton IR graph as a GitHub-compatible Mermaid flowchart.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.escape(value) ⇒ Object

RBS:

  • (String value) -> String



32
33
34
35
# File 'lib/ibex/codegen/mermaid.rb', line 32

def escape(value)
  value.to_s.gsub("&", "&amp;").gsub("<", "&lt;").gsub(">", "&gt;").gsub("|", "&#124;")
       .gsub('"', "&quot;").gsub(/\s+/, " ")
end

.render(automaton) ⇒ Object

RBS:

  • (IR::Automaton automaton) -> String



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ibex/codegen/mermaid.rb', line 10

def render(automaton)
  labels = SymbolLabels.build(automaton.grammar)
  lines = ["flowchart LR"]
  automaton.states.each do |state|
    lines << %(  state_#{state.id}["State #{state.id}"])
    state.transitions.each do |symbol_id, target|
      label = escape(labels.fetch(symbol_id) { raise Ibex::Error, "missing grammar symbol id #{symbol_id}" })
      lines << "  state_#{state.id} -->|#{label}| state_#{target}"
    end
  end
  conflicting = automaton.states.reject { |state| state.conflicts.empty? }
  unless conflicting.empty?
    lines << "  classDef conflict fill:#fee2e2,stroke:#b91c1c,stroke-width:2px"
    conflicting.each { |state| lines << "  class state_#{state.id} conflict;" }
  end
  "#{lines.join("\n")}\n"
end

Instance Method Details

#self?.escapeString

RBS:

  • (String value) -> String

Parameters:

  • value (String)

Returns:

  • (String)


13
# File 'sig/ibex/codegen/mermaid.rbs', line 13

def self?.escape: (String value) -> String

#self?.renderString

RBS:

  • (IR::Automaton automaton) -> String

Parameters:

Returns:

  • (String)


8
# File 'sig/ibex/codegen/mermaid.rbs', line 8

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