Exemplo n.º 1
0
        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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 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;
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        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);
        }
Exemplo n.º 8
0
        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."));
        }
Exemplo n.º 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));
        }
Exemplo n.º 10
0
        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));
        }
Exemplo n.º 11
0
 public RuleTokenKeeper(RuleTokenKeeper tokenKeeper)
 {
     _tokens = tokenKeeper._tokens;
     _index  = tokenKeeper._index;
 }