private static bool TryTakeMatch(RuleTokenKeeper tokenKeeper, out ApplyIfRuleClause clause)
        {
            var next = tokenKeeper.Next;

            if (next.TokenType == ApplyIfTokenType.Match)
            {
                var work = new RuleTokenKeeper(tokenKeeper);
                work.Take();

                if (work.Next.TokenType == ApplyIfTokenType.OpenParen)
                {
                    work.Take();

                    if (EntityReferenceParser.TryTakeEntityReference(work, out var entityReference))
                    {
                        if (work.Next.TokenType == ApplyIfTokenType.CloseParen)
                        {
                            work.Take();
                        }

                        tokenKeeper.Swap(work);
                        clause = new MatchClause(entityReference);
                        return(true);
                    }
                }
            }

            clause = null;
            return(false);
        }
Exemple #2
0
        internal static bool TryTakeEntityReference(RuleTokenKeeper tokenKeeper, out EntityReference clause)
        {
            var next = tokenKeeper.Next;

            if (next.TokenType == ApplyIfTokenType.Identifier)
            {
                var work   = new RuleTokenKeeper(tokenKeeper);
                var entity = work.Take().Text;

                if (work.Next.TokenType == ApplyIfTokenType.Period)
                {
                    work.Take();
                    if (work.Next.TokenType == ApplyIfTokenType.Identifier)
                    {
                        var field = work.Take().Text;
                        tokenKeeper.Swap(work);
                        clause = new EntityReference(entity, field);
                        return(true);
                    }
                }

                clause = null;
                return(false);
            }

            clause = null;
            return(false);
        }
        private static bool TryTakeRuleSequence(RuleTokenKeeper tokenKeeper, out ApplyIfRuleClause clause)
        {
            var work = new RuleTokenKeeper(tokenKeeper);

            if (TryTakeRuleClause(work, out var left))
            {
                if (work.Next.TokenType == ApplyIfTokenType.Comma)
                {
                    ApplyIfRuleClause right;
                    work.Take();
                    if (TryTakeRuleSequence(work, out right) || TryTakeRuleClause(work, out right))
                    {
                        if (work.Finished)
                        {
                            tokenKeeper.Swap(work);
                            clause = new RuleSequenceClause(left, right);
                            return(true);
                        }
                    }
                }
            }

            clause = null;
            return(false);
        }
        private static bool TryTakeRuleClause(RuleTokenKeeper tokenKeeper, out ApplyIfRuleClause clause)
        {
            if (TryTakeCleanInput(tokenKeeper, out clause) ||
                TryTakeFirstError(tokenKeeper, out clause) ||
                TryTakeMatch(tokenKeeper, out clause))
            {
                return(true);
            }

            clause = null;
            return(false);
        }
Exemple #5
0
        public void Swap(RuleTokenKeeper tokenKeeper)
        {
            var oldTokens = _tokens;

            _tokens = tokenKeeper._tokens;

            var oldIndex = _index;

            _index = tokenKeeper._index;

            tokenKeeper._tokens = oldTokens;
            tokenKeeper._index  = oldIndex;
        }
        private static bool TryTakeFirstError(RuleTokenKeeper tokenKeeper, out ApplyIfRuleClause clause)
        {
            var next = tokenKeeper.Next;

            if (next.TokenType == ApplyIfTokenType.FirstError)
            {
                clause = new FirstErrorClause();
                tokenKeeper.Take();
                return(true);
            }

            clause = null;
            return(false);
        }
        private static bool TryTakeCleanInput(RuleTokenKeeper tokenKeeper, out ApplyIfRuleClause clause)
        {
            var next = tokenKeeper.Next;

            if (next.TokenType == ApplyIfTokenType.CleanInput)
            {
                clause = new CleanInputClause();
                tokenKeeper.Take();
                return(true);
            }

            clause = null;
            return(false);
        }
        private static ApplyIfRuleClause TakeClause(RuleTokenKeeper tokenKeeper)
        {
            ApplyIfRuleClause clause;

            if (TryTakeRuleSequence(tokenKeeper, out clause) ||
                TryTakeRuleClause(tokenKeeper, out clause))
            {
                if (tokenKeeper.Finished)
                {
                    return(clause);
                }
            }

            return(new ApplyIfRuleErrorClause("Unable to parse ApplyIf rule."));
        }
Exemple #9
0
        private static EntityReferenceParseResult PerformParse(IEnumerable <ApplyIfToken> tokens, out string error)
        {
            var tokenKeeper = new RuleTokenKeeper(tokens.ToList());

            if (tokenKeeper.Finished)
            {
                error = "No entity reference found.";
                return(null);
            }

            if (TryTakeEntityReference(tokenKeeper, out var reference))
            {
                error = null;
                return(new EntityReferenceParseResult(reference));
            }

            error = "Unable to parse entity reference";
            return(new EntityReferenceParseResult(error));
        }
        private static ApplyIfRule PerformParse(IEnumerable <ApplyIfToken> tokens, out string error)
        {
            var tokenKeeper = new RuleTokenKeeper(tokens.ToList());

            if (tokenKeeper.Finished)
            {
                error = null;
                return(null);
            }

            var clause = TakeClause(tokenKeeper);

            if (clause is ApplyIfRuleErrorClause errorClause)
            {
                error = errorClause.Error;
            }
            else
            {
                error = null;
            }

            return(new ApplyIfRule(clause));
        }
Exemple #11
0
 public RuleTokenKeeper(RuleTokenKeeper tokenKeeper)
 {
     _tokens = tokenKeeper._tokens;
     _index  = tokenKeeper._index;
 }