Esempio n. 1
0
        public StateMachine CreateMachine()
        {
            Log(LogEventLevel.Information, "Configuring the state machine");
            var machine = new StateMachine(LexerState.Initial);

            machine.OnUnhandledTrigger((state, symbol) => ReturnToken(CurrentToken, symbol));
            machine.OnTransition(transition =>
            {
                Log(LogEventLevel.Information, "Transition: {0} -> ({1}) -> {2}", true, transition.Source,
                    Regex.Escape(transition.Trigger.ToString()), transition.Destination);
                CurrentToken.Append((char)transition.Trigger.Value);
            });

            machine.Configure(LexerState.Initial)
            .Permit(Symbol.Letter, LexerState.String)
            .Permit(Symbol.Digit, LexerState.Number)
            .Permit(Symbol.Operator, LexerState.Operator)
            .Permit(Symbol.Point, LexerState.Point)
            .Permit(Symbol.Less, LexerState.LessOperator)
            .Permit(Symbol.Greater, LexerState.GreaterOperator)
            .Permit(Symbol.Equal, LexerState.AssignmentOperator)
            .Permit(Symbol.Exclamation, LexerState.Not)
            .Permit(Symbol.Splitter, LexerState.Splitter)
            .Permit(Symbol.Comma, LexerState.Comma)
            .Permit(Symbol.Hypen, LexerState.Hypen)
            .Permit(Symbol.Colon, LexerState.Colon)
            .Ignore(Symbol.Space)
            .OnUnhandled(Error);

            machine.Configure(LexerState.String)
            .PermitReentry(Symbol.Letter)
            .PermitReentry(Symbol.Digit)
            .OnUnhandled(ReturnIdOrTokenOrLabel);

            machine.Configure(LexerState.Number)
            .PermitReentry(Symbol.Digit)
            .Permit(Symbol.Point, LexerState.NumberWithPoint)
            .OnUnhandled(ReturnConst);

            machine.Configure(LexerState.NumberWithPoint)
            .PermitReentry(Symbol.Digit)
            .OnUnhandled(ReturnConst);

            machine.Configure(LexerState.Point)
            .Permit(Symbol.Digit, LexerState.NumberWithPoint)
            .OnUnhandled(Error);

            machine.Configure(LexerState.LessOperator)
            .Permit(Symbol.Equal, LexerState.LessEqual);

            machine.Configure(LexerState.GreaterOperator)
            .Permit(Symbol.Equal, LexerState.GreaterEqual);

            machine.Configure(LexerState.AssignmentOperator)
            .Permit(Symbol.Equal, LexerState.EqualOperator);

            machine.Configure(LexerState.Not)
            .Permit(Symbol.Equal, LexerState.NotEqual);

            machine.Configure(LexerState.Splitter)
            .PermitReentry(Symbol.Splitter);

            return(machine);
        }