Class: Ast::Merge::SectionTyping::Classifier Abstract

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

Overview

This class is abstract.

Subclass and implement #classify

Base class for AST-aware section classifiers.

Subclasses implement ‘classify(node)` to identify section boundaries and extract section names from AST nodes.

Direct Known Subclasses

CallableClassifier, CompositeClassifier

Instance Method Summary collapse

Instance Method Details

#classifies?(node) ⇒ Boolean

Check if a node can be classified by this classifier.

Parameters:

  • node (Object)

    Node to check

Returns:

  • (Boolean)


140
141
142
# File 'lib/ast/merge/section_typing.rb', line 140

def classifies?(node)
  !classify(node).nil?
end

#classify(node) ⇒ TypedSection?

This method is abstract.

Subclasses must implement this method

Classify a single AST node.

Parameters:

  • node (Object)

    An AST node to classify

Returns:

  • (TypedSection, nil)

    Section info if node starts a section, nil otherwise

Raises:

  • (NotImplementedError)


100
101
102
# File 'lib/ast/merge/section_typing.rb', line 100

def classify(node)
  raise NotImplementedError, "#{self.class}#classify must be implemented"
end

#classify_all(children) ⇒ Array<TypedSection>

Classify all children of a parent node.

Iterates through child nodes and classifies each, grouping consecutive unclassified nodes into preamble/interstitial sections.

Parameters:

  • children (Array, Enumerable)

    Child nodes to classify

Returns:



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/ast/merge/section_typing.rb', line 111

def classify_all(children)
  sections = []
  unclassified_buffer = []

  children.each do |child|
    if (section = classify(child))
      # Flush unclassified buffer as preamble/interstitial
      if unclassified_buffer.any?
        sections << build_unclassified_section(unclassified_buffer)
        unclassified_buffer = []
      end
      sections << section
    else
      unclassified_buffer << child
    end
  end

  # Flush remaining unclassified nodes
  if unclassified_buffer.any?
    sections << build_unclassified_section(unclassified_buffer)
  end

  sections
end