Ejemplo n.º 1
0
        public static ITokenMatching HasLine(this ITokenMatching self, Func <IMatchedTokenMatching, ITokenMatching> inner)
        {
            if (!(self is IMatchedTokenMatching matchedTokenMatching))
            {
                return(self);
            }

            if (matchedTokenMatching.AllTokens.Count <= matchedTokenMatching.EndIndex)
            {
                return(TokenMatching <object> .MakeNotMatch(self.Context));
            }

            if (matchedTokenMatching.AllTokens[matchedTokenMatching.EndIndex].Is1(out var token) && token.SafeIs(out LineToken line))
            {
                if (inner(TokenMatching <object> .MakeStart(line.Tokens.Select(x => OrType.Make <IToken, ISetUp>(x)).ToList(), self.Context, 0)) is IMatchedTokenMatching)
                {
                    // uhhh new object?
                    // todo make a IMatchedTokenMatching with out a generic just for this
                    return(TokenMatching <object> .MakeMatch(matchedTokenMatching, new object(), matchedTokenMatching.EndIndex + 1));
                }
                ;
                return(TokenMatching <object> .MakeNotMatch(self.Context));
            }

            return(TokenMatching <object> .MakeNotMatch(self.Context));
        }
Ejemplo n.º 2
0
        //public static ITokenMatching<T> HasStruct<T>(this ITokenMatching self, IMaker<T> pattern, out T t)
        //    where T : struct
        //{

        //    if (!(self is IMatchedTokenMatching firstMatched))
        //    {
        //        t = default;
        //        return TokenMatching<T>.MakeNotMatch(self.Context);
        //    }

        //    var res = pattern.TryMake(firstMatched);
        //    if (res is IMatchedTokenMatching<T> matched)
        //    {
        //        t = matched.Value;
        //        return res;
        //    }

        //    t = default;
        //    return res;
        //}


        public static ITokenMatching HasSquare(this ITokenMatching self, Func <IMatchedTokenMatching, ITokenMatching> inner)
        {
            if (!(self is IMatchedTokenMatching matchedTokenMatching))
            {
                return(self);
            }

            var index = matchedTokenMatching.EndIndex;

            if (matchedTokenMatching.AllTokens.Count < index)
            {
                return(TokenMatching <object> .MakeNotMatch(self.Context));
            }

            if (matchedTokenMatching.AllTokens[index].Is1(out var token) && token.SafeIs(out SquareBacketToken squareBacketToken))
            {
                if (inner(TokenMatching <object> .MakeStart(squareBacketToken.Tokens.Select(x => OrType.Make <IToken, ISetUp>(x)).ToList(), self.Context, 0)) is IMatchedTokenMatching <object> mtm)
                {
                    return(TokenMatching <object> .MakeMatch(matchedTokenMatching, mtm.Value, index + 1));
                }
                ;
                return(TokenMatching <object> .MakeNotMatch(self.Context));
            }

            return(TokenMatching <object> .MakeNotMatch(self.Context));
        }
Ejemplo n.º 3
0
        public static ITokenMatching <T> HasOne <T>(
            this ITokenMatching self,
            Func <ITokenMatching, ITokenMatching <T> >[] items,
            out T res)
        {
            if (!(self is IMatchedTokenMatching matchedTokenMatching))
            {
                res = default;
                return(TokenMatching <T> .MakeNotMatch(self.Context));
            }

            var results = items.Select(x => x(self)).ToArray();

            var goodResults = results.OfType <IMatchedTokenMatching <T> >().ToArray();

            if (goodResults.Count() > 1)
            {
                throw new Exception("more than one should not match!");
            }

            if (goodResults.Count() == 1)
            {
                res = goodResults.First().Value;
                return(goodResults.First());
            }

            res = default;
            return(TokenMatching <T> .MakeNotMatch(self.Context));
        }
Ejemplo n.º 4
0
        public static ITokenMatching <T> HasOne <T>(
            this ITokenMatching self,
            Func <ITokenMatching, ITokenMatching <T> > first,
            Func <ITokenMatching, ITokenMatching <T> > second,
            out T res)
        {
            if (!(self is IMatchedTokenMatching))
            {
                res = default;
                return(TokenMatching <T> .MakeNotMatch(self.Context));
            }

            var firstResult  = first(self);
            var secondResult = second(self);

            if (firstResult is IMatchedTokenMatching <T> && secondResult is IMatchedTokenMatching <T> )
            {
                throw new Exception("should not match both!");
            }

            if (firstResult is IMatchedTokenMatching <T> firstMatched)
            {
                res = firstMatched.Value;
                return(firstResult);
            }

            if (secondResult is IMatchedTokenMatching <T> secondMatched)
            {
                res = secondMatched.Value;
                return(secondResult);
            }

            res = default;
            return(TokenMatching <T> .MakeNotMatch(self.Context));
        }
Ejemplo n.º 5
0
 public static ITokenMatching <T> ConvertIfMatched <T>(this ITokenMatching tokenMatching, Func <T> convert, IMatchedTokenMatching source)
 {
     if (tokenMatching.SafeIs(out IMatchedTokenMatching matched))
     {
         return(TokenMatching <T> .MakeMatch(source, convert(), matched.EndIndex));
     }
     return(TokenMatching <T> .MakeNotMatch(tokenMatching.Context));
 }
Ejemplo n.º 6
0
 public static ITokenMatching <T> ConvertIfMatched <T, T1, T2, T3>(this ITokenMatching <T1, T2, T3> tokenMatching, Func <T1, T2, T3, T> convert, IMatchedTokenMatching source)
 {
     if (tokenMatching.SafeIs(out IMatchedTokenMatching <T1, T2, T3> matched))
     {
         return(TokenMatching <T> .MakeMatch(source, convert(matched.Value1, matched.Value2, matched.Value3), matched.EndIndex));
     }
     return(TokenMatching <T> .MakeNotMatch(tokenMatching.Context));
 }
Ejemplo n.º 7
0
        public static ITokenMatching Has(this ITokenMatching self, IMaker pattern)
        {
            if (!(self is IMatchedTokenMatching))
            {
                return(self);
            }

            return(pattern.TryMake(self));
        }
Ejemplo n.º 8
0
 public static ITokenMatching <T> GetValue <T>(this ITokenMatching <T> self, out T value)
 {
     if (self is IMatchedTokenMatching <T> matched)
     {
         value = matched.Value;
         return(matched);
     }
     value = default;
     return(self);
 }
Ejemplo n.º 9
0
        public ITokenMatching TryMake(ITokenMatching self)
        {
            if (self.SafeIs(out IMatchedTokenMatching matched) && matched.AllTokens.Count >= matched.EndIndex)
            {
                return(self);
            }

            // smells a little... <object>
            return(TokenMatching <object> .MakeNotMatch(self.Context));
        }
Ejemplo n.º 10
0
        public ITokenMatching TryMake(ITokenMatching self)
        {
            if (self is IMatchedTokenMatching matched && !matched.Tokens.Any())
            {
                return(self);
            }

            // smells a little... <object>
            return(TokenMatching <object> .MakeNotMatch(self.Context));
        }
Ejemplo n.º 11
0
        public static ITokenMatching <T1, T2> Has <T1, T2>(this ITokenMatching <T1> self, IMaker <T2> pattern)
            where T1 : class
            where T2 : class
        {
            if (self is IMatchedTokenMatching <T1> firstMatched)
            {
                var res = pattern.TryMake(firstMatched);
                if (res is IMatchedTokenMatching <T2> matched)
                {
                    return(TokenMatching <T1, T2> .MakeMatch(firstMatched, firstMatched.Value, matched.Value, matched.EndIndex));
                }
            }

            return(TokenMatching <T1, T2> .MakeNotMatch(self.Context));
        }
Ejemplo n.º 12
0
        public static ITokenMatching OptionalHas(this ITokenMatching self, IMaker pattern)
        {
            if (!(self is IMatchedTokenMatching))
            {
                return(self);
            }

            var next = pattern.TryMake(self);

            if (next is IMatchedTokenMatching)
            {
                return(next);
            }

            return(self);
        }
Ejemplo n.º 13
0
        public static ITokenMatching <T> Has <T>(this ITokenMatching <T> self, IMaker pattern)
        {
            if (!(self is IMatchedTokenMatching <T> matchedTokenMatching))
            {
                return(self);
            }

            var patternMatch = pattern.TryMake(self);

            if (!(patternMatch is IMatchedTokenMatching matchedPattern))
            {
                return(TokenMatching <T> .MakeNotMatch(patternMatch.Context));
            }

            return(TokenMatching <T> .MakeMatch(matchedPattern.Tokens, matchedPattern.Context, matchedTokenMatching.Value));
        }
Ejemplo n.º 14
0
        public static ITokenMatching <T> Has <T>(this ITokenMatching self, IMaker <T> pattern, out T t)
        {
            t = default;

            if (!(self is IMatchedTokenMatching firstMatched))
            {
                return(TokenMatching <T> .MakeNotMatch(self.Context));
            }

            var res = pattern.TryMake(firstMatched);

            if (res is IMatchedTokenMatching <T> matched)
            {
                t = matched.Value;
            }
            return(res);
        }
Ejemplo n.º 15
0
        public static ITokenMatching <T> Has <T>(this ITokenMatching self, IMaker <T> pattern)
            where T : class
        {
            if (!(self is IMatchedTokenMatching firstMatched))
            {
                return(TokenMatching <T> .MakeNotMatch(self.Context));
            }

            var res = pattern.TryMake(firstMatched);

            if (res is IMatchedTokenMatching <T> matched)
            {
                return(matched);
            }

            return(res);
        }
Ejemplo n.º 16
0
        public static ITokenMatching OptionalHas <T>(this ITokenMatching self, IMaker <T> pattern, out T t)
            where T : class
        {
            if (!(self is IMatchedTokenMatching matchedTokenMatching))
            {
                t = default;
                return(self);
            }

            var res = pattern.TryMake(matchedTokenMatching);

            if (res is IMatchedTokenMatching <T> matched)
            {
                t = matched.Value;
                return(res);
            }

            t = default;
            return(self);
        }
Ejemplo n.º 17
0
        public static ITokenMatching <T> Has <T>(this ITokenMatching self, IMaker <T> pattern, out T t)
        {
            if (!(self is IMatchedTokenMatching firstMatched))
            {
#pragma warning disable CS8601 // Possible null reference assignment.
                t = default;
#pragma warning restore CS8601 // Possible null reference assignment.
                return(TokenMatching <T> .MakeNotMatch(self.Context));
            }

            var res = pattern.TryMake(firstMatched);
            if (res is IMatchedTokenMatching <T> matched)
            {
                t = matched.Value;
                return(res);
            }
#pragma warning disable CS8601 // Possible null reference assignment.
            t = default;
#pragma warning restore CS8601 // Possible null reference assignment.
            return(res);
        }
Ejemplo n.º 18
0
        public static ITokenMatching HasElement(this ITokenMatching self, Func <IMatchedTokenMatching, ITokenMatching> inner)
        {
            if (!(self is IMatchedTokenMatching matchedTokenMatching))
            {
                return(self);
            }

            if (matchedTokenMatching.Tokens.Any().Not())
            {
                return(TokenMatching <object> .MakeNotMatch(self.Context));
            }

            if (matchedTokenMatching.Tokens.First() is ElementToken elementToken)
            {
                if (inner(TokenMatching <object> .MakeStart(elementToken.Tokens, self.Context)) is IMatchedTokenMatching matched)
                {
                    return(TokenMatching <object> .MakeStart(matched.Tokens.Skip(1).ToArray(), self.Context));
                }
                ;
                return(TokenMatching <object> .MakeNotMatch(self.Context));
            }

            return(TokenMatching <object> .MakeNotMatch(self.Context));
        }