Skip to content

Commit 5baff93

Browse files
authored
Merge pull request #451 from mcarmonaa/improvement/docs-add-uast-examples
docs/using-gitbase: add UAST UDFs examples
2 parents 96cf80f + 0148f86 commit 5baff93

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

docs/using-gitbase/examples.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,75 @@ CREATE INDEX files_lang_idx ON files USING pilosa (language(file_path, blob_cont
126126
```sql
127127
DROP INDEX files_lang_idx ON files;
128128
```
129+
130+
# UAST UDFs Examples
131+
132+
First of all, you should check out the [bblfsh documentation](https://docs.sourced.tech/babelfish) to get yourself familiar with UAST concepts.
133+
134+
Also, you can take a look to all the UDFs and their signatures in the [functions section](/docs/using-gitbase/functions.md)
135+
136+
## Retrieving UASTs with the UDF `uast`
137+
138+
```sql
139+
SELECT file_path, uast(blob_content, language(file_path)) FROM files;
140+
```
141+
142+
This function allows you to directly filter the retrieved UAST by performing a XPATH query on it:
143+
144+
```sql
145+
SELECT file_path, uast(blob_content, language(file_path), "//FuncLit") FROM files;
146+
```
147+
148+
This UDF will give you `semantic` UASTs by default. To get some other type see the UDF [`uast_mode`](#retrieving-different-kinds-of-uasts-using-uast_mode).
149+
150+
## Retrieving different kinds of UASTs using `uast_mode`
151+
152+
[bblfsh](https://docs.sourced.tech/babelfish) UAST modes: `semantic`, `annotated`, `native`
153+
154+
```sql
155+
SELECT file_path, uast_mode("semantic", blob_content, language(file_path)) FROM files;
156+
157+
SELECT file_path, uast_mode("annotated", blob_content, language(file_path)) FROM files;
158+
159+
SELECT file_path, uast_mode("native", blob_content, language(file_path)) FROM files;
160+
```
161+
162+
## Filtering UASTs by XPath queries
163+
164+
```sql
165+
SELECT file_path, uast_xpath(uast(blob_content, language(file_path)), "//FieldList") FROM files;
166+
167+
SELECT file_path, uast_xpath(uast_mode("annotated", blob_content, language(file_path)), "//*[@roleFunction]") FROM files;
168+
```
169+
170+
## Extracting information from UAST nodes
171+
172+
You can retrieve information from the UAST nodes either through the special selectors `@type`, `@token`, `@role`, `@startpos`, `@endpos`:
173+
174+
```sql
175+
SELECT file_path, uast_extract(uast(blob_content, language(file_path), "//FuncLit"), "@startpos") FROM files;
176+
```
177+
178+
or through a specific property:
179+
180+
```sql
181+
SELECT file_path, uast_extract(uast(blob_content, language(file_path), "//FuncLit"), "internalRole") FROM files;
182+
```
183+
184+
As result, you will get an array of arrays showing a list of the retrieved information for each of the given nodes:
185+
186+
```sh
187+
+-------------------+------------------------------------------------------------------------------------------------+
188+
| file_path | uast_extract(uast(files.blob_content, language(files.file_path), "//FuncLit"), "internalRole") |
189+
+-------------------+------------------------------------------------------------------------------------------------+
190+
| benchmark_test.go | [["Args"],["Args"],["Args"],["Args"],["Args"],["Args"],["Args"]] |
191+
+-------------------+------------------------------------------------------------------------------------------------+
192+
```
193+
194+
## Getting the children of a list of nodes
195+
196+
The UDF `uast_children` will return a flattened array of the children nodes from all the nodes in the given array.
197+
198+
```sql
199+
SELECT file_path, uast_children(uast(blob_content, language(file_path), "//FuncLit")) FROM files;
200+
```

0 commit comments

Comments
 (0)