Module: Ibex::Codegen::RailroadDocumentation

Included in:
Railroad
Defined in:
lib/ibex/codegen/railroad_documentation.rb,
sig/ibex/codegen/railroad_documentation.rbs

Overview

Documentation layout helpers mixed into the railroad renderer singleton.

Constant Summary collapse

DOCUMENTATION_COLUMNS =

Signature:

  • Integer

Returns:

  • (Integer)
72
DOCUMENTATION_LINE_HEIGHT =

Signature:

  • Integer

Returns:

  • (Integer)
18
DOCUMENTATION_GAP =

Signature:

  • Integer

Returns:

  • (Integer)
8

Instance Method Summary collapse

Instance Method Details

#append_rule_documentation(lines, symbol) ⇒ void

This method returns an undefined value.

RBS:

  • (Array[String] lines, IR::GrammarSymbol symbol) -> void

Parameters:



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ibex/codegen/railroad_documentation.rb', line 14

def append_rule_documentation(lines, symbol)
  # @type self: singleton(Railroad)
  documentation = symbol.documentation
  return unless documentation

  lines << "    <desc>#{escape(documentation)}</desc>"
  documentation_lines(documentation).each_with_index do |line, index|
    y = Railroad::SECTION_HEADER + DOCUMENTATION_GAP + (index * DOCUMENTATION_LINE_HEIGHT)
    lines << %(    <text class="rule-documentation" x="#{Railroad::PAGE_PADDING}" y="#{y}">#{escape(line)}</text>)
  end
end

#documentation_lines(documentation) ⇒ Array[String]

RBS:

  • (String? documentation) -> Array[String]

Parameters:

  • documentation (String, nil)

Returns:

  • (Array[String])


36
37
38
39
40
41
42
43
# File 'lib/ibex/codegen/railroad_documentation.rb', line 36

def documentation_lines(documentation)
  return [] unless documentation

  documentation.split("\n", -1).flat_map do |line|
    characters = line.each_char.to_a
    characters.empty? ? [""] : characters.each_slice(DOCUMENTATION_COLUMNS).map(&:join)
  end
end

#escape(value) ⇒ String

RBS:

  • (String | Integer value) -> String

Parameters:

  • value (String, Integer)

Returns:

  • (String)


46
47
48
49
50
51
# File 'lib/ibex/codegen/railroad_documentation.rb', line 46

def escape(value)
  xml = value.to_s.encode(Encoding::UTF_8, invalid: :replace, undef: :replace)
             .gsub(/[^\u0009\u000A\u000D\u0020-\uD7FF\uE000-\uFFFD\u{10000}-\u{10FFFF}]/u, "\uFFFD")
  xml.gsub("&", "&amp;").gsub("<", "&lt;").gsub(">", "&gt;")
     .gsub('"', "&quot;").gsub("'", "&apos;")
end

#section_header_height(symbol) ⇒ Integer

RBS:

  • (IR::GrammarSymbol symbol) -> Integer

Parameters:

Returns:

  • (Integer)


27
28
29
30
31
32
33
# File 'lib/ibex/codegen/railroad_documentation.rb', line 27

def section_header_height(symbol)
  # @type self: singleton(Railroad)
  lines = documentation_lines(symbol.documentation)
  return Railroad::SECTION_HEADER if lines.empty?

  Railroad::SECTION_HEADER + DOCUMENTATION_GAP + (lines.length * DOCUMENTATION_LINE_HEIGHT)
end

#track(from, to) ⇒ String

RBS:

  • (Integer from, Integer to) -> String

Parameters:

  • from (Integer)
  • to (Integer)

Returns:

  • (String)


54
55
56
# File 'lib/ibex/codegen/railroad_documentation.rb', line 54

def track(from, to)
  %(<line class="track" x1="#{from}" y1="0" x2="#{to}" y2="0"/>)
end