import assert from 'assert' ; export function decodeTypeIdentifier ( typeIdentifier ) { return typeIdentifier . replace ( /(\$_|_\$_|_\$)(?=(\$_|_\$_|_\$)*([^_$]|$))/g , m => { switch ( m ) { case '$_' : return '(' ; case '_$' : return ')' ; case '_$_' : return ',' ; default : throw new Error ( 'Unreachable' ) ; } } ) ; } export function normalizeTypeIdentifier ( typeIdentifier ) { return decodeTypeIdentifier ( typeIdentifier ) . replace ( /_storage_ptr\b/g , '_storage' ) ; } export function stabilizeTypeIdentifier ( typeIdentifier ) { let decoded = decodeTypeIdentifier ( typeIdentifier ) ; const re = /(t_struct|t_enum|t_contract)\(/g ; let match ; while ( ( match = re . exec ( decoded ) ) ) { let i ; let d = 1 ; for ( i = match . index + match [ 0 ] . length ; d !== 0 ; i ++ ) { assert ( i < decoded . length , 'index out of bounds' ) ; const c = decoded [ i ] ; if ( c === '(' ) { d += 1 ; } else if ( c === ')' ) { d -= 1 ; } } const re2 = /\d+_?/y ; re2 . lastIndex = i ; decoded = decoded . replace ( re2 , '' ) ; } return decoded ; }