@solidity-ide/antlr-parser
v1.0.1
Published
Ethereum Solidity language parser via ANTLR4.
Downloads
4
Maintainers
Readme
antlr-parser
Parser for Ethereum's Solidity language generated by ANTLR4 and wrapped in RxJS.
Installation
npm i -S @solidity-ide/antlr-parser
Example
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"]);
Documentation
SolidityParser Class
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.
Constructor
constructor (fireComplete = false)
False by default. If true, the Observables will complete
after the first parsing.
Methods
parseText (...code: string[]): void
Parses Solidity code provided as a string.
parseFile (...path: string[]): void
Loads and parses Solidity code from a file.
Observable (): Observable<IRuleContextEvent>
Emits Rule Context events as it walks the tree of parsed Solidity code.
ErrorObservable (): Observable<ISyntaxErrorEvent>
Emits any Syntax Error events from parsed Solidity code.
complete (): void
Cleans up the class. Basically invoking complete()
on any Observables.
IRuleContextEvent Interface
Emited by SolidityParser#Observable()
.
export interface IRuleContextEvent {
type: string;
context: RuleContext;
}
ISyntaxErrorEvent Interface
Emitted by SolidityParser#ErrorObservable
.
export interface ISyntaxErrorEvent {
line: number;
column: number;
message: string;
error: Error;
}
RuleContextEvent Types
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
Special Thanks
This library started with the ANTL4 grammar definition created by Federico Bond (@federicobond).