Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove generated node names #498

Merged
merged 1 commit into from
Jun 16, 2023
Merged
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
5 changes: 5 additions & 0 deletions .changeset/selfish-apricots-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"changelog": minor
---

flatten unnamed CST nodes into parent nodes
44 changes: 20 additions & 24 deletions crates/codegen/syntax/src/code_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ impl CodeGenerator {
stream.set_position(save);
None
}
Pass { node, .. } => Some(node),
Pass { builder, .. } => Some(builder.build()),
}
};

Expand All @@ -318,17 +318,19 @@ impl CodeGenerator {
stream.set_position(save);
None
}
Pass { node, .. } => Some(node),
Pass { builder, .. } => Some(builder.build()),
}
};

let token = cst::Node::token(
kind,
Range { start, end },
leading_trivia,
trailing_trivia,
);

return Pass {
node: cst::Node::token(
kind,
Range { start, end },
leading_trivia,
trailing_trivia,
),
builder: cst::NodeBuilder::single(token),
error: None,
};
}
Expand All @@ -353,13 +355,15 @@ impl CodeGenerator {

let end = stream.position();

let token = cst::Node::token(
kind,
Range { start, end },
None,
None,
);

return Pass {
node: cst::Node::token(
kind,
Range { start, end },
None,
None,
),
builder: cst::NodeBuilder::single(token),
error: None,
};
}
Expand All @@ -378,23 +382,15 @@ impl CodeGenerator {
let internal_function = quote! {
#[inline]
pub(crate) fn #internal_function_name(&self, stream: &mut Stream) -> ParserResult {
match self.#dispatch_function_name(stream) {
Pass{ node, error } => Pass{ node: cst::Node::top_level_rule(RuleKind::#kind, node), error },
fail => fail
}
self.#dispatch_function_name(stream).with_kind(RuleKind::#kind)
}
};
format!("{functions}\n\n{internal_function}")
} else {
let external_function_name = format_ident!("maybe_parse_{name}", name = name.to_snake_case());
let external_function = quote! {
pub(crate) fn #external_function_name(&self, stream: &mut Stream) -> Option<ParserResult> {
self.#dispatch_function_name(stream).map(|body|
match body {
Pass{ node, error } => Pass{ node: cst::Node::top_level_rule(RuleKind::#kind, node), error },
fail => fail
}
)
self.#dispatch_function_name(stream).map(|body| body.with_kind(RuleKind::#kind))
}
};
let internal_function = quote! {
Expand Down
Loading