protected override MatchContext Match(Ust ust, MatchContext context)
        {
            var memberRef = ust as MemberReferenceExpression;

            if (memberRef == null)
            {
                return(context.Fail());
            }

            MatchContext newContext = Target.MatchUst(memberRef.Target, context);

            if (!newContext.Success)
            {
                // Ugly hack to handle optional `this`
                if (Target is PatternMemberReferenceExpression patternMemberReference && patternMemberReference.Target is PatternThisReferenceToken)
                {
                    newContext = patternMemberReference.Name.MatchUst(memberRef.Target, context);
                }

                if (!newContext.Success)
                {
                    return(newContext);
                }
            }

            newContext = Name.MatchUst(memberRef.Name, newContext);

            return(newContext.AddUstIfSuccess(memberRef));
        }
Beispiel #2
0
        public override MatchContext Match(TypeDeclaration typeDeclaration, MatchContext context)
        {
            MatchContext newContext = Modifiers.MatchSubset(typeDeclaration.Modifiers, context);

            if (!newContext.Success)
            {
                return(newContext);
            }

            if (Name != null)
            {
                newContext = Name.MatchUst(typeDeclaration.Name, newContext);
                if (!newContext.Success)
                {
                    return(newContext);
                }
            }

            newContext = BaseTypes.MatchSubset(typeDeclaration.BaseTypes, newContext);

            if (!newContext.Success)
            {
                return(newContext);
            }

            if (Body != null)
            {
                if (!typeDeclaration.TypeMembers.Any(m => Body.Match(m, newContext).Success))
                {
                    return(newContext.Fail());
                }
            }

            return(newContext.AddUstIfSuccess(typeDeclaration));
        }
        protected override MatchContext Match(Ust ust, MatchContext context)
        {
            var assign = ust as AssignmentExpression;

            if (assign == null)
            {
                return(context.Fail());
            }

            MatchContext newContext = Left.MatchUst(assign.Left, context);

            if (newContext.Success)
            {
                if (Right != null && assign.Right != null)
                {
                    newContext = Right.MatchUst(assign.Right, newContext);
                }
                else if (Right != null && assign.Right == null ||
                         Right == null && assign.Right != null)
                {
                    newContext = newContext.Fail();
                }
            }

            return(newContext.AddUstIfSuccess(assign));
        }
        protected override MatchContext Match(Ust ust, MatchContext context)
        {
            var binaryOperatorExpression = ust as BinaryOperatorExpression;

            if (binaryOperatorExpression == null)
            {
                return(context.Fail());
            }

            MatchContext newContext = Left.MatchUst(binaryOperatorExpression.Left, context);

            if (!newContext.Success)
            {
                return(newContext);
            }

            newContext = Operator.MatchUst(binaryOperatorExpression.Operator, newContext);
            if (!newContext.Success)
            {
                return(newContext);
            }

            newContext = Right.MatchUst(binaryOperatorExpression.Right, newContext);

            return(newContext.AddUstIfSuccess(binaryOperatorExpression));
        }
Beispiel #5
0
        protected override MatchContext Match(Ust ust, MatchContext context)
        {
            var methodDeclaration = ust as MethodDeclaration;

            if (methodDeclaration == null)
            {
                return(context.Fail());
            }

            MatchContext newContext = Modifiers.MatchSubset(methodDeclaration.Modifiers, context);

            if (!newContext.Success)
            {
                return(newContext);
            }

            newContext = Name.MatchUst(methodDeclaration.Name, newContext);
            if (!newContext.Success)
            {
                return(newContext);
            }

            if (!AnyBody)
            {
                if (Body != null)
                {
                    if (Body is PatternArbitraryDepth || Body is PatternStatements)
                    {
                        newContext = Body.MatchUst(methodDeclaration.Body, newContext);
                    }
                    else
                    {
                        var otherStatements = methodDeclaration.Body.Statements;
                        if (otherStatements.Count == 1)
                        {
                            newContext = Body.MatchUst(otherStatements.First(), newContext);
                        }
                        else
                        {
                            return(newContext.Fail());
                        }
                    }

                    if (!newContext.Success)
                    {
                        return(newContext);
                    }
                }
                else if (methodDeclaration.Body != null)
                {
                    return(newContext.Fail());
                }
            }

            return(newContext.AddUstIfSuccess(methodDeclaration));
        }
        public override MatchContext Match(ObjectCreateExpression objectCreateExpression, MatchContext context)
        {
            MatchContext newContext = Type.MatchUst(objectCreateExpression.Type, context);

            if (!newContext.Success)
            {
                return(newContext);
            }

            newContext = Arguments.Match(objectCreateExpression.Arguments, newContext);

            return(newContext.AddUstIfSuccess(objectCreateExpression));
        }
Beispiel #7
0
        public override MatchContext Match(ParameterDeclaration parameterDeclaration, MatchContext context)
        {
            MatchContext newContext = Type.MatchUst(parameterDeclaration.Type, context);

            if (!newContext.Success)
            {
                return(newContext);
            }

            newContext = Name.MatchUst(parameterDeclaration.Name, newContext);

            return(newContext.AddUstIfSuccess(parameterDeclaration));
        }
        public override MatchContext Match(MemberReferenceExpression memberRef, MatchContext context)
        {
            MatchContext newContext = Target.MatchUst(memberRef.Target, context);

            if (!newContext.Success)
            {
                return(newContext);
            }

            newContext = Name.MatchUst(memberRef.Name, newContext);

            return(newContext.AddUstIfSuccess(memberRef));
        }
        protected override MatchContext Match(Ust ust, MatchContext context)
        {
            var arrayCreationExpression = ust as ArrayCreationExpression;

            if (arrayCreationExpression == null)
            {
                return(context.Fail());
            }

            // If we're looking for a stack allocated array there should be NO `new` keyword
            if (StackAllocated && arrayCreationExpression.KeywordNew != null)
            {
                return(context.Fail());
            }

            MatchContext newContext = Type.MatchUst(arrayCreationExpression.Type, context);

            if (!newContext.Success)
            {
                return(newContext);
            }

            if (Sizes.Count != arrayCreationExpression.Sizes.Count)
            {
                return(context.Fail());
            }
            for (int i = 0; i < Sizes.Count; i++)
            {
                newContext = Sizes[i].MatchUst(arrayCreationExpression.Sizes[i], newContext);
                if (!newContext.Success)
                {
                    return(newContext);
                }
            }

            if (Initializers.Count != arrayCreationExpression.Initializers.Count)
            {
                return(context.Fail());
            }
            for (int i = 0; i < Initializers.Count; i++)
            {
                newContext = Initializers[i].MatchUst(arrayCreationExpression.Initializers[i], newContext);
                if (!newContext.Success)
                {
                    return(newContext);
                }
            }

            return(newContext.AddUstIfSuccess(arrayCreationExpression));
        }
        public override MatchContext Match(AssignmentExpression assign, MatchContext context)
        {
            MatchContext newContext = Left.MatchUst(assign.Left, context);

            if (newContext.Success)
            {
                if (Right != null && assign.Right != null)
                {
                    newContext = Right.MatchUst(assign.Right, newContext);
                }
                else if ((Right != null && assign.Right == null) ||
                         (Right == null && assign.Right != null))
                {
                    newContext = newContext.Fail();
                }
            }

            return(newContext.AddUstIfSuccess(assign));
        }
Beispiel #11
0
        protected override MatchContext Match(Ust ust, MatchContext context)
        {
            var invocation = ust as InvocationExpression;

            if (invocation == null)
            {
                return(context.Fail());
            }

            context = Target.MatchUst(invocation.Target, context);
            if (!context.Success)
            {
                return(context);
            }

            context = Arguments.MatchUst(invocation.Arguments, context);

            return(context.AddUstIfSuccess(invocation));
        }
Beispiel #12
0
        public override MatchContext Match(BinaryOperatorExpression binaryOperatorExpression, MatchContext context)
        {
            MatchContext newContext = Left.MatchUst(binaryOperatorExpression.Left, context);

            if (!newContext.Success)
            {
                return(newContext);
            }

            newContext = Operator.Match(binaryOperatorExpression.Operator, newContext);
            if (!newContext.Success)
            {
                return(newContext);
            }

            newContext = Right.MatchUst(binaryOperatorExpression.Right, newContext);

            return(newContext.AddUstIfSuccess(binaryOperatorExpression));
        }
Beispiel #13
0
        protected override MatchContext Match(Ust ust, MatchContext context)
        {
            var typeDeclaration = ust as TypeDeclaration;

            if (typeDeclaration == null)
            {
                return(context.Fail());
            }

            MatchContext newContext = Modifiers.MatchSubset(typeDeclaration.Modifiers, context);

            if (!newContext.Success)
            {
                return(newContext);
            }

            if (Name != null)
            {
                newContext = Name.MatchUst(typeDeclaration.Name, newContext);
                if (!newContext.Success)
                {
                    return(newContext);
                }
            }

            newContext = BaseTypes.MatchSubset(typeDeclaration.BaseTypes, newContext);

            if (!newContext.Success)
            {
                return(newContext);
            }

            if (Body != null)
            {
                if (!typeDeclaration.TypeMembers.Any(m => Body.MatchUst(m, newContext).Success))
                {
                    return(newContext.Fail());
                }
            }

            return(newContext.AddUstIfSuccess(typeDeclaration));
        }
        protected override MatchContext Match(Ust ust, MatchContext context)
        {
            var objectCreateExpression = ust as ObjectCreateExpression;

            if (objectCreateExpression == null)
            {
                return(context.Fail());
            }

            MatchContext newContext = Type.MatchUst(objectCreateExpression.Type, context);

            if (!newContext.Success)
            {
                return(newContext);
            }

            newContext = Arguments.MatchUst(objectCreateExpression.Arguments, newContext);

            return(newContext.AddUstIfSuccess(objectCreateExpression));
        }
        protected override MatchContext Match(Ust ust, MatchContext context)
        {
            var parameterDeclaration = ust as ParameterDeclaration;

            if (parameterDeclaration == null)
            {
                return(context.Fail());
            }

            MatchContext newContext = Type.MatchUst(parameterDeclaration.Type, context);

            if (!newContext.Success)
            {
                return(newContext);
            }

            newContext = Name.MatchUst(parameterDeclaration.Name, newContext);

            return(newContext.AddUstIfSuccess(parameterDeclaration));
        }
        protected override MatchContext Match(Ust ust, MatchContext context)
        {
            var unaryOperatorExpression = ust as UnaryOperatorExpression;

            if (unaryOperatorExpression == null)
            {
                return(context.Fail());
            }

            MatchContext newContext = Expression.MatchUst(unaryOperatorExpression.Expression, context);

            if (!newContext.Success)
            {
                return(newContext);
            }

            newContext = Operator.MatchUst(unaryOperatorExpression.Operator, newContext);

            return(newContext.AddUstIfSuccess(ust));
        }
Beispiel #17
0
        protected override MatchContext Match(Ust ust, MatchContext context)
        {
            var argsUst = ust as ArgsUst;

            if (argsUst == null)
            {
                return(context.Fail());
            }

            List <Expression> args       = argsUst.Collection;
            MatchContext      newContext = MatchContext.CreateWithInputParamsAndVars(context);
            var matchedTextSpans         = new List <TextSpan>();
            int patternArgInd            = 0;
            int argInd = 0;

            while (argInd < args.Count)
            {
                if (patternArgInd >= Args.Count)
                {
                    break;
                }

                newContext = MatchContext.CreateWithInputParamsAndVars(newContext);
                if (Args[patternArgInd] is PatternMultipleExpressions)
                {
                    if (patternArgInd + 1 < Args.Count)
                    {
                        Expression arg = UstUtils.GetArgWithoutModifier(args[argInd]);
                        newContext = Args[patternArgInd + 1].MatchUst(arg, newContext);
                        matchedTextSpans.AddRange(newContext.Locations);
                        if (newContext.Success)
                        {
                            patternArgInd += 2;
                        }
                    }
                    else
                    {
                        matchedTextSpans.AddRange(newContext.Locations);
                    }

                    argInd += 1;
                }
                else
                {
                    Expression arg = UstUtils.GetArgWithoutModifier(args[argInd]);
                    newContext = Args[patternArgInd].MatchUst(arg, newContext);
                    if (!newContext.Success)
                    {
                        break;
                    }

                    matchedTextSpans.AddRange(newContext.Locations);
                    patternArgInd += 1;
                    argInd        += 1;
                }
            }

            if (patternArgInd < Args.Count && Args[patternArgInd] is PatternMultipleExpressions)
            {
                patternArgInd += 1;
            }

            newContext = argInd != args.Count || patternArgInd != Args.Count
                ? context.Fail()
                : context.AddMatches(matchedTextSpans);

            return(newContext.AddUstIfSuccess(argsUst));
        }