Parent

Files

Parslet::Parser::PrettyPrinter

A helper class that formats Citrus and Treetop grammars as a string.

Attributes

visitor[R]

Public Class Methods

new(visitor_klass) click to toggle source
# File lib/parslet/export.rb, line 73
def initialize(visitor_klass)
  @visitor = visitor_klass.new(self)
end

Public Instance Methods

deferred(name, content) click to toggle source

Whenever the visitor encounters an rule in a parslet, it defers the pretty printing of the rule by calling this method.

# File lib/parslet/export.rb, line 114
def deferred(name, content)
  @todo ||= []
  @todo << [name, content]
end
mangle_name(str) click to toggle source

Mangles names so that Citrus and Treetop can live with it. This mostly transforms some of the things that Ruby allows into other patterns. If there is collision, we will not detect it for now.

# File lib/parslet/export.rb, line 123
def mangle_name(str)
  str.to_s.sub(/\?$/, '_p')
end
pretty_print(name, parslet) click to toggle source

Pretty prints the given parslet using the visitor that has been configured in initialize. Returns the string representation of the Citrus or Treetop grammar.

# File lib/parslet/export.rb, line 81
def pretty_print(name, parslet)
  output = "grammar #{name}\n"
  
  output << rule('root', parslet)
  
  seen = Set.new
  loop do
    # @todo is constantly filled by the visitor (see #deferred). We 
    # keep going until it is empty.
    break if @todo.empty?
    name, block = @todo.shift

    # Track what rules we've already seen. This breaks loops.
    next if seen.include?(name)
    seen << name

    output << rule(name, block.call)
  end
  
  output << "end\n"
end
rule(name, parslet) click to toggle source

Formats a rule in either dialect.

# File lib/parslet/export.rb, line 105
def rule(name, parslet)
  "  rule #{mangle_name name}\n" << 
  "    " << parslet.accept(visitor) << "\n" <<
  "  end\n"
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.