Esempio n. 1
0
        public override ParseResult Parse(ParseContext ctx)
        {
            var oldPos = ctx.CurrentPosition;

            var         matchedParseMark    = 0;
            ParseResult matchedChoiceResult = null;
            var         matchedChoice       = Elements.FirstOrDefault(e =>
            {
                ctx.CurrentPosition = oldPos;
                ctx.StartMatch();
                var result = e.Element.Parse(ctx);

                var resultIsSuccess = result?.IsSuccess ?? false;
                if (resultIsSuccess)
                {
                    ctx.EndMatch(SaveChoice, this);
                }
                else
                {
                    ctx.UndoMatch();
                }

                matchedParseMark = !(result?.IsOptionallyMatched ?? false) ? result?.ParseMark ?? 0 : 0;

                if (!resultIsSuccess)
                {
                    ctx.CurrentPosition = oldPos;
                }

                if (resultIsSuccess)
                {
                    matchedChoiceResult = result;
                }
                return(resultIsSuccess);
            });

            if (matchedChoice == null)
            {
                return(ParseResult.Failure(ctx));
            }

            var success = ParseResult.Success(ctx);

            if (!(matchedChoiceResult?.IsOptionallyMatched ?? false))
            {
                success.ParseMark ^= matchedChoice.ParseMark ^ matchedParseMark;
            }

            return(success);
        }