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

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

Overview

Parser-state metadata parallel to Green preorder occurrences.

Defined Under Namespace

Classes: Entry

Constant Summary collapse

VERSION =

Signature:

  • Integer

Returns:

  • (Integer)
1
ENTRY_BYTES =

Signature:

  • Integer

Returns:

  • (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.

RBS:

  • (left_states: Array[Integer?], grammar_digest: String, state_count: Integer, production_count: Integer) -> void

Parameters:

  • left_states: (Array[Integer?])
  • grammar_digest: (String)
  • state_count: (Integer)
  • production_count: (Integer)


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_digestString (readonly)

Signature:

  • String

Returns:

  • (String)


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

def grammar_digest
  @grammar_digest
end

#left_statesArray[Integer?] (readonly)

Signature:

  • Array[Integer?]

Returns:

  • (Array[Integer?])


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

def left_states
  @left_states
end

#production_countInteger (readonly)

Signature:

  • Integer

Returns:

  • (Integer)


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

def production_count
  @production_count
end

#state_countInteger (readonly)

Signature:

  • Integer

Returns:

  • (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

RBS:

  • (Hash[Symbol, untyped] tables) -> bool

Parameters:

  • tables (Hash[Symbol, untyped])

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_bytesInteger

RBS:

  • () -> Integer

Returns:

  • (Integer)


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

def estimated_bytes = @left_states.length * ENTRY_BYTES

#left_state(preorder_index) ⇒ Integer?

RBS:

  • (Integer preorder_index) -> Integer?

Parameters:

  • preorder_index (Integer)

Returns:

  • (Integer, nil)


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) ⇒ Array[Integer?]

RBS:

  • (Integer preorder_index, GreenNode | GreenToken element) -> Array[Integer?]

Parameters:

Returns:

  • (Array[Integer?])


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_hHash[String, untyped]

RBS:

  • () -> Hash[String, untyped]

Returns:

  • (Hash[String, untyped])


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