Class: ABI::Type::Parser
- Inherits:
-
Object
- Object
- ABI::Type::Parser
- Defined in:
- lib/abicoder/parser.rb
Overview
nested inside Type - why? why not?
Constant Summary collapse
- TUPLE_TYPE_RX =
/^\((.*)\) ((\[[0-9]*\])*) /x
- BASE_TYPE_RX =
Crazy regexp to seperate out base type component (eg. uint), size (eg. 256, 128, nil), array component (eg. [], [45], nil)
/([a-z]*) ([0-9]*) ((\[[0-9]*\])*) /x
Class Method Summary collapse
- ._parse_array_type(subtype, dims) ⇒ Object
- ._parse_base_type(str) ⇒ Object
- ._parse_dims(str) ⇒ Object
- ._parse_tuple_type(str) ⇒ Object
- ._validate_base_type(base, sub) ⇒ Object
- .parse(type) ⇒ Object
Class Method Details
._parse_array_type(subtype, dims) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/abicoder/parser.rb', line 81 def self._parse_array_type( subtype, dims ) ## ## todo/check - double check if the order in reverse ## in solidity / abi encoding / decoding? ## dims.each do |dim| subtype = if dim == -1 Array.new( subtype ) else FixedArray.new( subtype, dim ) end end subtype end |
._parse_base_type(str) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/abicoder/parser.rb', line 52 def self._parse_base_type( str ) _, base, subscript, dimension = BASE_TYPE_RX.match( str ).to_a ## note: use [Integer,Integer] array in the future for sub ## for fixed (e.g. 128x128 => [128,128]) or such ## for now always assume single integer (as string) sub = subscript == '' ? nil : subscript.to_i ## e.g. turn "[][1][2]" into [-1,1,2] ## or "" into [] -- that is, empty array dims = _parse_dims( dimension ) [base, sub, dims] end |
._parse_dims(str) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/abicoder/parser.rb', line 67 def self._parse_dims( str ) dims = str.scan( /\[[0-9]*\]/ ) ## note: return -1 for dynamic array size e.g. [] ## e.g. "[]"[1...-1] => "" ## "[0]"[1...-1] => "0" ## "[1]"[1...-1] => "1" dims = dims.map do |dim| size = dim[1...-1] size == '' ? -1 : size.to_i end dims end |
._parse_tuple_type(str) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/abicoder/parser.rb', line 121 def self._parse_tuple_type( str ) ## note: types assumes string WITHOUT enclosing () e.g. ## tuple(string,string,bool) => expected as "string,string,bool" depth = 0 collected = [] current = '' ### todo/fix: replace with a simple parser!!! ## allow () and move verbose tuple() too!!! str.each_char do |c| case c when ',' then if depth == 0 collected << current current = '' else current += c end when '(' then depth += 1 current += c when ')' then depth -= 1 current += c else current += c end end collected << current unless current.empty? collected end |
._validate_base_type(base, sub) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/abicoder/parser.rb', line 98 def self._validate_base_type( base, sub ) case base when 'string' # note: string can not have any suffix raise ParseError, "String cannot have suffix" if sub when 'bytes' raise ParseError, "Maximum 32 bytes for fixed-length bytes" if sub && sub > 32 when 'uint', 'int' raise ParseError, "Integer type must have numerical suffix" unless sub raise ParseError, "Integer size out of bounds" unless sub >= 8 && sub <= 256 raise ParseError, "Integer size must be multiple of 8" unless sub % 8 == 0 when 'address' raise ParseError, "Address cannot have suffix" if sub when 'bool' raise ParseError, "Bool cannot have suffix" if sub else ## puts " type: >#{type}<" raise ParseError, "Unrecognized type base: #{base}" end end |
.parse(type) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/abicoder/parser.rb', line 14 def self.parse( type ) if type =~ TUPLE_TYPE_RX types = _parse_tuple_type( $1 ) dims = _parse_dims( $2 ) parsed_types = types.map{ |t| parse( t ) } return _parse_array_type( Tuple.new( parsed_types ), dims ) end base, sub, dims = _parse_base_type( type ) _validate_base_type( base, sub ) subtype = case base when 'string' then String.new when 'bytes' then sub ? FixedBytes.new( sub ) : Bytes.new when 'uint' then Uint.new( sub ) when 'int' then Int.new( sub ) when 'address' then Address.new when 'bool' then Bool.new else ## puts " type: >#{type}<" raise ParseError, "Unrecognized type base: #{base}" end _parse_array_type( subtype, dims ) end |