Пример #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)));
        }
Пример #2
0
        internal ParseResult(ParseContext ctx, SymbolExpectedError tailError, params ParseResult[] children)
        {
            Parser    = ctx.Parser;
            TailError = tailError;
            Index     = ctx.InitialOffset;
            Length    = ctx.Offset - ctx.InitialOffset;

            _children = children;
            Input     = ctx.Input;
        }
Пример #3
0
        internal ParseResult(ParseContext ctx, SymbolExpectedError tailError, params ParseResult[] children)
        {
            Parser    = ctx.Parser;
            TailError = tailError;
            Index     = ctx.InitialOffset;
            Length    = ctx.Offset - ctx.InitialOffset;

            _trim = ctx.Parent != null && ctx.Parent.WhitespacePolicy == WhitespacePolicy.Ignore;

            _children = children;
            Input     = ctx.Input;
        }
Пример #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;
                }
            }
        }