Class: Ibex::Runtime::CST::ParseMemo

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

Overview

Parser-state metadata parallel to Green preorder occurrences.

Defined Under Namespace

Classes: Entry

Constant Summary collapse

VERSION =

: Integer

1
ENTRY_BYTES =

: Integer

8

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(left_states:, grammar_digest:, state_count:, production_count:) ⇒ ParseMemo

Returns a new instance of ParseMemo.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ibex/runtime/cst/incremental/parse_memo.rb', line 19

def initialize(left_states:, grammar_digest:, state_count:, production_count:)
  unless left_states.all? { |state| state.nil? || state.between?(0, state_count - 1) }
    raise ArgumentError, "parse memo contains an invalid parser state"
  end

  @left_states = left_states.dup.freeze
  @grammar_digest = grammar_digest.dup.freeze
  @state_count = state_count
  @production_count = production_count
  freeze
end

Instance Attribute Details

#grammar_digestObject (readonly)

: String



13
14
15
# File 'lib/ibex/runtime/cst/incremental/parse_memo.rb', line 13

def grammar_digest
  @grammar_digest
end

#left_statesObject (readonly)

: Array



12
13
14
# File 'lib/ibex/runtime/cst/incremental/parse_memo.rb', line 12

def left_states
  @left_states
end

#production_countObject (readonly)

: Integer



15
16
17
# File 'lib/ibex/runtime/cst/incremental/parse_memo.rb', line 15

def production_count
  @production_count
end

#state_countObject (readonly)

: Integer



14
15
16
# File 'lib/ibex/runtime/cst/incremental/parse_memo.rb', line 14

def state_count
  @state_count
end

Instance Method Details

#compatible?(tables) ⇒ Boolean

Returns:

  • (Boolean)


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

def compatible?(tables)
  @grammar_digest == tables[:grammar_digest] &&
    @state_count == tables[:state_count] &&
    @production_count == tables[:production_count]
end

#estimated_bytesObject



50
# File 'lib/ibex/runtime/cst/incremental/parse_memo.rb', line 50

def estimated_bytes = @left_states.length * ENTRY_BYTES

#left_state(preorder_index) ⇒ Object



32
# File 'lib/ibex/runtime/cst/incremental/parse_memo.rb', line 32

def left_state(preorder_index) = @left_states.fetch(preorder_index)

#slice(preorder_index, element) ⇒ Object

Raises:

  • (IndexError)


35
36
37
38
39
40
# File 'lib/ibex/runtime/cst/incremental/parse_memo.rb', line 35

def slice(preorder_index, element)
  value = @left_states.slice(preorder_index, element.descendant_count)
  return value if value && value.length == element.descendant_count

  raise IndexError, "parse memo subtree range is outside the preorder state array"
end

#to_hObject



53
54
55
# File 'lib/ibex/runtime/cst/incremental/parse_memo.rb', line 53

def to_h
  { "version" => VERSION, "left_states" => @left_states }.freeze
end