Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
372 changes: 361 additions & 11 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ log = { workspace = true }
rayon = { workspace = true}
sysinfo = "0.33.1"
rkyv.workspace = true

[dev-dependencies]
test-log = { workspace = true }
[workspace]
members = [
"codegen-sdk-analyzer",
Expand All @@ -40,3 +41,5 @@ serde = { version = "1.0.217", features = ["derive"] }
serde_json = "1.0.138"
anyhow = { version = "1.0.95", features = ["backtrace"] }
rkyv = { version = "0.8.10", features = ["bytes-1","pointer_width_64"] }
test-log = "0.2.17"
enum_delegate = "0.2.0"
2 changes: 2 additions & 0 deletions codegen-sdk-analyzer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ edition = "2024"
[dependencies]
salsa = "0.16.1"
codegen-sdk-ast = { path = "../codegen-sdk-ast" }
[dev-dependencies]
test-log = { workspace = true }
2 changes: 1 addition & 1 deletion codegen-sdk-analyzer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub fn add(left: u64, right: u64) -> u64 {
mod tests {
use super::*;

#[test]
#[test_log::test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
Expand Down
2 changes: 1 addition & 1 deletion codegen-sdk-ast-generator/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use codegen_sdk_common::language::Language;
pub fn generate_ast(language: &Language) -> anyhow::Result<String> {
let content = format!(
"
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct {language_struct_name}File {{
node: {language_name}::{root_node_name},
path: PathBuf
Expand Down
1 change: 1 addition & 0 deletions codegen-sdk-ast/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![recursion_limit = "512"]
use codegen_sdk_common::{File, HasNode};
pub use codegen_sdk_cst::*;
pub trait Named {
Expand Down
3 changes: 3 additions & 0 deletions codegen-sdk-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ base64 = "0.22.1"
buildid = "1.0.3"
sha2 = "0.10.8"
zstd = { version = "0.13.2", features = ["zstdmt"] }
enum_delegate = { workspace = true }
[dev-dependencies]
test-log = { workspace = true }
[features]
python = ["dep:tree-sitter-python"]
json = ["dep:tree-sitter-json"]
Expand Down
15 changes: 15 additions & 0 deletions codegen-sdk-common/src/language.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use std::num::NonZeroU16;

use convert_case::{Case, Casing};
use tree_sitter::Parser;

use crate::{
errors::ParseError,
parser::{Node, parse_node_types},
};
#[derive(Debug)]
pub struct Language {
pub name: &'static str,
pub struct_name: &'static str,
Expand Down Expand Up @@ -50,6 +53,18 @@ impl Language {
.type_name
.to_case(Case::Pascal)
}
pub fn kind_id(&self, name: &str, named: bool) -> u16 {
self.tree_sitter_language.id_for_node_kind(name, named)
}
pub fn kind_name(&self, id: u16) -> Option<&str> {
self.tree_sitter_language.node_kind_for_id(id)
}
pub fn field_id(&self, name: &str) -> Option<NonZeroU16> {
self.tree_sitter_language.field_id_for_name(name)
}
pub fn field_name(&self, id: u16) -> Option<&str> {
self.tree_sitter_language.field_name_for_id(id)
}
}
#[cfg(feature = "java")]
pub mod java;
Expand Down
12 changes: 12 additions & 0 deletions codegen-sdk-common/src/language/go.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use super::Language;
lazy_static! {
pub static ref Go: Language = Language::new(
"go",
"Go",
tree_sitter_go::NODE_TYPES,
&["go"],
tree_sitter_go::LANGUAGE.into(),
tree_sitter_go::TAGS_QUERY,
)
.unwrap();
}
12 changes: 12 additions & 0 deletions codegen-sdk-common/src/language/ruby.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use super::Language;
lazy_static! {
pub static ref Ruby: Language = Language::new(
"ruby",
"Ruby",
tree_sitter_ruby::NODE_TYPES,
&["rb"],
tree_sitter_ruby::LANGUAGE.into(),
tree_sitter_ruby::TAGS_QUERY,
)
.unwrap();
}
12 changes: 12 additions & 0 deletions codegen-sdk-common/src/language/rust.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use super::Language;
lazy_static! {
pub static ref Rust: Language = Language::new(
"rust",
"Rust",
tree_sitter_rust::NODE_TYPES,
&["rs"],
tree_sitter_rust::LANGUAGE.into(),
tree_sitter_rust::TAGS_QUERY,
)
.unwrap();
}
4 changes: 2 additions & 2 deletions codegen-sdk-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ pub mod parser;
#[macro_use]
extern crate lazy_static;
pub mod naming;
mod point;
pub use point::Point;
pub mod serialize;
pub mod tree;
pub use tree::{Point, Range};
8 changes: 6 additions & 2 deletions codegen-sdk-common/src/naming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub fn normalize_string(string: &str) -> String {
let escaped = String::from_iter(string.chars().map(escape_char));
escaped
}
pub fn normalize_type_name(type_name: &str) -> String {
pub fn normalize_type_name(type_name: &str, named: bool) -> String {
let mut cased = type_name.to_string();
if type_name.chars().any(|c| c.is_ascii_alphabetic()) {
cased = cased.to_case(Case::Pascal);
Expand All @@ -78,5 +78,9 @@ pub fn normalize_type_name(type_name: &str) -> String {
"Type name '{}' contains invalid characters",
type_name
);
escaped
if named {
escaped
} else {
format!("Anonymous{}", escaped)
}
}
12 changes: 6 additions & 6 deletions codegen-sdk-common/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub struct Node {
#[serde(rename = "type")]
pub type_name: String,
Expand All @@ -15,28 +15,28 @@ pub struct Node {
pub children: Option<Children>,
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub struct Fields {
#[serde(flatten)]
pub fields: std::collections::HashMap<String, FieldDefinition>,
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub struct FieldDefinition {
pub multiple: bool,
pub required: bool,
#[serde(default)]
pub types: Vec<TypeDefinition>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct TypeDefinition {
#[serde(rename = "type")]
pub type_name: String,
pub named: bool,
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)]
pub struct Children {
pub multiple: bool,
pub required: bool,
Expand All @@ -52,7 +52,7 @@ pub fn parse_node_types(node_types: &str) -> anyhow::Result<Vec<Node>> {
mod tests {
use super::*;
use crate::language::python::Python;
#[test]
#[test_log::test]
fn test_parse_node_types() {
let cst = parse_node_types(Python.node_types).unwrap();
assert!(!cst.is_empty());
Expand Down
Loading