Пример #1
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));
        }
Пример #2
0
        public IPopulateScope <IFrontendCodeElement, ISetUpSideNode> ParseParenthesisOrElement(IToken token)
        {
            if (token is ElementToken elementToken)
            {
                // smells
                // why did i write this agian?
                // why would an element be wrapped in parenthesis ?
                // maybe I can just remove??
                // maybe we have a parentthesis matcher?
                if (elementToken.Tokens.Count() == 1 && elementToken.Tokens.First() is ParenthesisToken parenthesisToken)
                {
                    return(ParseLine(parenthesisToken.Tokens));
                }

                foreach (var tryMatch in elementMakers)
                {
                    if (TokenMatching <IPopulateScope <IFrontendCodeElement, ISetUpSideNode> > .MakeStart(elementToken.Tokens, this)
                        .Has(tryMatch, out var res)
                        .Has(new DoneMaker())
                        is IMatchedTokenMatching)
                    {
                        return(res);
                    }
                }
            }
            else if (token is ParenthesisToken parenthesisToken)
            {
                return(ParseLine(parenthesisToken.Tokens));
            }

            throw new Exception("");
        }
Пример #3
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));
        }
Пример #4
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));
        }
Пример #5
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));
        }
Пример #6
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));
 }
Пример #7
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));
 }
Пример #8
0
 public ITokenMatching <ISetUp <IBox <IFrontendCodeElement>, Tpn.ITypeProblemNode> > TryMake(IMatchedTokenMatching elementToken)
 {
     if (elementToken.AllTokens[elementToken.EndIndex].Is1(out var token) && token.SafeIs(out ParenthesisToken parenthesisToken))
     {
         var res = elementToken.Context.ParseParenthesis(parenthesisToken);
         if (res.Is1(out var setUp))
         {
             return(TokenMatching <ISetUp <IBox <IFrontendCodeElement>, Tpn.ITypeProblemNode> > .MakeMatch(elementToken, setUp, elementToken.EndIndex + 1));
         }
     }
     return(TokenMatching <ISetUp <IBox <IFrontendCodeElement>, Tpn.ITypeProblemNode> > .MakeNotMatch(elementToken.Context));
 }
Пример #9
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));
        }
Пример #10
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));
        }
Пример #11
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);
        }
Пример #12
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);
        }
Пример #13
0
        public IPopulateScope <IFrontendType, ISetUpSideNode> ParseTypeLine(IEnumerable <IToken> tokens)
        {
            foreach (var operationMatcher in typeOperationMatchers)
            {
                if (TokenMatching <IPopulateScope <ICodeElement, ISetUpSideNode> > .MakeStart(tokens.ToArray(), this)
                    .Has(operationMatcher, out var res)
                    is IMatchedTokenMatching)
                {
                    return(res);
                }
            }

            if (tokens.Count() == 1)
            {
                return(ParseParenthesisOrElementType(tokens.Single()));
            }

            throw new Exception("");
        }
Пример #14
0
        private IOrType <ISetUp <IBox <T>, Tpn.ITypeProblemNode>, IError> InnerParseLine <T>(IReadOnlyList <IToken> tokens, IMaker <ISetUp <IBox <T>, Tpn.ITypeProblemNode> >[] makers)
        {
            var myList = tokens.Select(x => OrType.Make <IToken, ISetUp <IBox <T>, Tpn.ITypeProblemNode> >(x)).ToList();

            foreach (var maker in makers)
            {
                // TODO
                // {796AF658-9160-41E2-AD6D-A6D5B905482F}
                // some makers actually want to go right to left
                // x :=  aggregate < filter < transform < filter < somelist
                // x := y := z := 1
                // ++ ++ ++ ++ a
                // a of b of c (as opposed to c.b.a)

top:
                for (int i = 0; i < myList.Count; i++)
                {
                    var matching = TokenMatching <ISetUp <ICodeElement, Tpn.ITypeProblemNode> > .MakeStart(myList, this, i);

                    if (matching.Has(maker, out var element).SafeIs(out IMatchedTokenMatching <ISetUp <IBox <T>, Tpn.ITypeProblemNode> > matched))
                    {
                        myList.RemoveRange(matched.StartIndex, matched.EndIndex - matched.StartIndex);
                        myList.Insert(matched.StartIndex, OrType.Make <IToken, ISetUp <IBox <T>, Tpn.ITypeProblemNode> >(matched.Value));
                        goto top;
                    }
                }
            }

            if (myList.Count > 1)
            {
                return(OrType.Make <ISetUp <IBox <T>, Tpn.ITypeProblemNode>, IError>(Error.Other("expected to parse down to one element (this can mean your as missing a ;)")));
            }

            var single = myList.Single();

            if (single.Is2(out var setup))
            {
                return(OrType.Make <ISetUp <IBox <T>, Tpn.ITypeProblemNode>, IError>(setup));
            }

            return(OrType.Make <ISetUp <IBox <T>, Tpn.ITypeProblemNode>, IError>(Error.Other("token could not be matched")));
        }
Пример #15
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);
        }
Пример #16
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));
        }