Пример #1
0
        public static ITimeable BuildTimeTernary(List <Token> tokens)
        {
            IBoolable condition = BoolableBuilder.Build(GetTernaryCondition(tokens));

            if (condition.IsNull())
            {
                return(null);
            }

            ITimeable confirmationCase = TimeableBuilder.Build(GetTernaryConfirmation(tokens));

            if (confirmationCase.IsNull())
            {
                return(null);
            }

            ITimeable negationCase = TimeableBuilder.Build(GetTernaryNegation(tokens));

            if (negationCase.IsNull())
            {
                return(null);
            }

            return(new TimeTernary(condition, confirmationCase, negationCase));
        }
Пример #2
0
        private static ITimeable BuildFromDateAndClock(List <Token> tokens)
        {
            List <Token> beforeComma = new List <Token>();
            List <Token> afterComma  = new List <Token>();
            int          level       = 0;
            bool         pastComma   = false;

            foreach (Token tok in tokens)
            {
                if (tok.GetTokenType().Equals(TokenType.BracketOn))
                {
                    level++;
                }
                if (tok.GetTokenType().Equals(TokenType.BracketOff))
                {
                    level--;
                }

                if (tok.GetTokenType().Equals(TokenType.Comma) && level == 0)
                {
                    pastComma = true;
                }
                else
                {
                    if (pastComma)
                    {
                        afterComma.Add(tok);
                    }
                    else
                    {
                        beforeComma.Add(tok);
                    }
                }
            }

            ITimeable itim = TimeableBuilder.Build(beforeComma);

            if (itim.IsNull())
            {
                return(null);
            }

            IClock clock = BuildClock(afterComma);

            if (clock.IsNull())
            {
                return(null);
            }

            if (itim is Time)
            {
                (itim as Time).SetNewClock(clock);
                return(itim);
            }
            else
            {
                return(new TimeableWithClock(itim, clock));
            }
        }
Пример #3
0
        private static IBoolable BuildBetweenTimes(ITimeable tim, List <Token> left, List <Token> right)
        {
            ITimeable timLeft  = TimeableBuilder.Build(left);
            ITimeable timRight = TimeableBuilder.Build(right);

            if (timLeft.IsNull() || timRight.IsNull())
            {
                return(null);
            }
            else
            {
                return(new BetweenTimes(tim, timLeft, timRight));
            }
        }
Пример #4
0
        private static IBoolable BuildBetween(List <Token> tokens)
        {
            int betweenIndex = TokenGroups.IndexOfTokenOutsideBrackets(tokens, TokenType.Between);
            int andIndex     = TokenGroups.IndexOfTokenOutsideBrackets(tokens, TokenType.And);

            if (andIndex < betweenIndex)
            {
                return(null);
            }

            if (betweenIndex == andIndex - 1)
            {
                throw new SyntaxErrorException("ERROR! Expression 'between' do not contain definition of value of left boundary.");
            }

            if (betweenIndex == 0)
            {
                throw new SyntaxErrorException("ERROR! Expression 'between' starts with keyword 'between' and thus do not contain comparing value.");
            }

            if (andIndex == tokens.Count - 1)
            {
                throw new SyntaxErrorException("ERROR! Expression 'between' ends with keyword 'and' and thus do not contain definition of value of right boundary.");
            }

            List <Token> valueTokens = tokens.Take(betweenIndex).ToList();
            List <Token> leftTokens  = tokens.GetRange(betweenIndex + 1, andIndex - betweenIndex - 1);
            List <Token> rightTokens = tokens.Skip(andIndex + 1).ToList();

            INumerable inum = NumerableBuilder.Build(valueTokens);

            if (!inum.IsNull())
            {
                return(BuildBetweenNumbers(inum, leftTokens, rightTokens));
            }

            ITimeable itim = TimeableBuilder.Build(valueTokens);

            if (!itim.IsNull())
            {
                return(BuildBetweenTimes(itim, leftTokens, rightTokens));
            }

            return(null);
        }
Пример #5
0
        private static IBoolable BuildTimeComparison(List <Token> tokens)
        {
            int index = tokens.TakeWhile(x => !TokenGroups.IsTimeComparingSign(x.GetTokenType())).Count();

            if (index == 0 || index == tokens.Count - 1)
            {
                return(null);
            }

            ComparisonType type        = tokens[index].GetTokenType().Equals(TokenType.IsAfter) ? ComparisonType.Bigger : ComparisonType.Smaller;
            List <Token>   leftTokens  = tokens.GetRange(0, index);
            List <Token>   rightTokens = tokens.GetRange(index + 1, tokens.Count - index - 1);
            ITimeable      leftL       = TimeableBuilder.Build(leftTokens);
            ITimeable      rightL      = TimeableBuilder.Build(rightTokens);

            if (leftL.IsNull() || rightL.IsNull())
            {
                return(null);
            }

            return(new TimeComparison(leftL, rightL, type));
        }
Пример #6
0
        public static ITimeable BuildRelativeTime(List <Token> tokens)
        {
            List <RelativeTimeStruct> relativeTimes = new List <RelativeTimeStruct>();
            List <Token> currentTokens = new List <Token>();

            foreach (Token tok in tokens)
            {
                currentTokens.Add(tok);

                if (IsTimeDirection(tok))
                {
                    List <RelativeTimeStruct> rtss = BuildRelativeTimeStructs(currentTokens.Take(currentTokens.Count - 1).ToList(), currentTokens.Last());
                    if (rtss.IsNull())
                    {
                        return(null);
                    }
                    else
                    {
                        relativeTimes.AddRange(rtss);
                    }
                    currentTokens.Clear();
                }
            }

            if (currentTokens.Count == 0)
            {
                throw new SyntaxErrorException("ERROR! Relative time expression do not have definition for reference time.");
            }

            ITimeable itim = TimeableBuilder.Build(currentTokens);

            if (itim.IsNull())
            {
                return(null);
            }

            return(new RelativeTimeExpression(relativeTimes, itim));
        }
Пример #7
0
        public static IStringable Build(List <Token> tokens)
        {
            // try to build Numerable
            INumerable inu = NumerableBuilder.Build(tokens);

            if (!inu.IsNull())
            {
                return(inu as IStringable);
            }

            // try to build Timeable
            ITimeable itim = TimeableBuilder.Build(tokens);

            if (!itim.IsNull())
            {
                return(itim as IStringable);
            }

            // remove first and last bracket if it is there
            while (tokens[0].GetTokenType().Equals(TokenType.BracketOn) && tokens[tokens.Count - 1].GetTokenType().Equals(TokenType.BracketOff) &&
                   !Brackets.ContainsIndependentBracketsPairs(tokens, BracketsType.Normal))
            {
                List <Token> tokensCopy = tokens.Select(t => t.Clone()).ToList();
                tokensCopy.RemoveAt(tokens.Count - 1);
                tokensCopy.RemoveAt(0);
                tokens = tokensCopy;
            }

            // try to build simple one-token Stringable
            if (tokens.Count == 1)
            {
                if (tokens[0].GetTokenType().Equals(TokenType.Variable))
                {
                    string str = tokens[0].GetContent();
                    if (InterVariables.GetInstance().Contains(str, InterVarType.String))
                    {
                        return(new StringVariableRefer(str));
                    }
                    else
                    {
                        // try to build reference to date or clock time
                        IStringable istr = BuildTimeVariableRefer(tokens[0]);
                        if (!istr.IsNull())
                        {
                            return(istr);
                        }
                    }
                }
                if (tokens[0].GetTokenType().Equals(TokenType.StringConstant))
                {
                    return(new StringConstant(tokens[0].GetContent()));
                }
            }

            //try to build string function
            if (Functions.IsPossibleFunction(tokens))
            {
                IStringable istr = StringFunction.Build(tokens);
                if (!istr.IsNull())
                {
                    return(istr);
                }
            }

            // try to build string ternary
            if (TernaryBuilder.IsPossibleTernary(tokens))
            {
                IStringable istr = TernaryBuilder.BuildStringTernary(tokens);
                if (!istr.IsNull())
                {
                    return(istr);
                }
            }

            // try to build reference to n-th element of list of strings
            if (tokens.Count > 3 && tokens[0].GetTokenType().Equals(TokenType.Variable) && tokens[1].GetTokenType().Equals(TokenType.SquareBracketOn) &&
                tokens[tokens.Count - 1].GetTokenType().Equals(TokenType.SquareBracketOff))
            {
                IStringable istr = BuildListElement(tokens);
                if (!istr.IsNull())
                {
                    return(istr);
                }
            }

            // try to build concatenated string -> text merged by +
            if (TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.Plus) && !TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.Comma))
            {
                return(BuildConcatenated(tokens));
            }
            else
            {
                return(null);
            }
        }