Skip to content

Commit f836994

Browse files
experimenting with tree-sitter
1 parent 3320ecb commit f836994

File tree

7 files changed

+177
-0
lines changed

7 files changed

+177
-0
lines changed

.cargo/config.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,9 @@ test_details = ["test", "--target", "aarch64-apple-darwin"]
44
[build]
55
target = "wasm32-wasip1"
66

7+
[env]
8+
CC_wasm32_wasip1 = { value = "/Users/christian/.wasi-sdk/bin/clang", force = true }
9+
CFLAGS_wasm32_wasip1 = { value = "--sysroot=/Users/christian/.wasi-sdk/share/wasi-sysroot", force = true }
10+
711
[target.'cfg(all(target_arch = "wasm32"))']
812
runner = "viceroy run -C ../../fastly.toml -- "

Cargo.lock

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,6 @@ urlencoding = "2.1"
5656
uuid = { version = "1.18", features = ["v4"] }
5757
validator = { version = "0.20", features = ["derive"] }
5858
which = "8"
59+
tree-sitter = { version = "0.26.2" }
60+
tree-sitter-javascript = { version = "0.25.0" }
61+

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,26 @@ See https://asdf-vm.com/guide/getting-started.html#_2-configure-asdf
112112
git clone [email protected]:IABTechLab/trusted-server.git
113113
```
114114

115+
### WASI SDK (Required for Tree-sitter)
116+
117+
This project uses tree-sitter for JavaScript parsing, which requires the WASI SDK to compile C code for WebAssembly.
118+
119+
#### Download and Install WASI SDK
120+
121+
```sh
122+
cd /tmp
123+
curl -LO https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-24/wasi-sdk-24.0-arm64-macos.tar.gz
124+
tar -xzf wasi-sdk-24.0-arm64-macos.tar.gz
125+
mv wasi-sdk-24.0-arm64-macos ~/.wasi-sdk
126+
```
127+
128+
:warning: For Linux or x86_64 macOS, download the appropriate release from [WASI SDK releases](https://github.com/WebAssembly/wasi-sdk/releases/tag/wasi-sdk-24):
129+
- Linux x86_64: `wasi-sdk-24.0-x86_64-linux.tar.gz`
130+
- Linux arm64: `wasi-sdk-24.0-arm64-linux.tar.gz`
131+
- macOS x86_64: `wasi-sdk-24.0-x86_64-macos.tar.gz`
132+
133+
The WASI SDK paths are already configured in `.cargo/config.toml` and will be used automatically during build.
134+
115135
### Configure
116136

117137
#### Edit configuration files

crates/common/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ uuid = { workspace = true }
4545
validator = { workspace = true }
4646
ed25519-dalek = { workspace = true }
4747
once_cell = { workspace = true }
48+
tree-sitter = { workspace = true }
49+
tree-sitter-javascript = { workspace = true }
4850

4951
[build-dependencies]
5052
config = { workspace = true }

crates/common/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,5 @@ pub mod streaming_processor;
4343
pub mod streaming_replacer;
4444
pub mod synthetic;
4545
pub mod test_support;
46+
pub mod tree_sitter_test;
4647
pub mod tsjs;
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
use tree_sitter::{Parser, Tree};
2+
3+
/// Initialize a tree-sitter parser with JavaScript language support
4+
pub fn create_js_parser() -> Parser {
5+
let mut parser = Parser::new();
6+
let language = tree_sitter_javascript::LANGUAGE.into();
7+
parser
8+
.set_language(&language)
9+
.expect("Failed to set JavaScript language");
10+
parser
11+
}
12+
13+
/// Parse JavaScript source code and return the syntax tree
14+
pub fn parse_js(source: &str) -> Option<Tree> {
15+
let mut parser = create_js_parser();
16+
parser.parse(source, None)
17+
}
18+
19+
#[cfg(test)]
20+
mod tests {
21+
use super::*;
22+
23+
#[test]
24+
fn test_parser_creation() {
25+
let parser = create_js_parser();
26+
// Parser should be created successfully with JavaScript language
27+
assert!(parser.language().is_some());
28+
}
29+
30+
#[test]
31+
fn test_parse_simple_function() {
32+
let source = "function add(a, b) { return a + b; }";
33+
let tree = parse_js(source).expect("Failed to parse JavaScript");
34+
35+
let root_node = tree.root_node();
36+
assert_eq!(root_node.kind(), "program");
37+
assert_eq!(root_node.child_count(), 1);
38+
39+
// First child should be a function declaration
40+
let function_node = root_node.child(0).expect("Should have a child");
41+
assert_eq!(function_node.kind(), "function_declaration");
42+
}
43+
44+
#[test]
45+
fn test_parse_variable_declaration() {
46+
let source = "const x = 42;";
47+
let tree = parse_js(source).expect("Failed to parse JavaScript");
48+
49+
let root_node = tree.root_node();
50+
assert_eq!(root_node.kind(), "program");
51+
52+
// First child should be a lexical declaration
53+
let declaration = root_node.child(0).expect("Should have a child");
54+
assert_eq!(declaration.kind(), "lexical_declaration");
55+
}
56+
57+
#[test]
58+
fn test_parse_complex_code() {
59+
let source = r#"
60+
class Calculator {
61+
constructor() {
62+
this.result = 0;
63+
}
64+
65+
add(x, y) {
66+
return x + y;
67+
}
68+
}
69+
70+
const calc = new Calculator();
71+
console.log(calc.add(5, 3));
72+
"#;
73+
74+
let tree = parse_js(source).expect("Failed to parse JavaScript");
75+
let root_node = tree.root_node();
76+
77+
assert_eq!(root_node.kind(), "program");
78+
// Should have at least 3 children: class, const declaration, expression statement
79+
assert!(root_node.child_count() >= 3);
80+
81+
// Verify the class declaration
82+
let class_node = root_node.child(0).expect("Should have first child");
83+
assert_eq!(class_node.kind(), "class_declaration");
84+
}
85+
86+
#[test]
87+
fn test_parse_arrow_function() {
88+
let source = "const multiply = (a, b) => a * b;";
89+
let tree = parse_js(source).expect("Failed to parse JavaScript");
90+
91+
let root_node = tree.root_node();
92+
assert_eq!(root_node.kind(), "program");
93+
94+
let declaration = root_node.child(0).expect("Should have a child");
95+
assert_eq!(declaration.kind(), "lexical_declaration");
96+
}
97+
98+
#[test]
99+
fn test_parse_with_syntax_error() {
100+
// This should still produce a tree, but with error nodes
101+
let source = "function broken( { return x; }";
102+
let tree = parse_js(source);
103+
104+
assert!(tree.is_some());
105+
let tree = tree.unwrap();
106+
assert!(tree.root_node().has_error());
107+
}
108+
}

0 commit comments

Comments
 (0)