Class: Ibex::Tables::CompactActions
- Inherits:
-
Compact
- Object
- Compact
- Ibex::Tables::CompactActions
show all
- Defined in:
- lib/json5/generated_parser.rb
Overview
Compact action table with an integer-coded hot representation and a
compatible decoded lookup surface for the generic runtime.
Constant Summary
collapse
- ACCEPT_CODE =
0
- ERROR_CODE =
-1
- SHIFT_BASE =
1
- REDUCE_BASE =
-2
Constants inherited
from Compact
Ibex::Tables::Compact::DENSE_CELL_LIMIT
Instance Attribute Summary collapse
Attributes inherited from Compact
#checks, #dense_values, #dense_width, #offsets, #row_count, #values
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Compact
#lookup
Constructor Details
#initialize(offsets:, codes:, checks:, row_count:, encoding: :signed, column_count: nil) ⇒ CompactActions
Returns a new instance of CompactActions.
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
|
# File 'lib/json5/generated_parser.rb', line 9758
def initialize(offsets:, codes:, checks:, row_count:, encoding: :signed, column_count: nil)
unless encoding == :signed
raise ArgumentError, "unknown compact action encoding #{encoding.inspect}; expected :signed"
end
@codes = codes.freeze
@column_count = column_count
@dense_codes = dense_action_layout(offsets, codes, checks, row_count, column_count)
decoded_cache = {} decoded = codes.map do |code|
next unless code
unpacked = self.class.unpack(code) decoded_cache[code] ||= unpacked
end
super(offsets: offsets, values: decoded, checks: checks, row_count: row_count)
end
|
Instance Attribute Details
#codes ⇒ Object
9677
9678
9679
|
# File 'lib/json5/generated_parser.rb', line 9677
def codes
@codes
end
|
#column_count ⇒ Object
9679
9680
9681
|
# File 'lib/json5/generated_parser.rb', line 9679
def column_count
@column_count
end
|
#dense_codes ⇒ Object
9678
9679
9680
|
# File 'lib/json5/generated_parser.rb', line 9678
def dense_codes
@dense_codes
end
|
Class Method Details
.build(rows) ⇒ Object
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
|
# File 'lib/json5/generated_parser.rb', line 9683
def build(rows)
packed_rows = rows.map do |row|
row.transform_values { |action| pack(action) }
end
layout = Compact.build(packed_rows, dense: false)
codes = layout.values column_count = rows.flat_map(&:keys).max.to_i + 1
column_count = nil if (rows.length * column_count) > Compact::DENSE_CELL_LIMIT
new(
offsets: layout.offsets,
codes: codes,
checks: layout.checks,
row_count: layout.row_count,
encoding: :signed,
column_count: column_count
)
end
|
.pack(action) ⇒ Object
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
|
# File 'lib/json5/generated_parser.rb', line 9719
def pack(action)
return unless action
case action.fetch(0)
when :accept then ACCEPT_CODE
when :error then ERROR_CODE
when :shift
state = action.fetch(1) SHIFT_BASE + state
when :reduce
production = action.fetch(1) REDUCE_BASE - production
else raise ArgumentError, "unknown compact parser action #{action.inspect}"
end
end
|
.packed(offsets, codes, checks, row_count:, encoding: :signed, column_count: nil) ⇒ Object
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
|
# File 'lib/json5/generated_parser.rb', line 9703
def packed(offsets, codes, checks, row_count:, encoding: :signed, column_count: nil)
unless encoding == :signed
raise ArgumentError, "unknown compact action encoding #{encoding.inspect}; expected :signed"
end
new(
offsets: PackedIntegers.decode_required(offsets),
codes: PackedIntegers.decode_signed(codes),
checks: PackedIntegers.decode(checks),
row_count: row_count,
encoding: encoding,
column_count: column_count
)
end
|
.unpack(code) ⇒ Object
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
|
# File 'lib/json5/generated_parser.rb', line 9736
def unpack(code)
return unless code
if code == ACCEPT_CODE
accept = [:accept].freeze return accept
end
if code == ERROR_CODE
error = [:error].freeze return error
end
if code.positive?
shift = [:shift, code - SHIFT_BASE].freeze return shift
end
[:reduce, REDUCE_BASE - code].freeze end
|
Instance Method Details
#row(row) ⇒ Object
rubocop:disable Lint/UselessMethodDefinition -- narrows the inherited row contract for typed parser actions.
9778
9779
9780
|
# File 'lib/json5/generated_parser.rb', line 9778
def row(row)
super end
|