Sha256: 00ecb1647b19feb3751a9cf10918856a6a5ab647330477132e0cfde2cbb7a608

Contents?: true

Size: 1.27 KB

Versions: 8

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true
# Base class for formatted textual dump of a "model"
#
class Puppet::Pops::Model::TreeDumper
  attr_accessor :indent_count
  def initialize initial_indentation = 0
    @@dump_visitor ||= Puppet::Pops::Visitor.new(nil,"dump",0,0)
    @indent_count = initial_indentation
  end

  def dump(o)
    format(do_dump(o))
  end

  def do_dump(o)
    @@dump_visitor.visit_this_0(self, o)
  end

  def indent
    "  " * indent_count
  end

  def format(x)
    result = ''.dup
    parts = format_r(x)
    parts.each_index do |i|
      if i > 0
        # separate with space unless previous ends with whitespace or (
        result << ' ' if parts[i] != ")" && parts[i-1] !~ /.*(?:\s+|\()$/ && parts[i] !~ /^\s+/
      end
      result << parts[i].to_s
    end
    result
  end

  def format_r(x)
    result = []
    case x
    when :break
      result << "\n" + indent
    when :indent
      @indent_count += 1
    when :dedent
      @indent_count -= 1
    when Array
      result << '('
      result += x.collect {|a| format_r(a) }.flatten
      result << ')'
    when Symbol
      result << x.to_s # Allows Symbols in arrays e.g. ["text", =>, "text"]
    else
      result << x
    end
    result
  end

  def is_nop? o
    o.nil? || o.is_a?(Puppet::Pops::Model::Nop)
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
puppet-8.3.0 lib/puppet/pops/model/tree_dumper.rb
puppet-8.3.0-x86-mingw32 lib/puppet/pops/model/tree_dumper.rb
puppet-8.3.0-x64-mingw32 lib/puppet/pops/model/tree_dumper.rb
puppet-8.3.0-universal-darwin lib/puppet/pops/model/tree_dumper.rb
puppet-8.3.1 lib/puppet/pops/model/tree_dumper.rb
puppet-8.3.1-x86-mingw32 lib/puppet/pops/model/tree_dumper.rb
puppet-8.3.1-x64-mingw32 lib/puppet/pops/model/tree_dumper.rb
puppet-8.3.1-universal-darwin lib/puppet/pops/model/tree_dumper.rb