You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+18-2
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ A Solidity parser for Python built on top of a robust ANTLR4 grammar
7
7
8
8
```
9
9
#> pip3 install solidity_parser
10
-
#> python3 -m solidity_parser <path_to_contract.sol> # prettyprints tree
10
+
#> python3 -m solidity_parser <parse|outline> <path_to_contract.sol> # print parse tree or sourceUnit outline
11
11
```
12
12
13
13
## HowTo
@@ -20,7 +20,9 @@ import pprint
20
20
from solidity_parser import parser
21
21
22
22
sourceUnit = parser.parse_file(sys.argv[1])
23
-
pprint.pprint(sourceUnit)
23
+
pprint.pprint(sourceUnit)
24
+
# see output below
25
+
24
26
```
25
27
26
28
output:
@@ -51,7 +53,21 @@ output:
51
53
52
54
Parse-tree nodes can be accessed both like dictionaries or via object attributes. Nodes always carry a `type` field to hint the type of AST node. The start node is always of type `sourceUnit`.
53
55
56
+
## Accessing AST items in an Object Oriented fashion
57
+
58
+
```python
59
+
# ... continuing from previous snippet
54
60
61
+
# subparse into objects for nicer interfaces:
62
+
# create a nested object structure from AST
63
+
sourceUnitObject = parser.objectify(sourceUnit)
64
+
65
+
# access imports, contracts, functions, ... (see outline example in __main__.py)
66
+
sourceUnitObject.imports # []
67
+
sourceUnitObject.pragmas # []
68
+
sourceUnitObject.contracts.keys() # get all contract names
69
+
sourceUnitObject.contracts["contractName"].functions.keys() # get all functions in contract: "contractName"
0 commit comments