Exemplo n.º 1
0
        internal ParseResult Parse(ParseContext parentContext)
        {
            var ctx = new ParseContext(parentContext, this);

            if (!parentContext.IsUnique(ctx))
            {
                return(ctx.Error("Infinite parsing loop detected."));
            }

            ParseResult         failed    = null;
            SymbolExpectedError tailError = null;
            var children = OnParse(ctx)
                           .TakeWhile(x => (failed = failed ?? (x.Success ? null : x)) == null &&
                                      (tailError = x.TailError ?? tailError) == tailError)
                           .SelectMany(x => x.FlattenHierarchy ? x : Single(x))
                           .ToArray();

            if (failed != null)
            {
                tailError = ctx.Expected(tailError, failed.Error as SymbolExpectedError);
            }

            return((failed != null && failed.Fatal)
                ? failed.Error
                : SubstituteResult(new ParseResult(ctx, failed != null
                    ? ctx.Expected(failed.Error as SymbolExpectedError, tailError)
                    : tailError, children)));
        }
Exemplo n.º 2
0
 protected override IEnumerable <ParseResult> OnParse(ParseContext ctx)
 {
     if (!ctx.Match(_literal))
     {
         yield return(ctx.Expected(ExpectingDescription));
     }
 }
Exemplo n.º 3
0
        protected override IEnumerable <ParseResult> OnParse(ParseContext ctx)
        {
            var match = _regex.Match(ctx.Input, ctx.Offset);

            if (!match.Success || match.Index != ctx.Offset)
            {
                yield return(ctx.Expected(_regex));

                yield break;
            }

            ctx.Advance(match.Length);
        }
Exemplo n.º 4
0
        protected override IEnumerable <ParseResult> OnParse(ParseContext ctx)
        {
            SymbolExpectedError tailError = null;

// ReSharper disable once ConvertClosureToMethodGroup
            foreach (var result in Parsers.Select(x => ctx.Parse(x)))
            {
                if (result.Success || tailError == null)
                {
                    if (tailError != null)
                    {
                        result.TailError = ctx.Expected(result.TailError, tailError);
                    }

                    yield return(result);

                    tailError = result.TailError;
                }
                else
                {
                    var error = result.Error as SymbolExpectedError;
                    if (error != null)
                    {
                        yield return(ctx.Expected(tailError, error));
                    }
                    else
                    {
                        yield return(result);
                    }
                }

                if (!result.Success)
                {
                    yield break;
                }
            }
        }
Exemplo n.º 5
0
        protected override IEnumerable <ParseResult> OnParse(ParseContext ctx)
        {
            if (Definition == null)
            {
                throw new Exception(String.Format("LateDefined \"{0}\" Not yet defined.", Name));
            }

            var match = ctx.Parse(Definition);

            if (!match.Success)
            {
                return(Single(match.Error.Priority < 0 ? ctx.Expected(this) : match));
            }

            return(!_collapse?Single(match) : Enumerable.Empty <ParseResult>());
        }