Socket
Socket
Sign inDemoInstall

@solidity-parser/parser

Package Overview
Dependencies
Maintainers
1
Versions
50
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@solidity-parser/parser

A Solidity parser built from a robust ANTLR 4 grammar


Version published
Weekly downloads
314K
decreased by-6.81%
Maintainers
1
Weekly downloads
 
Created

What is @solidity-parser/parser?

@solidity-parser/parser is an npm package designed to parse Solidity source code into an Abstract Syntax Tree (AST). This allows developers to analyze, transform, and manipulate Solidity code programmatically. It is particularly useful for building tools that need to understand the structure of Solidity code, such as linters, code formatters, and static analysis tools.

What are @solidity-parser/parser's main functionalities?

Parsing Solidity Code

This feature allows you to parse Solidity source code into an Abstract Syntax Tree (AST). The code sample demonstrates how to parse a simple Solidity contract and print the resulting AST.

const parser = require('@solidity-parser/parser');
const sourceCode = 'contract MyContract { uint256 public value; }';
const ast = parser.parse(sourceCode);
console.log(JSON.stringify(ast, null, 2));

Handling Parse Errors

This feature allows you to handle errors that occur during parsing. The code sample demonstrates how to catch and handle a parse error when the Solidity source code is incomplete or malformed.

const parser = require('@solidity-parser/parser');
const sourceCode = 'contract MyContract { uint256 public value;'; // Missing closing brace
try {
  const ast = parser.parse(sourceCode);
} catch (error) {
  console.error('Parse error:', error);
}

Traversing the AST

This feature allows you to traverse the AST and perform actions on specific nodes. The code sample demonstrates how to traverse the AST and print the name of each contract defined in the Solidity source code.

const parser = require('@solidity-parser/parser');
const sourceCode = 'contract MyContract { uint256 public value; }';
const ast = parser.parse(sourceCode);
parser.visit(ast, {
  ContractDefinition: (node) => {
    console.log('Contract name:', node.name);
  }
});

Other packages similar to @solidity-parser/parser

FAQs

Package last updated on 17 Jan 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc