public ParseResult <string> TryParse(CharParserContext context, int off, int len)
            {
                if (len < target.Length)
                {
                    return(new ParseFailure <string>(off, "Expected \"" + target + "\", found EOF"));
                }
                else
                {
                    string text = context.Text;

                    if (Utility.IsValidRange(text, off, len))
                    {
                        string inputRegion = text.Substring(off, target.Length);
                        if (string.Compare(inputRegion, target, style) == 0)
                        {
                            return(new ParseSuccess <string>(off, target.Length, inputRegion));
                        }
                        else
                        {
                            return(new ParseFailure <string>(off, "Expected \"" + target + "\""));
                        }
                    }
                    else
                    {
                        return(new ParseFailure <string>(off, "Invalid range"));
                    }
                }
            }
            public ParseResult <T> TryParse(CharParserContext context, int off, int len)
            {
                ParseFailure <T> error = null;

                foreach (ICharParser <T> subParser in subParsers)
                {
                    ParseResult <T> pr = context.TryParseAt(subParser, off, len);

                    if (pr is ParseSuccess <T> )
                    {
                        return(pr);
                    }
                    else
                    {
                        ParseFailure <T> pf = (ParseFailure <T>)pr;
                        if (error == null)
                        {
                            error = pf;
                        }
                        else
                        {
                            error = new ParseFailure <T>(error.Errors.AddRange(pf.Errors));
                        }
                    }
                }

                System.Diagnostics.Debug.Assert(error != null);

                return(error);
            }
            public ParseResult <TOut> TryParse(CharParserContext context, int off, int len)
            {
                ParseResult <TIn> pr1 = context.TryParseAt(subParser, off, len);

                return(pr1.Visit <ParseResult <TOut> >
                       (
                           success =>
                {
                    Option <TOut> conversionResult = conversionFunc(success.Value);

                    if (conversionResult.HasValue)
                    {
                        return new ParseSuccess <TOut>(success.Position, success.Length, conversionResult.Value);
                    }
                    else
                    {
                        return new ParseFailure <TOut>(success.Position, failureMessage);
                    }
                },
                           failure =>
                {
                    return new ParseFailure <TOut>(failure.Errors);
                }
                       ));
            }
 public ParseResult <T> TryParse(CharParserContext context, int off, int len)
 {
     if (SubParser == null)
     {
         return(new ParseFailure <T>(off, "Reference to parser not initialized"));
     }
     else
     {
         return(context.TryParseAt(SubParser, off, len));
     }
 }
 public ParseResult <string> TryParse(CharParserContext context, int off, int len)
 {
     if (Utility.IsValidRange(context.Text, off, len))
     {
         return(new ParseSuccess <string>(off, 0, string.Empty));
     }
     else
     {
         return(new ParseFailure <string>(off, "Invalid range"));
     }
 }
            public ParseResult <Match> TryParse(CharParserContext context, int off, int len)
            {
                Match m = regex.Match(context.Text, off, len);

                if (m.Success && m.Index == off)
                {
                    return(new ParseSuccess <Match>(m.Index, m.Length, m));
                }
                else
                {
                    return(new ParseFailure <Match>(off, failureMessage ?? $"Failed to parse {regex.ToString().Quoted()}"));
                }
            }
            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");
                }
            }
 public ParseResult <Nothing> TryParse(CharParserContext context, int pos, int len)
 {
     if (Utility.IsValidRange(context.Text, pos, len))
     {
         if (len == 0)
         {
             return(new ParseSuccess <Nothing>(pos, 0, Nothing.Value));
         }
         else
         {
             return(new ParseFailure <Nothing>(pos, "EOF expected"));
         }
     }
     else
     {
         return(new ParseFailure <Nothing>(pos, "Invalid range"));
     }
 }
            public ParseResult <ImmutableList <T> > TryParse(CharParserContext context, int off, int len)
            {
                int pos = off;
                ImmutableList <T> results = ImmutableList <T> .Empty;

                foreach (ICharParser <T> subParser in subParsers)
                {
                    ParseResult <T> subResult = context.TryParseAt(subParser, pos, len - (pos - off));

                    if (subResult is ParseSuccess <T> )
                    {
                        var subSuccess = (ParseSuccess <T>)subResult;
                        pos    += subSuccess.Length;
                        results = results.Add(subSuccess.Value);
                    }
                    else
                    {
                        var subFailure = (ParseFailure <T>)subResult;
                        return(new ParseFailure <ImmutableList <T> >(subFailure.Errors));
                    }
                }

                return(new ParseSuccess <ImmutableList <T> >(off, pos - off, results));
            }
            public ParseResult <TOut> TryParse(CharParserContext context, int off, int len)
            {
                ParseResult <TIn> pr1 = context.TryParseAt(subParser, off, len);

                return(pr1.Visit <ParseResult <TOut> >
                       (
                           success =>
                {
                    try
                    {
                        TOut conversionResult = conversionFunc(success.Value);
                        return new ParseSuccess <TOut>(success.Position, success.Length, conversionResult);
                    }
                    catch (Exception exc)
                    {
                        return new ParseFailure <TOut>(success.Position, failureMessage ?? exc.ToString());
                    }
                },
                           failure =>
                {
                    return new ParseFailure <TOut>(failure.Errors);
                }
                       ));
            }
        public static ParseResult <V> TryParse <V>(ICharParser <V> parser, string text)
        {
            CharParserContext cpc = new CharParserContext(text);

            return(cpc.TryParseAt(parser, 0, text.Length));
        }