Consensys / surya

A set of utilities for exploring Solidity contracts
Apache License 2.0
1.06k stars 118 forks source link

Enable Standard JSON Output for Parsed AST #163

Closed MatthewEthanTam closed 2 years ago

MatthewEthanTam commented 2 years ago

Feature Description:

Adding standard JSON output from parser.parse using flag -j or --json.

Why I need this:

Using Surya in a project to parse Solidity contracts to JSON format, to create code property graphs using Joern.

Example usage

surya parse -j contracts/Migration.sol

Example output

{"type":"SourceUnit","children":[{"type":"PragmaDirective","name":"solidity","value":">=0.4.22 <0.9.0"},{"type":"ContractDefinition","name":"Migrations","baseContracts":[],"subNodes":[{"type":"StateVariableDeclaration","variables":[{"type":"VariableDeclaration","typeName":{"type":"ElementaryTypeName","name":"address","stateMutability":null},"name":"owner","identifier":{"type":"Identifier","name":"owner"},"expression":{"type":"MemberAccess","expression":{"type":"Identifier","name":"msg"},"memberName":"sender"},"visibility":"public","isStateVar":true,"isDeclaredConst":false,"isIndexed":false,"isImmutable":false,"override":null,"storageLocation":null}],"initialValue":{"type":"MemberAccess","expression":{"type":"Identifier","name":"msg"},"memberName":"sender"}},{"type":"StateVariableDeclaration","variables":[{"type":"VariableDeclaration","typeName":{"type":"ElementaryTypeName","name":"uint","stateMutability":null},"name":"last_completed_migration","identifier":{"type":"Identifier","name":"last_completed_migration"},"expression":null,"visibility":"public","isStateVar":true,"isDeclaredConst":false,"isIndexed":false,"isImmutable":false,"override":null,"storageLocation":null}],"initialValue":null},{"type":"ModifierDefinition","name":"restricted","parameters":[],"body":{"type":"Block","statements":[{"type":"ExpressionStatement","expression":{"type":"FunctionCall","expression":{"type":"Identifier","name":"require"},"arguments":[{"type":"BinaryOperation","operator":"==","left":{"type":"MemberAccess","expression":{"type":"Identifier","name":"msg"},"memberName":"sender"},"right":{"type":"Identifier","name":"owner"}},{"type":"StringLiteral","value":"This function is restricted to the contract's owner","parts":["This function is restricted to the contract's owner"],"isUnicode":[false]}],"names":[],"identifiers":[]}},{"type":"ExpressionStatement","expression":{"type":"Identifier","name":"_"}}]},"isVirtual":false,"override":null},{"type":"FunctionDefinition","name":"setCompleted","parameters":[{"type":"VariableDeclaration","typeName":{"type":"ElementaryTypeName","name":"uint","stateMutability":null},"name":"completed","identifier":{"type":"Identifier","name":"completed"},"storageLocation":null,"isStateVar":false,"isIndexed":false,"expression":null}],"returnParameters":null,"body":{"type":"Block","statements":[{"type":"ExpressionStatement","expression":{"type":"BinaryOperation","operator":"=","left":{"type":"Identifier","name":"last_completed_migration"},"right":{"type":"Identifier","name":"completed"}}}]},"visibility":"public","modifiers":[{"type":"ModifierInvocation","name":"restricted","arguments":null}],"override":null,"isConstructor":false,"isReceiveEther":false,"isFallback":false,"isVirtual":false,"stateMutability":null}],"kind":"contract"}]}