Module: Unparser

Defined in:
lib/unparser.rb,
lib/unparser/ast.rb,
lib/unparser/cli.rb,
lib/unparser/dsl.rb,
lib/unparser/diff.rb,
lib/unparser/anima.rb,
lib/unparser/color.rb,
lib/unparser/buffer.rb,
lib/unparser/either.rb,
lib/unparser/writer.rb,
lib/unparser/concord.rb,
lib/unparser/emitter.rb,
lib/unparser/comments.rb,
lib/unparser/constants.rb,
lib/unparser/equalizer.rb,
lib/unparser/adamantium.rb,
lib/unparser/emitter/if.rb,
lib/unparser/generation.rb,
lib/unparser/validation.rb,
lib/unparser/anima/error.rb,
lib/unparser/emitter/def.rb,
lib/unparser/emitter/for.rb,
lib/unparser/emitter/pin.rb,
lib/unparser/writer/send.rb,
lib/unparser/emitter/args.rb,
lib/unparser/emitter/case.rb,
lib/unparser/emitter/dstr.rb,
lib/unparser/emitter/dsym.rb,
lib/unparser/emitter/hash.rb,
lib/unparser/emitter/mlhs.rb,
lib/unparser/emitter/pair.rb,
lib/unparser/emitter/root.rb,
lib/unparser/emitter/send.rb,
lib/unparser/emitter/xstr.rb,
lib/unparser/node_details.rb,
lib/unparser/node_helpers.rb,
lib/unparser/abstract_type.rb,
lib/unparser/emitter/alias.rb,
lib/unparser/emitter/array.rb,
lib/unparser/emitter/begin.rb,
lib/unparser/emitter/block.rb,
lib/unparser/emitter/cbase.rb,
lib/unparser/emitter/class.rb,
lib/unparser/emitter/float.rb,
lib/unparser/emitter/index.rb,
lib/unparser/emitter/masgn.rb,
lib/unparser/emitter/match.rb,
lib/unparser/emitter/range.rb,
lib/unparser/emitter/splat.rb,
lib/unparser/emitter/super.rb,
lib/unparser/emitter/undef.rb,
lib/unparser/emitter/yield.rb,
lib/unparser/writer/binary.rb,
lib/unparser/writer/rescue.rb,
lib/unparser/emitter/binary.rb,
lib/unparser/emitter/kwargs.rb,
lib/unparser/emitter/lambda.rb,
lib/unparser/emitter/module.rb,
lib/unparser/emitter/regexp.rb,
lib/unparser/emitter/rescue.rb,
lib/unparser/emitter/simple.rb,
lib/unparser/writer/resbody.rb,
lib/unparser/anima/attribute.rb,
lib/unparser/emitter/defined.rb,
lib/unparser/emitter/hookexe.rb,
lib/unparser/emitter/kwbegin.rb,
lib/unparser/emitter/argument.rb,
lib/unparser/emitter/flipflop.rb,
lib/unparser/emitter/in_match.rb,
lib/unparser/emitter/match_as.rb,
lib/unparser/emitter/variable.rb,
lib/unparser/emitter/match_alt.rb,
lib/unparser/emitter/match_var.rb,
lib/unparser/emitter/op_assign.rb,
lib/unparser/emitter/primitive.rb,
lib/unparser/node_details/send.rb,
lib/unparser/writer/send/unary.rb,
lib/unparser/emitter/assignment.rb,
lib/unparser/emitter/case_guard.rb,
lib/unparser/emitter/case_match.rb,
lib/unparser/emitter/in_pattern.rb,
lib/unparser/emitter/match_rest.rb,
lib/unparser/emitter/repetition.rb,
lib/unparser/writer/send/binary.rb,
lib/unparser/writer/send/regular.rb,
lib/unparser/emitter/find_pattern.rb,
lib/unparser/emitter/hash_pattern.rb,
lib/unparser/emitter/array_pattern.rb,
lib/unparser/emitter/const_pattern.rb,
lib/unparser/emitter/flow_modifier.rb,
lib/unparser/emitter/match_pattern.rb,
lib/unparser/writer/dynamic_string.rb,
lib/unparser/emitter/match_pattern_p.rb,
lib/unparser/writer/send/conditional.rb,
lib/unparser/ast/local_variable_scope.rb,
lib/unparser/adamantium/method_builder.rb,
lib/unparser/writer/send/attribute_assignment.rb

Overview

Library namespace

Defined Under Namespace

Modules: AST, AbstractType, Adamantium, Constants, DSL, Generation, NodeDetails, NodeHelpers, RequireBlock, Writer Classes: Anima, Buffer, Builder, CLI, Color, Comments, Concord, Diff, Either, Emitter, Equalizer, InvalidNodeError, Validation

Constant Summary collapse

EMPTY_STRING =
''.freeze
EMPTY_ARRAY =
[].freeze
UnknownNodeError =
Class.new(ArgumentError)

Class Method Summary collapse

Class Method Details

.buffer(source, identification = '(string)') ⇒ Parser::Source::Buffer

Construct a parser buffer from string

Parameters:

  • source (String)

Returns:

  • (Parser::Source::Buffer)


147
148
149
# File 'lib/unparser.rb', line 147

def self.buffer(source, identification = '(string)')
  Parser::Source::Buffer.new(identification, source: source)
end

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

Parse string into AST

Parameters:

  • source (String)

Returns:

  • (Parser::AST::Node, nil)


105
106
107
# File 'lib/unparser.rb', line 105

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

.parse_either(source) ⇒ Either<Parser::SyntaxError, (Parser::ASTNode, nil)>

Parse string into either syntax error or AST

Parameters:

  • source (String)

Returns:

  • (Either<Parser::SyntaxError, (Parser::ASTNode, nil)>)


114
115
116
117
118
# File 'lib/unparser.rb', line 114

def self.parse_either(source)
  Either.wrap_error(Parser::SyntaxError) do
    parser.parse(buffer(source))
  end
end

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

Parse string into AST, with comments

Parameters:

  • source (String)

Returns:

  • (Parser::AST::Node)


125
126
127
# File 'lib/unparser.rb', line 125

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

Returns:

  • (Parser::Base)


134
135
136
137
138
139
140
# File 'lib/unparser.rb', line 134

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

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

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

Parameters:

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

Returns:

  • (String)

Raises:

  • InvalidNodeError if the node passed is invalid



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/unparser.rb', line 60

def self.unparse(node, comment_array = [])
  return '' if node.nil?

  Buffer.new.tap do |buffer|
    Emitter::Root.new(
      buffer,
      node,
      Comments.new(comment_array)
    ).write_to_buffer
  end.content
end

.unparse_either(node) ⇒ Either<Exception, String>

Unparse capturing errors

This is mostly useful for writing testing tools against unparser.

Parameters:

  • node (Parser::AST::Node, nil)

Returns:



96
97
98
# File 'lib/unparser.rb', line 96

def self.unparse_either(node)
  Either.wrap_error(Exception) { unparse(node) }
end

.unparse_validate(node, comment_array = []) ⇒ Either<Validation,String>

Unparse with validation

Parameters:

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

Returns:



78
79
80
81
82
83
84
85
86
87
# File 'lib/unparser.rb', line 78

def self.unparse_validate(node, comment_array = [])
  generated = unparse(node, comment_array)
  validation = Validation.from_string(generated)

  if validation.success?
    Either::Right.new(generated)
  else
    Either::Left.new(validation)
  end
end