DIP 50 - AST macros
Jacob Carlborg
doob at me.com
Mon Nov 11 02:04:30 PST 2013
On 2013-11-11 10:38, Rikki Cattermole wrote:
> An example of this might be:
>
> macro foo (Context context, Ast!(string) str)
> if (lexer.isSymbol(0, SymbolTypes.Class))
> {
> return str;
> }
>
> class Bar {
> int i;
> }
>
> foo {
> Bar(7);
> }
> The above code would succeed however the below code will give a compiler
> error stating that the given macro is not found.
>
> struct Haz {
> int i;
> }
>
> foo {
> Haz(9);
> }
>
> The above code is based on the assumption of a D lexer in phobos.
> This is simpler set of code which won't require a lexer.
>
> macro foo (Context context, Ast!(string) str)
> if (str == "working")
> {
> return "";
> }
>
> foo {fails}
> foo {working}
> In these cases its a simple string test to determine if the text given
> is specified value.
>
> In the original example I gave, I was using regex to make the point of
> validation for a given line passed to the macro.
My idea of handling errors in macros is more something like triggering a
real compile error. That could be done via the context parameter,
something like:
macro foo (Context context, Ast!(string) str)
{
if (str.eval() == "bar")
context.error("Illegal value 'bar'"); // this will trigger the
compile error
}
--
/Jacob Carlborg
More information about the Digitalmars-d
mailing list