Skip to content

Commit 39e0e35

Browse files
author
colinmcneil
committed
Add range insertion
1 parent 583db92 commit 39e0e35

File tree

1 file changed

+102
-7
lines changed

1 file changed

+102
-7
lines changed
Lines changed: 102 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
11
---
22
tools:
3-
- name: run-tree-sitter-flow
4-
description: Runs a flow with fd and tree sitter in the current directory.
3+
- name: fdfind
4+
description: Runs fd in the current directory.
55
parameters:
66
type: object
77
properties:
88
fd_args:
99
type: string
1010
description: The args to pass to fd
11+
out_file:
12+
type: string
13+
description: The path to output
14+
container:
15+
image: vonwig/tree_sitter
16+
command:
17+
- "fdfind {{fd_args|safe}} > {{out_file|safe}}"
18+
- name: run-tree-sitter-flow
19+
description: Runs a flow with fd and tree sitter in the current directory.
20+
parameters:
21+
type: object
22+
properties:
23+
in_paths_file:
24+
type: string
25+
description: The path of a file containing all paths to parse
1126
output_dir:
1227
type: string
1328
description: The path to output files to
1429
container:
1530
image: vonwig/tree_sitter
1631
command:
17-
- "fdfind {{fd_args|safe}} | while read -r file; do mkdir -p \"{{output_dir|safe}}/$(dirname $file)\" && tree-sitter parse \"$file\" > \"{{output_dir|safe}}/$file.parsed\"; done"
32+
- "while read -r file; do mkdir -p \"{{output_dir|safe}}/$(dirname $file)\" && tree-sitter parse \"$file\" > \"{{output_dir|safe}}/$file.parsed\"; done < {{in_paths_file|safe}}"
1833
- name: delete_folder
1934
description: Runs rm -rf on a folder
2035
parameters:
@@ -27,12 +42,92 @@ tools:
2742
image: vonwig/bash_alpine
2843
command:
2944
- "rm -rf {{path|safe}}"
45+
- name: run-javascript-sandbox
46+
description: execute javascript code
47+
parameters:
48+
type: object
49+
properties:
50+
javascript:
51+
type: string
52+
description: the javascript code to run
53+
container:
54+
image: vonwig/javascript-runner
55+
command:
56+
- "{{javascript|safe}}"
57+
- name: sqlite
58+
description: run the sqlite command
59+
parameters:
60+
type: object
61+
properties:
62+
database:
63+
type: string
64+
description: the path to the database
65+
sql:
66+
type: string
67+
description: the sql statement to run
68+
container:
69+
image: vonwig/sqlite:latest
70+
command:
71+
- "{{database}}"
72+
- "{{sql}}"
3073
---
3174

3275
# prompt user
3376

34-
Delete `/thread/top-level/`
77+
1. Delete `/thread/ast`, `/thread/paths.txt`, and ``/thread/ranges.sql`
78+
79+
2. Run fd with args `-e py` to `/thread/paths.txt`
80+
81+
3. Run tree sitter on input `/thread/paths.txt` and output_dir `/thread/ast`.
82+
83+
4. Prepare a JS script to run in the sandbox which
84+
Reads `/thread/paths.txt`
85+
For each path, read the contents of parsed tree file at `/thread/ast/${path}.parsed`.
86+
87+
An example parsed file:
88+
89+
/thread/ast/main.py.parsed
90+
```
91+
(module [0, 0] - [6, 0]
92+
(comment [0, 0] - [0, 21])
93+
(import_from_statement [2, 0] - [2, 37]
94+
module_name: (dotted_name [2, 5] - [2, 22]
95+
(identifier [2, 5] - [2, 13])
96+
(identifier [2, 14] - [2, 22]))
97+
name: (dotted_name [2, 30] - [2, 37]
98+
(identifier [2, 30] - [2, 37])))
99+
(if_statement [4, 0] - [5, 17]
100+
condition: (comparison_operator [4, 3] - [4, 25]
101+
(identifier [4, 3] - [4, 11])
102+
(string [4, 15] - [4, 25]
103+
(string_start [4, 15] - [4, 16])
104+
(string_content [4, 16] - [4, 24])
105+
(string_end [4, 24] - [4, 25])))
106+
consequence: (block [5, 4] - [5, 17]
107+
(expression_statement [5, 4] - [5, 17]
108+
(call [5, 4] - [5, 17]
109+
function: (attribute [5, 4] - [5, 15]
110+
object: (identifier [5, 4] - [5, 11])
111+
attribute: (identifier [5, 12] - [5, 15]))
112+
arguments: (argument_list [5, 15] - [5, 17]))))))
113+
```
114+
115+
Use a global regex match
116+
```re
117+
/\n \((.*) \[(\d*), (\d*)\] - \[(\d*), (\d*)\]/gm
118+
```
119+
to get the ranges of each top level node into capturing groups.
120+
Write the ranges to `/thread/ranges.sql` in the form of sql
121+
`INSERT INTO RANGES (ROW, END_ROW, COLUMN, END_COLUMN, PATH) values (...)`
122+
123+
Example output of script would be the creation of file `/thread/ranges.sql` with contents:
124+
125+
```sql
126+
INSERT INTO RANGES (ROW, COLUMN, END_ROW, END_COLUMN, NODE_TYPE, PATH) values (0,0,0,21,'comment', 'main.py')
127+
INSERT INTO RANGES (ROW, COLUMN, END_ROW, END_COLUMN, NODE_TYPE, PATH) values (2,0,2,37,'import_from_statement', 'main.py')
128+
INSERT INTO RANGES (ROW, COLUMN, END_ROW, END_COLUMN, NODE_TYPE, PATH) values (4,0,5,17,'if_statement', 'main.py')
129+
```
130+
131+
5. Execute the code in the javascript sandbox.
35132

36-
Run tree-sitter-flow
37-
fd args are `-e py`
38-
use `/thread/top-level` as output dir
133+
6. Finally, run `.read /thread/ranges.sql` in db.sqlite

0 commit comments

Comments
 (0)