Code Monkey home page Code Monkey logo

sqltraverse's Introduction

Overview | Features | Installation | Credits | Issues


Overview

SQLTraverse is AST (abstract syntax tree) walker. This allows you to move through the AST generated by the Codeschool sqlite-parser in a structured manner. If you've ever used Estraverse before it works in the exact same way (in fact we use Estraverse under the hood).

⬆️

Features

The following code will do a simple walk of the AST passed to it. We can take actions either when we enter the node or when we leave the node.

sqltraverse.traverse(ast, {
    enter: function (node, parent) {
        if (node.type == 'statement' || node.type == 'assignment')
            return sqltraverse.VisitorOption.Skip;
    },
    leave: function (node, parent) {
        if (node.type == 'literal')
          console.log(node.value);
    }
});

We can use this.skip, this.remove and this.break functions instead of using Skip, Remove and Break.

sqltraverse.traverse(ast, {
    enter: function (node) {
        this.break();
    }
});

And sqltraverse provides sqltraverse.replace function. When returning node from enter/leave, current node is replaced with it.

result = sqltraverse.replace(tree, {
    enter: function (node) {
        // Replace it with replaced.
        if (node.type === 'literal')
            return replaced;
    }
});

By passing visitor.keys mapping, we can extend sqltraverse traversing functionality.

// This tree contains a user-defined `TestExpression` node.
var tree = {
    type: 'TestExpression',

    // This 'argument' is the property containing the other **node**.
    argument: {
        type: 'literal',
        value: 20
    },

    // This 'extended' is the property not containing the other **node**.
    extended: true
};
sqltraverse.traverse(tree, {
    enter: function (node) { },

    // Extending the existing traversing rules.
    keys: {
        // TargetNodeName: [ 'keys', 'containing', 'the', 'other', '**node**' ]
        TestExpression: ['argument']
    }
});

By passing visitor.fallback option, we can control the behavior when encountering unknown nodes.

// This tree contains a user-defined `TestExpression` node.
var tree = {
    type: 'TestExpression',

    // This 'argument' is the property containing the other **node**.
    argument: {
        type: 'literal',
        value: 20
    },

    // This 'extended' is the property not containing the other **node**.
    extended: true
};
sqltraverse.traverse(tree, {
    enter: function (node) { },

    // Iterating the child **nodes** of unknown nodes.
    fallback: 'iteration'
});

When visitor.fallback is a function, we can determine which keys to visit on each node.

// This tree contains a user-defined `TestExpression` node.
var tree = {
    type: 'TestExpression',

    // This 'argument' is the property containing the other **node**.
    argument: {
        type: 'literal',
        value: 20
    },

    // This 'extended' is the property not containing the other **node**.
    extended: true
};
sqltraverse.traverse(tree, {
    enter: function (node) { },

    // Skip the `argument` property of each node
    fallback: function(node) {
        return Object.keys(node).filter(function(key) {
            return key !== 'argument';
        });
    }
});

⬆️

Installation

⬆️

Using this project

⬆️

Credits

jdrew1303

Copyright (c) 2016

Issues

  • Tests, tests and more tests.
  • Documentation currently under construction (The examples need to be worked through for SQL instead of JavaScript).
  • AST is currently in a state of flux for some node types. We also probably need a builder for these nodes (another project).
  • You need to pass in ast.statement and not just the raw ast given back from the parser. (This is an issue with the base node not having a type.)

⬆️

sqltraverse's People

Contributors

jdrew1303 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.