Skip to content

Example Rust

BAL-Birdt edited this page Jul 13, 2026 · 2 revisions

Example: Rust

This page demonstrates a Crodox grammar definition for Rust (.rs) files, covering functions, structs, enums, traits, impl blocks, control flow, and example patch conditions for re-integration.

Work in progress: This grammar and the patch definitions below are not complete. They are a starting point meant to be extended and adapted to your project.


Full Definition

<~"FROM".rs~>

    <*comment_line*> // -->> \n <*>
    <*comment_block*> /* -->> */ <*>

    <*()*> ( <---> ) <*>
    <*[]*> [ <---> ] <*>
    <*{}*> { <---> } <*>

    <:use:>
        use <<"FROM">> <| :: { <? <<name:up>> <| , <||> |> ?> } <||> :: <<name:up>> <||> :: * |> ;
    <:>

    <:mod:>
        <| pub <||> pub ( crate ) <||> pub ( super ) <||> |>
        mod <<name:up>> <| ; <||> { <---> } |>
    <:>

    <:attribute:>
        # <| ! <||> |> [ -->> ] \n
    <:>

    <:let:>
        let <| mut <||> |> <<name>> <| : <<'name'>> <||> |> = -->> ;
    <:>

    <:const:>
        <| pub <||> pub ( crate ) <||> |>
        const <<name:up>> : <<'name'>> = -->> ;
    <:>

    <:static:>
        <| pub <||> pub ( crate ) <||> |>
        <| static mut <||> static |> <<name:up>> : <<'name'>> = -->> ;
    <:>

    <:param:>
        <| & <| mut <||> |> <||> |> <<name>> <| : <<'name'>> <||> |> <| , <||> ) |>
    <:>

    <:function:>
        <| pub <||> pub ( crate ) <||> pub ( super ) <||> |>
        <| async <||> |>
        <| unsafe <||> |>
        fn <<name:up>> ( <? <-{param}-> ?> )
        <| -> <<'name'>> <||> |>
        <| ; <||> { <---> } |>
    <:>

    <:field:>
        <| pub <||> pub ( crate ) <||> |>
        <<name>> : <<'name'>> <| , <||> \n |>
    <:>

    <:struct:>
        <| pub <||> pub ( crate ) <||> |>
        struct <<name:up>>
        <| { <? <-{field}-> ?> } <||> ( <? <<'name'>> <| , <||> |> ?> ) ; <||> ; |>
    <:>

    <:variant:>
        <<name>> <| { <? <-{field}-> ?> } <||> ( <? <<'name'>> <| , <||> |> ?> ) <||> |> <| , <||> \n |>
    <:>

    <:enum:>
        <| pub <||> pub ( crate ) <||> |>
        enum <<name:up>> { <? <-{variant}-> ?> }
    <:>

    <:trait:>
        <| pub <||> pub ( crate ) <||> |>
        <| unsafe <||> |>
        trait <<name:up>> { <---> }
    <:>

    <:impl:>
        <| unsafe <||> |>
        impl <| <<'name'>> for <||> |> <<'name'>> { <---> }
    <:>

    <:match:>
        match -->> { <---> }
    <:>

    <:if:>
        <| if let <||> if |> -->> { <---> }
        <| else { <---> } <||> else if -->> { <---> } <||> |>
    <:>

    <:loop:>
        loop { <---> }
    <:>

    <:while:>
        <| while let <||> while |> -->> { <---> }
    <:>

    <:for:>
        for <<name>> in -->> { <---> }
    <:>

    <:return:>
        return <| -->> ; <||> ; |>
    <:>

    <:macro_call:>
        <<'name'>> ! <| ( -->> ) <||> [ -->> ] <||> { -->> } |> <| ; <||> \n |>
    <:>

<~>

Breakdown

Structural Objects (Hidden)

Object Description
comment_line Single-line comments (// ... \n) using Jump
comment_block Block comments (/* ... */) using Jump
(), [], {} Structural delimiters for calls, arrays/slices, and blocks

Imports and Modules

Object Description
use use path::Item with optional { multi, import } or ::* glob. Uses path variable <<"FROM">> for cross-file dependency and :up scope so imported names are visible to siblings
mod Module declaration, either as a file reference (mod name;) or an inline block (mod name { ... }). Uses :up scope
attribute #[...] or #![...] outer/inner attributes using Jump

Declarations

Object Description
let Local variable binding with optional mut, type annotation, and initializer
const Compile-time constant with type annotation. Uses :up scope
static Static item, optionally mut, with type annotation

Functions

Object Description
param Function parameter: optional reference (& / &mut), name, and optional type annotation. Used as a Reference inside function
function fn name(params) -> ReturnType { ... } with optional visibility, async, unsafe. Uses :up scope and <-{param}-> typed sub-body for parameters

Types

Object Description
field A struct or enum field: optional visibility, name, type. Used via <-{field}-> in struct and variant
struct Struct definition: named struct with fields, tuple struct, or unit struct
variant Enum variant: unit, tuple, or struct form. Used via <-{variant}-> in enum
enum Enum definition with one or more variants
trait Trait definition. Method signatures inside are matched by function via sub-body
impl Impl block, optionally implementing a trait (impl Trait for Type)

Control Flow

Object Description
match match expr { ... } - the body is a plain sub-body; individual arms are not captured separately
if if or if let with optional else / else if chain
loop Infinite loop { ... } block
while while or while let loop
for for item in iterable { ... } - binds the loop variable with <<name>>
return return value; or bare return;

Macros

Object Description
macro_call Invocation of any macro: name!(...), name![...], or name!{ ... }. Uses reference variable <<'name'>> to link to the macro definition

Patch Conditions

The following conditions are a starting point for the Patch Maker. They assume a standard Rust project layout (src/lib.rs, src/main.rs, individual module files).

Work in progress: These patch conditions and their mod content are not complete. They are examples to build on.


title==="mod"

Triggered when a mod declaration is extracted.

Suggested mod - Target: src/lib.rs

pub mod <-name.0->;

Registers the new module in the crate root so it is visible to consumers.


title==="struct"

Triggered when a struct definition is extracted.

Suggested mod - Target: src/lib.rs

pub use <-name.0->::<-name.0->;

Re-exports the struct from the crate root. Adjust the path to match your module structure.


title==="enum"

Triggered when an enum definition is extracted.

Suggested mod - Target: src/lib.rs

pub use <-name.0->::<-name.0->;

Same re-export pattern as for structs.


title==="trait"

Triggered when a trait definition is extracted.

Suggested mod - Target: src/lib.rs

pub use <-name.0->::<-name.0->;

Makes the trait accessible at the crate root.


title==="function"

Triggered when a top-level fn is extracted.

Suggested mod - Target: src/lib.rs

pub use <-name.0->::<-name.0->;

Re-exports the function from the module it was extracted from.


Notes

  • Generics (<T>, <T: Trait>) are not yet matched explicitly. They are consumed by the sub-body (<--->) of the enclosing object.
  • Lifetime annotations ('a) are treated as part of surrounding token sequences and are not captured as standalone variables yet.
  • Derive macros (#[derive(Debug, Clone)]) are matched by the attribute object.
  • The impl object captures both inherent impls and trait impls. The condition <<'name'>> for distinguishes them at runtime.

Clone this wiki locally