|
| 1 | +import { ASTNode } from 'solidity-parser-antlr'; |
| 2 | + |
| 3 | +const stresc = (s: string) => `\"${s}\"`; |
| 4 | +const indent = (s: string) => `\t${s.replace(/\n/g, '\n\t')}`; |
| 5 | +const block = (s: string) => `{\n${indent(s)}\n}`; |
| 6 | +const unparen = (s: string) => s.replace(/^\((.*)\)$/, '$1'); |
| 7 | + |
| 8 | +const visitor = { |
| 9 | + // Source level |
| 10 | + |
| 11 | + SourceUnit: ({children}) => |
| 12 | + children.map(unparse).join('\n'), |
| 13 | + |
| 14 | + PragmaDirective: ({ name, value }) => |
| 15 | + `pragma ${name} ${value};`, |
| 16 | + |
| 17 | + ImportDirective: ({ path, symbolAliases }) => |
| 18 | + `import `+ |
| 19 | + (symbolAliases ? `{${symbolAliases.map(([from, to]) => |
| 20 | + from + (to ? ` as ${to}` : '') |
| 21 | + ).join(', ')}} from `: '') + |
| 22 | + `${stresc(path)};`, |
| 23 | + |
| 24 | + ContractDefinition: ({name, kind, baseContracts, subNodes}) => |
| 25 | + `${kind} ${name} ${(baseContracts.length > 0 ? 'is ' : '')}` + |
| 26 | + baseContracts.map(unparse).join(', ') + |
| 27 | + block('\n' + subNodes.map(unparse).join('\n\n')), |
| 28 | + |
| 29 | + InheritanceSpecifier: ({baseName: {namePath}}) => |
| 30 | + namePath, |
| 31 | + |
| 32 | + // Contract level |
| 33 | + |
| 34 | + UsingForDeclaration: ({typeName, libraryName}) => |
| 35 | + `using ${libraryName} for ${unparse(typeName)};`, |
| 36 | + |
| 37 | + StateVariableDeclaration: ({variables}) => |
| 38 | + variables.map(unparse).join(', ') + ';', |
| 39 | + |
| 40 | + StructDefinition: ({name, members}) => |
| 41 | + `struct ${name} ${block(members.map(unparse).join(';\n') + ';')}`, |
| 42 | + |
| 43 | + EnumDefinition: ({name, members}) => |
| 44 | + `enum ${name} ${block(members.map(unparse).join(',\n'))}`, |
| 45 | + |
| 46 | + EnumValue: ({name}) => |
| 47 | + name, |
| 48 | + |
| 49 | + EventDefinition: ({ name, parameters }) => |
| 50 | + `event ${name}${unparse(parameters)};`, |
| 51 | + |
| 52 | + ModifierDefinition: ({name, parameters, body}) => |
| 53 | + `modifier ${name}${Array.isArray(parameters) ? '' : unparse(parameters)} ${unparse(body)}`, |
| 54 | + // Note: when there is no parameter block, instead of an ASTNode there is a [] |
| 55 | + |
| 56 | + FunctionDefinition: ({visibility, name, parameters, body, modifiers, isConstructor, stateMutability}) => |
| 57 | + (isConstructor ? 'constructor' : `function ${name}`) + |
| 58 | + unparse(parameters) + ' ' + |
| 59 | + (visibility && visibility != 'default' ? visibility + ' ' : '') + (stateMutability || '') + '\n' + |
| 60 | + indent(modifiers.map(unparse).join('\n')) + '\n' + |
| 61 | + (body ? unparse(body) : ';'), |
| 62 | + |
| 63 | + ParameterList: ({parameters}) => |
| 64 | + `(${parameters.map(unparse).join(', ')})`, |
| 65 | + |
| 66 | + Parameter: ({typeName, name, storageLocation}) => |
| 67 | + `${unparse(typeName)} ${storageLocation || ''} ${name || ''}`, |
| 68 | + |
| 69 | + ModifierInvocation: ({name, arguments: args}) => |
| 70 | + `${name}(${args.map(unparse).join(', ')})`, |
| 71 | + |
| 72 | + // Statements |
| 73 | + |
| 74 | + Block: ({statements}) => |
| 75 | + block(statements.map(unparse).join('\n')), |
| 76 | + |
| 77 | + VariableDeclarationStatement: ({variables, initialValue}) => |
| 78 | + variables.map(unparse) + |
| 79 | + (initialValue ? ` = ${unparse(initialValue)};` : ';'), |
| 80 | + |
| 81 | + ExpressionStatement: ({expression}) => |
| 82 | + `${unparse(expression)};`, |
| 83 | + |
| 84 | + EmitStatement: ({eventCall}) => |
| 85 | + `emit ${unparen(unparse(eventCall))};`, |
| 86 | + |
| 87 | + ReturnStatement: ({expression}) => |
| 88 | + `return ${expression ? unparse(expression) : ''};`, |
| 89 | + |
| 90 | + BreakStatement: ({}) => |
| 91 | + `break;`, |
| 92 | + |
| 93 | + ContinueStatement: ({}) => |
| 94 | + `continue;`, |
| 95 | + |
| 96 | + ThrowStatement: ({}) => |
| 97 | + `throw;`, |
| 98 | + |
| 99 | + IfStatement: ({condition, trueBody, falseBody}) => |
| 100 | + `if (${unparse(condition)})\n${unparse(trueBody)}` + |
| 101 | + (falseBody ? `else\n${unparse(falseBody)}` : ''), |
| 102 | + |
| 103 | + ForStatement: ({initExpression: i, conditionExpression: c, loopExpression: l, body}) => |
| 104 | + `for (${unparse(i).replace(';','')}; ${unparse(c)}; ${unparse(l).replace(';','')}) ${unparse(body)}`, |
| 105 | + |
| 106 | + InlineAssemblyStatement: ({language, body}) => // TODO language |
| 107 | + `assembly ${unparse(body)}`, |
| 108 | + |
| 109 | + // Types |
| 110 | + |
| 111 | + ElementaryTypeName: ({name}) => |
| 112 | + name, |
| 113 | + |
| 114 | + UserDefinedTypeName: ({namePath}) => |
| 115 | + namePath, |
| 116 | + |
| 117 | + ArrayTypeName: ({baseTypeName, length}) => |
| 118 | + `${unparse(baseTypeName)}[${length ? unparse(length) : ''}]`, |
| 119 | + |
| 120 | + Mapping: ({keyType, valueType}) => |
| 121 | + `mapping (${unparse(keyType)} => ${unparse(valueType)})`, |
| 122 | + |
| 123 | + // Expressions |
| 124 | + |
| 125 | + Identifier: ({ name }) => |
| 126 | + name, |
| 127 | + |
| 128 | + BooleanLiteral: ({ value }) => |
| 129 | + value ? 'true' : 'false', |
| 130 | + |
| 131 | + NumberLiteral: ({number, subdenomination}) => // TODO subdenomination |
| 132 | + number, |
| 133 | + |
| 134 | + StringLiteral: ({value}) => |
| 135 | + stresc(value), |
| 136 | + |
| 137 | + FunctionCall: ({expression, arguments: args, names}) => // TODO: names |
| 138 | + `(${unparse(expression)}(${args.map(unparse).join(', ')}))`, |
| 139 | + |
| 140 | + Conditional: ({condition, trueExpression, falseExpression}) => |
| 141 | + `(${unparse(condition)} ? ${unparse(trueExpression)} : ${unparse(falseExpression)})`, |
| 142 | + |
| 143 | + UnaryOperation: ({operator, subExpression, isPrefix}) => |
| 144 | + `(${isPrefix ? operator : ''}${unparse(subExpression)}${isPrefix ? '' : operator})`, |
| 145 | + |
| 146 | + BinaryOperation: ({operator, left, right}) => |
| 147 | + `(${unparse(left)} ${operator} ${unparse(right)})`, |
| 148 | + |
| 149 | + MemberAccess: ({expression, memberName}) => |
| 150 | + `(${unparse(expression)}.${memberName})`, |
| 151 | + |
| 152 | + IndexAccess: ({base, index}) => |
| 153 | + `(${unparse(base)}[${unparse(index)}])`, |
| 154 | + |
| 155 | + ElementaryTypeNameExpression: ({typeName}) => |
| 156 | + `(${unparse(typeName)})`, |
| 157 | + |
| 158 | + VariableDeclaration: ({typeName, name, visibility, isDeclaredConst, isIndexed, expression}) => |
| 159 | + `${unparse(typeName)} ` + |
| 160 | + (isIndexed ? 'indexed ' : '') + |
| 161 | + (visibility && visibility != 'default' ? visibility + ' ' : '') + |
| 162 | + (isDeclaredConst ? 'constant ' : '') + |
| 163 | + `${name}` + |
| 164 | + (expression ? ` = ${unparse(expression)}` : ''), |
| 165 | + |
| 166 | + NewExpression: ({typeName}) => |
| 167 | + `(new ${unparse(typeName)})`, |
| 168 | + |
| 169 | + TupleExpression: ({components}) => |
| 170 | + `[${components.map(unparse).join(', ')}]`, |
| 171 | + |
| 172 | + // Assembly |
| 173 | + |
| 174 | + AssemblyBlock: ({operations}) => |
| 175 | + block(operations.map(unparse).join('\n')), |
| 176 | + |
| 177 | + AssemblyAssignment: ({names, expression}) => |
| 178 | + `${names.map(unparse).join(', ')} := ${unparse(expression)}`, |
| 179 | + |
| 180 | + AssemblyLocalDefinition: ({names, expression}) => |
| 181 | + `let ${names.map(unparse).join(', ')} := ${unparse(expression)}`, |
| 182 | + |
| 183 | + AssemblyCall: ({functionName, arguments: args}) => |
| 184 | + args.length == 0 ? |
| 185 | + functionName : |
| 186 | + `${functionName}(${args.map(unparse).join(', ')})`, |
| 187 | + |
| 188 | + AssemblyIf: ({condition, body}) => |
| 189 | + `if ${unparse(condition)} ${unparse(body)}`, |
| 190 | + |
| 191 | + AssemblyFor: ({pre, condition, post, body}) => |
| 192 | + `for ${[pre, condition, post, body].map(unparse).join(' ')}`, |
| 193 | + |
| 194 | + AssemblySwitch: ({expression, cases}) => |
| 195 | + `switch ${unparse(expression)}\n${cases.map(unparse).join('\n')}`, |
| 196 | + |
| 197 | + AssemblyCase: ({value, block}) => |
| 198 | + `case ${unparse(value)} ${unparse(block)}` |
| 199 | + |
| 200 | + DecimalNumber: ({ value }) => |
| 201 | + value, |
| 202 | + |
| 203 | + HexNumber: ({ value }) => |
| 204 | + value, |
| 205 | +} |
| 206 | + |
| 207 | +export function unparse(ast: ASTNode): string { |
| 208 | + return (visitor[ast.type] || (a => { |
| 209 | + console.log(a); |
| 210 | + console.trace(); |
| 211 | + return `<${a.type}>`; |
| 212 | + }))(ast); |
| 213 | +} |
0 commit comments