public override object VisitPrimitiveExpression(PrimitiveExpression primitiveExpression, object data)
 {
     return new CodePrimitiveExpression(primitiveExpression.Value);
 }
 public override sealed object VisitPrimitiveExpression(PrimitiveExpression primitiveExpression, object data)
 {
     this.BeginVisit(primitiveExpression);
     object result = this.TrackedVisitPrimitiveExpression(primitiveExpression, data);
     this.EndVisit(primitiveExpression);
     return result;
 }
 public virtual object TrackedVisitPrimitiveExpression(PrimitiveExpression primitiveExpression, object data)
 {
     return base.VisitPrimitiveExpression(primitiveExpression, data);
 }
 public virtual object VisitPrimitiveExpression(PrimitiveExpression primitiveExpression, object data)
 {
     Debug.Assert((primitiveExpression != null));
     return null;
 }
 public virtual object VisitPrimitiveExpression(PrimitiveExpression primitiveExpression, object data)
 {
     throw new global::System.NotImplementedException("PrimitiveExpression");
 }
示例#6
0
        /// <summary>
        /// Returns the existing expression plus the specified integer value.
        /// The old <paramref name="expr"/> object is not modified, but might be a subobject on the new expression
        /// (and thus its parent property is modified).
        /// </summary>
        public static Expression AddInteger(Expression expr, int value)
        {
            PrimitiveExpression pe = expr as PrimitiveExpression;

            if (pe != null && pe.Value is int)
            {
                int newVal = (int)pe.Value + value;
                return(new PrimitiveExpression(newVal, newVal.ToString(System.Globalization.NumberFormatInfo.InvariantInfo)));
            }
            BinaryOperatorExpression boe = expr as BinaryOperatorExpression;

            if (boe != null && boe.Op == BinaryOperatorType.Add)
            {
                // clone boe:
                boe = new BinaryOperatorExpression(boe.Left, boe.Op, boe.Right);

                boe.Right = AddInteger(boe.Right, value);
                if (boe.Right is PrimitiveExpression && ((PrimitiveExpression)boe.Right).Value is int)
                {
                    int newVal = (int)((PrimitiveExpression)boe.Right).Value;
                    if (newVal == 0)
                    {
                        return(boe.Left);
                    }
                    else if (newVal < 0)
                    {
                        ((PrimitiveExpression)boe.Right).Value = -newVal;
                        boe.Op = BinaryOperatorType.Subtract;
                    }
                }
                return(boe);
            }
            if (boe != null && boe.Op == BinaryOperatorType.Subtract)
            {
                pe = boe.Right as PrimitiveExpression;
                if (pe != null && pe.Value is int)
                {
                    int newVal = (int)pe.Value - value;
                    if (newVal == 0)
                    {
                        return(boe.Left);
                    }

                    // clone boe:
                    boe = new BinaryOperatorExpression(boe.Left, boe.Op, boe.Right);

                    if (newVal < 0)
                    {
                        newVal = -newVal;
                        boe.Op = BinaryOperatorType.Add;
                    }
                    boe.Right = new PrimitiveExpression(newVal, newVal.ToString(System.Globalization.NumberFormatInfo.InvariantInfo));
                    return(boe);
                }
            }
            BinaryOperatorType opType = BinaryOperatorType.Add;

            if (value < 0)
            {
                value  = -value;
                opType = BinaryOperatorType.Subtract;
            }
            return(new BinaryOperatorExpression(expr, opType, new PrimitiveExpression(value, value.ToString(System.Globalization.NumberFormatInfo.InvariantInfo))));
        }
示例#7
0
        void SimpleNonInvocationExpression(out Expression pexpr)
        {
            Expression expr;
            CollectionInitializerExpression cie;
            TypeReference type = null;
            string name = String.Empty;
            Location startLocation = la.Location;
            pexpr = null;

            if (StartOf(34)) {
            switch (la.kind) {
            case 3: {
                Get();
                pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat };
                break;
            }
            case 4: {
                Get();
                pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat };
                break;
            }
            case 7: {
                Get();
                pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat };
                break;
            }
            case 6: {
                Get();
                pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat };
                break;
            }
            case 5: {
                Get();
                pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat };
                break;
            }
            case 9: {
                Get();
                pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat };
                break;
            }
            case 8: {
                Get();
                pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat };
                break;
            }
            case 217: {
                Get();
                pexpr = new PrimitiveExpression(true, "true");
                break;
            }
            case 122: {
                Get();
                pexpr = new PrimitiveExpression(false, "false");
                break;
            }
            case 165: {
                Get();
                pexpr = new PrimitiveExpression(null, "null");
                break;
            }
            case 37: {
                Get();
                Expr(out expr);
                Expect(38);
                pexpr = new ParenthesizedExpression(expr);
                break;
            }
            case 2: case 58: case 62: case 64: case 65: case 66: case 67: case 70: case 87: case 98: case 104: case 107: case 116: case 121: case 126: case 133: case 139: case 143: case 146: case 147: case 170: case 176: case 178: case 184: case 203: case 212: case 213: case 223: case 224: case 230: {
                Identifier();
                pexpr = new IdentifierExpression(t.val);
                    pexpr.StartLocation = t.Location; pexpr.EndLocation = t.EndLocation;

                if (la.kind == Tokens.OpenParenthesis && Peek(1).kind == Tokens.Of) {
                    Expect(37);
                    Expect(169);
                    TypeArgumentList(((IdentifierExpression)pexpr).TypeArguments);
                    Expect(38);
                }
                break;
            }
            case 68: case 71: case 82: case 99: case 100: case 109: case 141: case 151: case 168: case 196: case 201: case 202: case 208: case 221: case 222: case 225: {
                string val = String.Empty;
                if (StartOf(13)) {
                    PrimitiveTypeName(out val);
                } else {
                    Get();
                    val = "System.Object";
                }
                pexpr = new TypeReferenceExpression(new TypeReference(val, true));
                break;
            }
            case 153: {
                Get();
                pexpr = new ThisReferenceExpression();
                break;
            }
            case 158: case 159: {
                Expression retExpr = null;
                if (la.kind == 158) {
                    Get();
                    retExpr = new BaseReferenceExpression() { StartLocation = t.Location, EndLocation = t.EndLocation };
                } else {
                    Get();
                    retExpr = new ClassReferenceExpression() { StartLocation = t.Location, EndLocation = t.EndLocation };
                }
                Expect(26);
                IdentifierOrKeyword(out name);
                pexpr = new MemberReferenceExpression(retExpr, name) { StartLocation = startLocation, EndLocation = t.EndLocation };
                break;
            }
            case 130: {
                Get();
                Expect(26);
                Identifier();
                type = new TypeReference(t.val ?? "");
                type.IsGlobal = true;
                pexpr = new TypeReferenceExpression(type);
                break;
            }
            case 162: {
                ObjectCreateExpression(out expr);
                pexpr = expr;
                break;
            }
            case 35: {
                CollectionInitializer(out cie);
                pexpr = cie;
                break;
            }
            case 94: case 106: case 219: {
                CastType castType = CastType.Cast;
                if (la.kind == 106) {
                    Get();
                } else if (la.kind == 94) {
                    Get();
                    castType = CastType.Conversion;
                } else {
                    Get();
                    castType = CastType.TryCast;
                }
                Expect(37);
                Expr(out expr);
                Expect(22);
                TypeName(out type);
                Expect(38);
                pexpr = new CastExpression(type, expr, castType);
                break;
            }
            case 76: case 77: case 78: case 79: case 80: case 81: case 83: case 85: case 86: case 90: case 91: case 92: case 93: case 95: case 96: case 97: {
                CastTarget(out type);
                Expect(37);
                Expr(out expr);
                Expect(38);
                pexpr = new CastExpression(type, expr, CastType.PrimitiveConversion);
                break;
            }
            case 57: {
                Get();
                SimpleExpr(out expr);
                pexpr = new AddressOfExpression(expr);
                break;
            }
            case 129: {
                Get();
                Expect(37);
                GetTypeTypeName(out type);
                Expect(38);
                pexpr = new TypeOfExpression(type);
                break;
            }
            case 220: {
                Get();
                SimpleExpr(out expr);
                Expect(144);
                TypeName(out type);
                pexpr = new TypeOfIsExpression(expr, type);
                break;
            }
            case 135: {
                ConditionalExpression(out pexpr);
                break;
            }
            case 10: case 16: case 17: case 18: case 19: {
                XmlLiteralExpression(out pexpr);
                break;
            }
            }
            } else if (StartOf(35)) {
            if (la.kind == 26) {
                Get();
                if (la.kind == 10) {
                    Get();
                    IdentifierOrKeyword(out name);
                    Expect(11);
                    pexpr = new XmlMemberAccessExpression(null, XmlAxisType.Element, name, true) { StartLocation = startLocation, EndLocation = t.EndLocation };
                } else if (StartOf(33)) {
                    IdentifierOrKeyword(out name);
                    pexpr = new MemberReferenceExpression(null, name) { StartLocation = startLocation, EndLocation = t.EndLocation };
                } else SynErr(281);
            } else if (la.kind == 29) {
                Get();
                IdentifierOrKeyword(out name);
                pexpr = new BinaryOperatorExpression(null, BinaryOperatorType.DictionaryAccess, new PrimitiveExpression(name, name) { StartLocation = t.Location, EndLocation = t.EndLocation });
            } else {
                XmlAxisType axisType = XmlAxisType.Element; bool isXmlIdentifier = false;
                if (la.kind == 27) {
                    Get();
                    axisType = XmlAxisType.Descendents;
                } else {
                    Get();
                    axisType = XmlAxisType.Attribute;
                }
                if (la.kind == 10) {
                    Get();
                    isXmlIdentifier = true;
                }
                IdentifierOrKeyword(out name);
                if (la.kind == 11) {
                    Get();
                }
                pexpr = new XmlMemberAccessExpression(null, axisType, name, isXmlIdentifier);
            }
            } else SynErr(282);
            if (pexpr != null) {
                pexpr.StartLocation = startLocation;
                pexpr.EndLocation = t.EndLocation;
            }
        }
 public void TestPrimitiveExpression()
 {
     object output = new PrimitiveExpression(5, "5").AcceptVisitor(new CodeDomVisitor(), null);
     Assert.IsTrue(output is CodePrimitiveExpression);
     Assert.AreEqual(((CodePrimitiveExpression)output).Value, 5);
 }