Skip to content

Solidity Parser for Coverage Testing utilizing new Peggy PEG.js Parser

License

Notifications You must be signed in to change notification settings

wrong7/solidity-pegjs-parser

 
 

Repository files navigation

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.

TODO

  • Typescript support in next release via ts-peggy
  • Documentation update

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

About

Solidity Parser for Coverage Testing utilizing new Peggy PEG.js Parser

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PEG.js 57.0%
  • JavaScript 21.5%
  • Solidity 21.3%
  • Shell 0.2%