Class: MessagePack::IDL::Evaluator

Inherits:
Object
  • Object
show all
Includes:
ProcessorModule
Defined in:
lib/msgpack/idl/evaluator.rb

Defined Under Namespace

Classes: InheritAllMark, InheritMark, InheritMarkWithCheck, Template

Instance Attribute Summary

Attributes included from ProcessorModule

#log

Instance Method Summary collapse

Methods included from ProcessorModule

#log_error, #log_trace, #log_warn

Constructor Details

#initializeEvaluator



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/msgpack/idl/evaluator.rb', line 84

def initialize
  @names = {}  # name:String => AST::Element

  @types = {}  # name:String => AST::Type
  @generic_types = []  # Template

  @global_namespace = [""]   # Namespace
  @lang_namespace = {}     # lang:String => scope:Namespace

  @service_versions = {} # serviceName:String => (IR::Service, [(IR::ServiceVersion, AST::ServiceVersion)])

  init_built_in

  @ir_types = []
  @ir_services = []
  @ir_applications = []
end

Instance Method Details

#evaluate(ast) ⇒ Object



102
103
104
105
106
# File 'lib/msgpack/idl/evaluator.rb', line 102

def evaluate(ast)
  ast.each {|e|
    evaluate_one(e)
  }
end

#evaluate_inheritanceObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/msgpack/idl/evaluator.rb', line 159

def evaluate_inheritance
  @ir_services = @service_versions.values.map {|s,versions|
    versions = versions.sort_by {|sv,ast| sv.version }

    super_versions = []
    versions.each do |sv,ast|
      begin
        real_functions = []
        sv.functions.each {|f|
          case f
          when InheritAllMark
            begin
              if super_versions.empty?
                raise InheritanceError, "Inherit on the oldest version is invalid"
              end
              last = super_versions.last
              last.functions.each {|ifunc|
                real_functions << IR::InheritedFunction.new(last.version, ifunc)
              }
            rescue => error
              raise_error(error, f.ast)
            end

          when InheritMark
            begin
              if super_versions.empty?
                raise InheritanceError, "Inherit on the oldest version is invalid"
              end
              inherit_func = nil
              inherit_version = nil
              super_versions.reverse_each do |ssv|
                inherit_func = ssv.functions.find {|ifunc| f.name == ifunc.name }
                if inherit_func
                  inherit_version = ssv.version
                  break
                end
              end

              unless inherit_func
                raise InheritanceError, "No such function: #{f.name}"
              end

              if f.is_a?(InheritMarkWithCheck)
                if inherit_func.args != f.func.args ||
                    inherit_func.return_type != f.func.return_type ||
                    inherit_func.exceptions != f.func.exceptions
                  raise InheritanceError, "Function signature is mismatched with #{s.name}:#{inherit_version}.#{f.name}"
                end
              end

              real_functions << IR::InheritedFunction.new(inherit_version, inherit_func)
            rescue => error
              raise_error(error, f.ast)
            end

          when IR::Function
            real_functions << f

          else
            raise "Unknown partially evaluated function: #{f.inspect}"
          end
        }

        if real_functions.uniq!
          # may be caused by InheritAllMark
          # FIXME show warning?
        end

        sv.functions = real_functions

        super_versions << sv
      rescue => error
        raise_error(error, ast)
      end
    end
    s.versions = super_versions

    s
  }

  self
end

#evaluate_one(e) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
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
154
155
156
157
# File 'lib/msgpack/idl/evaluator.rb', line 108

def evaluate_one(e)
  case e
  when AST::Namespace
    add_namespace(e)

  when AST::Exception
    check_name(e.name, e)
    if e.super_class
      super_message = resolve_type(e.super_class)
      if !super_message.is_a?(IR::Exception)
        raise InvalidNameError, "Super class of the exception `#{e.super_class}' must be an exception"
      end
    end
    new_fields = resolve_fields(e.fields, super_message)
    add_exception(e.name, super_message, new_fields)

  when AST::Message
    check_name(e.name, e)
    if e.super_class
      super_message = resolve_type(e.super_class)
      if !super_message.is_a?(IR::Message)
        raise InvalidNameError, "Super class of the message `#{e.super_class}' must be a message"
      end
    end
    new_fields = resolve_fields(e.fields, super_message)
    add_message(e.name, super_message, new_fields)

  when AST::Enum
    check_name(e.name, e)
    fields = resolve_enum_fields(e.fields)
    add_enum(e.name, fields)

  when AST::Service
    v = e.version || 0
    check_service_version(e.name, v)
    funcs = resolve_service_partial(e.functions)
    add_service_version(e, e.name, v, funcs)

  when AST::Application
    check_name(e.name, e)
    scopes = resolve_scopes(e.scopes)
    add_application(e.name, scopes)

  else
    raise SemanticsError, "Unknown toplevel AST element `#{e.class}'"
  end

rescue => error
  raise_error(error, e)
end

#evaluate_spec(lang) ⇒ Object



242
243
244
245
246
247
248
249
# File 'lib/msgpack/idl/evaluator.rb', line 242

def evaluate_spec(lang)
  lang = lang.to_s
  ns = spec_namespace(lang)
  types = spec_types(lang)
  services = spec_services(lang)
  applications = spec_applications(lang)
  IR::Spec.new(ns, types, services, applications)
end