|
1 | | -import Parser from "tree-sitter"; |
2 | | -import Bash from "tree-sitter-bash"; |
3 | | - |
4 | | -const parser = new Parser(); |
5 | | -parser.setLanguage(Bash.language as any); |
| 1 | +const parser = async () => { |
| 2 | + try { |
| 3 | + const { default: Parser } = await import("tree-sitter") |
| 4 | + const Bash = await import("tree-sitter-bash") |
| 5 | + const p = new Parser() |
| 6 | + p.setLanguage(Bash.language as any) |
| 7 | + return p |
| 8 | + } catch (e) { |
| 9 | + const { default: Parser } = await import("web-tree-sitter") |
| 10 | + const { default: treeWasm } = await import("web-tree-sitter/tree-sitter.wasm" as string, { with: { type: "wasm" } }) |
| 11 | + await Parser.init({ |
| 12 | + locateFile() { |
| 13 | + return treeWasm |
| 14 | + }, |
| 15 | + }) |
| 16 | + const { default: bashWasm } = await import("tree-sitter-bash/tree-sitter-bash.wasm" as string, { |
| 17 | + with: { type: "wasm" }, |
| 18 | + }) |
| 19 | + const bashLanguage = await Parser.Language.load(bashWasm) |
| 20 | + const p = new Parser() |
| 21 | + p.setLanguage(bashLanguage) |
| 22 | + return p |
| 23 | + } |
| 24 | +} |
6 | 25 |
|
7 | | -const sourceCode = `cd --foo foo/bar && echo "hello" && cd ../baz`; |
| 26 | +const sourceCode = `cd --foo foo/bar && echo "hello" && cd ../baz` |
8 | 27 |
|
9 | | -const tree = parser.parse(sourceCode); |
| 28 | +const tree = await parser().then((p) => p.parse(sourceCode)) |
10 | 29 |
|
11 | 30 | // Function to extract commands and arguments |
12 | | -function extractCommands( |
13 | | - node: any, |
14 | | -): Array<{ command: string; args: string[] }> { |
15 | | - const commands: Array<{ command: string; args: string[] }> = []; |
| 31 | +function extractCommands(node: any): Array<{ command: string; args: string[] }> { |
| 32 | + const commands: Array<{ command: string; args: string[] }> = [] |
16 | 33 |
|
17 | 34 | function traverse(node: any) { |
18 | 35 | if (node.type === "command") { |
19 | | - const commandNode = node.child(0); |
| 36 | + const commandNode = node.child(0) |
20 | 37 | if (commandNode) { |
21 | | - const command = commandNode.text; |
22 | | - const args: string[] = []; |
| 38 | + const command = commandNode.text |
| 39 | + const args: string[] = [] |
23 | 40 |
|
24 | 41 | // Extract arguments |
25 | 42 | for (let i = 1; i < node.childCount; i++) { |
26 | | - const child = node.child(i); |
| 43 | + const child = node.child(i) |
27 | 44 | if (child && child.type === "word") { |
28 | | - args.push(child.text); |
| 45 | + args.push(child.text) |
29 | 46 | } |
30 | 47 | } |
31 | 48 |
|
32 | | - commands.push({ command, args }); |
| 49 | + commands.push({ command, args }) |
33 | 50 | } |
34 | 51 | } |
35 | 52 |
|
36 | 53 | // Traverse children |
37 | 54 | for (let i = 0; i < node.childCount; i++) { |
38 | | - traverse(node.child(i)); |
| 55 | + traverse(node.child(i)) |
39 | 56 | } |
40 | 57 | } |
41 | 58 |
|
42 | | - traverse(node); |
43 | | - return commands; |
| 59 | + traverse(node) |
| 60 | + return commands |
44 | 61 | } |
45 | 62 |
|
46 | 63 | // Extract and display commands |
47 | | -console.log("Source code: " + sourceCode); |
48 | | -const commands = extractCommands(tree.rootNode); |
49 | | -console.log("Extracted commands:"); |
| 64 | +console.log("Source code: " + sourceCode) |
| 65 | +const commands = extractCommands(tree.rootNode) |
| 66 | +console.log("Extracted commands:") |
50 | 67 | commands.forEach((cmd, index) => { |
51 | | - console.log(`${index + 1}. Command: ${cmd.command}`); |
52 | | - console.log(` Args: [${cmd.args.join(", ")}]`); |
53 | | -}); |
| 68 | + console.log(`${index + 1}. Command: ${cmd.command}`) |
| 69 | + console.log(` Args: [${cmd.args.join(", ")}]`) |
| 70 | +}) |
0 commit comments