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<=>exitPragmaDirectiveenterPragmaName<=>exitPragmaNameenterPragmaValue<=>exitPragmaValueenterVersion<=>exitVersionenterVersionOperator<=>exitVersionOperatorenterVersionConstraint<=>exitVersionConstraintenterImportDeclaration<=>exitImportDeclarationenterImportDirective<=>exitImportDirectiveenterContractDefinition<=>exitContractDefinitionenterInheritanceSpecifier<=>exitInheritanceSpecifierenterContractPart<=>exitContractPartenterStateVariableDeclaration<=>exitStateVariableDeclarationenterUsingForDeclaration<=>exitUsingForDeclarationenterStructDefinition<=>exitStructDefinitionenterModifierDefinition<=>exitModifierDefinitionenterFunctionDefinition<=>exitFunctionDefinitionenterEventDefinition<=>exitEventDefinitionenterEnumValue<=>exitEnumValueenterEnumDefinition<=>exitEnumDefinitionenterIndexedParameterList<=>exitIndexedParameterListenterParameterList<=>exitParameterListenterTypeNameList<=>exitTypeNameListenterVariableDeclaration<=>exitVariableDeclarationenterTypeName<=>exitTypeNameenterUserDefinedTypeName<=>exitUserDefinedTypeNameenterMapping<=>exitMappingenterFunctionTypeName<=>exitFunctionTypeNameenterStorageLocation<=>exitStorageLocationenterBlock<=>exitBlockenterStatement<=>exitStatemententerExpressionStatement<=>exitExpressionStatemententerIfStatement<=>exitIfStatemententerWhileStatement<=>exitWhileStatemententerPlaceholderStatement<=>exitPlaceholderStatemententerSimpleStatement<=>exitSimpleStatemententerForStatement<=>exitForStatemententerInlineAssemblyStatement<=>exitInlineAssemblyStatemententerDoWhileStatement<=>exitDoWhileStatemententerContinueStatement<=>exitContinueStatemententerBreakStatement<=>exitBreakStatemententerReturnStatement<=>exitReturnStatemententerThrowStatement<=>exitThrowStatemententerVariableDeclarationStatement<=>exitVariableDeclarationStatemententerIdentifierList<=>exitIdentifierListenterElementaryTypeName<=>exitElementaryTypeNameenterExpression<=>exitExpressionenterPrimaryExpression<=>exitPrimaryExpressionenterExpressionList<=>exitExpressionListenterNameValueList<=>exitNameValueListenterFunctionCall<=>exitFunctionCallenterFunctionCallArguments<=>exitFunctionCallArgumentsenterNewExpression<=>exitNewExpressionenterInlineAssemblyBlock<=>exitInlineAssemblyBlockenterAssemblyItem<=>exitAssemblyItementerAssemblyLocalBinding<=>exitAssemblyLocalBindingenterAssemblyAssignment<=>exitAssemblyAssignmententerFunctionalAssemblyExpression<=>exitFunctionalAssemblyExpressionenterArrayLiteral<=>exitArrayLiteralenterTupleLiteral<=>exitTupleLiteralenterElementaryTypeNameExpression<=>exitElementaryTypeNameExpressionenterNumberLiteral<=>exitNumberLiteral
This library started with the ANTL4 grammar definition created by Federico Bond (@federicobond).