import * as parser from 'solidity-parser-antlr' ; module example method param { String } input return { Object } function parse ( input ) { const opts = { } ; return parser . parse ( input , opts ) ; } example method param { String } input return { Object } function parseEnum ( input ) { const parsed = parse ( input ) ; const { members } = ( parsed as any ) . children [ 0 ] . subNodes [ 0 ] ; const myEnum = members . filter ( member => member . type === 'EnumValue' ) . reduce ( ( acc , member , index ) => { acc [ member . name ] = index ; acc [ index ] = member . name ; return acc ; } , { } ) ; return myEnum ; } example method param { String } input return { Object } function parseFields ( input , prefix ? ) { const graph = parse ( input ) ; const contract = ( graph as any ) . children . filter ( child => child . type === 'ContractDefinition' ) [ 0 ] ; const stateVariableDeclarations = contract . subNodes . filter ( child => child . type === 'StateVariableDeclaration' ) ; const result = stateVariableDeclarations . reduce ( ( acc , stateVariable ) => { const variable = stateVariable . variables [ 0 ] ; if ( ! variable . expression ) return acc ; if ( prefix !== undefined && variable . name . indexOf ( prefix ) !== 0 ) return acc ; function getNameValue ( variable ) { switch ( variable . expression . type ) { case 'StringLiteral' : return { name : variable . name , value : variable . expression . value } ; case 'NumberLiteral' : return { name : variable . name , value : variable . expression . number } ; default : throw new Error ( `Unknown Expressions ${ variable . expression . type }` ) ; } } const { name , value } = getNameValue ( variable ) ; acc [ name ] = value ; if ( acc [ value ] !== undefined ) throw new Error ( `Duplicate expressions value. var:${ name } value:${ value }` ) ; acc [ value ] = name ; return acc ; } , { } ) ; return result ; } export default { parse , parseEnum , parseFields , } ;