Git Product home page Git Product logo

solidity-pegjs-parser's Introduction

Solidity PEGJS Grammar Parser

nodejs

GitHub Source

npm solidity-pegjs-parser

Abstract

$ npm install pegis-solidity

Ideal for AST use-cases

Overview

pegis-solidity

original consensys/solidity-parser with additional project specific grammar rules

  • Main change is adoption of peggy as the peg.js framework has been stagnant, this is a drop in replacement.

Latest Changelog

  • Added support for gwei units.
    uint256 gas = 10 gwei;
  • Fixed a bug that didn't recognize empty TryCatch statements with a return expresion.
    try Foo.bar() returns (uint result) {} catch {}
  • Fixed a bug that didn't recognize Enum declarations
    enum Foo {
        bar
    }

Thanks to https://github.com/wrong7

Reference for Changes

v2 changes start here: 18 August 2021

  • feat(refactor): migrate to peggy and various improvements 87b594a
  • build(refactor): improve build process 0de7a77
  • chore(repo): remove dead and legacy artifacts 012fec1

Usage

import { solidityparser } from 'pegis-solidity';

command line

$ ./node_modules/.boin/pegis-solidity $PWD/file_name.js

Example

Consider this solidity code as input:

import "Foo.sol";

contract MyContract {
  mapping (uint => address) public addresses;
}

Generated output as AST output:

{
  "type": "Program",
  "body": [
    {
      "type": "ImportStatement",
      "value": "Foo.sol"
    },
    {
      "type": "ContractStatement",
      "name": "MyContract",
      "is": [],
      "body": [
        {
          "type": "ExpressionStatement",
          "expression": {
            "type": "DeclarativeExpression",
            "name": "addresses",
            "literal": {
              "type": "Type",
              "literal": {
                "type": "MappingExpression",
                "from": {
                  "type": "Type",
                  "literal": "uint",
                  "members": [],
                  "array_parts": []
                },
                "to": {
                  "type": "Type",
                  "literal": "address",
                  "members": [],
                  "array_parts": []
                }
              },
              "members": [],
              "array_parts": []
            },
            "is_constant": false,
            "is_public": true
          }
        }
      ]
    }
  ]
}
var SolidityParser = require('pegis-solidity');

// Parse Solidity code as a string:
var result = SolidityParser.parse('contract { ... }');

// Or, parse a file:
var result = SolidityParser.parseFile('./path/to/file.sol');

Updates to Grammar

A full list can be found under the DIFF.md document here

 HexStringLiteral
-  = HexToken StringLiteral
+  = HexToken val:StringLiteral {
+    return {
+      type: "HexLiteral",
+      value: val,
+      start: location().start.offset,
+      end: location().end.offset
+    };
+  }

License

ISC / MIT

solidity-pegjs-parser's People

Contributors

sambacha avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

wrong7 seanpm2001

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.