Skip to content

Example Basics

Benedict Albrecht edited this page Jun 2, 2026 · 5 revisions

Example: Basics

A minimal Crodox grammar using pseudo-code with .ts files. This example is designed as a simple starting point to learn the core concepts before moving to real-world grammars.

This is not real TypeScript - the .ts extension is used as a placeholder. A matching test repository is available for parsing.

Work in progress: This grammar is not complete. It is a starting point meant to be extended and adapted to your needs.


Full Definition

<~"FROM".ts~>

    <:import:>
        import <| <<name:up>> <||> { <<name:up>> } |> from <<"FROM".ts>> <| ; <||> \n |>
    <:>

    <:class:>
        class <<NAME:up>> ( <? <| , <||> <<NAME:down>> |> ?> ) { <-function, class, refference, variable-> }
    <:>

    <:function:>
        function <<NAME:up>> ( <? <| , <||> <<NAME:down>> |> ?> ) { <-function_-> }
    <:>

    <:variable:>
        let <<NAME>> <? = <<'name'>> ?> ;
    <:>

    <:refference:>
        <<'name'>> . <<![class|function]name!>> ( <? <| , <||> |> <<'name'>> ?> ) ;
    <:>

<~>

Breakdown

File Definition

Element Description
<~"FROM".ts~> Matches all .ts files. "FROM" is the path variable for cross-file linking.

Objects

Object Description
import Imports a name or destructured name from another file. Uses :up scope so the imported name is visible to all siblings. <<"FROM".ts>> creates a path dependency to the referenced file.
class Class with an uppercase variable <<NAME:up>> (overwrites, no dependency). Parameters use <<NAME:down>> to pass names into the class body. Body allows nested functions, classes, references, and variables.
function Function with <<NAME:up>>. Parameters with <<NAME:down>>. Body uses <-function_-> - the underscore means it accepts any object type as children (wildcard sub-body).
variable let declaration. Optional initializer = <<'name'>> uses a reference variable - creates a dependency without overwriting.
refference Method call on an object. <<![class|function]name!>> uses dependency selection - only links to objects of type class or function. Parameters repeat with <<'name'>> references.

Concepts Demonstrated

Concept Where What it does
Path variable <<"FROM".ts>> import Links files to each other
:up scope import, class, function Makes names visible to parent and older siblings
:down scope class, function Passes names into child objects (parameters)
Uppercase variable <<NAME>> class, function, variable Overwrites the value, does not create a dependency
Reference variable <<'name'>> variable, refference Creates a dependency, does not overwrite
Dependency selection <<![class|function]name!>> refference Limits which object types the reference can link to
Or statement <| ... <||> ... |> import, class, function Alternative patterns (e.g., direct import vs destructured)
Repeat <? ?> class, function, refference Repeats parameters separated by commas
Sub-body <-...-> class Named sub-body - only listed object types allowed as children
Wildcard sub-body <-name_-> function Underscore suffix - accepts any object type

See also: Angular Example - C# Example - Python Example - Syntax Overview

Clone this wiki locally