Module: Typedocs::GrammerPrinter

Defined in:
lib/typedocs/grammer_printer.rb

Class Method Summary collapse

Class Method Details



2
3
4
5
# File 'lib/typedocs/grammer_printer.rb', line 2

def self.print_grammer(out)
  parser = Typedocs::Parser::ASTBuilder.new
  print_recursive(out, parser.root)
end


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/typedocs/grammer_printer.rb', line 7

def self.print_recursive(out, root)
  waiting = [root]
  ignore = {}

  until waiting.empty?
    entity = waiting.pop
    next if ignore[entity]
    ignore[entity] = true

    if entity.kind_of?(Parslet::Atoms::Entity)
      out.puts "#{'%20s' % entity.to_s} <- #{entity.parslet.to_s.gsub(/[a-z_]+:/, '')}"
    end

    atoms = Parslet::Atoms

    case entity
    when atoms::Sequence
      entity.parslets.reverse.each do|pl|
        waiting.push pl
      end
    when atoms::Lookahead
      waiting.push entity.bound_parslet
    when atoms::Repetition
      waiting.push entity.parslet
    when atoms::Alternative
      entity.alternatives.reverse.each do|pl|
        waiting.push pl
      end
    when atoms::Entity
      waiting.push entity.parslet
    when atoms::Named
      waiting.push entity.parslet
    when atoms::Re, atoms::Str
    else
      raise "Unsupported class: #{entity.class}"
    end
  end
end