Class: Ast::Merge::EmitterBase
- Inherits:
-
Object
- Object
- Ast::Merge::EmitterBase
- 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.)
Instance Attribute Summary collapse
-
#indent_level ⇒ Integer
readonly
Current indentation level.
-
#indent_size ⇒ Integer
readonly
Spaces per indent level.
-
#lines ⇒ Array<String>
readonly
Output lines.
Instance Method Summary collapse
-
#clear ⇒ Object
Clear the emitter state.
-
#clear_subclass_state ⇒ Object
Hook for subclasses to clear their own state.
-
#dedent ⇒ Object
Decrease indentation level.
-
#emit_blank_line ⇒ Object
Emit a blank line.
-
#emit_leading_comments(comments) ⇒ Object
Emit leading comments from CommentTracker.
-
#emit_raw_lines(raw_lines) ⇒ Object
Emit raw lines as-is (for preserving exact formatting).
-
#emit_tracked_comment(comment) ⇒ Object
Emit a comment from CommentTracker hash Subclasses should override this to handle format-specific comment syntax.
-
#indent ⇒ Object
Increase indentation level.
-
#initialize(indent_size: 2, **options) ⇒ EmitterBase
constructor
Initialize a new emitter.
-
#initialize_subclass_state(**options) ⇒ Object
Hook for subclasses to initialize their own state.
-
#to_s ⇒ String
Get the output as a single string Subclasses may override to customize output format (e.g., to_json, to_yaml).
Constructor Details
#initialize(indent_size: 2, **options) ⇒ EmitterBase
Initialize a new emitter
34 35 36 37 38 39 |
# File 'lib/ast/merge/emitter_base.rb', line 34 def initialize(indent_size: 2, **) @lines = [] @indent_level = 0 @indent_size = indent_size initialize_subclass_state(**) end |
Instance Attribute Details
#indent_level ⇒ Integer (readonly)
Returns Current indentation level.
25 26 27 |
# File 'lib/ast/merge/emitter_base.rb', line 25 def indent_level @indent_level end |
#indent_size ⇒ Integer (readonly)
Returns Spaces per indent level.
28 29 30 |
# File 'lib/ast/merge/emitter_base.rb', line 28 def indent_size @indent_size end |
#lines ⇒ Array<String> (readonly)
Returns Output lines.
22 23 24 |
# File 'lib/ast/merge/emitter_base.rb', line 22 def lines @lines end |
Instance Method Details
#clear ⇒ Object
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_state ⇒ Object
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 |
#dedent ⇒ Object
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_line ⇒ Object
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
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)
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
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 |
#indent ⇒ Object
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
43 44 45 |
# File 'lib/ast/merge/emitter_base.rb', line 43 def initialize_subclass_state(**) # Override in subclasses if needed end |
#to_s ⇒ String
Get the output as a single string
Subclasses may override to customize output format (e.g., to_json, to_yaml)
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 |