コード例 #1
0
 public bool Equals(ParseResult <T> x, ParseResult <T> y)
 {
     if (x is ParseSuccess <T> )
     {
         ParseSuccess <T> xs = (ParseSuccess <T>)x;
         if (y is ParseSuccess <T> )
         {
             ParseSuccess <T> ys = (ParseSuccess <T>)y;
             return(xs.Position == ys.Position && xs.Length == ys.Length && resultComparer.Equals(xs.Value, ys.Value));
         }
         else
         {
             return(false);
         }
     }
     else
     {
         ParseFailure <T> xf = (ParseFailure <T>)x;
         if (y is ParseSuccess <T> )
         {
             return(false);
         }
         else
         {
             ParseFailure <T> yf = (ParseFailure <T>)y;
             return(xf.Errors.Count == yf.Errors.Count && Enumerable.Range(0, xf.Errors.Count).All(i => xf.Errors[i] == yf.Errors[i]));
         }
     }
 }
コード例 #2
0
 public int GetHashCode(ParseResult <T> obj)
 {
     if (obj is ParseSuccess <T> )
     {
         ParseSuccess <T> ps = (ParseSuccess <T>)obj;
         return($"{ps.Position},{ps.Length},{resultComparer.GetHashCode(ps.Value)}".GetHashCode());
     }
     else
     {
         ParseFailure <T> pf = (ParseFailure <T>)obj;
         return(string.Join(", ", pf.Errors.Select(f => f.GetHashCode())).GetHashCode());
     }
 }
コード例 #3
0
            public ParseResult <ImmutableList <T> > TryParse(CharParserContext context, int off, int len)
            {
                int pos = off;
                ImmutableList <T> results = ImmutableList <T> .Empty;

                if (repeating)
                {
                    ParseResult <T> pr;

                    while (true)
                    {
                        pr = context.TryParseAt(subParser, pos, len - (pos - off));
                        if (pr is ParseFailure <T> )
                        {
                            break;
                        }
                        ParseSuccess <T> ps = (ParseSuccess <T>)pr;
                        pos    += ps.Length;
                        results = results.Add(ps.Value);
                    }

                    if (!optional && results.Count == 0)
                    {
                        return(new ParseFailure <ImmutableList <T> >(((ParseFailure <T>)pr).Errors));
                    }
                    else
                    {
                        return(new ParseSuccess <ImmutableList <T> >(off, pos - off, results));
                    }
                }
                else if (optional)
                {
                    ParseResult <T> pr = context.TryParseAt(subParser, off, len);

                    return(pr.Visit <ParseResult <ImmutableList <T> > >
                           (
                               success =>
                    {
                        return new ParseSuccess <ImmutableList <T> >(success.Position, success.Length, ImmutableList <T> .Empty.Add(success.Value));
                    },
                               failure =>
                    {
                        return new ParseSuccess <ImmutableList <T> >(off, 0, ImmutableList <T> .Empty);
                    }
                           ));
                }
                else
                {
                    throw new InvalidOperationException("Neither optional nor repeating");
                }
            }
コード例 #4
0
        public static Func <ParseResult <T>, string> GetParseResultStringConverter <T>(Func <T, string> resultToString)
        {
            Func <ParseResult <T>, string> toString = delegate(ParseResult <T> pr)
            {
                if (pr is ParseSuccess <T> )
                {
                    ParseSuccess <T> ps = (ParseSuccess <T>)pr;
                    return($"{{ success, pos = {ps.Position}, len = {ps.Length}, value = {resultToString(ps.Value)} }}");
                }
                else
                {
                    ParseFailure <T> pf = (ParseFailure <T>)pr;
                    return($"{{ failure, {string.Join(",", pf.Errors.Select(f => $"{{ pos = {f.Position}, message = {f.Message.Quoted()} }}"))} }}");
                }
            };

            return(toString);
        }