Aravinth
Aravinth

Reputation: 401

What is an AST transformation?

What is an AST transformation in general? I came across these words when reading Groovy blog's. But what it is in general?

Upvotes: 40

Views: 18211

Answers (4)

coderaiser
coderaiser

Reputation: 827

AST is a tree representation of the abstract syntactic structure of source code written in a programming language.

When there is a need to transform code changing it parts usually transformer operates with tree representation of source code to find the node which requires changes using Visitor Pattern and to apply this changes.

For example putout code transformer for JavaScript supports direct manipulation with AST tree this way:

const putout = require('putout');

const removeDebugger = {
    report: () => 'debugger should not be used',
    fix: (path) => {
        path.remove();
    },
    traverse: ({push}) = ({
        'DebuggerStatement': (path) => {
            push(path);
        }
    }),
};

putout('const a = 5; debugger', {
    fix: true,
    plugins: [
        ['remove-debugger', removeDebugger]
    ]
});
// returns
({
    code: 'const a = 5;',
    places: [],
});

Anyways there is much simpler way to manipulate with AST used in @putout/plugin-remove-debugger:

const removeDebugger = {
    report: () => 'debugger should not be used',
    replace: () = ({
        'debugger': ''
    }),
};

In this example one expression replaced with another using templates language of @putout/engine-runner that helps to write simple code transformation without touching AST at all.

Worth mention that inside of replace transformation anyways used AST because it's the most powerful way of manipulation with source code.

Upvotes: 0

Markus
Markus

Reputation: 3353

AST means Abstract Syntax Tree, which is basically an abstract representation of code / any syntactic structure. A transformation is an action modifying this tree (i.e. transforming the existing AST to a new AST). For more information have a look here: http://en.wikipedia.org/wiki/Abstract_syntax_tree

Upvotes: 29

Ira Baxter
Ira Baxter

Reputation: 95352

The simple answer is any function that converts one AST, into another AST.

A more sophisticated view can be found in my SO answer on Model-driven development: What is a transform?

Upvotes: 5

SK-logic
SK-logic

Reputation: 9715

In addition to what have been mentioned already, you might also be interested in a broader and more fundamental concept of Term rewriting.

Upvotes: 7

Related Questions