Class: Ast::Merge::Text::FileAnalysis

Inherits:
Object
  • Object
show all
Includes:
FileAnalyzable
Defined in:
lib/ast/merge/text/file_analysis.rb

Overview

Text file analysis class for the text-based AST.

This class parses plain text files into a simple AST structure where:

  • Top-level nodes are LineNodes (one per line)

  • Nested nodes are WordNodes (words within each line, split on word boundaries)

This provides a minimal AST implementation that can be used to test the merge infrastructure with any text-based content.

Examples:

Basic usage

analysis = FileAnalysis.new("Hello world\nGoodbye world")
analysis.statements.size  # => 2
analysis.statements[0].words.size  # => 2

With freeze blocks

content = <<~TEXT
  Line one
  # text-merge:freeze
  Frozen content
  # text-merge:unfreeze
  Line four
TEXT
analysis = FileAnalysis.new(content, freeze_token: "text-merge")
analysis.freeze_blocks.size  # => 1

Constant Summary collapse

DEFAULT_FREEZE_TOKEN =

Default freeze token for text files

"text-merge"

Instance Attribute Summary collapse

Attributes included from FileAnalyzable

#freeze_token, #lines, #signature_generator, #source

Instance Method Summary collapse

Methods included from FileAnalyzable

#freeze_block_at, #freeze_blocks, #generate_signature, #in_freeze_block?, included, #line_at, #normalized_line, #signature_at

Constructor Details

#initialize(source, freeze_token: DEFAULT_FREEZE_TOKEN, signature_generator: nil) ⇒ FileAnalysis

Initialize a new FileAnalysis

Parameters:

  • source (String)

    Source text content

  • freeze_token (String) (defaults to: DEFAULT_FREEZE_TOKEN)

    Token for freeze block markers

  • signature_generator (Proc, nil) (defaults to: nil)

    Custom signature generator



44
45
46
47
48
49
50
51
52
# File 'lib/ast/merge/text/file_analysis.rb', line 44

def initialize(source, freeze_token: DEFAULT_FREEZE_TOKEN, signature_generator: nil)
  @source = source
  # Split preserving empty lines, but remove trailing empty line from final newline
  @lines = source.split("\n", -1)
  @lines.pop if @lines.last&.empty? && source.end_with?("\n")
  @freeze_token = freeze_token
  @signature_generator = signature_generator
  @statements = parse_statements
end

Instance Attribute Details

#statementsArray<LineNode, FreezeNodeBase> (readonly)

Get all top-level statements (LineNodes and FreezeNodes)

Returns:



57
58
59
# File 'lib/ast/merge/text/file_analysis.rb', line 57

def statements
  @statements
end

Instance Method Details

#compute_node_signature(node) ⇒ Array?

Compute signature for a node

Parameters:

  • node (Object)

    Node to compute signature for

Returns:

  • (Array, nil)

    Signature array or nil



63
64
65
66
67
68
69
70
# File 'lib/ast/merge/text/file_analysis.rb', line 63

def compute_node_signature(node)
  case node
  when LineNode
    node.signature
  when FreezeNodeBase
    [:freeze_block, node.start_line, node.end_line]
  end
end

#fallthrough_node?(value) ⇒ Boolean

Check if a value is a fallthrough node

Parameters:

  • value (Object)

    Value to check

Returns:

  • (Boolean)

    True if fallthrough node



76
77
78
# File 'lib/ast/merge/text/file_analysis.rb', line 76

def fallthrough_node?(value)
  value.is_a?(LineNode) || value.is_a?(FreezeNodeBase) || super
end