Class: AbiCoderRb::Type::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/abi_coder_rb/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

Class Method Details

._parse_array_type(subtype, dims) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/abi_coder_rb/parser.rb', line 84

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/abi_coder_rb/parser.rb', line 56

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



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/abi_coder_rb/parser.rb', line 71

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.map do |dim|
    size = dim[1...-1]
    size == "" ? -1 : size.to_i
  end
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/abi_coder_rb/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 ","
      if depth == 0
        collected << current
        current = ""
      else
        current += c
      end
    when "("
      depth += 1
      current += c
    when ")"
      depth -= 1
      current += c
    else
      current += c
    end
  end
  collected << current unless current.empty?

  collected
end

._validate_base_type(base, sub) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/abi_coder_rb/parser.rb', line 100

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



11
12
13
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
42
43
44
45
# File 'lib/abi_coder_rb/parser.rb', line 11

def self.parse(type)
  type = type.strip

  if type =~ TUPLE_TYPE_RX
    types = _parse_tuple_type(::Regexp.last_match(1))
    dims = _parse_dims(::Regexp.last_match(2))

    parsed_types = types.map { |t| parse(t) }

    return _parse_array_type(Tuple.new(parsed_types), dims)
  end

  # uint256 => uint, 256, nil
  # uint256[2] => uint, 256, [2]
  # uint256[] => uint, 256, [-1]
  # bytes => bytes, nil, []
  base, sub, dims = _parse_base_type(type)

  sub ||= 256 if type.start_with?("uint") || type.start_with?("int") # default to 256 if no sub given
  _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