Solparse
Solparse is a JavaScript library providing a PEG.js-based parser for the Solidity programming language. It is currently at version 2.2.8, with its last major release dating back over seven years, indicating a sporadic and maintenance-driven release cadence. The library was originally a fork of Consensys' `solidity-parser` and is primarily maintained to support `Ethlint`, a Solidity linter. Due to its specific maintenance focus, the developers do not guarantee backward compatibility or new feature support for general use. Users are strongly advised to pin `solparse` as a fixed dependency in production systems. Alternatives like `solparse-exp-jb` (a more recent fork supporting newer Solidity features) or `@solidity-parser/parser` (an ANTLR-based parser) may offer more up-to-date Solidity syntax support.
Common errors
-
Syntax error: Expected "!= ", "%", "%=", "&", "&&", "&=", "*", "*=", "+", "++", "+", ",", "-", "--", "-=", "/", "/*", "/=", ";", "<", "<<", "<<=", "<=", "=", "==", ">", ">=", ">>", ">>=", ">>>", "?", "^", "^=", "|", "|=", "||", comment, end of line, or whitespace but "(" found.cause This generic syntax error often indicates that the Solidity code being parsed contains syntax elements not recognized by the older `solparse` version. A common example is the `emit` keyword for events, introduced in Solidity 0.4.21.fixEnsure your Solidity code adheres to an older syntax version supported by `solparse` (e.g., pre-0.4.21 for certain features), or migrate to a more current Solidity parser that supports modern syntax. -
TypeError: Cannot read properties of undefined (reading 'name')
cause Attempting to access properties of the AST object, such as `ast.body[0].name`, when the parsing failed or the AST structure is unexpected for the given Solidity code. This often happens if the input is invalid or a parsing error occurred before the AST could be fully formed.fixWrap `parse()` calls in a try-catch block to handle `SyntaxError` or `ParserError` gracefully. Inspect the `ast` object structure after successful parsing to ensure it matches expectations before accessing properties. Consider `console.log(JSON.stringify(ast, null, 2))` for debugging.
Warnings
- breaking Solparse does not guarantee backward compatibility and may introduce breaking changes without major version increments. Its development is tied to `Ethlint`'s needs rather than general public API stability.
- gotcha The package has not been updated in over seven years (last published January 13, 2019) and may not support newer Solidity syntax features (e.g., `emit` syntax introduced in 0.4.21, or features in Solidity 0.5.x, 0.6.x, 0.7.x, 0.8.x and later).
- gotcha The `solparse` package primarily targets CommonJS environments. While Node.js can often import CJS modules, direct named ESM imports will not work as the package exports a single default function.
Install
-
npm install solparse -
yarn add solparse -
pnpm add solparse
Imports
- parse
import { parse } from 'solparse'; const solparse = require('solparse');import parse from 'solparse';
- ParserError
import { ParserError } from 'solparse';import parse from 'solparse'; // ParserError is typically accessed via the default export if available // or caught in a try-catch block and checked with `e instanceof parse.ParserError`
Quickstart
import parse from 'solparse';
const solidityCode = `
pragma solidity ^0.8.0;
contract SimpleStorage {
uint256 public storedData;
function set(uint256 x) public {
storedData = x;
}
function get() public view returns (uint256) {
return storedData;
}
}
`;
try {
const ast = parse(solidityCode);
console.log('Successfully parsed Solidity code. AST:', JSON.stringify(ast, null, 2));
// You can now traverse and analyze the AST
console.log('Contract name:', ast.body[0].name);
} catch (e) {
if (e instanceof Error && e.name === 'SyntaxError') {
console.error('Parsing error:', e.message);
} else {
console.error('An unexpected error occurred:', e);
}
}