Module: Unparser

Defined in:
lib/unparser.rb,
lib/unparser/ast.rb,
lib/unparser/cli.rb,
lib/unparser/dsl.rb,
lib/unparser/buffer.rb,
lib/unparser/emitter.rb,
lib/unparser/comments.rb,
lib/unparser/cli/color.rb,
lib/unparser/constants.rb,
lib/unparser/cli/differ.rb,
lib/unparser/cli/source.rb,
lib/unparser/emitter/if.rb,
lib/unparser/emitter/def.rb,
lib/unparser/emitter/for.rb,
lib/unparser/emitter/case.rb,
lib/unparser/emitter/meta.rb,
lib/unparser/emitter/redo.rb,
lib/unparser/emitter/root.rb,
lib/unparser/emitter/send.rb,
lib/unparser/node_helpers.rb,
lib/unparser/preprocessor.rb,
lib/unparser/emitter/alias.rb,
lib/unparser/emitter/begin.rb,
lib/unparser/emitter/block.rb,
lib/unparser/emitter/cbase.rb,
lib/unparser/emitter/class.rb,
lib/unparser/emitter/empty.rb,
lib/unparser/emitter/index.rb,
lib/unparser/emitter/match.rb,
lib/unparser/emitter/retry.rb,
lib/unparser/emitter/splat.rb,
lib/unparser/emitter/super.rb,
lib/unparser/emitter/undef.rb,
lib/unparser/emitter/yield.rb,
lib/unparser/emitter/binary.rb,
lib/unparser/emitter/ensure.rb,
lib/unparser/emitter/lambda.rb,
lib/unparser/emitter/module.rb,
lib/unparser/emitter/rescue.rb,
lib/unparser/emitter/defined.rb,
lib/unparser/emitter/hookexe.rb,
lib/unparser/emitter/literal.rb,
lib/unparser/emitter/resbody.rb,
lib/unparser/emitter/argument.rb,
lib/unparser/emitter/flipflop.rb,
lib/unparser/emitter/variable.rb,
lib/unparser/emitter/op_assign.rb,
lib/unparser/emitter/assignment.rb,
lib/unparser/emitter/repetition.rb,
lib/unparser/emitter/send/unary.rb,
lib/unparser/emitter/send/binary.rb,
lib/unparser/emitter/literal/hash.rb,
lib/unparser/emitter/send/regular.rb,
lib/unparser/emitter/flow_modifier.rb,
lib/unparser/emitter/literal/array.rb,
lib/unparser/emitter/literal/range.rb,
lib/unparser/emitter/literal/regexp.rb,
lib/unparser/emitter/literal/dynamic.rb,
lib/unparser/ast/local_variable_scope.rb,
lib/unparser/emitter/send/conditional.rb,
lib/unparser/emitter/literal/primitive.rb,
lib/unparser/emitter/literal/singleton.rb,
lib/unparser/emitter/literal/dynamic_body.rb,
lib/unparser/emitter/literal/execute_string.rb,
lib/unparser/emitter/send/attribute_assignment.rb

Overview

Library namespace

Defined Under Namespace

Modules: AST, Constants, DSL, NodeHelpers Classes: Buffer, Builder, CLI, Color, Comments, Emitter, Preprocessor

Constant Summary collapse

EMPTY_STRING =
''.freeze
EMPTY_ARRAY =
[].freeze

Class Method Summary collapse

Class Method Details

.buffer(source) ⇒ Parser::Source::Buffer

Construct a parser buffer from string

Parameters:

  • source (String)

Returns:

  • (Parser::Source::Buffer)


88
89
90
91
92
# File 'lib/unparser.rb', line 88

def self.buffer(source)
  Parser::Source::Buffer.new('(string)').tap do |buffer|
    buffer.source = source
  end
end

.parse(source) ⇒ Parser::AST::Node

Parse string into AST

Parameters:

  • source (String)

Returns:

  • (Parser::AST::Node)


44
45
46
# File 'lib/unparser.rb', line 44

def self.parse(source)
  parser.parse(buffer(source))
end

.parse_with_comments(source) ⇒ Parser::AST::Node

Parse string into AST, with comments

Parameters:

  • source (String)

Returns:

  • (Parser::AST::Node)


53
54
55
# File 'lib/unparser.rb', line 53

def self.parse_with_comments(source)
  parser.parse_with_comments(buffer(source))
end

.parserParser::Base

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parser instance that produces AST unparser understands

ignore :reek:NestedIterators

Returns:

  • (Parser::Base)


64
65
66
67
68
69
70
71
# File 'lib/unparser.rb', line 64

def self.parser
  Parser::CurrentRuby.new(Builder.new).tap do |parser|
    parser.diagnostics.tap do |diagnostics|
      diagnostics.all_errors_are_fatal = true
      diagnostics.consumer             = method(:consume_diagnostic)
    end
  end
end

.unparse(node, comment_array = []) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Unparse an AST (and, optionally, comments) into a string

Parameters:

  • node (Parser::AST::Node, nil)
  • comment_array (Array) (defaults to: [])

Returns:

  • (String)


30
31
32
33
34
35
36
37
# File 'lib/unparser.rb', line 30

def self.unparse(node, comment_array = [])
  node = Preprocessor.run(node)
  buffer = Buffer.new
  comments = Comments.new(comment_array)
  root = Emitter::Root.new(Parser::AST::Node.new(:root, [node]), buffer, comments)
  Emitter.emitter(node, root).write_to_buffer
  buffer.content
end