Skip to content

Commit 4b8ad6c

Browse files
committed
Add macro_lib for AST node generation and enhance Codebase structure with new fields
1 parent bab2ee1 commit 4b8ad6c

File tree

4 files changed

+93
-7
lines changed

4 files changed

+93
-7
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ edition = "2021"
66
[dependencies]
77
syn = { version = "2.0.87", features = ["full"] }
88
thiserror = "1.0.67"
9+
macro_lib = { path = "macro_lib" }
910

1011
[dev-dependencies]
1112
soroban-sdk = "21.7.6"
13+
14+
workspace = { members = [".", "macro_lib"] }

macro_lib/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "macro_lib"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
proc-macro = true
8+
9+
[dependencies]
10+
syn = { version = "2.0", features = ["full"] }
11+
quote = "1.0"
12+
proc-macro2 = "1.0"

macro_lib/src/lib.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#![warn(clippy::pedantic)]
2+
extern crate proc_macro;
3+
4+
use proc_macro::TokenStream;
5+
use quote::quote;
6+
use syn::{parse_macro_input, Field, FieldMutability, Fields, Ident, ItemStruct, Type, Visibility};
7+
8+
#[proc_macro_attribute]
9+
pub fn ast_node(_attr: TokenStream, item: TokenStream) -> TokenStream {
10+
let mut ast = parse_macro_input!(item as ItemStruct);
11+
let fields_to_add = vec![
12+
("start_line", "u32"),
13+
("start_col", "u32"),
14+
("end_line", "u32"),
15+
("end_col", "u32"),
16+
];
17+
18+
match &mut ast.fields {
19+
Fields::Named(fields_named) => {
20+
for (field_name, field_type) in fields_to_add {
21+
let field_ident = Ident::new(field_name, proc_macro2::Span::call_site());
22+
let field_type: Type = syn::parse_str(field_type).unwrap();
23+
24+
let new_field = Field {
25+
attrs: Vec::new(),
26+
vis: Visibility::Inherited,
27+
mutability: FieldMutability::None,
28+
ident: Some(field_ident),
29+
colon_token: Some(Default::default()),
30+
ty: field_type,
31+
};
32+
fields_named.named.push(new_field);
33+
}
34+
}
35+
_ => panic!("Only named fields are supported"),
36+
}
37+
38+
let result = quote! {
39+
#ast
40+
};
41+
42+
result.into()
43+
}

src/ast.rs

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![warn(clippy::pedantic)]
22
use crate::errors::SDKErr;
3+
use macro_lib::ast_node;
34
use std::collections::HashMap;
45

56
fn parse_file(file_name: &str, content: &mut str) -> Result<syn::File, SDKErr> {
@@ -15,6 +16,8 @@ pub struct Codebase {
1516
ast_map: HashMap<String, syn::File>,
1617
free_functions: Vec<Function>,
1718
contracts: Vec<Contract>,
19+
structs: Vec<Struct>,
20+
enums: Vec<Enum>,
1821
}
1922

2023
impl Codebase {
@@ -23,6 +26,8 @@ impl Codebase {
2326
ast_map: HashMap::new(),
2427
free_functions: Vec::new(),
2528
contracts: Vec::new(),
29+
structs: Vec::new(),
30+
enums: Vec::new(),
2631
}
2732
}
2833

@@ -52,6 +57,7 @@ impl Codebase {
5257
}
5358
}
5459

60+
#[ast_node]
5561
#[derive(Clone)]
5662
pub struct Contract {
5763
name: String,
@@ -61,39 +67,61 @@ pub struct Contract {
6167
}
6268

6369
impl Contract {
64-
pub fn new(name: &str) -> Self {
70+
pub fn new(name: &str, start_line: u32, start_col: u32, end_line: u32, end_col: u32) -> Self {
6571
Contract {
6672
name: name.to_string(),
6773
functions: Vec::new(),
6874
structs: Vec::new(),
6975
enums: Vec::new(),
76+
start_line,
77+
start_col,
78+
end_line,
79+
end_col,
7080
}
7181
}
7282
}
7383

84+
#[ast_node]
7485
#[derive(Clone)]
7586
pub struct Function {}
7687

7788
impl Function {
78-
pub fn new() -> Self {
79-
Function {}
89+
pub fn new(start_line: u32, start_col: u32, end_line: u32, end_col: u32) -> Self {
90+
Function {
91+
start_line,
92+
start_col,
93+
end_line,
94+
end_col,
95+
}
8096
}
8197
}
8298

99+
#[ast_node]
83100
#[derive(Clone)]
84101
pub struct Struct {}
85102

86103
impl Struct {
87-
pub fn new() -> Self {
88-
Struct {}
104+
pub fn new(start_line: u32, start_col: u32, end_line: u32, end_col: u32) -> Self {
105+
Struct {
106+
start_line,
107+
start_col,
108+
end_line,
109+
end_col,
110+
}
89111
}
90112
}
91113

114+
#[ast_node]
92115
#[derive(Clone)]
93116
pub struct Enum {}
94117

95118
impl Enum {
96-
pub fn new() -> Self {
97-
Enum {}
119+
pub fn new(start_line: u32, start_col: u32, end_line: u32, end_col: u32) -> Self {
120+
Enum {
121+
start_line,
122+
start_col,
123+
end_line,
124+
end_col,
125+
}
98126
}
99127
}

0 commit comments

Comments
 (0)