Exemple #1
0
        public override MatchContext Match(Ust ust, MatchContext context)
        {
            var blockStatement = ust as BlockStatement;

            if (blockStatement == null ||
                (!(blockStatement.Parent is MethodDeclaration) &&
                 !(blockStatement.Parent is ConstructorDeclaration) &&
                 !(blockStatement.Parent is NamespaceDeclaration) &&
                 !(blockStatement.Parent is RootUst)))
            {
                return(context.Fail());
            }

            MatchContext newContext = MatchContext.CreateWithInputParamsAndVars(context);

            if (Statements == null || Statements.Count == 0)
            {
                if (blockStatement.Statements == null || blockStatement.Statements.Count == 0)
                {
                    newContext = newContext.AddMatch(blockStatement);
                }
                else
                {
                    return(context.Fail());
                }
            }
            else
            {
                IEnumerable <Statement> statements = blockStatement.Statements
                                                     .Where(statement =>
                                                            !(statement is TypeDeclarationStatement) &&
                                                            !(statement is WrapperStatement));
                Expression[] expressions = statements.SelectMany(statement =>
                                                                 statement
                                                                 .WhereDescendants(descendant =>
                                                                                   descendant is Expression expressionDescendant &&
                                                                                   !(expressionDescendant is Token)))
                                           .Cast <Expression>()
                                           .ToArray();

                var  matchedTextSpans    = new List <TextSpan>();
                int  patternStatementInd = 0;
                bool success             = false;
                for (int i = 0; i < expressions.Length; i++)
                {
                    newContext = MatchContext.CreateWithInputParamsAndVars(newContext);
                    newContext = Statements[patternStatementInd].MatchUst(expressions[i], newContext);
                    if (newContext.Success)
                    {
                        matchedTextSpans.AddRange(newContext.Locations);
                        patternStatementInd += 1;
                        if (patternStatementInd == Statements.Count)
                        {
                            success             = true;
                            patternStatementInd = 0;
                        }
                    }
                }

                if (success)
                {
                    context = context.AddMatches(matchedTextSpans);
                }
                else
                {
                    context = context.Fail();
                }
            }

            return(context);
        }
Exemple #2
0
        public override MatchContext Match(ArgsUst argsUst, MatchContext context)
        {
            MatchContext newContext;

            List <Expression> args = argsUst.Collection;

            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 multiExprArg)
                {
                    if (patternArgInd + 1 < Args.Count)
                    {
                        newContext = Args[patternArgInd + 1].MatchUst(args[argInd], newContext);
                        matchedTextSpans.AddRange(newContext.Locations);
                        if (newContext.Success)
                        {
                            patternArgInd += 2;
                        }
                    }
                    else
                    {
                        matchedTextSpans.AddRange(newContext.Locations);
                    }
                    argInd += 1;
                }
                else
                {
                    newContext = Args[patternArgInd].MatchUst(args[argInd], newContext);
                    if (!newContext.Success)
                    {
                        break;
                    }
                    else
                    {
                        matchedTextSpans.AddRange(newContext.Locations);
                    }
                    patternArgInd += 1;
                    argInd        += 1;
                }
            }

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

            if (argInd != args.Count || patternArgInd != Args.Count)
            {
                newContext = context.Fail();
            }
            else
            {
                newContext = context.AddMatches(matchedTextSpans);
            }

            return(newContext.AddUstIfSuccess(argsUst));
        }
 public override MatchContext Match(BooleanLiteral booleanLiteral, MatchContext context)
 {
     return((Boolean == null || Boolean.Value.Equals(booleanLiteral.Value))
         ? context.AddMatch(booleanLiteral)
         : context.Fail());
 }
Exemple #4
0
 public override MatchContext Match(IntLiteral intLiteral, MatchContext context)
 {
     return(intLiteral.Value == Value
         ? context.AddMatch(intLiteral)
         : context.Fail());
 }
 public override MatchContext Match(BinaryOperatorLiteral binaryOperatorLiteral, MatchContext context)
 {
     return(BinaryOperator.Equals(binaryOperatorLiteral.BinaryOperator)
         ? context.AddMatch(binaryOperatorLiteral)
         : context.Fail());
 }