Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit c17d9cb

Browse files
Denys Smirnovdennwc
authored andcommitted
split examples into different sections
Signed-off-by: Denys Smirnov <[email protected]>
1 parent f08fd07 commit c17d9cb

File tree

1 file changed

+42
-22
lines changed

1 file changed

+42
-22
lines changed

README.md

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,53 +46,73 @@ docker exec -it bblfshd bblfshctl driver install python bblfsh/python-driver:lat
4646

4747
Please, read the [getting started](https://doc.bblf.sh/using-babelfish/getting-started.html) guide to learn more about how to use and deploy a bblfshd.
4848

49+
### Parsing a file
50+
4951
```python
5052
import bblfsh
5153

5254
client = bblfsh.BblfshClient("localhost:9432")
5355
ctx = client.parse("/path/to/file.py")
5456
print(ctx)
55-
# or to get the results in a dictionary:
56-
resdict = ctx.get_all()
5757

58-
# "filter' allows you to use XPath queries to filter on result nodes:
58+
# You can also get the non semantic UAST or native AST:
59+
ctx = client.parse("file.py", mode=bblfsh.Modes.NATIVE)
60+
# Possible values for Modes: DEFAULT_MODE, NATIVE, PREPROCESSED, ANNOTATED, SEMANTIC
61+
```
62+
63+
To get the UAST as a dictionary:
64+
65+
```python
66+
ast = ctx.get_all()
67+
```
68+
69+
### UAST filtering (XPath)
70+
71+
`ctx.filter()` allows you to use XPath queries to filter on result nodes:
72+
73+
```python
5974
it = ctx.filter("//python:Call")
6075
for node in it:
76+
# print internal node:
6177
print(node)
62-
# or:
78+
# or get as Python dictionary/value:
6379
doSomething(node.get())
80+
```
6481

65-
# filter must be used when using XPath functions returning these types:
66-
# XPath queries can return different types (dicts, int, float, bool or str),
67-
# calling get() with an item will return the right type, but if you must ensure
68-
# that you are getting the expected type (to avoid errors in the queries) there
69-
# are alterative typed versions:
82+
XPath queries can return different types (`dict`, `int`, `float`, `bool` or `str`),
83+
calling `get()` with an item will return the right type, but if you must ensure
84+
that you are getting the expected type (to avoid errors in the queries) there
85+
are alternative typed versions:
86+
87+
```python
7088
x = next(ctx.filter("boolean(//*[@startOffset or @endOffset])")).get_bool()
7189
y = next(ctx.filter("name(//*[1])")).get_str()
7290
z = next(ctx.filter("count(//*)")).get_int() # or get_float()
91+
```
92+
93+
### Iteration
7394

74-
# You can also iterate using iteration orders different than the
75-
# default preorder using the `iterate` method on `parse` result or node objects:
95+
You can also iterate using iteration orders different than the
96+
default pre-order using the `iterate` method on `parse` result or node objects:
7697

98+
```python
7799
# Directly over parse results
78100
it = client.parse("/path/to/file.py").iterate(bblfsh.TreeOrder.POST_ORDER)
79-
for i in it: ...
101+
for node in it:
102+
print(node)
80103

81104
# Over filter results (which by default are already iterators with PRE_ORDER):
82105
ctx = client.parse("file.py")
83-
newiter = ctx.filter("//python:Call").iterate(bblfsh.TreeOrder.LEVEL_ORDER)
84-
for i in newiter: ...
106+
it = ctx.filter("//python:Call").iterate(bblfsh.TreeOrder.LEVEL_ORDER)
107+
for node in it:
108+
print(node)
85109

86110
# Over individual node objects to change the iteration order of
87111
# a specific subtree:
88-
ctx = client.parse("file.py")
89-
first_node = ctx.root
90-
newiter = first_node.iterate(bblfsh.TreeOrder.POSITION_ORDER)
91-
for i in newiter: ...
92-
93-
# You can also get the non semantic UAST or native AST:
94-
ctx = client.parse("file.py", mode=bblfsh.Modes.NATIVE)
95-
# Possible values for Modes: DEFAULT_MODE, NATIVE, PREPROCESSED, ANNOTATED, SEMANTIC
112+
ast = ctx.root
113+
it = ast.iterate(bblfsh.TreeOrder.POSITION_ORDER)
114+
for node in it:
115+
print(node)
96116
```
97117

98118
Please read the [Babelfish clients](https://doc.bblf.sh/using-babelfish/clients.html)

0 commit comments

Comments
 (0)