Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/todesking/serde_tree_sitter
Serde Deserializer for tree_sitter
https://github.com/todesking/serde_tree_sitter
serde tree-sitter
Last synced: about 1 month ago
JSON representation
Serde Deserializer for tree_sitter
- Host: GitHub
- URL: https://github.com/todesking/serde_tree_sitter
- Owner: todesking
- License: mit
- Created: 2024-03-30T18:32:35.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-04-13T15:57:09.000Z (10 months ago)
- Last Synced: 2024-11-01T14:20:55.238Z (3 months ago)
- Topics: serde, tree-sitter
- Language: Rust
- Homepage:
- Size: 49.8 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Serde deserializer for Tree-sitter
Deserializer for `tree_sitter::Node`.
You can map tree-sitter's parse result to Rust struct/enum with `#[derive(serde::Deserialize)]`.## Example
### Map node to enum
```javascript
// grammar.js
rules: {
expr: $ => choice($.expr_int, $.expr_bool)
}
``````rust
#[derive(serde::Deserialize, Debug)]
#[serde(rename_all = "snake_case")]
enum Expr {
ExprInt(i64),
ExprBool(bool),
}let tree: tree_sitter::Tree = parse("...")?;
serde_tree_sitter::from_tree::(tree, src, true)
```### Map named children to tuple struct
```javascript
// grammar.js
rules: {
let: $ => seq('let', $.ident, '=', $.expr),
// ...
}
``````rust
#[derive(serde::Deserialize, Debug)]
#[serde(rename="let")]
struct Let(Ident, Expr);
```### Map named children to vec
```javascript
rules: {
nums: $ => repeat(seq($.int, ',')),
// ...
}
``````rust
serde_tree_sitter::from_tree::>(tree, src, true)
```## Map field to struct
```javascript
rules: {
object: $ => seq('{', repeat(seq($.pair, ',')), '}'),
pair: $ => seq(field('key', $.ident), ':', field('value', $.expr),
// ...
}
``````rust
#[derive(serde::Deserialize, Debug)]
#[serde(rename="object")]
struct Object(Vec);#[derive(serde::Deserialize, Debug)]
#[serde(rename="pair")]
#[serde(rename_all="snake_case")]
struct Pair {
key: String,
value: Expr,
}
```