How to parse a DSL Block and deferred the execution

I am trying to write a DSL as follows:-

def method method_name, &block
   #do somthing...
   define_method method_name , &block
   #do somthing...
   dummy_method = instance_method method_name
   #do sumthing...
   dummy_method.bind(self).call
end

def option_hash_has key_name
   if option_hash.has_key?
      true
   else
      false
   end
end
def option_hash key_name
    option_hash[key_name]
end

now our DSL code is as follows

method :method_1 do
    if options_hash_has("XYZ")
        system("echo 'option XYZ is set'")
    else
        sytem("echo 'option XYZ is not set'")
    end
end

Now my requirement is to check syntax error of method block initially
and deferred the execution.

For example: if I write option_hash_hass instead of
option_has_has(in method_1 block), this would tell user when
dummy_method.bind(:self).call would execute. But I want to make this
parsing is first step of method.

def method method_name &block
   parse_block(&block) # with respect to current dsl
   # do everything same as i mentioned
end

How could I implement this parse_block(&block)?

Asmita C. wrote in post #1142419:

For example: if I write option_hash_hass instead of
option_has_has(in method_1 block), this would tell user when
dummy_method.bind(:self).call would execute. But I want to make this
parsing is first step of method.

Since Ruby is dynamic a wrong method name is never a syntax error.
You can only detect syntax errors during parsing. You need to execute
the code in order to find out about wrong method names.

How could I implement this parse_block(&block)?

You might be able to create something by instance_eval’ing the block
with a special instance that sets a context used only for validation of
the code. I think that can become complex pretty soon.

Kind regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs