GenCo
GenCo is an even simpler code generator for Rust, written for use in reproto.
The workhorse of GenCo is the quote!
macro. While tokens can be constructed,
manually, quote!
makes this process much easier.
GenCo does not deal with language-specific syntax, instead it limits itself to do the following basic necessities through specialization:
- Handle and collapse import statements.
- Quote strings according to language convention.
- Indents and spaces your code according to generic indentation rules.
Examples
The following are language specific examples for GenCo using the quote!
macro.
- Rust Example
- Java Example
- C# Example
- Dart Example (TODO)
- Go Example (TODO)
- JavaScript Example (TODO)
- Python Example (TODO)
The following is a simple example showcasing code generation for Rust.
use imported;
use ;
// Import the LittleEndian item, without referencing it through the last
// module component it is part of.
let little_endian = imported.qualified;
let big_endian = imported;
// This is a trait, so only import it into the scope (unless we intent to
// implement it).
let write_bytes_ext = imported.alias;
let tokens: = quote! ;
Indentation Rules
The quote!
macro has the following rules for dealing with indentation and
spacing.
Two tokens that are separated, are spaced. Regardless of how many spaces there are between them.
So:
let _: Tokens = quote!;
Becomes:
More that two line breaks are collapsed.
So:
let _: Tokens = quote! ;
Becomes:
Indentation is determined on a row-by-row basis. If a column is further in than the one on the preceeding row, it is indented one level deeper.
Like wise if a column starts before the previous rows column, it is indended one level shallower.
So:
let _: Tokens = quote! ;
Becomes: