[Python-ideas] AST Transformation Hooks for Domain Specific Languages

Eugene Toder eltoder at gmail.com
Fri Apr 8 15:15:44 CEST 2011


One of the tricky details is where to put ast.register() so that it
runs before module is parsed. Doing this in dsl.sql and importing it
in 'sql' modules is not enough. Sql modules will have to rely on
someone registering dsl early enough, e.g. with

import dsl.sql.register # register dsl
import sqlmodule # now import module using sql dsl

So registering dsl will be client's responsibility, rather than
something module can do for itself.
If this is OK, we can achieve similar effect without any changes to
Python -- for example, with import hooks. One can write a hook that
applies whatever AST transformations to modules loaded from specific
locations.

We can make AST transformation a part of module itself, e.g. with some
kind of "eager decorator". Taking your example:

@@dsl.sql.query
def lookup_address(name : dsl.sql.char, dob : dsl.sql.date) from dsl.sql:
   select address
   from people
   where name = {name} and dob = {dob}

Eager decorator has to be used by the fully qualified name. Parser
will import (and execute) defining module (dsl.sql in this example)
while compiling a module that uses it (not when module is executed, as
with normal decorator).

Eugene



More information about the Python-ideas mailing list