summaryrefslogtreecommitdiff
path: root/graph_macro/src/lib.rs
diff options
context:
space:
mode:
authorJSDurand <mmemmew@gmail.com>2023-06-02 15:00:48 +0800
committerJSDurand <mmemmew@gmail.com>2023-06-02 15:00:48 +0800
commit8486474f377faf2d800d79166a7abe6b975e3e50 (patch)
treef06baa22bafebf9393869664067e9cf308c92634 /graph_macro/src/lib.rs
parent1455da10f943e2aa1bdf26fb2697dafccc61e073 (diff)
Fix a bug of duplication from planting after sploing
I should have staged and committed these changes separately, but I am too lazy to deal with that. The main changes in this commit are that I added the derive macro that automates the delegation of the Graph trait. This saves a lot of boiler-plate codes. The second main change, perhaps the most important one, is that I found and tried to fix a bug that caused duplication of nodes. The bug arises from splitting or cloning a node multiple times, and immediately planting the same fragment under the new "sploned" node. That is, when we try to splone the node again, we found that we need to splone, because the node that was created by the same sploning process now has a different label because of the planting of the fragment. Then after the sploning, we plant the fragment again. This makes the newly sploned node have the same label (except for the clone index) and the same children as the node that was sploned and planted in the previous rounds. The fix is to check for the existence of a node that has the same set of children as the about-to-be-sploned node, except for the last one, which contains the about-to-be-planted fragment as a prefix. If that is the case, treat it as an already existing node, so that we do not have to splone the node again. This is consistent with the principle to not create what we do not need.
Diffstat (limited to 'graph_macro/src/lib.rs')
-rw-r--r--graph_macro/src/lib.rs55
1 files changed, 33 insertions, 22 deletions
diff --git a/graph_macro/src/lib.rs b/graph_macro/src/lib.rs
index 0f56f57..3b7742a 100644
--- a/graph_macro/src/lib.rs
+++ b/graph_macro/src/lib.rs
@@ -84,20 +84,20 @@ impl CompileError {
// I cannot use the parse method of the type TokenStream, as
// that will not set the spans properly.
- let compile_error_ident = TokenTree::Ident(Ident::new("compile_error", self.span.clone()));
+ let compile_error_ident = TokenTree::Ident(Ident::new("compile_error", self.span));
let mut exclamation_punct = TokenTree::Punct(Punct::new('!', Spacing::Alone));
- exclamation_punct.set_span(self.span.clone());
+ exclamation_punct.set_span(self.span);
let mut arg_mes_literal = TokenTree::Literal(Literal::string(&self.mes));
- arg_mes_literal.set_span(self.span.clone());
+ arg_mes_literal.set_span(self.span);
let arg_mes_stream = [arg_mes_literal].into_iter().collect();
let mut arg_group = TokenTree::Group(Group::new(Delimiter::Parenthesis, arg_mes_stream));
- arg_group.set_span(self.span.clone());
+ arg_group.set_span(self.span);
let mut semi_colon_punct = TokenTree::Punct(Punct::new(';', Spacing::Alone));
@@ -158,7 +158,7 @@ pub fn graph_derive(input: TokenStream) -> TokenStream {
let mut result = String::new();
if !generics.is_empty() {
- result.push_str("<");
+ result.push('<');
}
for generic in generics.iter() {
@@ -166,7 +166,7 @@ pub fn graph_derive(input: TokenStream) -> TokenStream {
}
if !generics.is_empty() {
- result.push_str(">");
+ result.push('>');
}
result
@@ -176,7 +176,7 @@ pub fn graph_derive(input: TokenStream) -> TokenStream {
let mut result = String::new();
if !generics.is_empty() {
- result.push_str("<");
+ result.push('<');
}
for generic in generics.iter() {
@@ -184,7 +184,7 @@ pub fn graph_derive(input: TokenStream) -> TokenStream {
}
if !generics.is_empty() {
- result.push_str(">");
+ result.push('>');
}
result
@@ -236,6 +236,11 @@ self.{field_name}.has_edge(nodea, nodeb)
fn replace_by_builder(&mut self, _builder: impl graph::builder::Builder<Result = Self>) {{
unimplemented!()
}}
+
+#[inline]
+fn print_viz(&self, filename: &str) -> Result<(), std::io::Error> {{
+self.{field_name}.print_viz(filename)
+}}
}}"
);
@@ -386,6 +391,10 @@ fn get_graph_field_name_type(
let mut cloned_body = body.clone();
+ while move_attributes(&mut cloned_body).is_ok() {}
+
+ let _ = get_visibility_mod(&mut cloned_body)?;
+
let mut result_name = get_first_ident(&mut cloned_body, g.span())?.to_string();
move_punct(&mut cloned_body, ':')?;
@@ -418,12 +427,19 @@ fn get_graph_field_name_type(
break;
}
- let _ = move_punct(&mut attr_stream, ',');
+ get_until(
+ &mut attr_stream,
+ |tree| matches!(tree, TokenTree::Punct(p) if p.as_char() == ','),
+ true,
+ true,
+ )?;
}
body.next();
if found_attribute {
+ while move_attributes(&mut body).is_ok() {}
+
get_visibility_mod(&mut body)?;
result_name = get_ident(&mut body)?.to_string();
@@ -557,12 +573,9 @@ fn get_first_ident(
input: &mut Peekable<impl Iterator<Item = TokenTree>>,
span: Span,
) -> Result<Ident, CompileError> {
- while let Some(tree) = input.next() {
- match tree {
- TokenTree::Ident(id) => {
- return Ok(id);
- }
- _ => {}
+ for tree in input {
+ if let TokenTree::Ident(id) = tree {
+ return Ok(id);
}
}
@@ -580,7 +593,7 @@ fn get_until(
let mut found_predicate = false;
if consume_boundary {
- while let Some(tree) = input.next() {
+ for tree in input.by_ref() {
if predicate(&tree) {
found_predicate = true;
break;
@@ -647,16 +660,14 @@ fn move_attributes(
match input.peek() {
Some(TokenTree::Group(g)) if g.delimiter() == Delimiter::Bracket => {
input.next();
+
+ Ok(())
}
Some(_) => {
- return myerror!("expected a group in square brackets", input);
- }
- _ => {
- return end_of_input;
+ myerror!("expected a group in square brackets", input)
}
+ _ => end_of_input,
}
-
- Ok(())
}
Some(_) => myerror!(error_mes, input),
_ => end_of_input,