Skip to content

Commit fd381f4

Browse files
committed
Refactor AST building functions to improve structure and add NodeKind support
1 parent 95b368a commit fd381f4

File tree

5 files changed

+177
-115
lines changed

5 files changed

+177
-115
lines changed

ast/src/builder.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ pub fn build_ast(root: Node, code: &[u8]) -> anyhow::Result<SourceFile> {
4242
let child_kind = child.kind();
4343

4444
match child_kind {
45-
"use_directive" => build_use_directive(&mut ast, &child, code),
45+
"use_directive" => {
46+
ast.add_use_directive(build_use_directive(&child, code));
47+
}
4648
_ => match build_definition(&child, code) {
4749
Ok(definition) => ast.add_definition(definition),
4850
Err(err) => {
@@ -57,7 +59,7 @@ pub fn build_ast(root: Node, code: &[u8]) -> anyhow::Result<SourceFile> {
5759
Ok(ast)
5860
}
5961

60-
fn build_use_directive(parent: &mut SourceFile, node: &Node, code: &[u8]) {
62+
fn build_use_directive(node: &Node, code: &[u8]) -> Rc<UseDirective> {
6163
let location = get_location(node, code);
6264
let mut segments = None;
6365
let mut imported_types = None;
@@ -85,12 +87,7 @@ fn build_use_directive(parent: &mut SourceFile, node: &Node, code: &[u8]) {
8587
imported_types = Some(founded_imported_types);
8688
}
8789

88-
parent.add_use_directive(Rc::new(UseDirective::new(
89-
imported_types,
90-
segments,
91-
from,
92-
location,
93-
)));
90+
Rc::new(UseDirective::new(imported_types, segments, from, location))
9491
}
9592

9693
fn build_spec_definition(node: &Node, code: &[u8]) -> Rc<SpecDefinition> {

ast/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![warn(clippy::pedantic)]
22
pub mod builder;
33
pub(crate) mod node;
4+
pub(crate) mod node_kind;
45
pub mod types;
56
pub(crate) mod types_impl;

ast/src/node.rs

Lines changed: 34 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use core::fmt;
55
use std::fmt::{Display, Formatter};
66
use std::{any::Any, cmp::Reverse};
77

8+
use crate::node_kind::NodeKind;
9+
810
#[derive(Clone, PartialEq, Eq, Debug, Default, serde::Serialize, serde::Deserialize)]
911
pub struct Location {
1012
pub offset_start: u32,
@@ -37,8 +39,8 @@ pub trait Node: Any + std::fmt::Debug {
3739
.unwrap_or_default()
3840
.to_string()
3941
}
40-
fn children(&self) -> Vec<Box<dyn Node>>;
41-
fn sorted_children(&self) -> Vec<Box<dyn Node>> {
42+
fn children(&self) -> Vec<NodeKind>;
43+
fn sorted_children(&self) -> Vec<NodeKind> {
4244
let mut children = self.children();
4345
children.sort_by_key(|c| Reverse(c.id()));
4446
children
@@ -61,31 +63,6 @@ pub enum Mutability {
6163
Constant,
6264
}
6365

64-
#[macro_export]
65-
macro_rules! location {
66-
($item:expr) => {{
67-
use syn::spanned::Spanned;
68-
#[allow(clippy::cast_possible_truncation)]
69-
$crate::node::Location {
70-
offset_start: $item.span().byte_range().start as u32,
71-
offset_end: $item.span().byte_range().end as u32,
72-
source: $item.span().source_text().unwrap_or_default(),
73-
start_line: $item.span().start().line as u32,
74-
start_column: $item.span().start().column as u32,
75-
end_line: $item.span().end().line as u32,
76-
end_column: $item.span().end().column as u32,
77-
}
78-
}};
79-
}
80-
81-
#[macro_export]
82-
macro_rules! source_code {
83-
($item:expr) => {{
84-
use syn::spanned::Spanned;
85-
$item.span().source_text().unwrap_or_default()
86-
}};
87-
}
88-
8966
#[macro_export]
9067
macro_rules! ast_enum {
9168
(
@@ -125,39 +102,39 @@ macro_rules! ast_enum {
125102
}
126103
}
127104

128-
// #[must_use]
129-
// pub fn children(&self) -> Vec<NodeKind> {
130-
// match self {
131-
// $(
132-
// $name::$arm(_a) => {
133-
// _a.children()
134-
// }
135-
// )*
136-
// }
137-
// }
105+
#[must_use]
106+
pub fn children(&self) -> Vec<$crate::node_kind::NodeKind> {
107+
match self {
108+
$(
109+
$name::$arm(_a) => {
110+
_a.children()
111+
}
112+
)*
113+
}
114+
}
138115
}
139116

140-
// impl From<&$name> for $crate::ast::node_type::NodeKind {
141-
// fn from(n: &$name) -> Self {
142-
// match n {
143-
// $(
144-
// $name::$arm(a) => {
145-
// $crate::ast::node_type::NodeKind::$name($name::$arm(a.clone()))
146-
// }
147-
// )*
148-
// }
149-
// }
150-
// }
117+
impl From<&$name> for $crate::node_kind::NodeKind {
118+
fn from(n: &$name) -> Self {
119+
match n {
120+
$(
121+
$name::$arm(a) => {
122+
$crate::node_kind::NodeKind::$name($name::$arm(a.clone()))
123+
}
124+
)*
125+
}
126+
}
127+
}
151128

152-
// impl From<$name> for $crate::ast::node_type::NodeKind {
153-
// fn from(n: $name) -> Self {
154-
// match n {
155-
// $(
156-
// $name::$arm(inner) => $crate::ast::node_type::NodeKind::$name($name::$arm(inner.clone())),
157-
// )*
158-
// }
159-
// }
160-
// }
129+
impl From<$name> for $crate::node_kind::NodeKind {
130+
fn from(n: $name) -> Self {
131+
match n {
132+
$(
133+
$name::$arm(inner) => $crate::node_kind::NodeKind::$name($name::$arm(inner.clone())),
134+
)*
135+
}
136+
}
137+
}
161138

162139
};
163140

ast/src/node_kind.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
use crate::{
4+
node::Location,
5+
types::{Definition, Expression, Literal, Statement, Type},
6+
};
7+
8+
#[derive(Clone, Debug, Serialize, Deserialize)]
9+
pub enum NodeKind {
10+
Definition(Definition),
11+
Statement(Statement),
12+
Expression(Expression),
13+
Literal(Literal),
14+
Type(Type),
15+
}
16+
17+
impl NodeKind {
18+
#[must_use]
19+
pub fn id(&self) -> u32 {
20+
match self {
21+
NodeKind::Definition(d) => d.id(),
22+
NodeKind::Statement(s) => s.id(),
23+
NodeKind::Expression(e) => e.id(),
24+
NodeKind::Literal(l) => l.id(),
25+
NodeKind::Type(t) => t.id(),
26+
}
27+
}
28+
29+
#[must_use]
30+
#[allow(clippy::cast_possible_truncation)]
31+
pub fn location(&self) -> Location {
32+
match self {
33+
NodeKind::Definition(d) => d.location().clone(),
34+
NodeKind::Statement(s) => s.location(),
35+
NodeKind::Expression(e) => e.location(),
36+
NodeKind::Literal(l) => l.location(),
37+
NodeKind::Type(t) => t.location(),
38+
}
39+
}
40+
41+
#[must_use]
42+
pub fn children(&self) -> Vec<NodeKind> {
43+
match self {
44+
NodeKind::Definition(definition) => definition.children(),
45+
NodeKind::Statement(statement) => statement.children(),
46+
NodeKind::Expression(expression) => expression.children(),
47+
NodeKind::Literal(literal) => literal.children(),
48+
NodeKind::Type(ty) => ty.children(),
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)