コード例 #1
0
        /// <summary>
        /// Tries to match provided rule one time.
        /// </summary>
        /// <param name="context">Parsing context</param>
        /// <returns>
        /// Original branch, if rule doesn't match, otherwise both - original branch and
        /// branch with consumed optional match.
        /// </returns>
        public async Task <IReadOnlyCollection <IParsingContext <TToken> > > Match(IParsingContext <TToken> context)
        {
            var branchesWithOptionalMatch = await rule.Match(context.Clone());

            if (branchesWithOptionalMatch != null && branchesWithOptionalMatch.Count > 0)
            {
                return
                    (new List <IParsingContext <TToken> >(branchesWithOptionalMatch)
                {
                    context
                });
            }

            // Optional rule didn't match, return original parsing context
            return(new[] { context });
        }
コード例 #2
0
        /// <summary>
        /// Matches decorated parser rule preceeded by necessary global rules.
        /// </summary>
        /// <param name="context">Parsing context</param>
        /// <returns>All possible parsing branches, if rule matches successfully, otherwise null.</returns>
        public Task <IReadOnlyCollection <IParsingContext <TToken> > > Match(IParsingContext <TToken> context)
        {
            var globalRules =
                excludedGlobalRules == null || excludedGlobalRules.Count == 0
                    ? context.Grammar.GlobalRules
                    : context.Grammar.GlobalRules.Where(IsIncludedGlobalRule).ToArray();

            if (globalRules.Length > 0)
            {
                var globalRulesRule =
                    new ZeroOrMoreTimesRule <TToken>(
                        globalRules.Length == 1
                            ? globalRules[0]
                            : new OneOfRule <TToken>(globalRules));

                return(new SequenceRule <TToken>(globalRulesRule, rule).Match(context));
            }
            else
            {
                return(rule.Match(context));
            }
        }