コード例 #1
0
ファイル: PatternOr.cs プロジェクト: superdullwolf/PT.PM
        protected override MatchContext Match(Ust ust, MatchContext context)
        {
            var matchedTextSpans = new List <TextSpan>();

            bool success = false;

            foreach (PatternUst pattern in Patterns)
            {
                var          altContext = MatchContext.CreateWithInputParamsAndVars(context);
                MatchContext match      = pattern.MatchUst(ust, altContext);
                if (match.Success)
                {
                    success = true;
                    matchedTextSpans.AddRange(match.Locations);
                    if (!context.FindAllAlternatives)
                    {
                        break;
                    }
                }
            }

            return(success
                ? context.AddMatches(matchedTextSpans)
                : context.Fail());
        }
コード例 #2
0
        private MatchContext EnumerateVarsOrFields(List <AssignmentExpression> variables, MatchContext context)
        {
            var matchedTextSpans = new List <TextSpan>();

            bool success = false;

            foreach (AssignmentExpression variable in variables)
            {
                var          altContext = MatchContext.CreateWithInputParamsAndVars(context);
                MatchContext match;
                if (Assignment.Right != null)
                {
                    match = Assignment.MatchUst(variable, altContext);
                }
                else
                {
                    match = Assignment.Left.MatchUst(variable.Left, altContext);
                }
                if (match.Success)
                {
                    success = true;
                    matchedTextSpans.AddRange(match.Locations);
                    if (!context.FindAllAlternatives)
                    {
                        break;
                    }
                }
            }

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

            return(context);
        }
コード例 #3
0
ファイル: PatternStatements.cs プロジェクト: smartfish/PT.PM
        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);
        }
コード例 #4
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));
        }
コード例 #5
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));
        }