···2201220122022202 let clause_guards = builder.end_clause_pattern(clause_pattern);
22032203 if let Some(guard) = clause.guard.as_ref() {
22042204+ let guard_ender = builder.start_clause_guard();
22042205 self.clause_guard(builder, guard, &variables_to_add_later);
22062206+ builder.end_clause_guard(guard_ender);
22052207 }
2206220822072209 // Finally we can generate the clause body. If the clause is
···111111 /// Represents a set of clause guards that has yet to be closed.
112112 type ClauseGuards;
113113114114+ /// Represents a single clause guard that has yet to be closed.
115115+ type Guard;
116116+114117 /// Represents an open clause body that has yet to be closed.
115118 type ClauseBody;
116119···936939 /// that function without generating anything inbetween.
937940 fn end_clause_pattern(&mut self, clause_pattern: Self::ClausePattern) -> Self::ClauseGuards;
938941942942+ /// You must call this before generating the guard of a case clause.
943943+ /// For example:
944944+ ///
945945+ /// ```ignore
946946+ /// let case = builder.start_case();
947947+ /// builder.variable("wibble");
948948+ /// let case = builder.end_case_subject();
949949+ ///
950950+ /// let clause = builder.start_case_clause();
951951+ /// builder.discard_pattern();
952952+ /// let clause = builder.end_clause_pattern();
953953+ /// let guard = builder.start_clause_guard();
954954+ /// builder.atom("true");
955955+ /// builder.builder.end_clause_guard(guard);
956956+ /// let clause = builder.end_clause_guards();
957957+ /// builder.int(1.into());
958958+ /// builder.end_clause_body();
959959+ ///
960960+ /// builder.end_case(case);
961961+ /// ```
962962+ ///
963963+ /// A clause might have multiple guards but, the way Gleam is compiled, you
964964+ /// should always call this function just once. A gleam guard is always
965965+ /// compiled as a single erlang guard with a single expression.
966966+ ///
967967+ fn start_clause_guard(&mut self) -> Self::Guard;
968968+969969+ /// This ends an open clause guards. Anything that is generated after
970970+ /// this is going to be a new guard among the guards of a case's guards.
971971+ fn end_clause_guard(&mut self, clause_guard: Self::Guard);
972972+939973 /// This ends the case clause's guards. Anything that is generated after
940974 /// this is going to be a statement inside the current case clause until
941975 /// `end_clause_body` is called.
···14911525 /// in two steps: first we generate the name, second we generate the type of
14921526 /// the field.
14931527 RecordField { expected: ExpectedRecordFieldItem },
15281528+ /// We're generating a guard in a possible series of guards in a clause.
15291529+ ClauseGuard,
14941530}
1495153114961532#[derive(Debug, Eq, PartialEq)]
···17141750 type CalledExpression = ();
17151751 type Call = ();
17161752 type Case = ();
17531753+ type Guard = ();
17171754 type CaseSubject = ();
17181755 type ClauseBody = ();
17191756 type ClauseGuards = ();
···23022339 });
23032340 }
2304234123422342+ fn start_clause_guard(&mut self) -> Self::Guard {
23432343+ self.new_clause_guard();
23442344+ self.position.push(ErlangSourceBuilderPosition::ClauseGuard);
23452345+ }
23462346+23472347+ fn end_clause_guard(&mut self, _clause_guard: Self::Guard) {
23482348+ self.close_currently_open_item();
23492349+ }
23502350+23052351 fn end_clause_guards(&mut self, _clause_guards: Self::ClauseGuards) -> Self::ClauseBody {
23062352 self.close_currently_open_item();
23072353 self.indentation += INDENT;
···26662712 }
26672713 }
2668271426692669- ErlangSourceBuilderPosition::CaseClause {
26702670- expected:
26712671- ExpectedCaseClauseItem::Guards {
26722672- first: first @ true,
26732673- },
26742674- } => {
26752675- *first = false;
27152715+ ErlangSourceBuilderPosition::ClauseGuard => {
26762716 self.code.push_str(" when ");
26772717 }
26782718···27312771 | ErlangSourceBuilderPosition::Map { .. }
27322772 | ErlangSourceBuilderPosition::RecordAttribute { .. }
27332773 | ErlangSourceBuilderPosition::CaseClause {
27342734- expected: ExpectedCaseClauseItem::Guards { first: false },
27742774+ expected: ExpectedCaseClauseItem::Guards { .. },
27352775 }
27362776 | ErlangSourceBuilderPosition::BitArraySegment {
27372777 expected: BitArraySegmentExpectedItem::Specifiers,
···28182858 | ErlangSourceBuilderPosition::AnonymousFunctionStatement { .. }
28192859 | ErlangSourceBuilderPosition::UnaryOperator
28202860 | ErlangSourceBuilderPosition::Case { .. }
28612861+ | ErlangSourceBuilderPosition::ClauseGuard
28212862 | ErlangSourceBuilderPosition::BinaryOperator { .. }
28222863 | ErlangSourceBuilderPosition::CaseClause { .. }
28232864 | ErlangSourceBuilderPosition::Map { .. }
···29412982 | ErlangSourceBuilderPosition::UnaryOperator
29422983 | ErlangSourceBuilderPosition::BinaryOperator { .. }
29432984 | ErlangSourceBuilderPosition::Case { .. }
29852985+ | ErlangSourceBuilderPosition::ClauseGuard
29442986 | ErlangSourceBuilderPosition::Map { .. }
29452987 | ErlangSourceBuilderPosition::MapField { .. }
29462988 | ErlangSourceBuilderPosition::MatchOperator {
···30883130 self.code.push_str("end");
30893131 }
3090313231333133+ ErlangSourceBuilderPosition::ClauseGuard => (),
30913134 ErlangSourceBuilderPosition::CaseClause { expected } => match expected {
30923135 ExpectedCaseClauseItem::Pattern => (),
30933136 // When the guards of a case clause are over we need to add the
···31913234 | ErlangSourceBuilderPosition::BinaryOperator { .. }
31923235 | ErlangSourceBuilderPosition::Case { .. }
31933236 | ErlangSourceBuilderPosition::CaseClause { .. }
32373237+ | ErlangSourceBuilderPosition::ClauseGuard
31943238 | ErlangSourceBuilderPosition::Map { .. }
31953239 | ErlangSourceBuilderPosition::MapField { .. }
31963240 | ErlangSourceBuilderPosition::MatchPattern { .. }
···32733317 self.code.push_str(&" ".repeat(self.indentation))
32743318 }
3275331933203320+ fn new_clause_guard(&mut self) {
33213321+ self.pop_leftover_items();
33223322+ let Some(ErlangSourceBuilderPosition::CaseClause {
33233323+ expected:
33243324+ ExpectedCaseClauseItem::Guards {
33253325+ first: first @ true,
33263326+ },
33273327+ }) = self.position.last_mut()
33283328+ else {
33293329+ invalid_code_for_position!(self, "new clause guard");
33303330+ };
33313331+33323332+ *first = false;
33333333+ }
33343334+32763335 /// You can call this before generating any expression (before the
32773336 /// `new_expression` call) if we can skip wrapping it in parentheses when it
32783337 /// appears as a bitstring segment value.
···34643523 | ErlangSourceBuilderPosition::BitArray { .. }
34653524 | ErlangSourceBuilderPosition::Map { .. }
34663525 | ErlangSourceBuilderPosition::Block { .. }
35263526+ | ErlangSourceBuilderPosition::ClauseGuard
34673527 | ErlangSourceBuilderPosition::UnaryOperator
34683528 | ErlangSourceBuilderPosition::FunctionType {
34693529 expected: