Version 10 by komaz
on Nov 22, 2010 18:44.

compared with
Version 11 by komaz
on Nov 22, 2010 19:22.

Key
This line was removed.
This word was removed. This word was added.
This line was added.

Changes (1)

View Page History
}
{noformat}

h3. Magic facets
DSL plugins is the backdoor which gives a full access to compiler API and allows to do almost everything. However, in such use cases, we don't need the return value of DSL expression. One of the ways of 'organizing chaos' can be something like below.
Let's define facet {{Magic}}:
{noformat}
facet class Magic
{
const Obj? kind
}
{noformat}

It's has a single purpose - we can put it on type or slot. The field {{kind}} has a single purpose too - we can assign DSL expression to it.
Also, we can define the base class for our 'magic' plugins, which could simplify the creation of plugins. Here's the basic idea:
{noformat}
abstract class MagicPlugin : DslPlugin
{
new make(Compiler c) : super(c) {}

override Expr compile(DslExpr expr)
{
loc := expr.loc
doMagic(type(loc, expr), slot(loc, expr))
return LiteralExpr.makeNull(expr.loc, ns)
}

**
** type - type definition of enclosing type
** method - method definition. If method is null,
** then magic applied to type, otherwise - to method
**
abstract Void doMagic(TypeDef type, SlotDef? slot)
}
{noformat}
The implementation of {{type}} and {{slot}} methods searching for appropriate definitions can be found [here|^MagicPlugin.fan].