protected override MatchContext Match(Ust ust, MatchContext context)
        {
            if (ust is IntLiteral intLiteral)
            {
                return(MinValue <= intLiteral.Value && MaxValue > intLiteral.Value
                    ? context.AddMatch(intLiteral)
                    : context.Fail());
            }
            if (ust is LongLiteral longLiteral)
            {
                return(MinValue <= longLiteral.Value && MaxValue > longLiteral.Value
                    ? context.AddMatch(longLiteral)
                    : context.Fail());
            }
            if (ust is BigIntLiteral bigIntLiteral)
            {
                return(MinValue <= bigIntLiteral.Value && MaxValue > bigIntLiteral.Value
                    ? context.AddMatch(bigIntLiteral)
                    : context.Fail());
            }

            if (context.UstConstantFolder != null &&
                context.UstConstantFolder.TryGetOrFold(ust, out FoldResult foldingResult))
            {
                context.MatchedWithFolded = true;
                if (foldingResult.Value is long longValue)
                {
                    return(MinValue <= longValue && MaxValue > longValue
                        ? context.AddMatches(foldingResult.TextSpans)
                        : context.Fail());
                }
            }

            return(context.Fail());
        }
示例#2
0
        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());
        }
示例#3
0
        protected override MatchContext Match(Ust ust, MatchContext context)
        {
            switch (ust)
            {
            case IntLiteral intLiteral:
                return(Value == intLiteral.Value
                    ? context.AddMatch(ust)
                    : context.Fail());

            case LongLiteral longLiteral:
                return(Value == longLiteral.Value
                    ? context.AddMatch(ust)
                    : context.Fail());

            case BigIntLiteral bigIntLiteral:
                return(Value == bigIntLiteral.Value
                    ? context.AddMatch(ust)
                    : context.Fail());
            }

            if (context.UstConstantFolder != null &&
                context.UstConstantFolder.TryGetOrFold(ust, out FoldResult foldingResult))
            {
                context.MatchedWithFolded = true;
                if (foldingResult.Value is int intValue)
                {
                    return(Value == intValue?context.AddMatches(foldingResult.TextSpans) : context.Fail());
                }
                if (foldingResult.Value is long longValue)
                {
                    return(Value == longValue?context.AddMatches(foldingResult.TextSpans) : context.Fail());
                }
                if (foldingResult.Value is BigInteger bigIntValue)
                {
                    return(Value == bigIntValue?context.AddMatches(foldingResult.TextSpans) : context.Fail());
                }
            }

            return(context.Fail());
        }
        protected override MatchContext Match(Ust ust, MatchContext context)
        {
            if (ust is StringLiteral stringLiteral)
            {
                return(String.Equals(stringLiteral.Text) ? context.AddMatch(stringLiteral) : context.Fail());
            }

            if (context.UstConstantFolder != null &&
                context.UstConstantFolder.TryGetOrFold(ust, out FoldResult foldingResult))
            {
                context.MatchedWithFolded = true;
                if (foldingResult.Value is string stringValue)
                {
                    return(String.Equals(stringValue) ? context.AddMatches(foldingResult.TextSpans) : context.Fail());
                }
            }

            return(context.Fail());
        }
示例#5
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);
        }
示例#6
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));
        }
示例#7
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);
        }
示例#8
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));
        }