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/color.rb,
lib/unparser/buffer.rb,
lib/unparser/writer.rb,
lib/unparser/emitter.rb,
lib/unparser/comments.rb,
lib/unparser/constants.rb,
lib/unparser/emitter/if.rb,
lib/unparser/generation.rb,
lib/unparser/validation.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/root.rb,
lib/unparser/emitter/send.rb,
lib/unparser/emitter/xstr.rb,
lib/unparser/node_details.rb,
lib/unparser/node_helpers.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/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/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/hash_pattern.rb,
lib/unparser/emitter/array_pattern.rb,
lib/unparser/emitter/const_pattern.rb,
lib/unparser/emitter/flow_modifier.rb,
lib/unparser/writer/dynamic_string.rb,
lib/unparser/writer/send/conditional.rb,
lib/unparser/ast/local_variable_scope.rb,
lib/unparser/writer/send/attribute_assignment.rb

Overview

Library namespace

Defined Under Namespace

Modules: AST, Constants, DSL, Generation, NodeDetails, NodeHelpers, Writer Classes: Buffer, Builder, CLI, Color, Comments, Diff, Emitter, 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)


140
141
142
# File 'lib/unparser.rb', line 140

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)


87
88
89
# File 'lib/unparser.rb', line 87

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

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

Parse string into either syntax error or AST

Parameters:

  • source (String)

Returns:

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


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

def self.parse_either(source)
  MPrelude::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)


107
108
109
# File 'lib/unparser.rb', line 107

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)


116
117
118
119
120
121
122
123
# File 'lib/unparser.rb', line 116

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)


41
42
43
44
45
46
47
48
49
50
51
# File 'lib/unparser.rb', line 41

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:

  • (Either<Exception, String>)


77
78
79
80
# File 'lib/unparser.rb', line 77

def self.unparse_either(node)
  MPrelude::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:



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

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

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