Skip to content

Example CSharp Archived

Benedict Albrecht edited this page Jun 18, 2026 · 1 revision

Example: C# (Archived)

This page demonstrates a Crodox grammar definition for C# projects, covering .cs files with classes, structs, enums, fields, methods, and common control flow patterns.

This example is archived and kept for reference.

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


Full Definition

<~"FILE".cs~>

    <:bracing1:> { <---> } <:>
    <:bracing2:> ( <---> ) <:>
    <:bracing3:> [ <---> ] <:>

    <:string1:> <| $ <||> |> ' -->> ' <:>
    <:string2:> <| $ <||> |> " -->> " <:>
    <:string3:> <| $ <||> |> ' -->> ' <:>

    <:using_systemA:> using System <? . <<'namespace'>> ?> ; <:>
    <:using_systemB:> using Microsoft <? . <<'namespace'>> ?> ; <:>
    <:using_statement:> using <| <<namespace:@:dot>> ; <||> ( <---> ) { <---> } |> <:>

    <:namespace:> namespace <<namespace:#:dot>> ; <:>
    <:comment_line:> // -->> \n <:>
    <:comment_block:> /* -->> */ <:>

    <:def:>
        <| string <||> int <||> <<'name'>> |>
        <| < <---> > <||> |>
        <<'name'>> <| ( <---> ) <||> |> <| , <||> ; <||> |>
    <:>

    <:statment:>
        <<'name'>> = <<'name'>> <| ; <||> |>
    <:>

    <:struct:>
        <| private <||> public |>
        <? <| readonly <||> partial <||> ref |> ?>
        struct <<name:up>> { <---> }
    <:>

    <:class_ext:>
        <? [ <---> ] ?>
        <| private <||> public |>
        <? <| protected <||> internal <||> file <||> unsafe <||> static <||> partial <||> abstract <||> sealed <||> readonly <||> ref |> ?>
        <| class <||> record <||> interface |>
        <| struct <||> |>
        <<name:up>>
        <? <| readonly <||> struct |> ?>
        <| < <---> > <||> |>
        <| <||> ( <---> ) |>
        <| : <? <<name:§:dot>> <| < <---> > <||> |> <| , <||> |> ?> <||> |>
        <?
            where <<name:§:dot>> : <? <| class <||> struct <||> new <||> <<name:§:dot>> |> <| ( <---> ) <||> |> <|, <||> |> ?>
        ?>
        <| { <-comment_line, comment_block, field_ext, enum_definition, indexer, generics_1-> } <||> ; |>
    <:>

    <:enum_definition:>
        <| public <||> protected <||> internal <||> private <||> |>
        <| partial <||> |>
        <| abstract <||> |>
        <| sealed <||> |>
        enum <<name:up>> { <? <<'name'>> <| , <||> |> ?> }
    <:>

    <:indexer:>
        public
        <| int <||> string <||> bool <||> double |>
        <| [ <| <? , ?> <||> |> ] <||> |>
        this [
            <? <| int <||> string <||> bool <||> double <||> long |> <<name:§:dot>> <| , <||> |> ?>
        ] {
            get { <---> }
            <| set { <---> } <||> |>
        }
    <:>

    <:ref:>
        <| await <||> |>
        <<name:§:dot>>
        <| < <---> > <||> |>
        <| ( <---> ) <? . <<'name'>> <| < <---> > <||> |> <| ( <---> ) <||> |> ?> <||> |>
        <| <||> switch { <---> } |>
        <| , <||> ; <||> \n <||> |>
    <:>

    <:generics_1:>
        <| [ <---> ] <||> |>
        <| public <||> private |> <| <<name:§:dot>> <||> |> <<'name'>> ( <---> )
        <| <||> \n |>
        { <---> }
    <:>

    <:generics_2:>
        <| public <||> private <||> |>
        <| <<name:§:dot>> <||> |> <<name:up>> <| ( <---> ) <||> |> <| ; <||> \n |>
    <:>

    <:var:>
        <| await <||> |>
        <| using <||> |>
        <| var <||> const <||> <<name:§:dot>> <||> |>
        <<'name'>> <| ( <---> ) <||> |> = <| new <||> |> <-{bracing1, bracing2, bracing3, string1, string2, string3, ref}-> <| ; <||> \n <||> |>
    <:>

    <:field_ext:>
        <? [ <---> ] ?>
        <| private <||> public |>
        <? <| internal <||> protected <||> static <||> abstract <||> ref <||> readonly <||> volatile <||> const <||> required <||> virtual <||> override <||> sealed <||> new <||> async <||> partial |> ?>
        <| int <||> string <||> bool <||> double <||> void <||> <<name:§:dot>> |>
        <| < <---> > <||> |>
        <| ? <||> |>
        <| [ <| <? , ?> <||> |> ] <||> |>
        <<name:up>>
        <| <||> ( <---> ) |>
        <| <||> { <-comment_line, comment_block, field_ext, enum_definition, indexer-> } |>
        <| <| = <||> => |> <-{bracing1, bracing2, bracing3, string1, string2, string3, ref, var}-> <||> ; <||> |>
    <:>

    <:throw:>
        throw <| <||> new |> <-{var, ref}->
    <:>

    <:if:>
        if ( <---> ) <-{var}->
    <:>

    <:switch:>
        switch ( <---> )
        {
            <? case <-{string1, string2, string3}-> :
                <--->
            ?>
            default :
                <--->
        }
    <:>

    <:return:>
        return <-{var, ref}->
    <:>

    <:try:>
        try { <---> } catch ( <---> ) { <---> }
    <:>

<~>

Breakdown

File Definition

Element Description
<~"FILE".cs~> Matches all .cs files. The path variable "FILE" enables cross-file linking.

Structural Helpers

Object Type Description
bracing1 Visible Matches { ... } blocks
bracing2 Visible Matches ( ... ) parentheses
bracing3 Visible Matches [ ... ] brackets
string1 Visible Single-quoted strings, with optional $ interpolation prefix
string2 Visible Double-quoted strings, with optional $ interpolation prefix
string3 Visible Single-quoted strings (alternate)
comment_line Visible Single-line comment // using Jump
comment_block Visible Block comment /* */ using Jump

Using and Namespace

Object Description
using_systemA using System.X.Y; references System namespaces via <<'namespace'>>
using_systemB using Microsoft.X.Y; references Microsoft namespaces
using_statement General using import or using (...) resource disposal block
namespace File-scoped namespace declaration with <<namespace:#:dot>>

Type Definitions

Object Description
class_ext Classes, records, interfaces, and structs with modifier support, generics, inheritance, and constraints
struct Struct definition with access modifier and optional readonly / partial / ref
enum_definition Enum with access modifiers and repeated enum members via <<'name'>>

Members

Object Description
field_ext Fields, properties, and methods with attributes, modifiers, body, and initializer support
generics_1 Public or private methods with optional attributes and body
generics_2 Short member declarations
indexer Indexer property this[...] with get/set accessors

Expressions and Statements

Object Description
def Simple definitions: type + name with optional generics and parameters
statment Assignment name = name
ref Method calls and member access chains with optional await, generics, fluent calls, and switch expressions
var Variable declarations with var, const, or typed variants

Control Flow

Object Description
if If statement with condition and variable sub-body
switch Switch statement with repeated case branches and default
try Try/catch block
throw Throw statement with optional new
return Return statement with variable or reference sub-body

See also: Angular Example - Python Example - T-SQL Example - Syntax Overview

Clone this wiki locally