Class: Unparser::Emitter

Inherits:
Object
  • Object
show all
Includes:
AbstractType, Adamantium::Flat, Constants
Defined in:
lib/unparser/emitter.rb,
lib/unparser/emitter/if.rb,
lib/unparser/emitter/def.rb,
lib/unparser/emitter/for.rb,
lib/unparser/emitter/case.rb,
lib/unparser/emitter/next.rb,
lib/unparser/emitter/redo.rb,
lib/unparser/emitter/root.rb,
lib/unparser/emitter/send.rb,
lib/unparser/emitter/alias.rb,
lib/unparser/emitter/begin.rb,
lib/unparser/emitter/block.rb,
lib/unparser/emitter/break.rb,
lib/unparser/emitter/cbase.rb,
lib/unparser/emitter/class.rb,
lib/unparser/emitter/empty.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/module.rb,
lib/unparser/emitter/rescue.rb,
lib/unparser/emitter/return.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/index.rb,
lib/unparser/emitter/send/unary.rb,
lib/unparser/emitter/send/binary.rb,
lib/unparser/emitter/send/regular.rb,
lib/unparser/emitter/literal/range.rb,
lib/unparser/emitter/literal/regexp.rb,
lib/unparser/emitter/literal/dynamic.rb,
lib/unparser/emitter/literal/composed.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

Overview

Emitter base class

Defined Under Namespace

Classes: Alias, ArgExpr, Argument, Arguments, Assignment, Begin, Binary, BinaryAssign, Block, BlockPass, Blockarg, Break, CBase, Case, Class, Const, Def, Defined, Empty, Ensure, FlipFlop, For, Hookexe, If, KeywordOptional, KeywordRest, Kwarg, Literal, Match, Module, Next, NthRef, OpAssign, Optarg, Post, Redo, Repetition, Resbody, Rescue, Restarg, Retry, Return, Root, SClass, Send, Splat, Super, Undef, Variable, When, Yield, ZSuper

Constant Summary collapse

REGISTRY =

Registry for node emitters

{}
NOINDENT =
[:rescue, :ensure].to_set.freeze
DEFAULT_DELIMITER =
', '.freeze
CURLY_BRACKETS =
IceNine.deep_freeze(%w({ }))

Constants included from Constants

Constants::BINARY_OPERATORS, Constants::COMMENT, Constants::KEYWORDS, Constants::K_ALIAS, Constants::K_AND, Constants::K_BEGIN, Constants::K_BREAK, Constants::K_CASE, Constants::K_CLASS, Constants::K_DEF, Constants::K_DEFINE, Constants::K_DEFINED, Constants::K_DO, Constants::K_EEND, Constants::K_ELSE, Constants::K_ELSIF, Constants::K_ENCODING, Constants::K_END, Constants::K_ENSURE, Constants::K_FALSE, Constants::K_FILE, Constants::K_FOR, Constants::K_IF, Constants::K_IN, Constants::K_MODULE, Constants::K_NEXT, Constants::K_NIL, Constants::K_NOT, Constants::K_OR, Constants::K_POSTEXE, Constants::K_PREEXE, Constants::K_REDO, Constants::K_RESCUE, Constants::K_RETRY, Constants::K_RETURN, Constants::K_SELF, Constants::K_SUPER, Constants::K_THEN, Constants::K_TRUE, Constants::K_UNDEF, Constants::K_UNLESS, Constants::K_UNTIL, Constants::K_WHEN, Constants::K_WHILE, Constants::K_YIELD, Constants::M_PC, Constants::M_PO, Constants::NL, Constants::TERMINATED, Constants::T_AMP, Constants::T_AND, Constants::T_ASN, Constants::T_ASR, Constants::T_COLON, Constants::T_DCL, Constants::T_DLT, Constants::T_DOT, Constants::T_DSPLAT, Constants::T_LT, Constants::T_NEG, Constants::T_OR, Constants::T_PIPE, Constants::T_SPLAT, Constants::UNARY_OPERATORS, Constants::WS

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.emit(*arguments) ⇒ self

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.

Emit node

Returns:

  • (self)


101
102
103
# File 'lib/unparser/emitter.rb', line 101

def self.emit(*arguments)
  new(*arguments).write_to_buffer
end

.emitter(node, parent) ⇒ Emitter

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.

Return emitter

Returns:



111
112
113
114
115
116
117
# File 'lib/unparser/emitter.rb', line 111

def self.emitter(node, parent)
  type = node.type
  klass = REGISTRY.fetch(type) do
    raise ArgumentError, "No emitter for node: #{type.inspect}"
  end
  klass.new(node, parent)
end

Instance Method Details

#dispatchundefined

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.

Dispatch node

Returns:

  • (undefined)


125
# File 'lib/unparser/emitter.rb', line 125

abstract_method :dispatch

#terminated?false, true

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.

Test if node is emitted as terminated expression

Returns:

  • (false)

    if emitted node is unambigous

  • (true)


136
137
138
# File 'lib/unparser/emitter.rb', line 136

def terminated?
  TERMINATED.include?(node.type)
end

#write_to_bufferself

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.

Trigger write to buffer

Returns:

  • (self)


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

def write_to_buffer
  emit_comments_before if buffer.fresh_line?
  dispatch
  comments.consume(node)
  emit_eof_comments if parent.is_a?(Root)
  self
end