tags:

views:

37

answers:

1

I'm implementing in Groovy a DSL for some existing file format. In this format we have a construct like


group basic_test {
   test vplan_testing  {
         dir: global_storage;
   };
};

And here I have problem with this dir: global_storage - groovy considers "dir:" as a label, so I can't handle it.

Do you have an idea how I can receive some callback (getProperty, invokeMissingMethod) for this construct?

Thank you!

A: 

I don't believe you can achieve that this way, you need to change your dsl a bit to be able to capture that information. Here's how you could achieve that:

class Foo {
    static plan = {
        vplan_testing {
            dir 'global_storage'
        }
    }
}

def closure = Foo.plan
closure.delegate = this
closure()

def methodMissing(String name, Object args) {   
    println "$name $args"     
    if(args[0] instanceof Closure) 
       args[0].call()
} 

The output will be

*dir [global_storage]*

or you could defined you dsl this way:

class Foo {
    static plan = {
        vplan_testing {
            test dir:'global_storage'
        }
    }
}

replace "test" by something meaningful to you domain. In this case the output would be

*test [[dir:global_storage]]*

Hope this helps

-ken

ken
Thank you. I hoped that I can use Groovy DSL as a parser for existing format w/o any change