Пример #1
0
        private void ParseStateProperty(StateNode stateNode)
        {
            var stateNameIdentifier = (IdentifierToken) Eat(TokenType.Identifier);
            var stateName = stateNameIdentifier.Identifier;
            var propertyInfo = typeof(StateNode).GetProperty(stateName);
            if (propertyInfo == null)
                ReportError("Unrecognized identifier: {0}", stateNameIdentifier, stateName);

            Eat(TokenType.Equal);

            var propertyType = propertyInfo.PropertyType;
            var underlyingType = Nullable.GetUnderlyingType(propertyType);

            object value = null;
            switch (PeekType())
            {
                case TokenType.Identifier:
                    if (!underlyingType.IsEnum)
                        ReportError("Invalid value for {0}.", stateName);
                    var stateValueIdentifier = (IdentifierToken) Eat(TokenType.Identifier);
                    var stateValue = stateValueIdentifier.Identifier;
                    if (!underlyingType.GetEnumNames().Contains(stateValue))
                        ReportError("Unexpected value: {0}", stateValueIdentifier, stateValue);
                    value = Enum.Parse(underlyingType, stateValue);
                    break;
                case TokenType.Literal:
                    if (underlyingType != typeof(float))
                        ReportError("Invalid value for {0}.", stateName);
                    value = ((FloatToken) Eat(TokenType.Literal)).Value;
                    break;
                case TokenType.True:
                    if (underlyingType != typeof(bool))
                        ReportError("Invalid value for {0}.", stateName);
                    Eat(TokenType.True);
                    value = true;
                    break;
                case TokenType.False:
                    if (underlyingType != typeof(bool))
                        ReportError("Invalid value for {0}.", stateName);
                    Eat(TokenType.False);
                    value = false;
                    break;
                default:
                    ReportError("Unexpected value for {0}.", stateName);
                    break;
            }
            propertyInfo.SetValue(stateNode, value, null);

            Eat(TokenType.Semicolon);
        }
Пример #2
0
        private StateNode ParseState()
        {
            var result = new StateNode();

            Eat(TokenType.State);
            Eat(TokenType.OpenCurly);

            while (PeekType() != TokenType.CloseCurly)
                ParseStateProperty(result);

            Eat(TokenType.CloseCurly);

            return result;
        }