Example #1
0
            public override Parser <TInput> VisitBest <TOutput>(BestParser <TInput, TOutput> parser)
            {
                if (state.State == 0)
                {
                    int minLength  = -1;
                    int maxLength  = -1;
                    int bestParser = -1;

                    // figure out which parser will consume most input
                    for (int i = 0; i < parser.Parsers.Count; i++)
                    {
                        var p      = parser.Parsers[i];
                        var length = p.ScanSafe(source, state.InputStart);

                        if (length > maxLength)
                        {
                            maxLength  = length;
                            bestParser = i;
                        }
                        else if (length < minLength)
                        {
                            minLength = length;
                        }
                    }

                    if (maxLength >= 0)
                    {
                        state.State           = 1;
                        state.NextOutputStart = state.OutputStart;
                        return(parser.Parsers[bestParser]);
                    }
                    else
                    {
                        state.InputLength = -1;
                        return(null);
                    }
                }
                else
                {
                    state.InputLength = state.LastResult;
                    return(null);
                }
            }
Example #2
0
            public override Parser <TInput> VisitBest <TOutput>(BestParser <TInput, TOutput> parser)
            {
                if (state.State == 0)
                {
                    state.BestFailedResult  = -1;
                    state.BestSuccessResult = -1;
                    state.State             = 1;
                    return(parser.Parsers[0]);
                }
                else
                {
                    if (state.LastResult > state.BestSuccessResult)
                    {
                        state.BestSuccessResult = state.LastResult;
                    }
                    else if (state.LastResult < state.BestFailedResult)
                    {
                        state.BestFailedResult = state.LastResult;
                    }

                    if (state.State >= parser.Parsers.Count)
                    {
                        if (state.BestSuccessResult >= 0)
                        {
                            state.InputLength = state.BestSuccessResult;
                            return(null);
                        }
                        else
                        {
                            state.InputLength = state.BestFailedResult;
                            return(null);
                        }
                    }
                    else
                    {
                        state.InputLength = 0;
                        var next = parser.Parsers[state.State];
                        state.State++;
                        return(next);
                    }
                }
            }
 public override void VisitBest(BestParser <TInput> parser)
 {
     WriteAlternation(parser.Parsers);
 }
 public override Parser <TInput> VisitBest <TOutput>(BestParser <TInput, TOutput> parser)
 {
     return(NextBest(parser.Parsers));
 }
Example #5
0
 public override int VisitBest(BestParser <TInput> parser, int start)
 {
     return(VisitFirst(parser.Parsers, start));
 }