/// <summary> /// Compiles state into <see cref="SwitchCase"/> expression. /// </summary> /// <param name="context">Context with compilation info.</param> /// <returns>Compiled state.</returns> internal SwitchCase Compile(AutomatonBuilderContext context) { var stateBody = compileStateBody(context); stateBody = context.MakeVoid(stateBody); return Expression.SwitchCase(stateBody, Expression.Constant(Id)); }
internal AutomatonBuilder() { _context = new AutomatonBuilderContext(); _rootState = _context.CreateNewState(); _lastTargetState = _rootState; }
internal Expression BuildParser(AutomatonBuilderContext context) { return buildBodyParser(context); }
private Expression compileStateBody(AutomatonBuilderContext context) { if (_director != null) { //we can directly compile the action if (_targetState == null) //action without target state return _director(context); else return Expression.Block( _director(context), context.GoToState(_targetState) ); } //compile byte switch table var byteCases = new List<SwitchCase>(); foreach (var byteTargetPair in _byteTargets) { var gotoStateExpression = context.GoToState(byteTargetPair.Value); var byteTargetCase = Expression.SwitchCase(gotoStateExpression, Expression.Constant(byteTargetPair.Key)); byteCases.Add(byteTargetCase); } //compile byte action table foreach (var byteActionPair in _byteActions) { var actionExpression = byteActionPair.Value(context); var byteActionCase = Expression.SwitchCase(context.MakeVoid(actionExpression), Expression.Constant(byteActionPair.Key)); byteCases.Add(byteActionCase); } if (_defaultByteTarget == null) { return Expression.Switch(context.InputVariable, byteCases.ToArray()); } else { //we are in state with default target return Expression.Switch(context.InputVariable, context.GoToState(_defaultByteTarget), byteCases.ToArray()); } }
/// <summary> /// Template method which builds parser for the header. /// </summary> internal abstract Expression buildBodyParser(AutomatonBuilderContext context);