As an example, Joachim Baumann describes how to implement a simple DSL for working with distances . Using this DSL, you can write things like this to calculate the total distance travelled:
3.m + 2.yd + 2.mi - 1.km
This is a simple and expressive DSL, but when you type this into a Groovy Editor in Groovy-Eclipse:
declares a new property. The full form is like this. Name is the only required field:
property name : "nameOfTask", type: String, declaringType: "java.lang.String", isStatic : false,
isDeprecated: false, provider : "A readable name for your DSL",
doc : "<b>Enter javadoc here</b> html is supported"
declares a new method. The full form is like this. Name is the only required field:
method name : "nameOfTask", type: String, declaringType: "java.lang.String",
params : [ arg1 : String, arg2 : Class],
namedParams : [arg3 : Long, arg4 : Short],
optionalParams: [arg5: Byte], isStatic : false,
doc : "<b>Enter javadoc here</b> html is supported", provider : "A readable name for your DSL"
A note on parameter kinds:
params : regular parameters are added via content assist on the method
namedParams : named parameters are added via content assist on the method prefixed by a name. And if content assist is invoked after a paren or a comma (with no text prefix), unused named parameters will be suggested.
optionalParams : optional parameters are only available when performing completion after a paren or comma (with no text prefix). They are not included during normal method content assist.
Parameter names may include whitespace and special characters as long as the parameter is quoted. For example, it is possible to simulate varargs like this:
method name : "meth", params : ["... values", String]
adds all of the public methods in the delegated type to the current type. For example:
will add all public methods of list to the current type for content assist, underlining, hovers, and navigation.
(alternative form with named arguments) adds all of the public methods in the delegated type to the current type. There is a possibility to parameterize using named arguments:
type : (required) the type to delegate to.
asCategpry : (optional) if true, treat the delegated type as a category and only include static methods where the first parameter matches the type of the current type.
useNamed : (optional) if true, content assist will be applied using named arguments.
except : (optional) a list of method names to exclude from the delegation (useful if these methods are already being added through some other contribution).
isDeprecated : (optional) if true then this contribution is deprecated and all references to delegated methods will appear in the UI with a line through them.