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
Create an analyzer additional file with the cdelta
extension. Your csproj file should contain something like this:
<ItemGroup>
<AdditionalFiles Include="MyAutomaton.cdelta" />
</ItemGroup>
namespace
for the class[modifier] [partial] automaton {Identifier}[<{Type}>]
modifier
the access modifier (optional, defaults tointernal
)partial
generates a partial class (optional){Identifier}
the name of your class<{Type}>
the input type for the automaton (optional, defaults toobject
)
[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 stateenter
generate a virtual state enter method (optional)exit
generate a virtual state exit method (optional)
transition {source} {target};
ortransition {source} {target} { [enter;] }
{source}
the source state{target}
the target stateenter
generate a virtual transition enter method (optional)
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; }
}
}
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);
}
var machine = new LowerCamelCase();
foreach (var c in "inputToTest")
machine.Invoke(c);
Assert.IsTrue(machine.IsAcceptingState);