예제 #1
0
 internal static IList <AbstractSkriptPatternElement> Flatten(AbstractSkriptPatternElement element)
 {
     return(element switch
     {
         SkriptPattern skriptPattern => skriptPattern.Children.SelectMany(Flatten).ToList(),
         _ => new List <AbstractSkriptPatternElement> {
             element
         }
     });
        protected bool TryToNarrowContext(ref ParseContext ctx, out ParseResult result)
        {
            result = null;
            List <AbstractSkriptPatternElement> parentElements;
            AbstractSkriptPatternElement        parent = this;
            {
                while (parent?.Parent != null)
                {
                    parent = parent.Parent;
                }


                parentElements = PatternHelper.Flatten(parent).ToList();
            }

            var indexOfThis = ElementIndex;

            if (indexOfThis == -1)
            {
                return(true);
            }

            var fromIndex = indexOfThis + 1;

            if (fromIndex > parentElements.Count)
            {
                return(true);
            }

            var possibleInputs =
                PatternHelper.GetPossibleInputs(parentElements.SubList(fromIndex, parentElements.Count));

            if (possibleInputs == null)
            {
                return(true);
            }
            var anyMatch = false;

            foreach (var element in possibleInputs)
            {
                switch (element)
                {
                case LiteralPatternElement literal:
                    var index = ctx.Text.SafeSubstring(ctx.CurrentPosition)
                                .IndexOf(literal.Value, StringComparison.InvariantCultureIgnoreCase);

                    if (index > -1)
                    {
                        var clone = ctx;
                        clone.MaxLengthOverride = ctx.CurrentPosition + index;
                        ctx      = clone;
                        anyMatch = true;
                    }

                    break;
                }
            }

            //TODO: Return true if anyMatch is true
            return(true);
        }