Skip to content

A C# source generator for finite-state machines ‐ easily referenced as a Roslyn analyzer.

License

Notifications You must be signed in to change notification settings

sungaila/Cdelta

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cdelta Logo

Cdelta

Azure DevOps builds (branch) Azure DevOps tests (branch) SonarCloud Quality Gate NuGet version NuGet downloads GitHub license

A C# source generator for finite-state machines ‐ easily referenced as a Roslyn analyzer.

Looking for the master's thesis C𝛿: Design and Implementation of a Transcompiler for Language Integrated Finite-State Machines in C♯? Look here: https://github.com/sungaila/CdeltaLegacy

Quick start

Step 1: Create *.cdelta text files

Create an analyzer additional file with the cdelta extension. Your csproj file should contain something like this:

<ItemGroup>
    <AdditionalFiles Include="MyAutomaton.cdelta" />
</ItemGroup>

Step 2: Define the automaton

  • namespace for the class
  • [modifier] [partial] automaton {Identifier}[<{Type}>]
    • modifier the access modifier (optional, defaults to internal)
    • partial generates a partial class (optional)
    • {Identifier} the name of your class
    • <{Type}> the input type for the automaton (optional, defaults to object)
  • [start] [end] state {Identifier}; or
  • [start] [end] state {Identifier} { [enter;] [exit;] }
    • start is initial state (used exactly once)
    • end is accepting state (used once at least)
    • {Identifier} the name of this state
    • enter generate a virtual state enter method (optional)
    • exit generate a virtual state exit method (optional)
  • transition {source} {target}; or
  • transition {source} {target} { [enter;] }
    • {source} the source state
    • {target} the target state
    • enter generate a virtual transition enter method (optional)

Example

namespace Cdelta.Tests
{
    internal automaton LowerCamelCaseMachine<char>
    {
        // available states
        start state Init;
        state UpperChar { enter; }
        end state LowerChar { enter; exit; }

        // available transitions
        transition Init LowerChar;
        transition LowerChar LowerChar;
        transition LowerChar UpperChar;
        transition UpperChar LowerChar { enter; }
    }
}

Step 3: Inherit and implement the automaton

class LowerCamelCase : LowerCamelCaseMachine
{
    protected override bool CanTransition_Init_LowerChar(char value) => char.IsLower(value);

    protected override bool CanTransition_LowerChar_LowerChar(char value) => char.IsLower(value);

    protected override bool CanTransition_LowerChar_UpperChar(char value) => char.IsUpper(value);

    protected override bool CanTransition_UpperChar_LowerChar(char value) => char.IsLower(value);
}

Step 4: Enjoy!

var machine = new LowerCamelCase();

foreach (var c in "inputToTest")
    machine.Invoke(c);

Assert.IsTrue(machine.IsAcceptingState);

About

A C# source generator for finite-state machines ‐ easily referenced as a Roslyn analyzer.

Resources

License

Stars

Watchers

Forks

Packages