difftreelog
style fix clippy warnings
in: master
24 files changed
Cargo.lockdiffbeforeafterboth--- a/Cargo.lock
+++ b/Cargo.lock
@@ -676,18 +676,18 @@
[[package]]
name = "jrsonnet-gcmodule"
-version = "0.4.1"
+version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "c33f4f6cdc60f5ae94ebae3dfe7f484ae79b364225d9b19601b24c804cfd8751"
+checksum = "f95b976a79e4000bb9e07ff0709dca0ea27bcf1952d4c17d91fb7364d6145683"
dependencies = [
"jrsonnet-gcmodule-derive",
]
[[package]]
name = "jrsonnet-gcmodule-derive"
-version = "0.4.1"
+version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2b30c95b285f9bb6709f1b3e6fc69b3a25e39b32ff987587fd108f0f22be5fa3"
+checksum = "51d928626220a310ff0cec815e80cf7fe104697184352ca21c40534e0b0d72d9"
dependencies = [
"proc-macro2",
"quote",
@@ -1602,7 +1602,7 @@
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
dependencies = [
- "windows-sys 0.59.0",
+ "windows-sys 0.61.2",
]
[[package]]
Cargo.tomldiffbeforeafterboth--- a/Cargo.toml
+++ b/Cargo.toml
@@ -20,7 +20,7 @@
jrsonnet-cli = { path = "./crates/jrsonnet-cli", version = "0.5.0-pre97" }
jrsonnet-types = { path = "./crates/jrsonnet-types", version = "0.5.0-pre97" }
jrsonnet-formatter = { path = "./crates/jrsonnet-formatter", version = "0.5.0-pre97" }
-jrsonnet-gcmodule = { version = "0.4.1" }
+jrsonnet-gcmodule = { version = "0.4.2" }
# Diagnostics.
# hi-doc is my library, which handles text formatting very well, but isn't polished enough yet
# Previous implementation was based on annotate-snippets, which I don't like for many reasons.
bindings/jsonnet/src/import.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/import.rs
+++ b/bindings/jsonnet/src/import.rs
@@ -66,8 +66,8 @@
base.as_ptr(),
rel.as_ptr(),
&mut found_here.cast_const(),
- &mut buf,
- &mut buf_len,
+ &raw mut buf,
+ &raw mut buf_len,
)
};
let buf_slice: &[u8] = unsafe { std::slice::from_raw_parts(buf.cast(), buf_len) };
crates/jrsonnet-evaluator/src/evaluate/destructure.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/destructure.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/destructure.rs
@@ -9,6 +9,9 @@
evaluate_method, evaluate_named_param, Context, Pending, Thunk, Val,
};
+#[cfg(feature = "exp-preserve-order")]
+use crate::evaluate;
+
#[allow(clippy::too_many_lines)]
#[allow(unused_variables)]
pub fn destruct<H: BuildHasher>(
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -143,7 +143,7 @@
false,
) {
let fctx = Pending::new();
- let mut new_bindings = FxHashMap::with_capacity(var.capacity_hint());
+ let mut new_bindings = FxHashMap::with_capacity(var.binds_len());
let obj = obj.clone();
let value = Thunk::evaluated(Val::Arr(ArrValue::lazy(vec![
Thunk::evaluated(Val::string(field.clone())),
crates/jrsonnet-evaluator/src/function/builtin.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/builtin.rs
+++ b/crates/jrsonnet-evaluator/src/function/builtin.rs
@@ -98,9 +98,9 @@
fn call(&self, _loc: CallLocation<'_>, args: &[Option<Thunk<Val>>]) -> Result<Val> {
let args = args
- .into_iter()
+ .iter()
.map(|a| a.as_ref().expect("legacy natives have no default params"))
- .map(|a| a.evaluate())
+ .map(Thunk::evaluate)
.collect::<Result<Vec<Val>>>()?;
self.handler.call(&args)
}
crates/jrsonnet-evaluator/src/obj/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/obj/mod.rs
+++ b/crates/jrsonnet-evaluator/src/obj/mod.rs
@@ -1,12 +1,5 @@
use std::{
- any::Any,
- cell::{Cell, RefCell},
- clone::Clone,
- collections::hash_map::Entry,
- fmt::{self, Debug},
- hash::{Hash, Hasher},
- num::Saturating,
- ops::ControlFlow,
+ any::Any, cell::{Cell, RefCell}, clone::Clone, cmp::Reverse, collections::hash_map::Entry, fmt::{self, Debug}, hash::{Hash, Hasher}, num::Saturating, ops::ControlFlow
};
use educe::Educe;
@@ -39,18 +32,19 @@
use jrsonnet_gcmodule::Trace;
- #[derive(Clone, Copy, Default, Debug, Trace)]
+ #[derive(Clone, Copy, Default, Debug, Trace, PartialEq, Eq, PartialOrd, Ord)]
pub struct FieldIndex(());
impl FieldIndex {
pub fn absolute(_v: u32) -> Self {
Self(())
}
+ #[must_use]
pub const fn next(self) -> Self {
Self(())
}
}
- #[derive(Clone, Copy, Default, Debug, Trace)]
+ #[derive(Clone, Copy, Default, Debug, Trace, PartialEq, Eq, PartialOrd, Ord)]
pub struct SuperDepth(());
impl SuperDepth {
pub(super) fn deepen(self) {}
@@ -59,8 +53,6 @@
#[cfg(feature = "exp-preserve-order")]
pub mod ordering {
- use std::cmp::Reverse;
-
use jrsonnet_gcmodule::Trace;
#[derive(Clone, Copy, Default, Debug, Trace, PartialEq, Eq, PartialOrd, Ord)]
@@ -69,6 +61,7 @@
pub fn absolute(v: u32) -> Self {
Self(v)
}
+ #[must_use]
pub fn next(self) -> Self {
Self(self.0 + 1)
}
@@ -78,22 +71,20 @@
pub struct SuperDepth(u32);
impl SuperDepth {
pub(super) fn deepen(&mut self) {
- self.0 += 1
+ self.0 += 1;
}
}
+}
+
+use ordering::{FieldIndex, SuperDepth};
- #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
- pub struct FieldSortKey(Reverse<SuperDepth>, FieldIndex);
- impl FieldSortKey {
- pub fn new(depth: SuperDepth, index: FieldIndex) -> Self {
- Self(Reverse(depth), index)
- }
+#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
+pub struct FieldSortKey(Reverse<SuperDepth>, FieldIndex);
+impl FieldSortKey {
+ pub fn new(depth: SuperDepth, index: FieldIndex) -> Self {
+ Self(Reverse(depth), index)
}
}
-
-#[cfg(feature = "exp-preserve-order")]
-use ordering::FieldSortKey;
-use ordering::{FieldIndex, SuperDepth};
// 0 - add
// 12 - visibility
@@ -795,7 +786,7 @@
struct FieldVisibilityData {
omitted_until: Saturating<usize>,
exists_visible: Option<Visibility>,
- #[cfg(feature = "exp-preserve-order")]
+ #[allow(dead_code, reason = "used for exp-object-ordering, ZST otherwise")]
key: FieldSortKey,
}
impl FieldVisibilityData {
@@ -804,7 +795,7 @@
.expect("non-existing fields shall be dropped at the end of fn fields_visibility()")
.is_visible()
}
- #[cfg(feature = "exp-preserve-order")]
+ #[allow(dead_code, reason = "used for exp-object-ordering, ZST otherwise")]
fn sort_key(&self) -> FieldSortKey {
self.key
}
@@ -818,12 +809,11 @@
let mut omit_index = Saturating(0);
for core in self.0.cores.iter().rev() {
core.0
- .enum_fields_core(&mut super_depth, &mut |_depth, _index, name, visibility| {
+ .enum_fields_core(&mut super_depth, &mut |depth, index, name, visibility| {
let entry = out.entry(name);
- let data = entry.or_insert(FieldVisibilityData {
+ let data = entry.or_insert_with(|| FieldVisibilityData {
exists_visible: None,
- #[cfg(feature = "exp-preserve-order")]
- key: FieldSortKey::new(_depth, _index),
+ key: FieldSortKey::new(depth, index),
omitted_until: omit_index,
});
match visibility {
crates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -61,9 +61,16 @@
}
}
+#[diagnostic::on_unimplemented(
+ note = "don't implement `ParseTypedObj` directly, it is automatically provided by `FromUntyped` derive"
+)]
pub trait ParseTypedObj: Typed {
fn parse(obj: &ObjValue) -> Result<Self>;
}
+
+#[diagnostic::on_unimplemented(
+ note = "don't implement `SerializeTypedObj` directly, it is automatically provided by `IntoUntyped` derive"
+)]
pub trait SerializeTypedObj: Typed {
fn serialize(self, out: &mut ObjValueBuilder) -> Result<()>;
fn into_object(self) -> Result<ObjValue> {
crates/jrsonnet-formatter/src/comments.rsdiffbeforeafterboth--- a/crates/jrsonnet-formatter/src/comments.rs
+++ b/crates/jrsonnet-formatter/src/comments.rs
@@ -136,7 +136,7 @@
}
line = new_line.to_string();
}
- p!(out, string(line.to_string()) nl);
+ p!(out, string(line.clone()) nl);
}
}
if doc {
crates/jrsonnet-formatter/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-formatter/src/lib.rs
+++ b/crates/jrsonnet-formatter/src/lib.rs
@@ -37,8 +37,7 @@
format_comments(&e.trivia, CommentLocation::EndOfItems, &mut items);
items.into_rc_path()
};
- let items =
- new_line_group(pi!(@i; items(o.into()) items(end_comments_items.into()))).into_rc_path();
+ let items = new_line_group(pi!(@i; items(o) items(end_comments_items.into()))).into_rc_path();
let indented = with_indent(pi!(@i; nl items(items.into())));
@@ -355,48 +354,48 @@
}
impl Printable for ArgsDesc {
fn print(&self, out: &mut PrintItems) {
- let start = LineNumber::new("args start line");
- let end = LineNumber::new("args end line");
- let multi_line = Rc::new(move |condition_context: &mut ConditionResolverContext| {
- is_multiple_lines(condition_context, start, end)
- });
-
- let (children, end_comments) = children_between::<Arg>(
- self.syntax().clone(),
- self.l_paren_token().map(Into::into).as_ref(),
- self.r_paren_token().map(Into::into).as_ref(),
- None,
- );
-
fn gen_args(children: Vec<Child<Arg>>, multi_line: ConditionResolver) -> PrintItems {
- let mut _out = PrintItems::new();
- let out = &mut _out;
+ let mut out = PrintItems::new();
let mut args = children.into_iter().peekable();
while let Some(ele) = args.next() {
if ele.should_start_with_newline {
p!(out, nl);
}
- format_comments(&ele.before_trivia, CommentLocation::AboveItem, out);
+ format_comments(&ele.before_trivia, CommentLocation::AboveItem, &mut out);
let arg = ele.value;
if arg.name().is_some() || arg.assign_token().is_some() {
- p!(out, {arg.name()} str(" = "));
+ p!(&mut out, {arg.name()} str(" = "));
}
- p!(out, { arg.expr() });
+ p!(&mut out, { arg.expr() });
let has_more = args.peek().is_some();
if has_more {
p!(out, str(","));
} else {
p!(out, if("trailing comma", multi_line, str(",")));
}
- format_comments(&ele.inline_trivia, CommentLocation::ItemInline, out);
+ format_comments(&ele.inline_trivia, CommentLocation::ItemInline, &mut out);
if has_more {
p!(out, if_else("arg separator", multi_line, nl)(sonl));
}
}
- _out
+
+ out
}
+ let start = LineNumber::new("args start line");
+ let end = LineNumber::new("args end line");
+ let multi_line = Rc::new(move |condition_context: &mut ConditionResolverContext| {
+ is_multiple_lines(condition_context, start, end)
+ });
+
+ let (children, end_comments) = children_between::<Arg>(
+ self.syntax().clone(),
+ self.l_paren_token().map(Into::into).as_ref(),
+ self.r_paren_token().map(Into::into).as_ref(),
+ None,
+ );
+
let args_items = new_line_group(gen_args(children, multi_line.clone())).into_rc_path();
let args_indented = with_indent(pi!(@i; nl items(args_items.into())));
@@ -447,6 +446,7 @@
}
impl Printable for ObjBody {
+ #[allow(clippy::too_many_lines)]
fn print(&self, out: &mut PrintItems) {
match self {
Self::ObjBodyComp(l) => {
@@ -507,6 +507,30 @@
p!(out, nl <i str("}"));
}
Self::ObjBodyMemberList(l) => {
+ fn gen_members(
+ children: Vec<Child<Member>>,
+ multi_line: ConditionResolver,
+ ) -> PrintItems {
+ let mut out = PrintItems::new();
+ let mut members = children.into_iter().peekable();
+ while let Some(mem) = members.next() {
+ if mem.should_start_with_newline {
+ p!(out, nl);
+ }
+ format_comments(&mem.before_trivia, CommentLocation::AboveItem, &mut out);
+ p!(&mut out, { mem.value });
+ let has_more = members.peek().is_some();
+ if has_more {
+ p!(out, str(","));
+ } else {
+ p!(out, if("trailing comma", multi_line, str(",")));
+ }
+ format_comments(&mem.inline_trivia, CommentLocation::ItemInline, &mut out);
+ p!(out, if_else("member separator", multi_line, nl)(sonl));
+ }
+ out
+ }
+
let (children, end_comments) = children_between::<Member>(
l.syntax().clone(),
l.l_brace_token().map(Into::into).as_ref(),
@@ -531,31 +555,6 @@
})
};
- fn gen_members(
- children: Vec<Child<Member>>,
- multi_line: ConditionResolver,
- ) -> PrintItems {
- let mut _out = PrintItems::new();
- let out = &mut _out;
- let mut members = children.into_iter().peekable();
- while let Some(mem) = members.next() {
- if mem.should_start_with_newline {
- p!(out, nl);
- }
- format_comments(&mem.before_trivia, CommentLocation::AboveItem, out);
- p!(out, { mem.value });
- let has_more = members.peek().is_some();
- if has_more {
- p!(out, str(","));
- } else {
- p!(out, if("trailing comma", multi_line, str(",")));
- }
- format_comments(&mem.inline_trivia, CommentLocation::ItemInline, out);
- p!(out, if_else("member separator", multi_line, nl)(sonl));
- }
- _out
- }
-
let members_items =
new_line_group(gen_members(children, multi_line.clone())).into_rc_path();
@@ -718,6 +717,27 @@
impl Printable for ExprArray {
fn print(&self, out: &mut PrintItems) {
+ fn gen_elements(children: Vec<Child<Expr>>, multi_line: ConditionResolver) -> PrintItems {
+ let mut out = PrintItems::new();
+ let mut els = children.into_iter().peekable();
+ while let Some(el) = els.next() {
+ if el.should_start_with_newline {
+ p!(out, nl);
+ }
+ format_comments(&el.before_trivia, CommentLocation::AboveItem, &mut out);
+ p!(&mut out, { el.value });
+ let has_more = els.peek().is_some();
+ if has_more {
+ p!(out, str(","));
+ } else {
+ p!(out, if("trailing comma", multi_line, str(",")));
+ }
+ format_comments(&el.inline_trivia, CommentLocation::ItemInline, &mut out);
+ p!(out, if_else("element separator", multi_line, nl)(sonl));
+ }
+ out
+ }
+
let (children, end_comments) = children_between::<Expr>(
self.syntax().clone(),
self.l_brack_token().map(Into::into).as_ref(),
@@ -740,28 +760,6 @@
Rc::new(move |ctx: &mut ConditionResolverContext| is_multiple_lines(ctx, start, end))
};
- fn gen_elements(children: Vec<Child<Expr>>, multi_line: ConditionResolver) -> PrintItems {
- let mut _out = PrintItems::new();
- let out = &mut _out;
- let mut els = children.into_iter().peekable();
- while let Some(el) = els.next() {
- if el.should_start_with_newline {
- p!(out, nl);
- }
- format_comments(&el.before_trivia, CommentLocation::AboveItem, out);
- p!(out, { el.value });
- let has_more = els.peek().is_some();
- if has_more {
- p!(out, str(","));
- } else {
- p!(out, if("trailing comma", multi_line, str(",")));
- }
- format_comments(&el.inline_trivia, CommentLocation::ItemInline, out);
- p!(out, if_else("element separator", multi_line, nl)(sonl))
- }
- _out
- }
-
let els_items = new_line_group(gen_elements(children, multi_line.clone())).into_rc_path();
let els = with_indent_eoi(multi_line, els_items.into(), end_comments);
@@ -800,7 +798,7 @@
Self::ExprString(s) => p!(out, { s.text() }),
Self::ExprNumber(n) => p!(out, { n.number() }),
Self::ExprArray(a) => {
- p!(out, { a })
+ p!(out, { a });
}
Self::ExprObject(obj) => {
p!(out, { obj.obj_body() });
@@ -860,6 +858,11 @@
// 0 for hard tabs
pub indent: u8,
}
+
+#[allow(
+ clippy::result_large_err,
+ reason = "TODO: there should be an intermediate representation for such reports"
+)]
pub fn format(input: &str, opts: &FormatOptions) -> Result<String, SnippetBuilder> {
let (parsed, errors) = jrsonnet_rowan_parser::parse(input);
if !errors.is_empty() {
crates/jrsonnet-macros/src/typed.rsdiffbeforeafterboth--- a/crates/jrsonnet-macros/src/typed.rs
+++ b/crates/jrsonnet-macros/src/typed.rs
@@ -156,7 +156,7 @@
// optional flatten is handled in same way as serde
if self.attr.flatten {
return quote! {
- #ident: <#ty as TypedObj>::parse(&obj).ok(),
+ #ident: <#ty as ParseTypedObj>::parse(&obj).ok(),
};
}
@@ -190,7 +190,7 @@
// optional flatten is handled in same way as serde
if self.attr.flatten {
return quote! {
- #ident: <#ty as TypedObj>::parse(&obj)?,
+ #ident: <#ty as ParseTypedObj>::parse(&obj)?,
};
}
@@ -232,12 +232,12 @@
if self.is_option {
quote! {
if let Some(value) = self.#ident {
- <#ty as TypedObj>::serialize(value, out)?;
+ <#ty as SerializeTypedObj>::serialize(value, out)?;
}
}
} else {
quote! {
- <#ty as TypedObj>::serialize(self.#ident, out)?;
+ <#ty as SerializeTypedObj>::serialize(self.#ident, out)?;
}
}
},
crates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/expr.rs
+++ b/crates/jrsonnet-parser/src/expr.rs
@@ -224,7 +224,8 @@
},
#[cfg(feature = "exp-destruct")]
Object {
- fields: Vec<(IStr, Option<Destruct>, Option<Spanned<Expr>>)>,
+ #[allow(clippy::type_complexity)]
+ fields: Vec<(IStr, Option<Destruct>, Option<Rc<Spanned<Expr>>>)>,
rest: Option<DestructRest>,
},
}
@@ -261,7 +262,7 @@
let mut out = 0;
for (_, into, _) in fields {
match into {
- Some(v) => out += v.capacity_hint(),
+ Some(v) => out += v.binds_len(),
// Field is destructured to default name
None => out += 1,
}
crates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -119,7 +119,7 @@
}
pub rule destruct_object(s: &ParserSettings) -> expr::Destruct
= "{" _
- fields:(name:id() into:(_ ":" _ into:destruct(s) {into})? default:(_ "=" _ v:expr(s) {v})? {(name, into, default)})**comma()
+ fields:(name:id() into:(_ ":" _ into:destruct(s) {into})? default:(_ "=" _ v:expr(s) {v})? {(name, into, default.map(Rc::new))})**comma()
rest:(
comma() rest:destruct_rest()? {rest}
/ comma()? {None}
crates/jrsonnet-rowan-parser/src/ast.rsdiffbeforeafterboth--- a/crates/jrsonnet-rowan-parser/src/ast.rs
+++ b/crates/jrsonnet-rowan-parser/src/ast.rs
@@ -2,8 +2,9 @@
use crate::{SyntaxKind, SyntaxNode, SyntaxNodeChildren, SyntaxToken};
-/// The main trait to go from untyped `SyntaxNode` to a typed ast. The
-/// conversion itself has zero runtime cost: ast and syntax nodes have exactly
+/// The main trait to go from untyped `SyntaxNode` to a typed ast.
+///
+/// The conversion itself has zero runtime cost: ast and syntax nodes have exactly
/// the same representation: a pointer to the tree root and a pointer to the
/// node itself.
pub trait AstNode {
crates/jrsonnet-rowan-parser/src/event.rsdiffbeforeafterboth--- a/crates/jrsonnet-rowan-parser/src/event.rs
+++ b/crates/jrsonnet-rowan-parser/src/event.rs
@@ -56,7 +56,7 @@
fn text_offset(&self) -> TextSize {
if self.offset == 0 {
return 0.into();
- };
+ }
self.lexemes.get(self.offset).map_or_else(
|| {
self.lexemes
crates/jrsonnet-rowan-parser/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-rowan-parser/src/lib.rs
+++ b/crates/jrsonnet-rowan-parser/src/lib.rs
@@ -26,7 +26,7 @@
use self::{
ast::support,
- generated::nodes::{Expr, ExprBinary, ExprObjExtend},
+ generated::nodes::{Expr, ExprObjExtend},
};
pub fn parse(input: &str) -> (SourceFile, Vec<LocatedSyntaxError>) {
crates/jrsonnet-rowan-parser/src/marker.rsdiffbeforeafterboth--- a/crates/jrsonnet-rowan-parser/src/marker.rs
+++ b/crates/jrsonnet-rowan-parser/src/marker.rs
@@ -141,7 +141,7 @@
new_m
}
/// Create new node around existing marker
- /// If previous_pos is set - the wrapping node would not include everything that happened between wrapped node end and the current position of the parser
+ /// If `previous_pos` is set - the wrapping node would not include everything that happened between wrapped node end and the current position of the parser
fn wrap_raw(
self,
p: &mut Parser,
crates/jrsonnet-rowan-parser/src/parser.rsdiffbeforeafterboth--- a/crates/jrsonnet-rowan-parser/src/parser.rs
+++ b/crates/jrsonnet-rowan-parser/src/parser.rs
@@ -52,8 +52,7 @@
write!(f, "unexpected {found:?}, expecting {expected}")
}
SyntaxError::Missing { expected } => write!(f, "missing {expected}"),
- SyntaxError::Custom { error } => write!(f, "{error}"),
- SyntaxError::Hint { error } => write!(f, "{error}"),
+ SyntaxError::Custom { error } | SyntaxError::Hint { error } => write!(f, "{error}"),
}
}
}
@@ -492,7 +491,7 @@
} else {
m.complete(p, MEMBER_FIELD_NORMAL)
};
- };
+ }
while p.at_ts(COMPSPEC) {
compspecs.push(compspec(p));
}
@@ -747,7 +746,7 @@
if p.at(T![:]) {
p.bump();
destruct(p);
- };
+ }
if p.at(T![=]) {
p.bump();
expr(p);
crates/jrsonnet-rowan-parser/src/string_block.rsdiffbeforeafterboth--- a/crates/jrsonnet-rowan-parser/src/string_block.rs
+++ b/crates/jrsonnet-rowan-parser/src/string_block.rs
@@ -11,7 +11,7 @@
use crate::SyntaxKind;
-pub(crate) fn lex_str_block_test<'d>(lex: &mut Lexer<'d, SyntaxKind>) {
+pub(crate) fn lex_str_block_test(lex: &mut Lexer<'_, SyntaxKind>) {
let _ = lex_str_block(lex);
}
@@ -48,7 +48,7 @@
}
fn eat_if(&mut self, f: impl Fn(char) -> bool) -> usize {
- if self.peek().map(f).unwrap_or(false) {
+ if self.peek().is_some_and(f) {
self.index += 1;
return 1;
}
@@ -141,9 +141,7 @@
}
}
-pub fn collect_lexed_str_block<'s>(
- input: &'s str,
-) -> Result<CollectStrBlock<'s>, StringBlockError> {
+pub fn collect_lexed_str_block(input: &str) -> Result<CollectStrBlock<'_>, StringBlockError> {
let mut collect = CollectStrBlock {
truncate: false,
lines: vec![],
@@ -179,7 +177,7 @@
}
fn mark_line(&mut self, line: &'d str) {
- self.lines.push(line)
+ self.lines.push(line);
}
}
crates/jrsonnet-rowan-parser/src/tests.rsdiffbeforeafterboth--- a/crates/jrsonnet-rowan-parser/src/tests.rs
+++ b/crates/jrsonnet-rowan-parser/src/tests.rs
@@ -2,7 +2,6 @@
#![cfg(test)]
use hi_doc::{Formatting, SnippetBuilder, Text};
-use thiserror::Error;
use crate::{parse, AstNode};
@@ -14,7 +13,7 @@
if !errors.is_empty() && !text.is_empty() {
writeln!(out, "===").unwrap();
for err in &errors {
- writeln!(out, "{:?}", err).unwrap();
+ writeln!(out, "{err:?}").unwrap();
}
let mut code = text.to_string();
crates/jrsonnet-stdlib/src/regex.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/regex.rs
+++ b/crates/jrsonnet-stdlib/src/regex.rs
@@ -4,7 +4,7 @@
use jrsonnet_evaluator::{
error::{ErrorKind::*, Result},
rustc_hash::FxBuildHasher,
- typed::Typed,
+ typed::{IntoUntyped, Typed},
val::StrValue,
IStr, ObjValue, ObjValueBuilder,
};
@@ -41,7 +41,7 @@
}
}
-#[derive(Typed)]
+#[derive(Typed, IntoUntyped)]
pub struct RegexMatch {
string: IStr,
captures: Vec<IStr>,
tests/tests/common.rsdiffbeforeafterboth--- a/tests/tests/common.rs
+++ b/tests/tests/common.rs
@@ -41,6 +41,7 @@
}
#[builtin]
+#[allow(dead_code)]
fn assert_throw(lazy: Thunk<Val>, message: String) -> Result<bool> {
match lazy.evaluate() {
Ok(_) => {
@@ -55,6 +56,7 @@
}
#[builtin]
+#[allow(dead_code)]
fn param_names(fun: FuncVal) -> Vec<String> {
fun.params()
.iter()
tests/tests/typed_obj.rsdiffbeforeafterboth--- a/tests/tests/typed_obj.rs
+++ b/tests/tests/typed_obj.rs
@@ -9,7 +9,7 @@
};
use jrsonnet_stdlib::ContextInitializer;
-#[derive(Clone, Typed, PartialEq, Debug)]
+#[derive(Clone, Typed, FromUntyped, IntoUntyped, PartialEq, Debug)]
struct A {
a: u32,
b: u16,
@@ -39,7 +39,7 @@
Ok(())
}
-#[derive(Clone, Typed, PartialEq, Debug)]
+#[derive(Clone, Typed, FromUntyped, IntoUntyped, PartialEq, Debug)]
struct B {
a: u32,
#[typed(rename = "c")]
@@ -62,7 +62,7 @@
Ok(())
}
-#[derive(Clone, Typed, PartialEq, Debug)]
+#[derive(Clone, Typed, FromUntyped, IntoUntyped, PartialEq, Debug)]
struct ObjectKind {
#[typed(rename = "apiVersion")]
api_version: String,
@@ -70,7 +70,7 @@
kind: String,
}
-#[derive(Clone, Typed, PartialEq, Debug)]
+#[derive(Clone, Typed, FromUntyped, IntoUntyped, PartialEq, Debug)]
struct Object {
#[typed(flatten)]
kind: ObjectKind,
@@ -104,7 +104,7 @@
Ok(())
}
-#[derive(Clone, Typed, PartialEq, Debug)]
+#[derive(Clone, Typed, FromUntyped, IntoUntyped, PartialEq, Debug)]
struct C {
a: Option<u32>,
b: u16,
@@ -142,14 +142,14 @@
Ok(())
}
-#[derive(Clone, Typed, PartialEq, Debug)]
+#[derive(Clone, Typed, FromUntyped, IntoUntyped, PartialEq, Debug)]
struct D {
#[typed(flatten(ok))]
e: Option<E>,
b: u16,
}
-#[derive(Clone, Typed, PartialEq, Debug)]
+#[derive(Clone, Typed, FromUntyped, IntoUntyped, PartialEq, Debug)]
struct E {
v: u32,
}
xtask/src/sourcegen/mod.rsdiffbeforeafterboth1use std::{collections::HashMap, path::PathBuf};23use anyhow::Result;4use ast::{lower, AstSrc};5use itertools::Itertools;6use kinds::{KindsSrc, TokenKind};7use proc_macro2::{Ident, Punct, Spacing, Span, TokenStream};8use quote::{format_ident, quote};9use ungrammar::Grammar;10use util::{ensure_file_contents, reformat, to_pascal_case, to_upper_snake_case};1112mod ast;13mod kinds;14mod util;1516enum SpecialName {17 Literal,18 Meta,19 Error,20}21fn classify_special(name: &str) -> Option<(SpecialName, &str)> {22 let name = name.strip_suffix('!')?;23 Some(if let Some(name) = name.strip_prefix("LIT_") {24 (SpecialName::Literal, name)25 } else if let Some(name) = name.strip_prefix("META_") {26 (SpecialName::Meta, name)27 } else if let Some(name) = name.strip_prefix("ERROR_") {28 (SpecialName::Error, name)29 } else {30 return None;31 })32}3334pub fn generate_ungrammar() -> Result<()> {35 let grammar: Grammar = include_str!(concat!(36 env!("CARGO_MANIFEST_DIR"),37 "/../crates/jrsonnet-rowan-parser/jsonnet.ungram"38 ))39 .parse()?;4041 let mut kinds = kinds::jsonnet_kinds();42 let ast = lower(&kinds, &grammar);4344 for token in grammar.tokens() {45 let token = &grammar[token];46 let token = &token.name.clone();47 if !kinds.is_token(token) {48 if let Some((special, name)) = classify_special(token) {49 match special {50 SpecialName::Literal => panic!("literal is not defined: {name}"),51 SpecialName::Meta => {52 eprintln!("implicit meta: {name}");53 kinds.define_token(TokenKind::Meta {54 grammar_name: token.to_owned(),55 name: format!("META_{name}"),56 });57 }58 SpecialName::Error => {59 eprintln!("implicit error: {name}");60 kinds.define_token(TokenKind::Error {61 grammar_name: token.to_owned(),62 name: format!("ERROR_{name}"),63 regex: None,64 priority: None,65 is_lexer_error: true,66 });67 }68 };69 continue;70 };71 let name = to_upper_snake_case(token);72 eprintln!("implicit kw: {token}");73 kinds.define_token(TokenKind::Keyword {74 code: token.to_owned(),75 name: format!("{name}_KW"),76 });77 }78 }79 for node in &ast.nodes {80 let name = to_upper_snake_case(&node.name);81 kinds.define_node(&name);82 }83 for enum_ in &ast.enums {84 let name = to_upper_snake_case(&enum_.name);85 kinds.define_node(&name);86 }87 for token_enum in &ast.token_enums {88 let name = to_upper_snake_case(&token_enum.name);89 kinds.define_node(&name);90 }9192 let syntax_kinds = generate_syntax_kinds(&kinds, &ast)?;9394 let nodes = generate_nodes(&kinds, &ast)?;95 ensure_file_contents(96 &PathBuf::from(concat!(97 env!("CARGO_MANIFEST_DIR"),98 "/../crates/jrsonnet-rowan-parser/src/generated/syntax_kinds.rs",99 )),100 &syntax_kinds,101 );102 ensure_file_contents(103 &PathBuf::from(concat!(104 env!("CARGO_MANIFEST_DIR"),105 "/../crates/jrsonnet-rowan-parser/src/generated/nodes.rs",106 )),107 &nodes,108 );109 Ok(())110}111112fn generate_syntax_kinds(kinds: &KindsSrc, grammar: &AstSrc) -> Result<String> {113 let t_macros = kinds.tokens().filter_map(TokenKind::expand_t_macros);114 let token_kinds = kinds.tokens().map(TokenKind::expand_kind);115116 let keywords = kinds117 .tokens()118 .filter(|k| matches!(k, TokenKind::Keyword { .. }))119 .map(TokenKind::name)120 .map(|n| format_ident!("{n}"));121122 let nodes = kinds123 .nodes124 .iter()125 .map(|name| format_ident!("{}", name))126 .collect::<Vec<_>>();127128 let enums = grammar129 .enums130 .iter()131 .map(|e| format_ident!("{}", to_upper_snake_case(&e.name)))132 .chain(133 grammar134 .token_enums135 .iter()136 .map(|e| format_ident!("{}", to_upper_snake_case(&e.name))),137 );138139 let ast = quote! {140 #![allow(bad_style, missing_docs, unreachable_pub, clippy::manual_non_exhaustive, clippy::match_like_matches_macro)]141 use logos::Logos;142143 /// The kind of syntax node, e.g. `IDENT`, `USE_KW`, or `STRUCT`.144 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Logos)]145 #[repr(u16)]146 pub enum SyntaxKind {147 #[doc(hidden)]148 TOMBSTONE,149 #[doc(hidden)]150 EOF,151 #(#token_kinds,)*152 LEXING_ERROR,153 __LAST_TOKEN,154 #(#nodes,)*155 #[doc(hidden)]156 __LAST,157 }158 use self::SyntaxKind::*;159160 impl SyntaxKind {161 pub fn is_keyword(self) -> bool {162 match self {163 #(#keywords)|* => true,164 _ => false,165 }166 }167 pub fn is_enum(self) -> bool {168 match self {169 #(#enums)|* => true,170 _ => false,171 }172 }173174 pub fn from_raw(r: u16) -> Self {175 assert!(r < Self::__LAST as u16);176 unsafe { std::mem::transmute(r) }177 }178 pub fn into_raw(self) -> u16 {179 self as u16180 }181 }182183 #[macro_export]184 macro_rules! T {#(#t_macros);*}185 #[allow(unused_imports)]186 pub use T;187 };188189 reformat(&ast.to_string())190}191192#[allow(clippy::too_many_lines, clippy::cognitive_complexity)]193fn generate_nodes(kinds: &KindsSrc, grammar: &AstSrc) -> Result<String> {194 let (node_defs, node_boilerplate_impls): (Vec<_>, Vec<_>) = grammar195 .nodes196 .iter()197 .map(|node| {198 let name = format_ident!("{}", node.name);199 let kind = format_ident!("{}", to_upper_snake_case(&node.name));200 let traits = node.traits.iter().map(|trait_name| {201 let trait_name = format_ident!("{}", trait_name);202 quote!(impl ast::#trait_name for #name {})203 });204205 let mut type_positions: HashMap<String, usize> = HashMap::new();206 let field_positions: Vec<_> = node207 .fields208 .iter()209 .map(|field| {210 let ty_str = field.ty().to_string();211 let pos = *type_positions.get(&ty_str).unwrap_or(&0);212 type_positions.insert(ty_str, pos + 1);213 pos214 })215 .collect();216217 let methods = node218 .fields219 .iter()220 .zip(field_positions.iter())221 .map(|(field, &pos)| {222 let method_name = field.method_name(kinds);223 let ty = field.ty();224225 if field.is_many() {226 quote! {227 pub fn #method_name(&self) -> AstChildren<#ty> {228 support::children(&self.syntax)229 }230 }231 } else if let Some(token_kind) = field.token_kind(kinds) {232 quote! {233 pub fn #method_name(&self) -> Option<#ty> {234 support::token(&self.syntax, #token_kind)235 }236 }237 } else if field.is_token_enum(grammar) {238 quote! {239 pub fn #method_name(&self) -> Option<#ty> {240 support::token_child(&self.syntax)241 }242 }243 } else if pos == 0 {244 quote! {245 pub fn #method_name(&self) -> Option<#ty> {246 support::children(&self.syntax).next()247 }248 }249 } else {250 quote! {251 pub fn #method_name(&self) -> Option<#ty> {252 support::children(&self.syntax).nth(#pos)253 }254 }255 }256 });257 (258 quote! {259 #[pretty_doc_comment_placeholder_workaround]260 #[derive(Debug, Clone, PartialEq, Eq, Hash)]261 pub struct #name {262 pub(crate) syntax: SyntaxNode,263 }264265 #(#traits)*266267 impl #name {268 #(#methods)*269 }270 },271 quote! {272 impl AstNode for #name {273 fn can_cast(kind: SyntaxKind) -> bool {274 kind == #kind275 }276 fn cast(syntax: SyntaxNode) -> Option<Self> {277 if Self::can_cast(syntax.kind()) { Some(Self { syntax }) } else { None }278 }279 fn syntax(&self) -> &SyntaxNode { &self.syntax }280 }281 },282 )283 })284 .unzip();285286 let (enum_defs, enum_boilerplate_impls): (Vec<_>, Vec<_>) = grammar287 .enums288 .iter()289 .map(|en| {290 let variants: Vec<_> = en291 .variants292 .iter()293 .map(|var| format_ident!("{}", var))294 .collect();295 let name = format_ident!("{}", en.name);296 let kinds: Vec<_> = variants297 .iter()298 .map(|name| format_ident!("{}", to_upper_snake_case(&name.to_string())))299 .collect();300 let traits = en.traits.iter().map(|trait_name| {301 let trait_name = format_ident!("{}", trait_name);302 quote!(impl ast::#trait_name for #name {})303 });304305 let ast_node = quote! {306 impl AstNode for #name {307 fn can_cast(kind: SyntaxKind) -> bool {308 match kind {309 #(#kinds)|* => true,310 _ => false,311 }312 }313 fn cast(syntax: SyntaxNode) -> Option<Self> {314 let res = match syntax.kind() {315 #(316 #kinds => #name::#variants(#variants { syntax }),317 )*318 _ => return None,319 };320 Some(res)321 }322 fn syntax(&self) -> &SyntaxNode {323 match self {324 #(325 #name::#variants(it) => &it.syntax,326 )*327 }328 }329 }330 };331332 (333 quote! {334 #[pretty_doc_comment_placeholder_workaround]335 #[derive(Debug, Clone, PartialEq, Eq, Hash)]336 pub enum #name {337 #(#variants(#variants),)*338 }339340 #(#traits)*341 },342 quote! {343 #(344 impl From<#variants> for #name {345 fn from(node: #variants) -> #name {346 #name::#variants(node)347 }348 }349 )*350 #ast_node351 },352 )353 })354 .unzip();355356 let (token_enum_defs, token_enum_boilerplate_impls): (Vec<_>, Vec<_>) = grammar357 .token_enums358 .iter()359 .map(|en| {360 let variants: Vec<_> = en361 .variants362 .iter()363 .map(|token| {364 format_ident!(365 "{}",366 to_pascal_case(kinds.token(token).expect("token exists").name())367 )368 })369 .collect();370 let name = format_ident!("{}", en.name);371 let kind_name = format_ident!("{}Kind", en.name);372 let kinds: Vec<_> = variants373 .iter()374 .map(|name| format_ident!("{}", to_upper_snake_case(&name.to_string())))375 .collect();376377 let ast_node = quote! {378 impl AstToken for #name {379 fn can_cast(kind: SyntaxKind) -> bool {380 #kind_name::can_cast(kind)381 }382 fn cast(syntax: SyntaxToken) -> Option<Self> {383 let kind = #kind_name::cast(syntax.kind())?;384 Some(#name { syntax, kind })385 }386 fn syntax(&self) -> &SyntaxToken {387 &self.syntax388 }389 }390391 impl #kind_name {392 fn can_cast(kind: SyntaxKind) -> bool {393 match kind {394 #(#kinds)|* => true,395 _ => false,396 }397 }398 pub fn cast(kind: SyntaxKind) -> Option<Self> {399 let res = match kind {400 #(#kinds => Self::#variants,)*401 _ => return None,402 };403 Some(res)404 }405 }406 };407408 (409 quote! {410 #[pretty_doc_comment_placeholder_workaround]411 #[derive(Debug, Clone, PartialEq, Eq, Hash)]412 pub struct #name { syntax: SyntaxToken, kind: #kind_name }413414 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]415 pub enum #kind_name {416 #(#variants,)*417 }418 },419 quote! {420 #ast_node421422 impl #name {423 pub fn kind(&self) -> #kind_name {424 self.kind425 }426 }427428 impl std::fmt::Display for #name {429 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {430 std::fmt::Display::fmt(self.syntax(), f)431 }432 }433 },434 )435 })436 .unzip();437438 let (any_node_defs, any_node_boilerplate_impls): (Vec<_>, Vec<_>) = grammar439 .nodes440 .iter()441 .flat_map(|node| node.traits.iter().map(move |t| (t, node)))442 .into_group_map()443 .into_iter()444 .sorted_by_key(|(k, _)| *k)445 .map(|(trait_name, nodes)| {446 let name = format_ident!("Any{}", trait_name);447 let trait_name = format_ident!("{}", trait_name);448 let kinds: Vec<_> = nodes449 .iter()450 .map(|name| format_ident!("{}", to_upper_snake_case(&name.name.to_string())))451 .collect();452453 (454 quote! {455 #[pretty_doc_comment_placeholder_workaround]456 #[derive(Debug, Clone, PartialEq, Eq, Hash)]457 pub struct #name {458 pub(crate) syntax: SyntaxNode,459 }460 impl ast::#trait_name for #name {}461 },462 quote! {463 impl #name {464 #[inline]465 pub fn new<T: ast::#trait_name>(node: T) -> #name {466 #name {467 syntax: node.syntax().clone()468 }469 }470 }471 impl AstNode for #name {472 fn can_cast(kind: SyntaxKind) -> bool {473 match kind {474 #(#kinds)|* => true,475 _ => false,476 }477 }478 fn cast(syntax: SyntaxNode) -> Option<Self> {479 Self::can_cast(syntax.kind()).then(|| #name { syntax })480 }481 fn syntax(&self) -> &SyntaxNode {482 &self.syntax483 }484 }485 },486 )487 })488 .unzip();489490 let enum_names = grammar.enums.iter().map(|it| &it.name);491 let node_names = grammar.nodes.iter().map(|it| &it.name);492493 let display_impls = enum_names494 .chain(node_names.clone())495 .map(|it| format_ident!("{}", it))496 .map(|name| {497 quote! {498 impl std::fmt::Display for #name {499 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {500 std::fmt::Display::fmt(self.syntax(), f)501 }502 }503 }504 });505506 let ast = quote! {507 #![allow(non_snake_case, clippy::match_like_matches_macro)]508509 use crate::{510 SyntaxNode, SyntaxToken, SyntaxKind::{self, *},511 ast::{AstNode, AstToken, AstChildren, support},512 T,513 };514515 #(#node_defs)*516 #(#enum_defs)*517 #(#token_enum_defs)*518 #(#any_node_defs)*519 #(#node_boilerplate_impls)*520 #(#enum_boilerplate_impls)*521 #(#token_enum_boilerplate_impls)*522 #(#any_node_boilerplate_impls)*523 #(#display_impls)*524 };525526 let ast = ast.to_string().replace("T ! [", "T![");527528 let mut res = String::with_capacity(ast.len() * 2);529530 let mut docs = grammar531 .nodes532 .iter()533 .map(|it| &it.doc)534 .chain(grammar.enums.iter().map(|it| &it.doc));535536 for chunk in ast.split("# [pretty_doc_comment_placeholder_workaround] ") {537 res.push_str(chunk);538 if let Some(doc) = docs.next() {539 write_doc_comment(doc, &mut res);540 }541 }542543 let res = reformat(&res)?;544 Ok(res.replace("#[derive", "\n#[derive"))545}546547fn write_doc_comment(contents: &[String], dest: &mut String) {548 use std::fmt::Write;549 for line in contents {550 writeln!(dest, "///{line}").unwrap();551 }552}553554pub fn escape_token_macro(token: &str) -> TokenStream {555 if "{}[]()$".contains(token) {556 let c = token.chars().next().unwrap();557 quote! { #c }558 } else if token.contains(|v| v == '$') {559 quote! { #token }560 } else if token.chars().all(|v| ('a'..='z').contains(&v)) {561 let i = Ident::new(&token, Span::call_site());562 quote! { #i }563 } else {564 let cs = token.chars().map(|c| Punct::new(c, Spacing::Joint));565 quote! { #(#cs)* }566 }567}1use std::{collections::HashMap, path::PathBuf};23use anyhow::Result;4use ast::{lower, AstSrc};5use itertools::Itertools;6use kinds::{KindsSrc, TokenKind};7use proc_macro2::{Ident, Punct, Spacing, Span, TokenStream};8use quote::{format_ident, quote};9use ungrammar::Grammar;10use util::{ensure_file_contents, reformat, to_pascal_case, to_upper_snake_case};1112mod ast;13mod kinds;14mod util;1516enum SpecialName {17 Literal,18 Meta,19 Error,20}21fn classify_special(name: &str) -> Option<(SpecialName, &str)> {22 let name = name.strip_suffix('!')?;23 Some(if let Some(name) = name.strip_prefix("LIT_") {24 (SpecialName::Literal, name)25 } else if let Some(name) = name.strip_prefix("META_") {26 (SpecialName::Meta, name)27 } else if let Some(name) = name.strip_prefix("ERROR_") {28 (SpecialName::Error, name)29 } else {30 return None;31 })32}3334pub fn generate_ungrammar() -> Result<()> {35 let grammar: Grammar = include_str!(concat!(36 env!("CARGO_MANIFEST_DIR"),37 "/../crates/jrsonnet-rowan-parser/jsonnet.ungram"38 ))39 .parse()?;4041 let mut kinds = kinds::jsonnet_kinds();42 let ast = lower(&kinds, &grammar);4344 for token in grammar.tokens() {45 let token = &grammar[token];46 let token = &token.name.clone();47 if !kinds.is_token(token) {48 if let Some((special, name)) = classify_special(token) {49 match special {50 SpecialName::Literal => panic!("literal is not defined: {name}"),51 SpecialName::Meta => {52 eprintln!("implicit meta: {name}");53 kinds.define_token(TokenKind::Meta {54 grammar_name: token.to_owned(),55 name: format!("META_{name}"),56 });57 }58 SpecialName::Error => {59 eprintln!("implicit error: {name}");60 kinds.define_token(TokenKind::Error {61 grammar_name: token.to_owned(),62 name: format!("ERROR_{name}"),63 regex: None,64 priority: None,65 is_lexer_error: true,66 });67 }68 }69 continue;70 }71 let name = to_upper_snake_case(token);72 eprintln!("implicit kw: {token}");73 kinds.define_token(TokenKind::Keyword {74 code: token.to_owned(),75 name: format!("{name}_KW"),76 });77 }78 }79 for node in &ast.nodes {80 let name = to_upper_snake_case(&node.name);81 kinds.define_node(&name);82 }83 for enum_ in &ast.enums {84 let name = to_upper_snake_case(&enum_.name);85 kinds.define_node(&name);86 }87 for token_enum in &ast.token_enums {88 let name = to_upper_snake_case(&token_enum.name);89 kinds.define_node(&name);90 }9192 let syntax_kinds = generate_syntax_kinds(&kinds, &ast)?;9394 let nodes = generate_nodes(&kinds, &ast)?;95 ensure_file_contents(96 &PathBuf::from(concat!(97 env!("CARGO_MANIFEST_DIR"),98 "/../crates/jrsonnet-rowan-parser/src/generated/syntax_kinds.rs",99 )),100 &syntax_kinds,101 );102 ensure_file_contents(103 &PathBuf::from(concat!(104 env!("CARGO_MANIFEST_DIR"),105 "/../crates/jrsonnet-rowan-parser/src/generated/nodes.rs",106 )),107 &nodes,108 );109 Ok(())110}111112fn generate_syntax_kinds(kinds: &KindsSrc, grammar: &AstSrc) -> Result<String> {113 let t_macros = kinds.tokens().filter_map(TokenKind::expand_t_macros);114 let token_kinds = kinds.tokens().map(TokenKind::expand_kind);115116 let keywords = kinds117 .tokens()118 .filter(|k| matches!(k, TokenKind::Keyword { .. }))119 .map(TokenKind::name)120 .map(|n| format_ident!("{n}"));121122 let nodes = kinds123 .nodes124 .iter()125 .map(|name| format_ident!("{}", name))126 .collect::<Vec<_>>();127128 let enums = grammar129 .enums130 .iter()131 .map(|e| format_ident!("{}", to_upper_snake_case(&e.name)))132 .chain(133 grammar134 .token_enums135 .iter()136 .map(|e| format_ident!("{}", to_upper_snake_case(&e.name))),137 );138139 let ast = quote! {140 #![allow(bad_style, missing_docs, unreachable_pub, clippy::manual_non_exhaustive, clippy::match_like_matches_macro)]141 use logos::Logos;142143 /// The kind of syntax node, e.g. `IDENT`, `USE_KW`, or `STRUCT`.144 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Logos)]145 #[repr(u16)]146 pub enum SyntaxKind {147 #[doc(hidden)]148 TOMBSTONE,149 #[doc(hidden)]150 EOF,151 #(#token_kinds,)*152 LEXING_ERROR,153 __LAST_TOKEN,154 #(#nodes,)*155 #[doc(hidden)]156 __LAST,157 }158 use self::SyntaxKind::*;159160 impl SyntaxKind {161 pub fn is_keyword(self) -> bool {162 match self {163 #(#keywords)|* => true,164 _ => false,165 }166 }167 pub fn is_enum(self) -> bool {168 match self {169 #(#enums)|* => true,170 _ => false,171 }172 }173174 pub fn from_raw(r: u16) -> Self {175 assert!(r < Self::__LAST as u16);176 unsafe { std::mem::transmute(r) }177 }178 pub fn into_raw(self) -> u16 {179 self as u16180 }181 }182183 #[macro_export]184 macro_rules! T {#(#t_macros);*}185 #[allow(unused_imports)]186 pub use T;187 };188189 reformat(&ast.to_string())190}191192#[allow(clippy::too_many_lines, clippy::cognitive_complexity)]193fn generate_nodes(kinds: &KindsSrc, grammar: &AstSrc) -> Result<String> {194 let (node_defs, node_boilerplate_impls): (Vec<_>, Vec<_>) = grammar195 .nodes196 .iter()197 .map(|node| {198 let name = format_ident!("{}", node.name);199 let kind = format_ident!("{}", to_upper_snake_case(&node.name));200 let traits = node.traits.iter().map(|trait_name| {201 let trait_name = format_ident!("{}", trait_name);202 quote!(impl ast::#trait_name for #name {})203 });204205 let mut type_positions: HashMap<String, usize> = HashMap::new();206 let field_positions: Vec<_> = node207 .fields208 .iter()209 .map(|field| {210 let ty_str = field.ty().to_string();211 let pos = *type_positions.get(&ty_str).unwrap_or(&0);212 type_positions.insert(ty_str, pos + 1);213 pos214 })215 .collect();216217 let methods = node218 .fields219 .iter()220 .zip(field_positions.iter())221 .map(|(field, &pos)| {222 let method_name = field.method_name(kinds);223 let ty = field.ty();224225 if field.is_many() {226 quote! {227 pub fn #method_name(&self) -> AstChildren<#ty> {228 support::children(&self.syntax)229 }230 }231 } else if let Some(token_kind) = field.token_kind(kinds) {232 quote! {233 pub fn #method_name(&self) -> Option<#ty> {234 support::token(&self.syntax, #token_kind)235 }236 }237 } else if field.is_token_enum(grammar) {238 quote! {239 pub fn #method_name(&self) -> Option<#ty> {240 support::token_child(&self.syntax)241 }242 }243 } else if pos == 0 {244 quote! {245 pub fn #method_name(&self) -> Option<#ty> {246 support::children(&self.syntax).next()247 }248 }249 } else {250 quote! {251 pub fn #method_name(&self) -> Option<#ty> {252 support::children(&self.syntax).nth(#pos)253 }254 }255 }256 });257 (258 quote! {259 #[pretty_doc_comment_placeholder_workaround]260 #[derive(Debug, Clone, PartialEq, Eq, Hash)]261 pub struct #name {262 pub(crate) syntax: SyntaxNode,263 }264265 #(#traits)*266267 impl #name {268 #(#methods)*269 }270 },271 quote! {272 impl AstNode for #name {273 fn can_cast(kind: SyntaxKind) -> bool {274 kind == #kind275 }276 fn cast(syntax: SyntaxNode) -> Option<Self> {277 if Self::can_cast(syntax.kind()) { Some(Self { syntax }) } else { None }278 }279 fn syntax(&self) -> &SyntaxNode { &self.syntax }280 }281 },282 )283 })284 .unzip();285286 let (enum_defs, enum_boilerplate_impls): (Vec<_>, Vec<_>) = grammar287 .enums288 .iter()289 .map(|en| {290 let variants: Vec<_> = en291 .variants292 .iter()293 .map(|var| format_ident!("{}", var))294 .collect();295 let name = format_ident!("{}", en.name);296 let kinds: Vec<_> = variants297 .iter()298 .map(|name| format_ident!("{}", to_upper_snake_case(&name.to_string())))299 .collect();300 let traits = en.traits.iter().map(|trait_name| {301 let trait_name = format_ident!("{}", trait_name);302 quote!(impl ast::#trait_name for #name {})303 });304305 let ast_node = quote! {306 impl AstNode for #name {307 fn can_cast(kind: SyntaxKind) -> bool {308 match kind {309 #(#kinds)|* => true,310 _ => false,311 }312 }313 fn cast(syntax: SyntaxNode) -> Option<Self> {314 let res = match syntax.kind() {315 #(316 #kinds => #name::#variants(#variants { syntax }),317 )*318 _ => return None,319 };320 Some(res)321 }322 fn syntax(&self) -> &SyntaxNode {323 match self {324 #(325 #name::#variants(it) => &it.syntax,326 )*327 }328 }329 }330 };331332 (333 quote! {334 #[pretty_doc_comment_placeholder_workaround]335 #[derive(Debug, Clone, PartialEq, Eq, Hash)]336 pub enum #name {337 #(#variants(#variants),)*338 }339340 #(#traits)*341 },342 quote! {343 #(344 impl From<#variants> for #name {345 fn from(node: #variants) -> #name {346 #name::#variants(node)347 }348 }349 )*350 #ast_node351 },352 )353 })354 .unzip();355356 let (token_enum_defs, token_enum_boilerplate_impls): (Vec<_>, Vec<_>) = grammar357 .token_enums358 .iter()359 .map(|en| {360 let variants: Vec<_> = en361 .variants362 .iter()363 .map(|token| {364 format_ident!(365 "{}",366 to_pascal_case(kinds.token(token).expect("token exists").name())367 )368 })369 .collect();370 let name = format_ident!("{}", en.name);371 let kind_name = format_ident!("{}Kind", en.name);372 let kinds: Vec<_> = variants373 .iter()374 .map(|name| format_ident!("{}", to_upper_snake_case(&name.to_string())))375 .collect();376377 let ast_node = quote! {378 impl AstToken for #name {379 fn can_cast(kind: SyntaxKind) -> bool {380 #kind_name::can_cast(kind)381 }382 fn cast(syntax: SyntaxToken) -> Option<Self> {383 let kind = #kind_name::cast(syntax.kind())?;384 Some(#name { syntax, kind })385 }386 fn syntax(&self) -> &SyntaxToken {387 &self.syntax388 }389 }390391 impl #kind_name {392 fn can_cast(kind: SyntaxKind) -> bool {393 match kind {394 #(#kinds)|* => true,395 _ => false,396 }397 }398 pub fn cast(kind: SyntaxKind) -> Option<Self> {399 let res = match kind {400 #(#kinds => Self::#variants,)*401 _ => return None,402 };403 Some(res)404 }405 }406 };407408 (409 quote! {410 #[pretty_doc_comment_placeholder_workaround]411 #[derive(Debug, Clone, PartialEq, Eq, Hash)]412 pub struct #name { syntax: SyntaxToken, kind: #kind_name }413414 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]415 pub enum #kind_name {416 #(#variants,)*417 }418 },419 quote! {420 #ast_node421422 impl #name {423 pub fn kind(&self) -> #kind_name {424 self.kind425 }426 }427428 impl std::fmt::Display for #name {429 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {430 std::fmt::Display::fmt(self.syntax(), f)431 }432 }433 },434 )435 })436 .unzip();437438 let (any_node_defs, any_node_boilerplate_impls): (Vec<_>, Vec<_>) = grammar439 .nodes440 .iter()441 .flat_map(|node| node.traits.iter().map(move |t| (t, node)))442 .into_group_map()443 .into_iter()444 .sorted_by_key(|(k, _)| *k)445 .map(|(trait_name, nodes)| {446 let name = format_ident!("Any{}", trait_name);447 let trait_name = format_ident!("{}", trait_name);448 let kinds: Vec<_> = nodes449 .iter()450 .map(|name| format_ident!("{}", to_upper_snake_case(&name.name)))451 .collect();452453 (454 quote! {455 #[pretty_doc_comment_placeholder_workaround]456 #[derive(Debug, Clone, PartialEq, Eq, Hash)]457 pub struct #name {458 pub(crate) syntax: SyntaxNode,459 }460 impl ast::#trait_name for #name {}461 },462 quote! {463 impl #name {464 #[inline]465 pub fn new<T: ast::#trait_name>(node: T) -> #name {466 #name {467 syntax: node.syntax().clone()468 }469 }470 }471 impl AstNode for #name {472 fn can_cast(kind: SyntaxKind) -> bool {473 match kind {474 #(#kinds)|* => true,475 _ => false,476 }477 }478 fn cast(syntax: SyntaxNode) -> Option<Self> {479 Self::can_cast(syntax.kind()).then(|| #name { syntax })480 }481 fn syntax(&self) -> &SyntaxNode {482 &self.syntax483 }484 }485 },486 )487 })488 .unzip();489490 let enum_names = grammar.enums.iter().map(|it| &it.name);491 let node_names = grammar.nodes.iter().map(|it| &it.name);492493 let display_impls = enum_names494 .chain(node_names.clone())495 .map(|it| format_ident!("{}", it))496 .map(|name| {497 quote! {498 impl std::fmt::Display for #name {499 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {500 std::fmt::Display::fmt(self.syntax(), f)501 }502 }503 }504 });505506 let ast = quote! {507 #![allow(non_snake_case, clippy::match_like_matches_macro)]508509 use crate::{510 SyntaxNode, SyntaxToken, SyntaxKind::{self, *},511 ast::{AstNode, AstToken, AstChildren, support},512 T,513 };514515 #(#node_defs)*516 #(#enum_defs)*517 #(#token_enum_defs)*518 #(#any_node_defs)*519 #(#node_boilerplate_impls)*520 #(#enum_boilerplate_impls)*521 #(#token_enum_boilerplate_impls)*522 #(#any_node_boilerplate_impls)*523 #(#display_impls)*524 };525526 let ast = ast.to_string().replace("T ! [", "T![");527528 let mut res = String::with_capacity(ast.len() * 2);529530 let mut docs = grammar531 .nodes532 .iter()533 .map(|it| &it.doc)534 .chain(grammar.enums.iter().map(|it| &it.doc));535536 for chunk in ast.split("# [pretty_doc_comment_placeholder_workaround] ") {537 res.push_str(chunk);538 if let Some(doc) = docs.next() {539 write_doc_comment(doc, &mut res);540 }541 }542543 let res = reformat(&res)?;544 Ok(res.replace("#[derive", "\n#[derive"))545}546547fn write_doc_comment(contents: &[String], dest: &mut String) {548 use std::fmt::Write;549 for line in contents {550 writeln!(dest, "///{line}").unwrap();551 }552}553554pub fn escape_token_macro(token: &str) -> TokenStream {555 if "{}[]()$".contains(token) {556 let c = token.chars().next().unwrap();557 quote! { #c }558 } else if token.contains('$') {559 quote! { #token }560 } else if token.chars().all(|v: char| v.is_ascii_lowercase()) {561 let i = Ident::new(token, Span::call_site());562 quote! { #i }563 } else {564 let cs = token.chars().map(|c| Punct::new(c, Spacing::Joint));565 quote! { #(#cs)* }566 }567}