···32323333/// This is an open runtime error to which more fields can still be added.
3434#[must_use]
3535-struct RuntimeError {
3535+struct RuntimeError<Map, Call> {
3636 /// This is the map that is going to be thrown by the `erlang:error` call.
3737- error_map: erlang_generation::Map,
3737+ error_map: Map,
3838 /// This is the call to `erlang:error` that will throw the error, with the
3939 /// map as an argument.
4040- erlang_error_call: erlang_generation::Call,
4040+ erlang_error_call: Call,
4141}
42424343/// Represents all the different kind of runtime errors that Gleam can raise.
···154154 ///
155155 variable_names: im::HashMap<SrcSpan, EcoString>,
156156157157- /// This keeps track of the number of throwaway variables that have already
157157+ /// This keeps track of the number of generated variables that have already
158158 /// been generated in the current function.
159159 /// For example if this is `2` it means we've already generated:
160160 ///
···164164 /// _value@2
165165 /// ```
166166 ///
167167- /// We need this to make sure that every time we generate a new throwaway
167167+ /// We need this to make sure that every time we generate a new generated
168168 /// variable it has a unique name not shadowing anything else.
169169 ///
170170- throwaway_variables: usize,
170170+ generated_variables: usize,
171171172172 /// This keeps track of all the names that are taken for the current
173173 /// function and can't be used when defining new variables.
···488488 module_generator,
489489 taken_names: im::HashMap::new(),
490490 variable_names: im::HashMap::new(),
491491- throwaway_variables: 0,
491491+ generated_variables: 0,
492492 }
493493 }
494494···562562 /// The generated name is guaranteed to always be unique for the given
563563 /// function.
564564 ///
565565- fn new_throwaway_variable(&mut self) -> EcoString {
566566- let name = if self.throwaway_variables == 0 {
565565+ fn new_generated_variable(&mut self) -> EcoString {
566566+ let name = if self.generated_variables == 0 {
567567 EcoString::from("_value")
568568 } else {
569569- eco_format!("_value@{}", self.throwaway_variables)
569569+ eco_format!("_value@{}", self.generated_variables)
570570 };
571571- self.throwaway_variables += 1;
571571+ self.generated_variables += 1;
572572 name
573573 }
574574···732732 // If an argument is made of just underscores, then that would result
733733 // in a syntax error in the generated Erlang, where the external
734734 // function is called with a discard `io:format(_, _)`!
735735- // So in this case we use a throwaway name to make sure the external
735735+ // So in this case we use a generated name to make sure the external
736736 // function can be called correctly.
737737 ArgNames::Discard { name, location }
738738 | ArgNames::LabelledDiscard {
···741741 ..
742742 } if is_external => {
743743 if name.chars().all(|char| char == '_') {
744744- self.new_throwaway_variable()
744744+ self.new_generated_variable()
745745 } else {
746746 self.new_erlang_variable(name, *location)
747747 }
···764764 // We go over each statement one by one and produce the code they need.
765765 for i in 0..statements.len() {
766766 match statements.get(i).expect("statement in range") {
767767- Statement::Expression(expression) => self.expr(builder, expression),
768768- Statement::Use(use_) => self.expr(builder, &use_.call),
767767+ Statement::Expression(expression) => self.expression(builder, expression),
768768+ Statement::Use(use_) => self.expression(builder, &use_.call),
769769 Statement::Assert(assert) => self.assert(builder, assert),
770770 Statement::Assignment(assignment) => match &assignment.kind {
771771 AssignmentKind::Let | AssignmentKind::Generated => {
···812812 }
813813 }
814814815815- fn expr<Output>(
815815+ fn expression<Output>(
816816 &mut self,
817817 builder: &mut impl ErlangBuilder<Output>,
818818 expression: &'a TypedExpr,
···858858 left,
859859 right,
860860 ..
861861- } => self.bin_op(builder, operator, left, right),
861861+ } => self.binary_operator(builder, operator, left, right),
862862863863 //
864864 // BitArrays, Lists, and Tuples.
···10911091 /// After you're done generating those additional fields remember you _must_
10921092 /// call `end_runtime_error` before generating any other piece of code!
10931093 ///
10941094- fn start_runtime_error<Output>(
10941094+ fn start_runtime_error<Output, Builder: ErlangBuilder<Output>>(
10951095 &mut self,
10961096- builder: &mut impl ErlangBuilder<Output>,
10961096+ builder: &mut Builder,
10971097 error_kind: RuntimeErrorKind,
10981098 location: SrcSpan,
10991099 message: Option<&'a TypedExpr>,
11001100- ) -> RuntimeError {
11011101- let call = builder.start_remote_call(ErlangModuleName::new("erlang"), "error");
11001100+ ) -> RuntimeError<Builder::Map, Builder::Call> {
11011101+ let call = builder.start_remote_call(ErlangModuleName::erlang(), "error");
11021102 let map = builder.start_map();
1103110311041104 builder.map_field();
···11461146 }
1147114711481148 /// This closes an open runtime error.
11491149- fn end_runtime_error<Output>(
11491149+ fn end_runtime_error<Output, Builder: ErlangBuilder<Output>>(
11501150 &self,
11511151- builder: &mut impl ErlangBuilder<Output>,
11521152- runtime_error: RuntimeError,
11511151+ builder: &mut Builder,
11521152+ runtime_error: RuntimeError<Builder::Map, Builder::Call>,
11531153 ) {
11541154 builder.end_map(runtime_error.error_map);
11551155 builder.end_call(runtime_error.erlang_error_call);
···11621162 ) {
11631163 if needs_begin_end_wrapping(expression) {
11641164 let block = builder.start_block();
11651165- self.expr(builder, expression);
11651165+ self.expression(builder, expression);
11661166 builder.end_block(block);
11671167 } else {
11681168- self.expr(builder, expression);
11681168+ self.expression(builder, expression);
11691169 }
11701170 }
11711171···12321232 // end
12331233 // ```
12341234 let clause = builder.start_case_clause();
12351235- let matched_value_name = self.new_throwaway_variable();
12351235+ let matched_value_name = self.new_generated_variable();
12361236 builder.match_pattern();
12371237 let mut generator = PatternGenerator::new(self);
12381238 generator.pattern(builder, pattern);
···1246124612471247 // This is the catch all branch to throw an error otherwise.
12481248 let clause = builder.start_case_clause();
12491249- let value_name = self.new_throwaway_variable();
12491249+ let value_name = self.new_generated_variable();
12501250 builder.variable_pattern(&value_name);
12511251 let clause = builder.end_clause_pattern(clause);
12521252 let clause = builder.end_clause_guards(clause);
···13551355 },
13561356 )
13571357 } else {
13581358- self.expr(builder, finally)
13581358+ self.expression(builder, finally)
13591359 }
13601360 }
13611361···14131413 };
1414141414151415 // If the left or right hand side are not simple variables we'll
14161416- // need to first assign those to throwaway variables and keep
14161416+ // need to first assign those to generated variables and keep
14171417 // track of those names.
14181418 let left = if !left.is_var() {
14191419- let name = self.new_throwaway_variable();
14191419+ let name = self.new_generated_variable();
14201420 builder.match_operator();
14211421 builder.variable_pattern(&name);
14221422 self.maybe_block_expr(builder, left);
14231423- AssertionExpression::from_throwaway_variable(name, left)
14231423+ AssertionExpression::from_generated_variable(name, left)
14241424 } else {
14251425 AssertionExpression::from_expression(left)
14261426 };
1427142714281428 let right = if !right.is_var() {
14291429- let name = self.new_throwaway_variable();
14291429+ let name = self.new_generated_variable();
14301430 builder.match_operator();
14311431 builder.variable_pattern(&name);
14321432 self.maybe_block_expr(builder, right);
14331433- AssertionExpression::from_throwaway_variable(name, right)
14331433+ AssertionExpression::from_generated_variable(name, right)
14341434 } else {
14351435 AssertionExpression::from_expression(right)
14361436 };
···14791479 let mut call_arguments = Vec::with_capacity(arguments.len());
14801480 for argument in arguments {
14811481 let argument = if !argument.value.is_var() {
14821482- let name = self.new_throwaway_variable();
14821482+ let name = self.new_generated_variable();
14831483 builder.match_operator();
14841484 builder.variable_pattern(&name);
14851485 self.maybe_block_expr(builder, &argument.value);
14861486- AssertionExpression::from_throwaway_variable(name, &argument.value)
14861486+ AssertionExpression::from_generated_variable(name, &argument.value)
14871487 } else {
14881488 AssertionExpression::from_expression(&argument.value)
14891489 };
···19041904 tuple: &'a TypedExpr,
19051905 index: u64,
19061906 ) {
19071907- let call = builder.start_remote_call(ErlangModuleName::new("erlang"), "element");
19071907+ let call = builder.start_remote_call(ErlangModuleName::erlang(), "element");
19081908 builder.int((index + 1).into());
19091909 self.maybe_block_expr(builder, tuple);
19101910 builder.end_call(call);
···22132213 if let TypedExpr::Block { statements, .. } = &clause.then {
22142214 self.statement_sequence(builder, statements);
22152215 } else {
22162216- self.expr(builder, &clause.then);
22162216+ self.expression(builder, &clause.then);
22172217 }
22182218 builder.end_clause_body(clause_body);
22192219 }
···24872487 });
24882488 }
2489248924902490- fn bin_op<Output>(
24902490+ fn binary_operator<Output>(
24912491 &mut self,
24922492 builder: &mut impl ErlangBuilder<Output>,
24932493 name: &'a BinOp,
···25482548 // We first have to evaluate the left hand side, and store its
25492549 // result in a variable to use later.
25502550 let left_name = if !is_left_hand_side_pure {
25512551- let left_name = self.new_throwaway_variable();
25512551+ let left_name = self.new_generated_variable();
25522552 builder.match_operator();
25532553 builder.variable_pattern(&left_name);
25542554 self.maybe_block_expr(builder, left);
···25772577 builder.end_clause_body(body);
2578257825792579 // _value -> left / _value
25802580- let denominator = self.new_throwaway_variable();
25802580+ let denominator = self.new_generated_variable();
25812581 let clause = builder.start_case_clause();
25822582 builder.variable_pattern(&denominator);
25832583 let guards = builder.end_clause_pattern(clause);
···26242624 } => {
26252625 // If the left hand side is not a pure expression we will have
26262626 // to evaluate it before the right hand side of the expression.
26272627- // So we assign it to a throwaway variable that we will then
26272627+ // So we assign it to a generated variable that we will then
26282628 // reference in the case expression's body.
26292629 // It will look something like this:
26302630 //
···26372637 // ```
26382638 //
26392639 let left_name = if !is_left_hand_side_pure {
26402640- let left_name = self.new_throwaway_variable();
26402640+ let left_name = self.new_generated_variable();
26412641 builder.match_operator();
26422642 builder.variable_pattern(&left_name);
26432643 self.maybe_block_expr(builder, left);
···26582658 builder.end_clause_body(body);
2659265926602660 // _value -> left div _value
26612661- let denominator = self.new_throwaway_variable();
26612661+ let denominator = self.new_generated_variable();
26622662 let clause = builder.start_case_clause();
26632663 builder.variable_pattern(&denominator);
26642664 let guards = builder.end_clause_pattern(clause);
···27632763 // unicode:characters_to_binary(<segment_value>, utf8, {utf16, big})
27642764 // ```
27652765 let call =
27662766- builder.start_remote_call(ErlangModuleName::new("unicode"), "characters_to_binary");
27662766+ builder.start_remote_call(ErlangModuleName::unicode(), "characters_to_binary");
27672767 {
27682768 self.maybe_block_expr(builder, &segment.value);
27692769 builder.atom("utf8");
···28142814 builder.int(int_value.clone());
28152815 }
28162816 } else {
28172817- let call = builder.start_remote_call(ErlangModuleName::new("erlang"), "max");
28172817+ let call = builder.start_remote_call(ErlangModuleName::erlang(), "max");
28182818 builder.int(BigInt::ZERO);
28192819 self.maybe_block_expr(builder, size);
28202820 builder.end_call(call);
···29162916 tuple: &'a TypedClauseGuard,
29172917 index: u64,
29182918 ) {
29192919- let call = builder.start_remote_call(ErlangModuleName::new("erlang"), "element");
29192919+ let call = builder.start_remote_call(ErlangModuleName::erlang(), "element");
29202920 builder.int((index + 1).into());
29212921 self.clause_guard(builder, tuple, &HashMap::new());
29222922 builder.end_call(call);
···29892989 arguments: usize,
29902990 ) {
29912991 let arguments = (0..arguments)
29922992- .map(|_| self.new_throwaway_variable())
29922992+ .map(|_| self.new_generated_variable())
29932993 .collect_vec();
29942994 let function = builder.start_anonymous_function(&arguments);
29952995···32653265pub fn record_definition(record_name: &str, fields: &[(&str, Arc<Type>)]) -> String {
32663266 let mut builder = ErlangSourceBuilder::new(None);
3267326732683268- let record = builder.start_record_attribute(&to_snake_case(record_name));
32683268+ builder.start_record_attribute(&to_snake_case(record_name));
3269326932703270 let type_printer = TypeGenerator::new("").var_as_any();
32713271 for (field_name, field_type) in fields {
···32743274 type_printer.type_(&mut builder, field_type);
32753275 }
3276327632773277- builder.end_record_attribute(record);
32773277+ builder.end_record_attribute(());
32783278 builder.into_output()
32793279}
32803280···35633563 self
35643564 }
3565356535663566- fn from_throwaway_variable(name: EcoString, original_expression: &'a TypedExpr) -> Self {
35663566+ fn from_generated_variable(name: EcoString, original_expression: &'a TypedExpr) -> Self {
35673567 Self {
35683568 runtime_value: Some(AssertedExpressionRuntimeValue::Variable(name)),
35693569 kind: if original_expression.is_literal() {
···36283628 /// That's because we know wether the assertion failed (it must be false),
36293629 /// or not (it must be true).
36303630 KnownBool(bool),
36313631- /// The asserted value was bound to a throwaway variable with the given
36313631+ /// The asserted value was bound to a generated variable with the given
36323632 /// name, and we can reference it using that name.
36333633 Variable(EcoString),
36343634 /// The asserted value is an expression we need to inline in the error.
···3535 pub fn new(gleam_module_name: &str) -> Self {
3636 Self(gleam_module_name.replace("/", "@").into())
3737 }
3838-}
39384040-#[must_use]
4141-/// Represents an open function that has yet to be closed.
4242-/// A function definition is started with `ErlangBuilder::start_function` and
4343-/// _must_ be closed using `ErlangBuilder::end_function`.
4444-pub struct Function {}
4545-4646-#[must_use]
4747-/// Represents an open function call that has yet to be closed.
4848-pub struct Call {}
4949-5050-#[must_use]
5151-/// Represents an open case expression that has yet to be closed.
5252-pub struct Case {}
5353-5454-#[must_use]
5555-/// Represents an open case clause pattern that has yet to be generated.
5656-pub struct ClausePattern {}
5757-5858-#[must_use]
5959-/// Represents a set of clause guards that has yet to be closed.
6060-pub struct ClauseGuards {}
6161-6262-#[must_use]
6363-/// Represents an open clause body that has yet to be closed.
6464-pub struct ClauseBody {}
3939+ /// The Erlang/OTP `erlang` module.
4040+ pub fn erlang() -> Self {
4141+ Self("erlang".into())
4242+ }
65436666-#[must_use]
6767-/// Represents an open tuple that has yet to be closed.
6868-pub struct Tuple {}
6969-7070-#[must_use]
7171-/// Represents an open map that has yet to be closed.
7272-pub struct Map {}
7373-7474-#[must_use]
7575-/// Represents an open bit array that has yet to be closed.
7676-pub struct BitArray {}
7777-7878-#[must_use]
7979-/// Represents an open bit array pattern that has yet to be closed.
8080-pub struct BitArrayPattern {}
8181-8282-#[must_use]
8383-/// Represents an open tuple type that has yet to be closed.
8484-pub struct TupleType {}
8585-8686-#[must_use]
8787-/// Represents an open tuple pattern that has yet to be closed.
8888-pub struct TuplePattern {}
8989-9090-#[must_use]
9191-/// Represents an open doc/moduledoc attribute.
9292-pub struct DocAttribute {}
9393-9494-#[must_use]
9595-/// Represents an open record attribute.
9696-pub struct RecordAttribute {}
9797-9898-#[must_use]
9999-/// Represents an open function type annotation that has yet to be closed after
100100-/// generating the arguments types and the return type.
101101-pub struct FunctionType {}
102102-103103-#[must_use]
104104-/// Represents an open named type that has yet to be closed after generating
105105-/// the types it takes as an argument (if any).
106106-pub struct NamedType {}
107107-108108-#[must_use]
109109-/// Represents an open alternative type that has yet to be closed after
110110-/// generating all of its alternatives.
111111-pub struct UnionType {}
112112-113113-#[must_use]
114114-/// Represents an open function type annotation that has yet to be closed after
115115-/// generating the arguments types and the return type.
116116-pub struct FunctionSpec {}
117117-118118-#[must_use]
119119-/// Represents an open block that has yet to be closed after generating the
120120-/// statements that go inside it.
121121-pub struct Block {}
122122-123123-#[must_use]
124124-/// Represents an open list of arguments' types in a function type annotation
125125-/// that has yet to be closed.
126126-pub struct FunctionTypeArguments {}
4444+ /// The Erlang/OTP `unicode` module.
4545+ pub fn unicode() -> Self {
4646+ Self("unicode".into())
4747+ }
4848+}
1274912850/// All the possible specifiers that can be used in a bit array segment.
12951pub enum BitArraySegmentSpecifier {
···15779/// https://www.erlang.org/doc/apps/erts/absform.html
15880///
15981pub trait ErlangBuilder<Output> {
8282+ /// Represents an open function that has yet to be closed.
8383+ /// A function definition is started with `ErlangBuilder::start_function` and
8484+ /// _must_ be closed using `ErlangBuilder::end_function`.
8585+ type Function;
8686+8787+ /// Represents an open function call that has yet to be closed.
8888+ type Call;
8989+9090+ /// Represents an open case expression that has yet to be closed.
9191+ type Case;
9292+9393+ /// Represents an open case clause pattern that has yet to be generated.
9494+ type ClausePattern;
9595+9696+ /// Represents a set of clause guards that has yet to be closed.
9797+ type ClauseGuards;
9898+9999+ /// Represents an open clause body that has yet to be closed.
100100+ type ClauseBody;
101101+102102+ /// Represents an open tuple that has yet to be closed.
103103+ type Tuple;
104104+105105+ /// Represents an open map that has yet to be closed.
106106+ type Map;
107107+108108+ /// Represents an open bit array that has yet to be closed.
109109+ type BitArray;
110110+111111+ /// Represents an open bit array pattern that has yet to be closed.
112112+ type BitArrayPattern;
113113+114114+ /// Represents an open tuple type that has yet to be closed.
115115+ type TupleType;
116116+117117+ /// Represents an open tuple pattern that has yet to be closed.
118118+ type TuplePattern;
119119+120120+ /// Represents an open doc/moduledoc attribute.
121121+ type DocAttribute;
122122+123123+ /// Represents an open record attribute.
124124+ type RecordAttribute;
125125+126126+ /// Represents an open function type annotation that has yet to be closed after
127127+ /// generating the arguments types and the return type.
128128+ type FunctionType;
129129+130130+ /// Represents an open named type that has yet to be closed after generating
131131+ /// the types it takes as an argument (if any).
132132+ type NamedType;
133133+134134+ /// Represents an open alternative type that has yet to be closed after
135135+ /// generating all of its alternatives.
136136+ type UnionType;
137137+138138+ /// Represents an open function type annotation that has yet to be closed after
139139+ /// generating the arguments types and the return type.
140140+ type FunctionSpec;
141141+142142+ /// Represents an open block that has yet to be closed after generating the
143143+ /// statements that go inside it.
144144+ type Block;
145145+146146+ /// Represents an open list of arguments' types in a function type annotation
147147+ /// that has yet to be closed.
148148+ type FunctionTypeArguments;
149149+160150 /// Creates a new `ErlangBuilder` data structure to generate Erlang code.
161151 /// If a module name is provided this will also automatically take care of
162152 /// generating the appropriate `-module` annotation at the very beginning.
···230220 /// -doc(false).
231221 /// ```
232222 ///
233233- fn start_doc_attribute(&mut self) -> DocAttribute;
223223+ fn start_doc_attribute(&mut self) -> Self::DocAttribute;
234224235225 /// Starts a `-moduledoc` attribute.
236226 /// What is generated after calling this function will end up inside the
···251241 /// -moduledoc(false).
252242 /// ```
253243 ///
254254- fn start_moduledoc_attribute(&mut self) -> DocAttribute;
244244+ fn start_moduledoc_attribute(&mut self) -> Self::DocAttribute;
255245256246 /// This closes the currently open doc/moduledoc attribute.
257247 /// Code generated after this is not gonna be part of it.
258248 ///
259259- fn end_doc_attribute(&mut self, attribute: DocAttribute);
249249+ fn end_doc_attribute(&mut self, attribute: Self::DocAttribute);
260250261251 /// This generates the code for a `-compile([]).` attribute where all the
262252 /// strings produces by the given iterator are going to be passed as atom
···311301 /// -record(wobble, { wibble :: ok }).
312302 /// ```
313303 ///
314314- fn start_record_attribute(&mut self, record_name: &str) -> RecordAttribute;
304304+ fn start_record_attribute(&mut self, record_name: &str) -> Self::RecordAttribute;
315305316306 /// This closes the currently open record attribute.
317307 /// Code generated after this is not gonna be part of it.
318308 ///
319319- fn end_record_attribute(&mut self, record: RecordAttribute);
309309+ fn end_record_attribute(&mut self, record: Self::RecordAttribute);
320310321311 /// This creates a record field inside a record attribute.
322312 /// After this you're supposed to generate two things:
···351341 /// -spec wibble(integer(), A) -> A.
352342 /// ```
353343 ///
354354- fn start_function_spec(&mut self, name: &str, arity: usize) -> FunctionSpec;
344344+ fn start_function_spec(&mut self, name: &str, arity: usize) -> Self::FunctionSpec;
355345356346 /// This closes the currently open function spec.
357347 /// Code generated after this is not gonna be part of this function spec.
358348 ///
359359- fn end_function_spec(&mut self, function_spec: FunctionSpec);
349349+ fn end_function_spec(&mut self, function_spec: Self::FunctionSpec);
360350361351 /// This starts an Erlang type spec.
362352 /// After this call you're expected to generate a single type; that's going
···413403 /// (integer(), A) -> A.
414404 /// ```
415405 ///
416416- fn start_function_type(&mut self) -> FunctionTypeArguments;
406406+ fn start_function_type(&mut self) -> Self::FunctionTypeArguments;
417407418408 /// This closes the currently open function type arguments list.
419409 /// This means that the next type that is generated is going to be the
···422412 /// After that you must call `end_function_type` to close the function
423413 /// type.
424414 ///
425425- fn end_function_type_arguments(&mut self, function_type: FunctionTypeArguments)
426426- -> FunctionType;
415415+ fn end_function_type_arguments(
416416+ &mut self,
417417+ function_type: Self::FunctionTypeArguments,
418418+ ) -> Self::FunctionType;
427419428420 /// This takes a function type and closes it.
429421 /// Code generated after this is not gonna be part of this function type.
430422 ///
431431- fn end_function_type(&mut self, function_type: FunctionType);
423423+ fn end_function_type(&mut self, function_type: Self::FunctionType);
432424433425 /// This starts a named type (either defined previously in this module, or
434426 /// a built-in Erlang type) with the given name.
···448440 /// wibble().
449441 /// ```
450442 ///
451451- fn start_named_type(&mut self, name: &str) -> NamedType;
443443+ fn start_named_type(&mut self, name: &str) -> Self::NamedType;
452444453445 /// This starts a remote named type with the given module and name.
454446 /// Any code generated after this is gonna be an argument of the open
···467459 /// wibble:wobble().
468460 /// ```
469461 ///
470470- fn start_remote_named_type(&mut self, module: ErlangModuleName, name: &str) -> NamedType;
462462+ fn start_remote_named_type(&mut self, module: ErlangModuleName, name: &str) -> Self::NamedType;
471463472464 /// This takes a named type and closes it.
473465 /// Code generated after this is not gonna be part of this named type.
474466 ///
475475- fn end_named_type(&mut self, named_type: NamedType);
467467+ fn end_named_type(&mut self, named_type: Self::NamedType);
476468477469 /// This starts a tuple type.
478470 /// Any code generated after this is gonna be one of the tuple items.
···492484 /// {nil, ok}.
493485 /// ```
494486 ///
495495- fn start_tuple_type(&mut self) -> TupleType;
487487+ fn start_tuple_type(&mut self) -> Self::TupleType;
496488497489 /// This takes a tuple type and closes it.
498490 /// Code generated after this is not gonna be part of this tuple type.
499491 ///
500500- fn end_tuple_type(&mut self, tuple: TupleType);
492492+ fn end_tuple_type(&mut self, tuple: Self::TupleType);
501493502494 /// This starts a union type.
503495 /// Any code generated after this is gonna be a possible alternative of this
···518510 /// ok | error.
519511 /// ```
520512 ///
521521- fn start_union_type(&mut self) -> UnionType;
513513+ fn start_union_type(&mut self) -> Self::UnionType;
522514523515 /// This takes a union type and closes it.
524516 /// Code generated after this is not gonna be part of this union type.
525517 ///
526526- fn end_union_type(&mut self, union_type: UnionType);
518518+ fn end_union_type(&mut self, union_type: Self::UnionType);
527519528520 /// This generated the code for a type variable with the given name.
529521 ///
···591583 name: &str,
592584 arity: usize,
593585 arguments_names: impl IntoIterator<Item = Name>,
594594- ) -> Function;
586586+ ) -> Self::Function;
595587596588 /// This starts an expression defining an anonymous function.
597589 /// Any code generated after this is gonna be a statement inside the
···614606 fn start_anonymous_function<Name: AsRef<str>>(
615607 &mut self,
616608 arguments_names: impl IntoIterator<Item = Name>,
617617- ) -> Function;
609609+ ) -> Self::Function;
618610619611 /// This takes a function and closes it.
620612 /// Code generated after this is not gonna be part of this function.
621613 ///
622622- fn end_function(&mut self, function: Function);
614614+ fn end_function(&mut self, function: Self::Function);
623615624616 /// This starts a block expression.
625617 /// Any code generated after this is gonna be a statement inside the open
···643635 /// end.
644636 /// ```
645637 ///
646646- fn start_block(&mut self) -> Block;
638638+ fn start_block(&mut self) -> Self::Block;
647639648640 /// This takes a block and closes it.
649641 /// Code generated after this is not gonna be part of this block.
650642 ///
651651- fn end_block(&mut self, block: Block);
643643+ fn end_block(&mut self, block: Self::Block);
652644653645 /// This starts a remote call.
654646 /// Any code generated after this is gonna be an argument of the open
···668660 /// io:format(~"Giacomo").
669661 /// ```
670662 ///
671671- fn start_remote_call(&mut self, module: ErlangModuleName, function: &str) -> Call;
663663+ fn start_remote_call(&mut self, module: ErlangModuleName, function: &str) -> Self::Call;
672664673665 /// This starts a function call.
674666 /// The expression generated immediately after this is going to be the thing
···690682 /// wibble(~"Hello", ~"Giacomo").
691683 /// ```
692684 ///
693693- fn start_call(&mut self) -> Call;
685685+ fn start_call(&mut self) -> Self::Call;
694686695687 /// This takes an open call and closes it.
696688 /// Code generated after this is not gonna be an argument to this call.
697689 ///
698698- fn end_call(&mut self, call: Call);
690690+ fn end_call(&mut self, call: Self::Call);
699691700692 /// This starts a tuple.
701693 /// Any code generated after this is gonna be an item of the tuple.
···715707 /// {~"Hello", 1}.
716708 /// ```
717709 ///
718718- fn start_tuple(&mut self) -> Tuple;
710710+ fn start_tuple(&mut self) -> Self::Tuple;
719711720712 /// This takes an open tuple and closes it.
721713 /// Code generated after this is not gonna be an item of the tuple.
722714 ///
723723- fn end_tuple(&mut self, tuple: Tuple);
715715+ fn end_tuple(&mut self, tuple: Self::Tuple);
724716725717 /// This starts an Erlang map.
726718 /// After this call you can add fields to the map using the `map_field`
···749741 /// }.
750742 /// ```
751743 ///
752752- fn start_map(&mut self) -> Map;
744744+ fn start_map(&mut self) -> Self::Map;
753745754746 /// This takes an open map and closes it.
755747 /// Code generated after this is not gonna be a map field.
756748 ///
757757- fn end_map(&mut self, map: Map);
749749+ fn end_map(&mut self, map: Self::Map);
758750759751 /// This is used to add new fields to an open map.
760752 /// After calling this you must generate exactly two values: the first one
···789781 /// <<1, ~"hello">>.
790782 /// ```
791783 ///
792792- fn start_bit_array(&mut self) -> BitArray;
784784+ fn start_bit_array(&mut self) -> Self::BitArray;
793785794786 /// This takes an open bit array and closes it.
795787 /// Code generated after this is not gonna be a segment of the bit array.
796788 ///
797797- fn end_bit_array(&mut self, bit_array: BitArray);
789789+ fn end_bit_array(&mut self, bit_array: Self::BitArray);
798790799791 /// This starts a new bit array segment. Make sure to call it after
800792 /// `start_bit_array`!
···894886 /// end.
895887 /// ```
896888 ///
897897- fn start_case(&mut self) -> Case;
889889+ fn start_case(&mut self) -> Self::Case;
898890899891 /// This ends an open case expression.
900892 /// Any code generated after this is not going to be part of it.
901893 ///
902902- fn end_case(&mut self, case: Case);
894894+ fn end_case(&mut self, case: Self::Case);
903895904896 /// This starts a new case clause inside a case expression.
905897 /// After this is called you must generate a single pattern and then call
···907899 ///
908900 /// For an example on how to generate a full case clause check the
909901 /// `start_case` documentation.
910910- fn start_case_clause(&mut self) -> ClausePattern;
902902+ fn start_case_clause(&mut self) -> Self::ClausePattern;
911903912904 /// This ends the case clause's pattern. After this you must generate the
913905 /// clause guards and then call `end_clause_guards`.
914906 /// If the clause you're generating has no guards you can immediately call
915907 /// that function without generating anything inbetween.
916916- fn end_clause_pattern(&mut self, clause_pattern: ClausePattern) -> ClauseGuards;
908908+ fn end_clause_pattern(&mut self, clause_pattern: Self::ClausePattern) -> Self::ClauseGuards;
917909918910 /// This ends the case clause's guards. Anything that is generated after
919911 /// this is going to be a statement inside the current case clause until
920912 /// `end_clause_body` is called.
921921- fn end_clause_guards(&mut self, clause_guards: ClauseGuards) -> ClauseBody;
913913+ fn end_clause_guards(&mut self, clause_guards: Self::ClauseGuards) -> Self::ClauseBody;
922914923915 /// This takes an open clause body and ends it.
924916 /// After this you can start generating new case clauses, or end the
925917 /// currently open case expression if this was the last clause!
926926- fn end_clause_body(&mut self, clause_body: ClauseBody);
918918+ fn end_clause_body(&mut self, clause_body: Self::ClauseBody);
927919928920 /// This creates a variable expression with the given name.
929921 /// For example:
···11181110 /// {~"Hello", _}.
11191111 /// ```
11201112 ///
11211121- fn start_tuple_pattern(&mut self) -> TuplePattern;
11131113+ fn start_tuple_pattern(&mut self) -> Self::TuplePattern;
1122111411231115 /// This takes an open tuple pattern and closes it.
11241116 /// Any code generated after this is not gonna be part of that pattern.
11251125- fn end_tuple_pattern(&mut self, tuple: TuplePattern);
11171117+ fn end_tuple_pattern(&mut self, tuple: Self::TuplePattern);
1126111811271119 /// This starts an Erlang bitstring (that's a Gleam's BitArray) pattern.
11281120 /// Any code generated after this is gonna be a segment of the pattern.
···11511143 /// <<1, _>>.
11521144 /// ```
11531145 ///
11541154- fn start_bit_array_pattern(&mut self) -> BitArrayPattern;
11461146+ fn start_bit_array_pattern(&mut self) -> Self::BitArrayPattern;
1155114711561148 /// This takes an open bit array pattern and closes it.
11571149 /// Code generated after this is not gonna be a segment of the bit array.
11581150 ///
11591159- fn end_bit_array_pattern(&mut self, bit_array: BitArrayPattern);
11511151+ fn end_bit_array_pattern(&mut self, bit_array: Self::BitArrayPattern);
1160115211611153 /// This creates a list pattern.
11621154 /// The next two generated values are going to be respectively the pattern
···16911683/// require a bit of extra book-keeping in the `new_x` functions.
16921684///
16931685impl ErlangBuilder<String> for ErlangSourceBuilder {
16861686+ type BitArray = ();
16871687+ type BitArrayPattern = ();
16881688+ type Block = ();
16891689+ type Call = ();
16901690+ type Case = ();
16911691+ type ClauseBody = ();
16921692+ type ClauseGuards = ();
16931693+ type ClausePattern = ();
16941694+ type DocAttribute = ();
16951695+ type Function = ();
16961696+ type FunctionSpec = ();
16971697+ type FunctionType = ();
16981698+ type FunctionTypeArguments = ();
16991699+ type Map = ();
17001700+ type NamedType = ();
17011701+ type RecordAttribute = ();
17021702+ type Tuple = ();
17031703+ type TuplePattern = ();
17041704+ type TupleType = ();
17051705+ type UnionType = ();
17061706+16941707 fn new(module: Option<ErlangModuleName>) -> Self {
16951708 Self {
16961709 code: if let Some(module) = module {
···17651778 self.code.push_str("]).\n");
17661779 }
1767178017681768- fn start_doc_attribute(&mut self) -> DocAttribute {
17811781+ fn start_doc_attribute(&mut self) -> Self::DocAttribute {
17691782 self.new_top_level_form();
17701783 self.code.push_str("-doc(");
17711784 self.position
17721785 .push(ErlangSourceBuilderPosition::DocAttribute);
17731773- DocAttribute {}
17741786 }
1775178717761776- fn start_moduledoc_attribute(&mut self) -> DocAttribute {
17881788+ fn start_moduledoc_attribute(&mut self) -> Self::DocAttribute {
17771789 self.new_top_level_form();
17781790 self.code.push_str("-moduledoc(");
17791791 self.position
17801792 .push(ErlangSourceBuilderPosition::DocAttribute);
17811781- DocAttribute {}
17821793 }
1783179417841784- fn end_doc_attribute(&mut self, _attribute: DocAttribute) {
17951795+ fn end_doc_attribute(&mut self, _attribute: Self::DocAttribute) {
17851796 self.close_currently_open_item();
17861797 }
17871798···18121823 self.code.push_str(").");
18131824 }
1814182518151815- fn start_record_attribute(&mut self, record_name: &str) -> RecordAttribute {
18261826+ fn start_record_attribute(&mut self, record_name: &str) -> Self::RecordAttribute {
18161827 self.new_top_level_form();
18171828 self.code.push_str("-record(");
18181829 self.code.push_str("e_atom_name(record_name));
···18201831 self.indentation += INDENT;
18211832 self.position
18221833 .push(ErlangSourceBuilderPosition::RecordAttribute { first: true });
18231823-18241824- RecordAttribute {}
18251834 }
1826183518271827- fn end_record_attribute(&mut self, _record: RecordAttribute) {
18361836+ fn end_record_attribute(&mut self, _record: Self::RecordAttribute) {
18281837 self.close_currently_open_item();
18291838 }
18301839···18361845 });
18371846 }
1838184718391839- fn start_function_spec(&mut self, name: &str, _arity: usize) -> FunctionSpec {
18481848+ fn start_function_spec(&mut self, name: &str, _arity: usize) -> Self::FunctionSpec {
18401849 self.new_top_level_form();
18411850 self.code.push_str("\n-spec ");
18421851 self.code.push_str("e_atom_name(name));
18431852 self.position
18441853 .push(ErlangSourceBuilderPosition::FunctionSpec);
18451845- FunctionSpec {}
18461854 }
1847185518481848- fn end_function_spec(&mut self, _function_spec: FunctionSpec) {
18561856+ fn end_function_spec(&mut self, _function_spec: Self::FunctionSpec) {
18491857 self.close_currently_open_item();
18501858 }
18511859···18771885 });
18781886 }
1879188718801880- fn start_function_type(&mut self) -> FunctionTypeArguments {
18881888+ fn start_function_type(&mut self) -> Self::FunctionTypeArguments {
18811889 self.new_type();
1882189018831891 let needs_wrapping =
···18941902 expected: ExpectedFunctionTypeItem::Arguments { first: true },
18951903 needs_wrapping,
18961904 });
18971897-18981898- FunctionTypeArguments {}
18991905 }
1900190619011907 fn end_function_type_arguments(
19021908 &mut self,
19031903- _function_type: FunctionTypeArguments,
19041904- ) -> FunctionType {
19091909+ _function_type: Self::FunctionTypeArguments,
19101910+ ) -> Self::FunctionType {
19051911 self.close_currently_open_item();
19061906-19071907- FunctionType {}
19081912 }
1909191319101910- fn end_function_type(&mut self, _function_type: FunctionType) {
19141914+ fn end_function_type(&mut self, _function_type: Self::FunctionType) {
19111915 self.close_currently_open_item();
19121916 }
1913191719141914- fn start_named_type(&mut self, name: &str) -> NamedType {
19181918+ fn start_named_type(&mut self, name: &str) -> Self::NamedType {
19151919 self.new_type();
19161920 self.position
19171921 .push(ErlangSourceBuilderPosition::NamedType { first: true });
19181922 self.code.push_str("e_atom_name(name));
19191923 self.code.push('(');
19201920-19211921- NamedType {}
19221924 }
1923192519241924- fn start_remote_named_type(&mut self, module: ErlangModuleName, name: &str) -> NamedType {
19261926+ fn start_remote_named_type(&mut self, module: ErlangModuleName, name: &str) -> Self::NamedType {
19251927 self.new_type();
19261928 self.position
19271929 .push(ErlangSourceBuilderPosition::NamedType { first: true });
···19291931 self.code.push(':');
19301932 self.code.push_str("e_atom_name(name));
19311933 self.code.push('(');
19321932-19331933- NamedType {}
19341934 }
1935193519361936- fn end_named_type(&mut self, _named_type: NamedType) {
19361936+ fn end_named_type(&mut self, _named_type: Self::NamedType) {
19371937 self.close_currently_open_item();
19381938 }
1939193919401940- fn start_tuple_type(&mut self) -> TupleType {
19401940+ fn start_tuple_type(&mut self) -> Self::TupleType {
19411941 self.new_type();
19421942 self.position
19431943 .push(ErlangSourceBuilderPosition::TupleType { first: true });
19441944 self.code.push('{');
19451945-19461946- TupleType {}
19471945 }
1948194619491949- fn end_tuple_type(&mut self, _tuple: TupleType) {
19471947+ fn end_tuple_type(&mut self, _tuple: Self::TupleType) {
19501948 self.close_currently_open_item();
19511949 }
1952195019531953- fn start_union_type(&mut self) -> UnionType {
19511951+ fn start_union_type(&mut self) -> Self::UnionType {
19541952 self.new_type();
19551953 self.position
19561954 .push(ErlangSourceBuilderPosition::UnionType { first: true });
19571957-19581958- UnionType {}
19591955 }
1960195619611961- fn end_union_type(&mut self, _union_type: UnionType) {
19571957+ fn end_union_type(&mut self, _union_type: Self::UnionType) {
19621958 self.close_currently_open_item();
19631959 }
19641960···19771973 name: &str,
19781974 _arity: usize,
19791975 arguments_names: impl IntoIterator<Item = Name>,
19801980- ) -> Function {
19761976+ ) -> Self::Function {
19811977 self.new_top_level_form();
19821978 self.code.push_str("e_atom_name(name));
19831979 self.code.push('(');
···19961992 self.indentation += INDENT;
19971993 self.position
19981994 .push(ErlangSourceBuilderPosition::FunctionStatement { first: true });
19991999-20002000- Function {}
20011995 }
2002199620031997 fn start_anonymous_function<Name: AsRef<str>>(
20041998 &mut self,
20051999 arguments_names: impl IntoIterator<Item = Name>,
20062006- ) -> Function {
20002000+ ) -> Self::Function {
20072001 self.new_expression();
20082002 self.code.push_str("fun(");
20092003···20212015 self.indentation += INDENT;
20222016 self.position
20232017 .push(ErlangSourceBuilderPosition::AnonymousFunctionStatement { first: true });
20242024-20252025- Function {}
20262018 }
2027201920282028- fn end_function(&mut self, _function: Function) {
20202020+ fn end_function(&mut self, _function: Self::Function) {
20292021 self.close_currently_open_item();
20302022 }
2031202320322032- fn start_block(&mut self) -> Block {
20242024+ fn start_block(&mut self) -> Self::Block {
20332025 self.new_expression();
20342026 self.code.push_str("begin");
20352027 self.indentation += INDENT;
20362028 self.position
20372029 .push(ErlangSourceBuilderPosition::Block { first: true });
20382038-20392039- Block {}
20402030 }
2041203120422042- fn end_block(&mut self, _block: Block) {
20322032+ fn end_block(&mut self, _block: Self::Block) {
20432033 self.close_currently_open_item();
20442034 }
2045203520462046- fn start_remote_call(&mut self, module: ErlangModuleName, function: &str) -> Call {
20362036+ fn start_remote_call(&mut self, module: ErlangModuleName, function: &str) -> Self::Call {
20472037 self.pop_leftover_items();
2048203820492039 // If this function call we're generating is itself being called then
···20672057 expected: ExpectedCallItem::Arguments { first: true },
20682058 called_item_needs_wrapping: false,
20692059 });
20702070- Call {}
20712060 }
2072206120732073- fn start_call(&mut self) -> Call {
20622062+ fn start_call(&mut self) -> Self::Call {
20742063 self.pop_leftover_items();
2075206420762065 // If this function call we're generating is itself being called then
···20912080 expected: ExpectedCallItem::FunctionToBeCalled,
20922081 called_item_needs_wrapping: false,
20932082 });
20942094- Call {}
20952083 }
2096208420972097- fn end_call(&mut self, _call: Call) {
20852085+ fn end_call(&mut self, _call: Self::Call) {
20982086 self.close_currently_open_item();
20992087 }
2100208821012101- fn start_tuple(&mut self) -> Tuple {
20892089+ fn start_tuple(&mut self) -> Self::Tuple {
21022090 self.new_expression();
21032091 self.code.push('{');
21042092 self.position
21052093 .push(ErlangSourceBuilderPosition::Tuple { first: true });
21062106-21072107- Tuple {}
21082094 }
2109209521102110- fn end_tuple(&mut self, _tuple: Tuple) {
20962096+ fn end_tuple(&mut self, _tuple: Self::Tuple) {
21112097 self.close_currently_open_item();
21122098 }
2113209921142114- fn start_map(&mut self) -> Map {
21002100+ fn start_map(&mut self) -> Self::Map {
21152101 self.new_expression();
21162102 self.code.push_str("#{");
21172103 self.indentation += INDENT;
21182104 self.position
21192105 .push(ErlangSourceBuilderPosition::Map { first: true });
21202120- Map {}
21212106 }
2122210721232123- fn end_map(&mut self, _map: Map) {
21082108+ fn end_map(&mut self, _map: Self::Map) {
21242109 self.close_currently_open_item();
21252110 }
21262111···21312116 });
21322117 }
2133211821342134- fn start_bit_array(&mut self) -> BitArray {
21192119+ fn start_bit_array(&mut self) -> Self::BitArray {
21352120 self.do_not_wrap_if_segment_value_or_size();
21362121 self.new_expression();
21372122 self.code.push_str("<<");
···21392124 kind: BitArrayKind::Expression,
21402125 first: true,
21412126 });
21422142-21432143- BitArray {}
21442127 }
2145212821462146- fn end_bit_array(&mut self, _bit_array: BitArray) {
21292129+ fn end_bit_array(&mut self, _bit_array: Self::BitArray) {
21472130 self.close_currently_open_item();
21482131 }
21492132···22302213 self.empty_list_of_kind(ListKind::Expression);
22312214 }
2232221522332233- fn start_case(&mut self) -> Case {
22162216+ fn start_case(&mut self) -> Self::Case {
22342217 self.new_expression();
22352218 self.code.push_str("case ");
22362219 self.position.push(ErlangSourceBuilderPosition::Case {
22372220 expected: ExpectedCaseItem::Subject,
22382221 });
22392239-22402240- Case {}
22412222 }
2242222322432243- fn end_case(&mut self, _case: Case) {
22242224+ fn end_case(&mut self, _case: Self::Case) {
22442225 self.close_currently_open_item();
22452226 }
2246222722472247- fn start_case_clause(&mut self) -> ClausePattern {
22282228+ fn start_case_clause(&mut self) -> Self::ClausePattern {
22482229 self.new_case_clause();
22492230 self.position.push(ErlangSourceBuilderPosition::CaseClause {
22502231 expected: ExpectedCaseClauseItem::Pattern,
22512232 });
22522252- ClausePattern {}
22532233 }
2254223422552255- fn end_clause_pattern(&mut self, _clause_pattern: ClausePattern) -> ClauseGuards {
22352235+ fn end_clause_pattern(&mut self, _clause_pattern: Self::ClausePattern) -> Self::ClauseGuards {
22562236 self.close_currently_open_item();
22572237 self.position.push(ErlangSourceBuilderPosition::CaseClause {
22582238 expected: ExpectedCaseClauseItem::Guards { first: true },
22592239 });
22602260-22612261- ClauseGuards {}
22622240 }
2263224122642264- fn end_clause_guards(&mut self, _clause_guards: ClauseGuards) -> ClauseBody {
22422242+ fn end_clause_guards(&mut self, _clause_guards: Self::ClauseGuards) -> Self::ClauseBody {
22652243 self.close_currently_open_item();
22662244 self.indentation += INDENT;
22672245 self.position.push(ErlangSourceBuilderPosition::CaseClause {
22682246 expected: ExpectedCaseClauseItem::Body { first: true },
22692247 });
22702270-22712271- ClauseBody {}
22722248 }
2273224922742274- fn end_clause_body(&mut self, _clause_body: ClauseBody) {
22502250+ fn end_clause_body(&mut self, _clause_body: Self::ClauseBody) {
22752251 self.close_currently_open_item();
22762252 }
22772253···23762352 self.code.push_str("e_atom_name(name));
23772353 }
2378235423792379- fn start_tuple_pattern(&mut self) -> TuplePattern {
23552355+ fn start_tuple_pattern(&mut self) -> Self::TuplePattern {
23802356 self.new_pattern();
23812357 self.code.push('{');
23822358 self.position
23832359 .push(ErlangSourceBuilderPosition::TuplePattern { first: true });
23842384- TuplePattern {}
23852360 }
2386236123872387- fn end_tuple_pattern(&mut self, _tuple: TuplePattern) {
23622362+ fn end_tuple_pattern(&mut self, _tuple: Self::TuplePattern) {
23882363 self.close_currently_open_item();
23892364 }
2390236523912391- fn start_bit_array_pattern(&mut self) -> BitArrayPattern {
23662366+ fn start_bit_array_pattern(&mut self) -> Self::BitArrayPattern {
23922367 self.new_pattern();
23932368 self.code.push_str("<<");
23942369 self.position.push(ErlangSourceBuilderPosition::BitArray {
23952370 kind: BitArrayKind::Pattern,
23962371 first: true,
23972372 });
23982398-23992399- BitArrayPattern {}
24002373 }
2401237424022402- fn end_bit_array_pattern(&mut self, _bit_array: BitArrayPattern) {
23752375+ fn end_bit_array_pattern(&mut self, _bit_array: Self::BitArrayPattern) {
24032376 self.close_currently_open_item();
24042377 }
24052378