DIP 50 - AST macros

Rikki Cattermole alphaglosined at gmail.com
Wed Nov 13 03:49:14 PST 2013


> From my point of view this whole idea is great as it makes it 
> easier
> what is already possible. For example, with current behavior if 
> I wanted
> to write.
>
> foo {
>   writeln("foo");
>   writeln("foo again");
> }

Currently this would be sent as a string. I have suggested 
previously (given a lexer implementation) a lexer would be 
included that can work by line and symbol. This is fine but it 
would mean the lexer would need to parse it before calling the 
macro itself. Perhaps it should be explored a forced parsing and 
validation of macro values?

> I would have to write:
>
> mixin(foo!(q{
>   writeln("foo");
>   writeln("foo again");
> }));

AST macros currently suggested do not work at the same stage as 
mixins, as far as I am aware. They work at a much earlier stage. 
Mixins have limits because of there lateness in lexing process.

> So the proposed behavior looks much nicer, and I agree with it 
> as the
> content of foo block is actually written in D and I think 
> whoever is
> reading it would be comfortable with it.
>
>
> However, for other, non-D syntax-es I would prefer something 
> like:
>
> query q{
>   from element in array
>   where element > 2
>   add element to data
> }

I find it intriguing to add a keyword to distinguish a D code 
block from a string. As currently they are parsed the same way.

> Which can be handled by:
>
> macro query (Context context, string dsl) {
>     return domainSpecificLanguageToD(dsl);
> }
>
> This in terms is already possible by writing the following, it 
> only
> allows to be written in a more readable way. And the q{ ... } 
> notation
> clearly points out that there is something special going on. 
> Also by
> passing such content as string user can implement custom (or 
> call one of
> the predefined) tokenizer/lexer/parser.
>
> mixin(query!(q{
>   from element in array
>   where element > 2
>   add element to data
> }));

Perhaps a different way of identifying such code is required?

query "{
    from element in array
    where element > 2
    add element to data
}

Although it would act as a triple quoted string, except only 
valid for macros maybe? This would signify that the following 
block of text was to be pushed into a macro. Otherwise it would 
be assumed as valid D code?

Assuming a lexer is available it could check that any code given 
to a macro is correct. I will be submitting a pull request for 
pragma error and warning so if it is not valid the macro could 
fire a compile error.


More information about the Digitalmars-d mailing list