1use super::*;
2use crate::fmt::FmtAdapter;
3use std::{fmt::Display, format_args as fmt};
4
5impl<T: Display + AstNode, A: AstTypes> Display for At<T, A> {
6 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
7 write!(f, "{}", self.0)
8 }
9}
10
11impl<T: AstNode, A: AstTypes> std::fmt::Debug for At<T, A> {
12 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
13 if f.alternate() {
14 f.write_str("/* ")?;
15 <A::Annotation as std::fmt::Display>::fmt(&self.1, f)?;
16 f.write_str(" */\n")?;
17 }
18 <T as std::fmt::Debug>::fmt(&self.0, f)
19 }
20}
21
22impl<A: AstTypes> std::fmt::Debug for Expr<A> {
23 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24 use std::fmt::Debug;
25 match self {
26 Self::Omitted => write!(f, "Omitted"),
27 Self::Id(arg0) => write!(f, "Id({arg0:?})"),
28 Self::MetId(arg0) => write!(f, "MetId({arg0:?})"),
29 Self::Lit(arg0) => write!(f, "Lit({arg0:?})"),
30 Self::Use(arg0) => f.debug_tuple("Use").field(arg0).finish(),
31 Self::Bind(arg0) => Debug::fmt(arg0, f),
32 Self::Make(arg0) => Debug::fmt(arg0, f),
33 Self::Match(arg0) => Debug::fmt(arg0, f),
34 Self::Op(arg0, arg1) => {
35 let mut tup = f.debug_tuple(&format!("{arg0:?}"));
36 for arg in arg1 {
37 tup.field(arg);
38 }
39 tup.finish()
40 }
41 }
42 }
43}
44
45impl<A: AstTypes> std::fmt::Debug for Pat<A> {
46 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47 match self {
48 Self::Ignore => write!(f, "Ignore"),
49 Self::Never => write!(f, "Never"),
50 Self::MetId(arg0) => write!(f, "MetId({arg0:?})"),
51 Self::Name(arg0) => write!(f, "Name({arg0:?})"),
52 Self::Value(arg0) => f.debug_tuple("Value").field(arg0).finish(),
53 Self::Op(arg0, arg1) => {
54 let mut tup = f.debug_tuple(&format!("{arg0:?}"));
55 for arg in arg1 {
56 tup.field(arg);
57 }
58 tup.finish()
59 }
60 }
61 }
62}
63
64impl<A: AstTypes> std::fmt::Debug for Bind<A> {
65 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66 let Self(op, generics, pat, exprs) = self;
67 f.debug_tuple(&format!("Bind::{op:?}"))
68 .field(generics)
69 .field(pat)
70 .field(exprs)
71 .finish()
72 }
73}
74
75impl<A: AstTypes> Display for Expr<A> {
76 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77 match self {
78 Self::Omitted => "...".fmt(f),
79 Self::Id(id) => id.fmt(f),
80 Self::MetId(id) => write!(f, "`{id}"),
81 Self::Lit(literal) => literal.fmt(f),
82 Self::Use(v) => write!(f, "use {v}"),
83 Self::Bind(v) => v.fmt(f),
84 Self::Make(v) => v.fmt(f),
85 Self::Match(v) => v.fmt(f),
86
87 Self::Op(op @ Op::Continue, exprs) => f.delimit(op, "").list(exprs, "!?,"),
88 Self::Op(op @ (Op::If | Op::While), exprs) => match exprs.as_slice() {
89 [cond, pass, At(Expr::Omitted, _)] => {
90 write!(f, "{op}{cond} {pass}")
91 }
92 [cond, pass, fail] => write!(f, "{op}{cond} {pass} else {fail}"),
93 other => f.delimit(fmt!("({op}, "), ")").list(other, ", "),
94 },
95 Self::Op(Op::Array, exprs) => f.delimit("[", "]").list(exprs, ", "),
96 Self::Op(Op::ArRep, exprs) => f.delimit("[", "]").list(exprs, "; "),
97 Self::Op(Op::Block, exprs) => f
98 .delimit_indented("{", "}")
99 .list_wrap("\n", exprs, "\n", "\n"),
100 Self::Op(Op::Group, exprs) if let [At(Expr::Op(Op::Do, _), _)] = &exprs[..] => {
101 f.delimit_indented("(", ")").list(exprs, ";\n")
102 }
103 Self::Op(Op::Group, exprs) => f.delimit("(", ")").list(exprs, ", "),
104 Self::Op(op @ (Op::MetaInner | Op::MetaOuter), exprs) => match &exprs[..] {
105 [meta, expr @ ..] => f.delimit(fmt!("{op}[{meta}]\n"), "").list(expr, ","),
106 [] => write!(f, "{op}[]"),
107 },
108
109 Self::Op(op @ Op::Call, exprs) => match exprs.as_slice() {
110 [callee, At(Expr::Op(Op::Tuple, args), _)] => {
111 f.delimit(fmt!("{callee}("), ")").list(args, ", ")
112 }
113 [callee, args @ ..] => f.delimit(fmt!("{callee}(?"), "?)").list(args, ", "),
114 [] => write!(f, "{op}"),
115 },
116 Self::Op(op @ Op::Index, exprs) => match exprs.as_slice() {
117 [callee, args @ ..] => f.delimit(fmt!("{callee}["), "]").list(args, ", "),
118 [] => write!(f, "{op}"),
119 },
120
121 Self::Op(Op::Tuple, exprs) if exprs.is_empty() => "()".fmt(f),
122 Self::Op(op @ (Op::Do | Op::Tuple | Op::Dot), exprs) => f.list(exprs, op),
123 Self::Op(op @ Op::Macro, exprs) => f.delimit(op, "").list(exprs, " => "),
124 Self::Op(op @ Op::Try, exprs) => f.delimit("(", fmt!("){op}")).list(exprs, ", "),
125 Self::Op(op, exprs) => match exprs.as_slice() {
126 [one] => write!(f, "{op}{one}"),
127 many => f.list(many, op),
128 },
129 }
130 }
131}
132
133impl Display for Op {
134 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
135 f.write_str(match self {
136 Op::Do => ";\n",
137 Op::As => " as ",
138 Op::Macro => "macro ",
139 Op::Block => "{}",
140 Op::Array => "[]",
141 Op::ArRep => "; ",
142 Op::Group => "()",
143 Op::Tuple => ", ",
144 Op::MetaInner => "#!",
145 Op::MetaOuter => "#",
146 Op::Try => "?",
147 Op::Index => "",
148 Op::Call => "",
149 Op::Pub => "pub ",
150 Op::Const => "const ",
151 Op::Static => "static ",
152 Op::Loop => "loop ",
153 Op::If => "if ",
154 Op::While => "while ",
155 Op::Defer => "defer ",
156 Op::Break => "break ",
157 Op::Return => "return ",
158 Op::Continue => "continue",
159 Op::Dot => ".",
160 Op::RangeEx => "..",
161 Op::RangeIn => "..=",
162 Op::Neg => "-",
163 Op::Not => "!",
164 Op::Identity => "!!",
165 Op::Refer => "&",
166 Op::Deref => "*",
167 Op::Mul => " * ",
168 Op::Div => " / ",
169 Op::Rem => " % ",
170 Op::Add => " + ",
171 Op::Sub => " - ",
172 Op::Shl => " << ",
173 Op::Shr => " >> ",
174 Op::And => " & ",
175 Op::Xor => " ^ ",
176 Op::Or => " | ",
177 Op::Lt => " < ",
178 Op::Leq => " <= ",
179 Op::Eq => " == ",
180 Op::Neq => " != ",
181 Op::Geq => " >= ",
182 Op::Gt => " > ",
183 Op::LogAnd => " && ",
184 Op::LogXor => " ^^ ",
185 Op::LogOr => " || ",
186 Op::Set => " = ",
187 Op::MulSet => " *= ",
188 Op::DivSet => " /= ",
189 Op::RemSet => " %= ",
190 Op::AddSet => " += ",
191 Op::SubSet => " -= ",
192 Op::ShlSet => " <<= ",
193 Op::ShrSet => " >>= ",
194 Op::AndSet => " &= ",
195 Op::XorSet => " ^= ",
196 Op::OrSet => " |= ",
197 })
198 }
199}
200
201impl<A: AstTypes> Display for Use<A> {
202 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
203 match self {
204 Self::Glob => "*".fmt(f),
205 Self::Name(name) => name.fmt(f),
206 Self::Alias(name, alias) => write!(f, "{name} as {alias}"),
207 Self::Path(segment, rest) => write!(f, "{segment}::{rest}"),
208 Self::Tree(items) => match items.len() {
209 0 => "{}".fmt(f),
210 1..=3 => f.delimit("{ ", " }").list(items, ", "),
211 _ => f
212 .delimit_indented("{", "}")
213 .list_wrap("\n", items, ",\n", ",\n"),
214 },
215 }
216 }
217}
218
219impl<A: AstTypes> Display for Bind<A> {
220 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
221 let Self(op, gens, pat, exprs) = self;
222 op.fmt(f)?;
223 if !gens.is_empty() {
224 f.delimit("<", "> ").list(gens, ", ")?;
225 }
226
227 match (op, exprs.as_slice()) {
228 (_, [At(Expr::Omitted, _)]) => write!(f, "{pat}"),
229 (BindOp::Fn | BindOp::Mod | BindOp::Impl, [At(Expr::Op(Op::Block, _), _)]) => {
230 f.delimit(fmt!("{pat} "), "").list(exprs, ",!? ")
231 }
232 (BindOp::Fn, _) => f.delimit(fmt!("{pat} = "), "").list(exprs, ""),
233 (BindOp::Mod | BindOp::Impl, _) => f.delimit(fmt!("{pat} "), "").list(exprs, "!?;"),
234 (BindOp::Struct | BindOp::Enum, _) => match pat.value() {
235 Pat::Op(PatOp::TypePrefixed, bind) => match bind.as_slice() {
237 [name, At(Pat::Op(PatOp::Record, parts), ..)] => f
238 .delimit_indented(fmt!("{name} {{"), "}")
239 .list_wrap("\n", parts, ",\n", ",\n"),
240 [name, At(Pat::Op(PatOp::Tuple, parts), ..)] => {
241 f.delimit(fmt!("{name}("), ")").list(parts, ", ")
242 }
243 _ => pat.fmt(f),
244 },
245 _ => pat.fmt(f),
246 },
247 (BindOp::For, [iter, pass, At(Expr::Omitted, _)]) => {
248 write!(f, "{pat} in {iter} {pass}")
249 }
250 (BindOp::For, [iter, pass, fail]) => write!(f, "{pat} in {iter} {pass} else {fail}"),
251 (BindOp::For, other) => f.delimit(fmt!("{pat} in [["), "]]!?").list(other, ", "),
252 (_, []) => write!(f, "{pat}"),
253 (_, [value]) => write!(f, "{pat} = {value}"),
254 (_, [value, fail]) => write!(f, "{pat} = {value} else {fail}"),
255
256 (_, other) => f.delimit(fmt!("{pat} ("), ")").list(other, ", "),
257 }
258 }
259}
260
261impl Display for BindOp {
262 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
263 f.write_str(match self {
264 Self::Let => "let ",
265 Self::Type => "type ",
266 Self::Struct => "struct ",
267 Self::Enum => "enum ",
268 Self::Fn => "fn ",
269 Self::Mod => "mod ",
270 Self::Impl => "impl ",
271 Self::For => "for ",
272 })
273 }
274}
275
276impl<A: AstTypes> Display for Make<A> {
277 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
278 let Self(expr, make_arms) = self;
279 f.delimit(fmt!("({expr} {{"), "})").list(make_arms, ", ")
280 }
281}
282
283impl<A: AstTypes> Display for MakeArm<A> {
284 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
285 match self {
286 Self(name, Some(body)) => write!(f, "{name}: {body}"),
287 Self(name, None) => write!(f, "{name}"),
288 }
289 }
290}
291
292impl<A: AstTypes> Display for Match<A> {
293 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
294 let Self(scrutinee, arms) = self;
295 f.delimit_indented(fmt!("match {scrutinee} {{"), "}")
296 .list_wrap("\n", arms, ";\n", ";\n")
297 }
298}
299
300impl<A: AstTypes> Display for MatchArm<A> {
301 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
302 let Self(pat, expr) = self;
303 write!(f, "{pat} => {expr}")
304 }
305}
306
307impl<A: AstTypes> Display for Pat<A> {
308 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
309 match self {
310 Self::Ignore => "_".fmt(f),
311 Self::Never => "!".fmt(f),
312 Self::Value(literal) => literal.fmt(f),
313 Self::MetId(name) => write!(f, "`{name}"),
314 Self::Name(name) => name.fmt(f),
315 Self::Op(PatOp::Record, pats) => f
316 .delimit_indented("{", "}")
317 .list_wrap("\n", pats, ",\n", ",\n"),
318 Self::Op(PatOp::Tuple, pats) => f.delimit("(", ")").list(pats, ", "),
319 Self::Op(PatOp::Slice, pats) => f.delimit("[", "]").list(pats, ", "),
320 Self::Op(op @ PatOp::ArRep, pats) => f.delimit("[", "]").list(pats, op),
321 Self::Op(op @ (PatOp::Typed | PatOp::Fn), pats) => match &pats[..] {
322 [fun] => write!(f, "fn {fun}"), pats => f.list(pats, op),
324 },
325 Self::Op(op @ PatOp::Alt, pats) => f.list(pats, op),
326 Self::Op(op @ PatOp::Generic, pats) => match &pats[..] {
327 [] => op.fmt(f),
328 [first, rest @ ..] => f.delimit(fmt!("{first}<"), ">").list(rest, ", "),
329 },
330 Self::Op(op @ PatOp::TypePrefixed, pats) => match &pats[..] {
331 [] => op.fmt(f),
332 [first, rest @ ..] => f.delimit(fmt!("{first}"), "").list(rest, ",? "),
333 },
334
335 Self::Op(op @ (PatOp::MetaInner | PatOp::MetaOuter), pats) => match &pats[..] {
336 [meta, pat @ ..] => f.delimit(fmt!("{op}[{meta}]\n"), "").list(pat, ","),
337 [] => write!(f, "{op}[]"),
338 },
339 Self::Op(op, pats) => match &pats[..] {
340 [] => op.fmt(f),
341 [rest] => write!(f, "{op}{rest}"),
342 _ => f.delimit("(", ")").list(pats, op),
343 },
344 }
345 }
346}
347
348impl Display for PatOp {
349 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
350 f.write_str(match self {
351 Self::MetaInner => "#!",
352 Self::MetaOuter => "#",
353 Self::Pub => "pub ",
354 Self::Mut => "mut ",
355 Self::Ref => "&",
356 Self::Ptr => "*",
357 Self::Rest => "..",
358 Self::RangeEx => "..",
359 Self::RangeIn => "..=",
360 Self::Record => ", ",
361 Self::Tuple => ", ",
362 Self::Slice => ", ",
363 Self::ArRep => "; ",
364 Self::Typed => ": ",
365 Self::Generic => "T<>",
366 Self::TypePrefixed => "T()",
367 Self::Fn => " -> ",
368 Self::Guard => " if ",
369 Self::Alt => " | ",
370 })
371 }
372}