Class: Parslet::Parser::PrettyPrinter

### Home Classes Methods

### In Files

- lib/parslet/export.rb

### Parent

Object

### Methods

- ::new
- #deferred
- #mangle_name
- #pretty_print
- #rule

### Files

- README

### Class/Module Index

Quicksearch

- Parslet
- Parslet::Atoms
- Parslet::Atoms::Alternative
- Parslet::Atoms::Base
- Parslet::Atoms::CanFlatten
- Parslet::Atoms::Context
- Parslet::Atoms::DSL
- Parslet::Atoms::Entity
- Parslet::Atoms::Lookahead
- Parslet::Atoms::Named
- Parslet::Atoms::Precedence
- Parslet::Atoms::Re
- Parslet::Atoms::Repetition
- Parslet::Atoms::Sequence
- Parslet::Atoms::Str
- Parslet::Cause
- Parslet::ClassMethods
- Parslet::DelayedMatchConstructor
- Parslet::ErrorReporter
- Parslet::ErrorReporter::Deepest
- Parslet::ErrorReporter::Tree
- Parslet::Expression
- Parslet::Expression::Treetop
- Parslet::Expression::Treetop::Parser
- Parslet::Expression::Treetop::Transform
- Parslet::ParseFailed
- Parslet::Parser
- Parslet::Parser::PrettyPrinter
- Parslet::Parser::Visitors
- Parslet::Parser::Visitors::Citrus
- Parslet::Parser::Visitors::Treetop
- Parslet::Pattern
- Parslet::Pattern::SequenceBind
- Parslet::Pattern::SimpleBind
- Parslet::Pattern::SubtreeBind
- Parslet::Slice
- Parslet::Source
- Parslet::Source::LineCache
- Parslet::Source::RangeSearch
- Parslet::Transform
- Parslet::Transform::Context
- Parslet::UnconsumedInput

No matching classes.

# 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.