Class: Ast::Merge::EmitterBase

Inherits:
Object
  • Object
show all
Defined in:
lib/ast/merge/emitter_base.rb

Overview

Base class for emitters that convert AST structures back to text.
Provides common functionality for tracking indentation, managing output lines,
and handling comments.

Subclasses implement format-specific emission methods (e.g., emit_pair for JSON,
emit_variable_assignment for Bash, etc.)

Examples:

Implementing a custom emitter

class MyEmitter < Ast::Merge::EmitterBase
  def emit_my_construct(data)
    add_comma_if_needed if @needs_separator
    @lines << "#{current_indent}my_syntax: #{data}"
    @needs_separator = true
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(indent_size: 2, **options) ⇒ EmitterBase

Initialize a new emitter

Parameters:

  • indent_size (Integer) (defaults to: 2)

    Number of spaces per indent level

  • options (Hash)

    Additional options for subclasses



34
35
36
37
38
39
# File 'lib/ast/merge/emitter_base.rb', line 34

def initialize(indent_size: 2, **options)
  @lines = []
  @indent_level = 0
  @indent_size = indent_size
  initialize_subclass_state(**options)
end

Instance Attribute Details

#indent_levelInteger (readonly)

Returns Current indentation level.

Returns:

  • (Integer)

    Current indentation level



25
26
27
# File 'lib/ast/merge/emitter_base.rb', line 25

def indent_level
  @indent_level
end

#indent_sizeInteger (readonly)

Returns Spaces per indent level.

Returns:

  • (Integer)

    Spaces per indent level



28
29
30
# File 'lib/ast/merge/emitter_base.rb', line 28

def indent_size
  @indent_size
end

#linesArray<String> (readonly)

Returns Output lines.

Returns:

  • (Array<String>)

    Output lines



22
23
24
# File 'lib/ast/merge/emitter_base.rb', line 22

def lines
  @lines
end

Instance Method Details

#clearObject

Clear the emitter state



87
88
89
90
91
# File 'lib/ast/merge/emitter_base.rb', line 87

def clear
  @lines = []
  @indent_level = 0
  clear_subclass_state
end

#clear_subclass_stateObject

Hook for subclasses to clear their own state



94
95
96
# File 'lib/ast/merge/emitter_base.rb', line 94

def clear_subclass_state
  # Override in subclasses if needed
end

#dedentObject

Decrease indentation level



104
105
106
# File 'lib/ast/merge/emitter_base.rb', line 104

def dedent
  @indent_level -= 1 if @indent_level > 0
end

#emit_blank_lineObject

Emit a blank line



48
49
50
# File 'lib/ast/merge/emitter_base.rb', line 48

def emit_blank_line
  @lines << ""
end

#emit_leading_comments(comments) ⇒ Object

Emit leading comments from CommentTracker

Parameters:

  • comments (Array<Hash>)

    Comment hashes with :text, :indent, etc.



55
56
57
58
59
# File 'lib/ast/merge/emitter_base.rb', line 55

def emit_leading_comments(comments)
  comments.each do |comment|
    emit_tracked_comment(comment)
  end
end

#emit_raw_lines(raw_lines) ⇒ Object

Emit raw lines as-is (for preserving exact formatting)

Parameters:

  • raw_lines (Array<String>)

    Lines to emit without modification



72
73
74
# File 'lib/ast/merge/emitter_base.rb', line 72

def emit_raw_lines(raw_lines)
  raw_lines.each { |line| @lines << line.chomp }
end

#emit_tracked_comment(comment) ⇒ Object

Emit a comment from CommentTracker hash
Subclasses should override this to handle format-specific comment syntax

Parameters:

  • comment (Hash)

    Comment hash with :text, :indent, :block, etc.

Raises:

  • (NotImplementedError)


65
66
67
# File 'lib/ast/merge/emitter_base.rb', line 65

def emit_tracked_comment(comment)
  raise NotImplementedError, "Subclasses must implement emit_tracked_comment"
end

#indentObject

Increase indentation level



99
100
101
# File 'lib/ast/merge/emitter_base.rb', line 99

def indent
  @indent_level += 1
end

#initialize_subclass_state(**options) ⇒ Object

Hook for subclasses to initialize their own state

Parameters:

  • options (Hash)

    Additional options



43
44
45
# File 'lib/ast/merge/emitter_base.rb', line 43

def initialize_subclass_state(**options)
  # Override in subclasses if needed
end

#to_sString

Get the output as a single string
Subclasses may override to customize output format (e.g., to_json, to_yaml)

Returns:

  • (String)


80
81
82
83
84
# File 'lib/ast/merge/emitter_base.rb', line 80

def to_s
  content = @lines.join("\n")
  content += "\n" unless content.empty? || content.end_with?("\n")
  content
end