Class: Ibex::Runtime::CST::Blender

Inherits:
Object
  • Object
show all
Defined in:
lib/ibex/runtime/cst/incremental/blender.rb

Overview

A state-aware token source that substitutes validated old subtrees.

Defined Under Namespace

Classes: Candidate

Constant Summary collapse

UNSAFE_FLAGS =

: Integer

Flags::CONTAINS_ERROR | Flags::CONTAINS_MISSING | Flags::CONTAINS_SKIPPED

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(old_root:, parse_memo:, lexed:, edits:, max_decomposed_nodes:, enabled: true) ⇒ Blender

Returns a new instance of Blender.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ibex/runtime/cst/incremental/blender.rb', line 60

def initialize(old_root:, parse_memo:, lexed:, edits:, max_decomposed_nodes:, enabled: true)
  @old_root = old_root
  @parse_memo = parse_memo
  @lexed = lexed
  @edits = TextEdit.normalize(edits)
  @max_decomposed_nodes = max_decomposed_nodes
  @enabled = enabled
  @candidates = {} #: Hash[Integer, Array[Candidate]]
  @new_offsets = offset_index(@lexed.memo.offsets)
  @old_tokens = @old_root.tokens.map(&:green)
  @token_index = 0
  @reused_descendants = 0
  @decomposed_nodes = 0
  @fallback_reason = nil #: Symbol?
  build_candidates if @enabled && reusable_root?
end

Instance Attribute Details

#decomposed_nodesObject (readonly)

: Integer



44
45
46
# File 'lib/ibex/runtime/cst/incremental/blender.rb', line 44

def decomposed_nodes
  @decomposed_nodes
end

#fallback_reasonObject (readonly)

: Symbol?



45
46
47
# File 'lib/ibex/runtime/cst/incremental/blender.rb', line 45

def fallback_reason
  @fallback_reason
end

#reused_descendantsObject (readonly)

: Integer



43
44
45
# File 'lib/ibex/runtime/cst/incremental/blender.rb', line 43

def reused_descendants
  @reused_descendants
end

Instance Method Details

#next_for_state(state) ⇒ Object

Return the next token or a whole nonterminal valid in the current LR state.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ibex/runtime/cst/incremental/blender.rb', line 79

def next_for_state(state)
  candidates = @candidates[@token_index]
  candidate = candidates&.find { |item| item.entry.left_state == state }
  if candidate
    @token_index += candidate.token_count
    @reused_descendants += candidate.entry.green.descendant_count
    return candidate.entry
  end

  token = @lexed.raw_tokens[@token_index]
  return false unless token

  @token_index += 1
  token
end

#token_countObject



96
# File 'lib/ibex/runtime/cst/incremental/blender.rb', line 96

def token_count = @lexed.memo.tokens.length