Class: Okura::Parser::CharType

Inherits:
Object
  • Object
show all
Defined in:
lib/okura/parser.rb

Instance Method Summary collapse

Constructor Details

#initializeCharType

Returns a new instance of CharType.



70
71
72
73
74
75
76
# File 'lib/okura/parser.rb', line 70

def initialize
  @callbacks={
    :mapping_single=>[],
    :mapping_range=>[],
    :define_type=>[]
  }
end

Instance Method Details

#on_chartype_def(&b) ⇒ Object



86
87
88
# File 'lib/okura/parser.rb', line 86

def on_chartype_def &b
  @callbacks[:define_type] << b
end

#on_mapping_range(&b) ⇒ Object



82
83
84
# File 'lib/okura/parser.rb', line 82

def on_mapping_range &b
  @callbacks[:mapping_range] << b
end

#on_mapping_single(&b) ⇒ Object



78
79
80
# File 'lib/okura/parser.rb', line 78

def on_mapping_single &b
  @callbacks[:mapping_single] << b
end

#parse(line) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/okura/parser.rb', line 96

def parse line
  cols=line.gsub(/\s*#.*$/,'').split(/\s+/)
    return if cols.empty?
  case cols[0]
  when /^0x([0-9a-fA-F]{4})(?:\.\.0x([0-9a-fA-F]{4}))?$/
    # mapping
    parse_error line unless cols.size >= 2
    type=cols[1]
    compat_types=cols[2..-1]
    from=$1.to_i(16)
    if $2
      # mapping(range)
      to=$2.to_i(16)
      @callbacks[:mapping_range].each{|c|
        c.call from,to,type,compat_types
      }
    else
      # mapping(single)
      @callbacks[:mapping_single].each{|c|
        c.call from,type,compat_types
      }
    end
  when /^\w+$/
    parse_error line unless cols.size == 4
    # typedef
    @callbacks[:define_type].each{|c|
      c.call cols[0],(cols[1]=='1'),(cols[2]=='1'),Integer(cols[3])
    }
  else
    # error
    parse_error line
  end
end

#parse_all(io) ⇒ Object



90
91
92
93
94
# File 'lib/okura/parser.rb', line 90

def parse_all io
  io.each_line {|line|
    parse line
  }
end