custom AST library

Andrej Mitrovic andrej.mitrovich at gmail.com
Tue Dec 21 13:57:59 PST 2010


On 12/21/10, Alex_Dovhal <alex_dovhal at yahoo.com> wrote:
> User can put entire function body or even almost full module (if don't care
> about compile time) into that DSL in order not to change from DSL to normal
> code and back.

Yep. You could even take it a step further and use some form of escape
mechanism, which would leave D code alone but parse the rest of the
string as a DSL. E.g.:

mixin(MyDSL!(q{

    $(D)
    float a = 1 ;    // will be mixed in as is
    int n = 10 ;
    void test(int t)
    {
        writeln(t);
    }
    $(/D)  // end of D code

   x = sum(i=0:10; a*i) ;  // will be parsed and reconstructed as D code
   y = sum(i=1:n; i*prod!(j=0:i ; i+j);
}));

So now you wouldn't have to write a lot of MyDSL blocks in-between D code.

> One more thing to think about - how to show correct error
> line number in there?

See https://docs.google.com/leaf?id=0B-f1J0HiG2ovNmQzNzM1ODYtNDM3ZC00N2I4LWI2YmUtYTIyM2Y2MzcyZWIw&sort=name&layout=list&num=50&pli=1
, there's a unittests.d module in /src in the archive. It uses a
parameter that is default initialized to the line (size_t line =
__LINE__))

I guess from there you could chomp up your string into an array for
each new line, and if your DSL parser finds a syntax error it would
report the line number as e.g. currentLineNumber + line, where
currentLineNumber is the line number you're currently parsing, e.g.
DSLStringArray[currentLineNumber] << this line has the error.

I think that's about it (but I haven't had my nighttime coffee yet so
don't trust me fully on this one :p).


More information about the Digitalmars-d mailing list