object IAstVisitor.Visit(LiteralValue literalValue)
 {
     return Visit(literalValue);
 }
        protected virtual object Visit(LiteralValue literalValue)
        {
            object ret;
            switch (literalValue.ValueType)
            {
                case LiteralValueType.Numeric:
                    {
                        sbyte signedByte;
                        byte usignedByte;
                        short signedShort;
                        ushort unsignedShort;
                        int signedInt;
                        uint unsignedInt;
                        long signedLong;
                        ulong unsignedLong;
                        Single single;
                        Double dble;
                        Decimal dec;
                        BigInteger bigInt;

                        if (byte.TryParse(literalValue.Value, NumberStyles.Any, this._culture, out usignedByte))
                            ret = usignedByte;
                        else if (sbyte.TryParse(literalValue.Value, NumberStyles.Any, this._culture, out signedByte))
                            ret = signedByte;
                        else if (short.TryParse(literalValue.Value, NumberStyles.Any, this._culture, out signedShort))
                            ret = signedShort;
                        else if (ushort.TryParse(literalValue.Value, NumberStyles.Any, this._culture, out unsignedShort))
                            ret = unsignedShort;
                        else if (int.TryParse(literalValue.Value, NumberStyles.Any, this._culture, out signedInt))
                            ret = signedInt;
                        else if (uint.TryParse(literalValue.Value, NumberStyles.Any, this._culture, out unsignedInt))
                            ret = unsignedInt;
                        else if (long.TryParse(literalValue.Value, NumberStyles.Any, this._culture, out signedLong))
                            ret = signedLong;
                        else if (ulong.TryParse(literalValue.Value, NumberStyles.Any, this._culture, out unsignedLong))
                            ret = unsignedLong;
                        else if (Single.TryParse(literalValue.Value, NumberStyles.Any, this._culture, out single))
                            ret = single;
                        else if (Double.TryParse(literalValue.Value, NumberStyles.Any, this._culture, out dble))
                            ret = dble;
                        else if (Decimal.TryParse(literalValue.Value, NumberStyles.Any, this._culture, out dec))
                            ret = dec;
                        else if (BigInteger.TryParse(literalValue.Value, NumberStyles.Any, this._culture, out bigInt))
                            ret = bigInt;
                        else
                            ret = literalValue.Value;

                        IEnumerable<Exception> exceptions;
                        if (!this._typeConverter.TryConvertType(this._culture, this._targetType.Peek(), ref ret, out exceptions))
                        {
                            this.RaiseError(new TypeError(this._targetType.Peek(), ret, literalValue, exceptions));
                        }
                    }

                    break;
                case LiteralValueType.String:
                    ret = literalValue.Value;
                    if (this._targetType.Peek() != typeof (string))
                    {
                        IEnumerable<Exception> exceptions;
                        if (!this._typeConverter.TryConvertType(this._culture, this._targetType.Peek(), ref ret, out exceptions))
                        {
                            this.RaiseError(new TypeError(this._targetType.Peek(), literalValue.Value, literalValue,
                                                          exceptions));
                        }
                    }
                    break;
                case LiteralValueType.Null:
                    ret = null;
                    break;
                case LiteralValueType.Boolean:
                    ret = StringComparer.InvariantCultureIgnoreCase.Equals(literalValue.Value, "$true");
                    if (this._targetType.Peek() != typeof (bool))
                    {
                        IEnumerable<Exception> exceptions;
                        if (!this._typeConverter.TryConvertType(this._culture, this._targetType.Peek(), ref ret, out exceptions))
                        {
                            this.RaiseError(new TypeError(this._targetType.Peek(), ret, literalValue,
                                                          exceptions));
                        }
                    }
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            return ret;
        }
Пример #3
0
        protected virtual AstNode GetAstNode(IEnumerator<Token> enumerator)
        {
            AstNode ret;
            switch (enumerator.Current.Type)
            {
                case TokenType.ParameterName:
                    ret = new ParameterName(enumerator.Current,
                                            enumerator.Current.Value);
                    enumerator.MoveNext();
                    break;
                case TokenType.SwitchParameter:
                    Token token = enumerator.Current;
                    enumerator.MoveNext();
                    ret = new SwitchParameter(token,
                                              token.Value,
                                              this.GetAstNode(enumerator));
                    break;
                case TokenType.NumericValue:
                    ret = new LiteralValue(
                                            enumerator.Current,
                                            LiteralValueType.Numeric,
                                            enumerator.Current.Value);
                    enumerator.MoveNext();
                    break;
                case TokenType.StringValue:
                    ret = new LiteralValue(
                                            enumerator.Current,
                                            LiteralValueType.String,
                                            enumerator.Current.Value);
                    enumerator.MoveNext();
                    break;
                case TokenType.BoolValue:
                    ret = new LiteralValue(
                                            enumerator.Current,
                                            LiteralValueType.Boolean,
                                            enumerator.Current.Value);
                    enumerator.MoveNext();
                    break;
                case TokenType.NullValue:
                    ret = new LiteralValue(
                                            enumerator.Current,
                                            LiteralValueType.Null,
                                            enumerator.Current.Value);
                    enumerator.MoveNext();
                    break;
                case TokenType.ListStart:
                    ret = this.GetSequence(enumerator);
                    break;
                case TokenType.DictionaryStart:
                    ret = this.GetAssocArray(enumerator);
                    break;
                //case TokenType.DictionaryValueSeperator:
                //case TokenType.DictionaryKeySeperator:
                case TokenType.DictionaryEnd:
                //case TokenType.ListValueSeperator:
                case TokenType.ListEnd:
                    throw new Exception("Invalid token sequence.");

                default:
                    throw new ArgumentOutOfRangeException();
            }

            if (enumerator.Current.Type == TokenType.ListValueSeperator)
            {
                enumerator.MoveNext();

                AstNode obj = this.GetAstNode(enumerator);
                if (obj is Sequence)
                {
                    var list = new List<AstNode>(((Sequence) obj).Elements);
                    list.Add(ret);
                    ret = new Sequence(ret.SourceInfo, list);
                }
                else
                {
                    ret = new Sequence(ret.SourceInfo, new[] {ret, obj});
                }
            }

            return ret;
        }