Git Product home page Git Product logo

soltar's Introduction

soltar

Generate Solidity Code from its Abstract Syntax Tree. (The AST must follow the Spider Monkey API for defining AST nodes).

#Application Don't like your peers' Solidity coding style? Always getting into the tabs vs. spaces debates? But still gotta comply with a standard style that everybody follows? No problem!

Write your Solidity code with style that aesthetically pleases you, generate its AST and give it to Soltar to generate code in a standard style your company follows.

Now you get to work with your own style on your local machine and push the code with style that pleases your boss!

#AST!

A couple of modules exist for you to generate AST of your Solidity Code:

solidity-parser (by tcoulter from Consensys)

npm install --save solidity-parser

solparse (I'm the author)

npm install solparse

#Installation

npm install --save soltar

#Documentation

To use Soltar in Browser, include:

<script src="soltar-bundle.js"></script>

You can then access the Soltar object by using window.Soltar or simply Soltar.

In order to access Soltar's functionality in Node.js, require() it like:

let Soltar = require ('soltar');

#API

  1. generate - The main function that takes 2 arguments: ast (the Solidity Code's abstract syntax tree (following the Spider monkey API) & options (optional) to confgure the output

  2. version - Get version information

#Example A typical AST would look like:

{
  "type": "Program",
  "body": [
    {
      "type": "ExpressionStatement",
      "expression": {
        "type": "AssignmentExpression",
        "operator": "=",
        "left": {
          "type": "DeclarativeExpression",
          "name": "myVar",
          "literal": {
            "type": "Type",
            "literal": "uint",
            "members": [],
            "array_parts": [
              3
            ]
          },
          "is_constant": false,
          "is_public": false,
          "is_memory": false
        },
        "right": {
          "type": "ArrayExpression",
          "elements": [
            {
              "type": "Literal",
              "value": 1
            },
            {
              "type": "Literal",
              "value": 2
            },
            {
              "type": "Literal",
              "value": 3
            }
          ]
        }
      }
    }
  ]
}

The default options configuration is:

let options = {
	format: {
		indent: {
			style: '\t',
			base: 0
		},
		newline: '\n',
		space: ' ',
		quotes: 'single',
		minify: false
	}
}

##Usage

/*
	AST is the solidity-parser generated Abstract Syntax Tree
	soltar is the require()d object
*/

let options = {
	format: {
		indent: {
			style: '\t',
			base: 0
		},
		newline: '\n\n',
		space: ' ',
		quotes: 'double'
	}
};
	
let sourceCode = soltar.generate (AST, options);

console.log (sourceCode);

##Output

contract Vote {

	address public creator;
	
	function Vote () {
	
		creator = msg.sender;
		
	}
	
}

The above solidity code corresponds to this Abstract Syntax Tree

See examples for a full contract example.

#Future enhancements:

1. Commandline utility

soltar's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

soltar's Issues

Status of this project

Hi @duaraghav8 , when I was looking up for AST to Solidity tools/libraries, soltar happens to be the first and the only result. As what it looks like, it doesn't support many new features of newer version of Solidity. Do you plan to add the support?

Thanks! :)

bracketing fuckups

if (a && (b || (!c))) { foo (); }

the AST for the above, when passed to soltar.generate (), generates the following:

if (a && b || !c) {\n\tfoo ();\n}

Another example

(89 * 2) - (12 + (34 / (y**2))) + (23 % (2**2))

output:

89 * 2 - 12 + 34 / y ** 2 + 23 % 2 ** 2;

there should be brackets :'(

Hex values are not handled

solidity-parser generates following representation of Hex Values:

{type: "Literal", value: '0x89'}

Need safe way to distinguish Hex values from typical strings (can't simply make a check for '0x' prefix, this can be part of a normal string also)

life is just unfair :'-(

Shitloads of new features in Solidity

New features are being added and the developers of solidity-parser are working hard at implementing the all in their Abstract Syntax tree. Keep up

Fails with `GENERATOR_OBJECT[item.type] is not a function`

I'm trying to set this up, using the AST generated from solparse with this file, but I get the following error:

/Users/danielque/git/cryptofin-solidity/node_modules/soltar/index.js:342
                                        GENERATOR_OBJECT [item.type] (item),
                                                                     ^

TypeError: GENERATOR_OBJECT[item.type] is not a function
    at /Users/danielque/git/cryptofin-solidity/node_modules/soltar/index.js:342:35
    at Array.forEach (<anonymous>)
    at CodeGenerator.ContractStatement (/Users/danielque/git/cryptofin-solidity/node_modules/soltar/index.js:339:14)
    at /Users/danielque/git/cryptofin-solidity/node_modules/soltar/index.js:830:34
    at Array.forEach (<anonymous>)
    at CodeGenerator.Program (/Users/danielque/git/cryptofin-solidity/node_modules/soltar/index.js:825:13)
    at CodeGenerator.generateStatement (/Users/danielque/git/cryptofin-solidity/node_modules/soltar/index.js:219:26)
    at CodeGenerator.generate (/Users/danielque/git/cryptofin-solidity/node_modules/soltar/index.js:228:15)
    at Object.generate (/Users/danielque/git/cryptofin-solidity/node_modules/soltar/index.js:1474:4)
    at Object.<anonymous> (/Users/danielque/git/cryptofin-solidity/test.js:105:31)

Without being familiar with the code, this is perhaps caused by lack of support for these?

  • PragmaStatement
  • LibraryStatement
  • ModifierArgument

Type: what is members: [ ] for?

in the Type node:

{ type: 'Type', literal: 'address', members: [], array_parts: [ 5 ] };

what the fuck is members: [] for??

TODO

  • Demo on gh-pages
  • Add Command-line utility
  • Write tests & set up coverage info
  • Minify soltar-bundle.js
  • browser compatibility
  • Inform of currently un-patched bug in StructDeclaration in solidity-parser and in IsStatement

Solidity-parser Bugs

List of (unfixed) bugs in solidity-parser

  1. No support for RegExp - the literal object's value gets set to {}, eg -
    {type: "Literal", value: {}}
  2. No 'name' field in StructDeclaration - this means we never get to know the name of the struct from the StructDeclaration Node object. (I've made a PR for this)
  3. IsStatement malfunctioning - When using inheritance with Contract or Library, the syntax is:
contract Car is Vehicle, Engine {
        //definition
}

Solidity parser doesn't parse the 'is' section properly. the is Array's first object is fine (vehicle), but subsequent element is just a comma, not another object for Engine.

  1. Exponentiation operator (double asterisk) not being parsed by solidity parser
  2. A statement like uint x = 2 days; doesn't get parsed. It gives error at start of 'days'.
  3. Parse of var (x) = 100; failing because x is surrounded by brackets (which is legal in solidity).

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.