A helper class that formats Citrus and Treetop grammars as a string.
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
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 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
Generated with the Darkfish Rdoc Generator 2.