Class: EBNF::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/ebnf/writer.rb

Constant Summary collapse

LINE_LENGTH =
80

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rules, out: $stdout, html: false, **options) ⇒ Writer

Returns a new instance of Writer.

Parameters:

  • rules (Array<Rule>)
  • options (Hash{Symbol => Object})
  • :out (#write)

    ($stdout)

Options Hash (**options):

  • :format (Symbol)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ebnf/writer.rb', line 59

def initialize(rules, out: $stdout, html: false, **options)
  @options = options.dup

  # Determine max LHS length
  max_id = rules.max_by {|r| r.id.to_s.length}.id.to_s.length
  max_sym = rules.max_by {|r| r.sym.to_s.length}.sym.to_s.length
  lhs_length = max_sym + 3
  lhs_fmt = "%<sym>-#{max_sym}s ::= "
  if max_id > 0
    lhs_fmt = "%<id>-#{max_id+2}s " + lhs_fmt
    lhs_length += max_id + 3
  end
  rhs_length = LINE_LENGTH - lhs_length

  if html
    # Output as formatted HTML
    begin
      require 'haml'
      hout = Haml::Engine.new(HAML_DESC).render(self, rules: rules) do |rule|
        formatted_expr = format(rule.expr)
        formatted_expr.length > rhs_length ? format(rule.expr, "\n") : formatted_expr
      end
      out.write hout
      return
    rescue LoadError
      $stderr.puts "Generating HTML requires haml gem to be loaded"
    end
  end

  # Format each rule, considering the available rhs size
  rules.each do |rule|
    buffer = if rule.pass?
      "%-#{lhs_length-2}s" % "@pass"
    else
      lhs_fmt % {id: "[#{rule.id}]", sym: rule.sym}
    end
    formatted_expr = format(rule.expr)
    if formatted_expr.length > rhs_length
      buffer << format(rule.expr, ("\n" + " " * lhs_length))
    else
      buffer << formatted_expr
    end
    out.puts(buffer)
  end
end

Class Method Details

.html(*rules) ⇒ Object

Write formatted rules to an IO like object as HTML

Parameters:

  • rules (Array<Rule>)

Returns:

  • (Object)


47
48
49
50
51
52
# File 'lib/ebnf/writer.rb', line 47

def self.html(*rules)
  require 'stringio' unless defined?(StringIO)
  buf = StringIO.new
  Writer.new(rules, out: buf, html: true)
  buf.string
end

Format rules to $stdout

Parameters:

  • rules (Array<Rule>)

Returns:

  • (Object)


28
29
30
# File 'lib/ebnf/writer.rb', line 28

def self.print(*rules)
  write($stdout, *rules)
end

.string(*rules) ⇒ Object

Format rules to a String

Parameters:

  • rules (Array<Rule>)

Returns:

  • (Object)


16
17
18
19
20
21
# File 'lib/ebnf/writer.rb', line 16

def self.string(*rules)
  require 'stringio' unless defined?(StringIO)
  buf = StringIO.new
  write(buf, *rules)
  buf.string
end

.write(out, *rules) ⇒ Object

Write formatted rules to an IO like object

Parameters:

  • out (Object)
  • rules (Array<Rule>)

Returns:

  • (Object)


38
39
40
# File 'lib/ebnf/writer.rb', line 38

def self.write(out, *rules)
  Writer.new(rules, out: out)
end