Class: Expressir::Express::RemarkAttacher

Inherits:
Object
  • Object
show all
Defined in:
lib/expressir/express/remark_attacher.rb

Overview

Handles attaching remarks (comments) to model elements after parsing.

NOTE: Post-processing remark attachment has inherent limitations for scope-based matching. Remarks with simple tags (like “WR1”) inside scopes (TYPE, ENTITY, etc.) cannot be perfectly matched without parsing context. This implementation prioritizes:

  1. Exact path matches (e.g., “schema.entity.WR1”)

  2. Proximity-based matching for simple tags

  3. NOT creating spurious schema-level items for ambiguous tags

Constant Summary collapse

CHILD_COLLECTION_ATTRIBUTES =

Child collection attributes to walk when traversing model elements. These are the named collections on model elements (e.g., entity.attributes, schema.entities, function.parameters). Extracted to a constant to avoid duplication between calculate_children_end_line and collect_children.

%i[
  schemas types entities functions procedures rules constants
  attributes derived_attributes inverse_attributes
  where_rules unique_rules informal_propositions
  parameters variables statements items remark_items
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ RemarkAttacher

Returns a new instance of RemarkAttacher.



25
26
27
28
29
30
31
32
# File 'lib/expressir/express/remark_attacher.rb', line 25

def initialize(source)
  @source = source
  @attached_spans = Set.new
  @line_cache = {}
  @model = nil
  @source_lines = nil # cached @source.lines
  @scope_map = nil # cached scope at each line number
end

Instance Method Details

#attach(model) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/expressir/express/remark_attacher.rb', line 34

def attach(model)
  @model = model
  remarks = extract_all_remarks

  # Build nodes_with_positions ONCE for both tagged and untagged remark passes.
  # This avoids double tree walk (381K nodes × 2 = 762K visits) which was
  # the largest memory overhead in remark attachment (~430MB for large files).
  nodes_with_positions = build_sorted_nodes_with_positions(model)

  attach_tagged_remarks(model, remarks, nodes_with_positions)
  attach_untagged_remarks(remarks, nodes_with_positions)

  # Free expensive data structures after attachment is complete.
  # These are only needed during the attach process.
  @source = nil
  @source_lines = nil
  @scope_map = nil
  @line_cache = nil

  model
end