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

Inherits:
Object
  • Object
show all
Defined in:
lib/json5/generated_parser.rb

Overview

Parser-state metadata parallel to Green preorder occurrences.

Defined Under Namespace

Classes: Entry

Constant Summary collapse

VERSION =

Signature:

  • Integer

1
ENTRY_BYTES =

Signature:

  • 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



2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
# File 'lib/json5/generated_parser.rb', line 2939

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)

Signature:

  • String



2933
2934
2935
# File 'lib/json5/generated_parser.rb', line 2933

def grammar_digest
  @grammar_digest
end

#left_statesObject (readonly)

Signature:

  • Array[Integer?]



2932
2933
2934
# File 'lib/json5/generated_parser.rb', line 2932

def left_states
  @left_states
end

#production_countObject (readonly)

Signature:

  • Integer



2935
2936
2937
# File 'lib/json5/generated_parser.rb', line 2935

def production_count
  @production_count
end

#state_countObject (readonly)

Signature:

  • Integer



2934
2935
2936
# File 'lib/json5/generated_parser.rb', line 2934

def state_count
  @state_count
end

Instance Method Details

#compatible?(tables) ⇒ Boolean

RBS:

  • (Hash[Symbol, String | Integer] tables) -> bool

Returns:

  • (Boolean)


2963
2964
2965
2966
2967
# File 'lib/json5/generated_parser.rb', line 2963

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

#estimated_bytesObject

RBS:

  • () -> Integer



2970
# File 'lib/json5/generated_parser.rb', line 2970

def estimated_bytes = @left_states.length * ENTRY_BYTES

#left_state(preorder_index) ⇒ Object

RBS:

  • (Integer preorder_index) -> Integer?



2952
# File 'lib/json5/generated_parser.rb', line 2952

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

#slice(preorder_index, element) ⇒ Object

RBS:

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

Raises:

  • (IndexError)


2955
2956
2957
2958
2959
2960
# File 'lib/json5/generated_parser.rb', line 2955

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

RBS:

  • () -> Hash[String, Integer | Array[Integer?]]



2973
2974
2975
# File 'lib/json5/generated_parser.rb', line 2973

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