Parser for Ethereum's Solidity language generated by ANTLR4 and wrapped in RxJS.
npm i -S @solidity-ide/antlr-parser
import "rxjs/add/operator/filter";
import { SolidityParser } from "../lib/SolidityParser";
const parser = new SolidityParser();
// Emits RuleContextEvents as it walks the tree.
parser.Observable()
.filter(r => r.type !== "enterEveryRule" && r.type !== "exitEveryRule")
.subscribe(
rule => console.log(rule.type),
);
// Emits syntax erros.
parser.ErrorObservable().subscribe(
e => console.log(e),
);
parser.parseFile("./contract.sol");
parser.parseFile("./contract2.sol", "./contact3.sol");
parser.parseFile(...["./contract4.sol", "./contact5.sol"]);
The main class for parsing Solidity code. Provides RX Observables that emit rule contexts and syntax errors.
One SolidityParser
instance can be used to parse multiple files. Distinguish between files with enterSourceUnit
and exitSourceUnit
event types. Optionally, set the Observables to
complete
after the first parsing.
False by default. If true, the Observables will complete
after the first parsing.
Parses Solidity code provided as a string.
Loads and parses Solidity code from a file.
Emits Rule Context events as it walks the tree of parsed Solidity code.
Emits any Syntax Error events from parsed Solidity code.
Cleans up the class. Basically invoking complete()
on any Observables.
Emited by SolidityParser#Observable()
.
export interface IRuleContextEvent {
type: string;
context: RuleContext;
}
Emitted by SolidityParser#ErrorObservable
.
export interface ISyntaxErrorEvent {
line: number;
column: number;
message: string;
error: Error;
}
The definitions for these rules can be found in the grammar definition (./bin/Solidity.g4
).
Event types come in pairs: enter*
and exit*
events.
enterEveryRule
<=>exitEveryRule
(Included for completeness. More helpful to filter out.)enterSourceUnit
<=>exitSourceUnit
(Marks the start and end of the document.)enterPragmaDirective
<=>exitPragmaDirective
enterPragmaName
<=>exitPragmaName
enterPragmaValue
<=>exitPragmaValue
enterVersion
<=>exitVersion
enterVersionOperator
<=>exitVersionOperator
enterVersionConstraint
<=>exitVersionConstraint
enterImportDeclaration
<=>exitImportDeclaration
enterImportDirective
<=>exitImportDirective
enterContractDefinition
<=>exitContractDefinition
enterInheritanceSpecifier
<=>exitInheritanceSpecifier
enterContractPart
<=>exitContractPart
enterStateVariableDeclaration
<=>exitStateVariableDeclaration
enterUsingForDeclaration
<=>exitUsingForDeclaration
enterStructDefinition
<=>exitStructDefinition
enterModifierDefinition
<=>exitModifierDefinition
enterFunctionDefinition
<=>exitFunctionDefinition
enterEventDefinition
<=>exitEventDefinition
enterEnumValue
<=>exitEnumValue
enterEnumDefinition
<=>exitEnumDefinition
enterIndexedParameterList
<=>exitIndexedParameterList
enterParameterList
<=>exitParameterList
enterTypeNameList
<=>exitTypeNameList
enterVariableDeclaration
<=>exitVariableDeclaration
enterTypeName
<=>exitTypeName
enterUserDefinedTypeName
<=>exitUserDefinedTypeName
enterMapping
<=>exitMapping
enterFunctionTypeName
<=>exitFunctionTypeName
enterStorageLocation
<=>exitStorageLocation
enterBlock
<=>exitBlock
enterStatement
<=>exitStatement
enterExpressionStatement
<=>exitExpressionStatement
enterIfStatement
<=>exitIfStatement
enterWhileStatement
<=>exitWhileStatement
enterPlaceholderStatement
<=>exitPlaceholderStatement
enterSimpleStatement
<=>exitSimpleStatement
enterForStatement
<=>exitForStatement
enterInlineAssemblyStatement
<=>exitInlineAssemblyStatement
enterDoWhileStatement
<=>exitDoWhileStatement
enterContinueStatement
<=>exitContinueStatement
enterBreakStatement
<=>exitBreakStatement
enterReturnStatement
<=>exitReturnStatement
enterThrowStatement
<=>exitThrowStatement
enterVariableDeclarationStatement
<=>exitVariableDeclarationStatement
enterIdentifierList
<=>exitIdentifierList
enterElementaryTypeName
<=>exitElementaryTypeName
enterExpression
<=>exitExpression
enterPrimaryExpression
<=>exitPrimaryExpression
enterExpressionList
<=>exitExpressionList
enterNameValueList
<=>exitNameValueList
enterFunctionCall
<=>exitFunctionCall
enterFunctionCallArguments
<=>exitFunctionCallArguments
enterNewExpression
<=>exitNewExpression
enterInlineAssemblyBlock
<=>exitInlineAssemblyBlock
enterAssemblyItem
<=>exitAssemblyItem
enterAssemblyLocalBinding
<=>exitAssemblyLocalBinding
enterAssemblyAssignment
<=>exitAssemblyAssignment
enterFunctionalAssemblyExpression
<=>exitFunctionalAssemblyExpression
enterArrayLiteral
<=>exitArrayLiteral
enterTupleLiteral
<=>exitTupleLiteral
enterElementaryTypeNameExpression
<=>exitElementaryTypeNameExpression
enterNumberLiteral
<=>exitNumberLiteral
This library started with the ANTL4 grammar definition created by Federico Bond (@federicobond).