示例#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
        public static IBoolable BuildTimTim(string name, List <Argument> args)
        {
            if (args.Count != 2)
            {
                throw new SyntaxErrorException("ERROR! Function " + name + " has to have 2 time arguments.");
            }

            ITimeable itim1 = TimeableBuilder.Build(args[0].tokens);
            ITimeable itim2 = TimeableBuilder.Build(args[1].tokens);

            if (itim1.IsNull())
            {
                throw new SyntaxErrorException("ERROR! First argument of function " + name + " cannot be read as time.");
            }
            if (itim2.IsNull())
            {
                throw new SyntaxErrorException("ERROR! Second argument of function " + name + " cannot be read as time.");
            }

            if (name.Equals("samedate") || name.Equals("samedates") ||
                name.Equals("thesamedate") || name.Equals("thesamedates"))
            {
                return(new FuncSamedates(itim1, itim2));
            }
            else if (name.Equals("sameclock") || name.Equals("sameclocks") ||
                     name.Equals("thesameclock") || name.Equals("thesameclocks"))
            {
                return(new FuncSamedates(itim1, itim2));
            }

            throw new SyntaxErrorException("ERROR! Function " + name + " not identified.");
        }
示例#3
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));
            }
        }
示例#4
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));
            }
        }
示例#5
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);
        }
示例#6
0
        public static INumerable BuildTimTim(string name, List <Argument> args)
        {
            if (args.Count != 2)
            {
                throw new SyntaxErrorException("ERROR! Function " + name + " has to have 2 time arguments.");
            }

            ITimeable itim1 = TimeableBuilder.Build(args[0].tokens);
            ITimeable itim2 = TimeableBuilder.Build(args[1].tokens);

            if (itim1.IsNull())
            {
                throw new SyntaxErrorException("ERROR! First argument of function " + name + " cannot be read as time.");
            }
            if (itim2.IsNull())
            {
                throw new SyntaxErrorException("ERROR! Second argument of function " + name + " cannot be read as time.");
            }

            if (name.Equals("yearsbetween"))
            {
                return(new Func__TimeBetween(itim1, itim2, TimeVariableType.Year));
            }
            else if (name.Equals("monthsbetween"))
            {
                return(new Func__TimeBetween(itim1, itim2, TimeVariableType.Month));
            }
            else if (name.Equals("daysbetween"))
            {
                return(new Func__TimeBetween(itim1, itim2, TimeVariableType.Day));
            }
            else if (name.Equals("hoursbetween"))
            {
                return(new Func__TimeBetween(itim1, itim2, TimeVariableType.Hour));
            }
            else if (name.Equals("minutesbetween"))
            {
                return(new Func__TimeBetween(itim1, itim2, TimeVariableType.Minute));
            }
            else if (name.Equals("secondsbetween"))
            {
                return(new Func__TimeBetween(itim1, itim2, TimeVariableType.Second));
            }
            throw new SyntaxErrorException("ERROR! Function " + name + " not identified.");
        }
示例#7
0
        public static INumerable BuildTim(string name, List <Argument> args)
        {
            if (args.Count != 1)
            {
                throw new SyntaxErrorException("ERROR! Function " + name + " has to have 1 time argument.");
            }

            ITimeable itim = TimeableBuilder.Build(args[0].tokens);

            if (itim.IsNull())
            {
                throw new SyntaxErrorException("ERROR! Argument of function " + name + " cannot be read as time.");
            }

            if (name.Equals("yearday") || name.Equals("dayofyear"))
            {
                return(new FuncYearday(itim));
            }
            throw new SyntaxErrorException("ERROR! Function " + name + " not identified.");
        }
示例#8
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));
        }
示例#9
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));
        }
示例#10
0
        public static ITimeable Build(List <Token> tokens)
        {
            // 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 Timeable
            if (tokens.Count == 1)
            {
                if (tokens[0].GetTokenType().Equals(TokenType.Variable))
                {
                    string str = tokens[0].GetContent();
                    if (InterVariables.GetInstance().Contains(str, InterVarType.Time))
                    {
                        return(new TimeVariableRefer(str));
                    }
                }
            }

            //try to build time function
            if (Functions.IsPossibleFunction(tokens))
            {
                ITimeable itim = TimeFunction.Build(tokens);
                if (!itim.IsNull())
                {
                    return(itim);
                }
            }

            // try to build time ternary
            if (TernaryBuilder.IsPossibleTernary(tokens))
            {
                ITimeable itim = TernaryBuilder.BuildTimeTernary(tokens);
                if (!itim.IsNull())
                {
                    return(itim);
                }
            }

            // try to build relative time expression
            if (tokens.Where(t => IsTimeDirection(t)).Count() > 0)
            {
                ITimeable itim = BuildRelativeTime(tokens);
                if (!itim.IsNull())
                {
                    return(itim);
                }
            }

            if (HasOneComma(tokens))
            {
                // try to build Timeable from date and clock
                ITimeable itim = BuildFromDateAndClock(tokens);
                if (!itim.IsNull())
                {
                    return(itim);
                }
            }
            else
            {
                // try to build Timeable from date only
                if (ContainMonth(tokens))
                {
                    ITimeable itim = BuildFromDate(tokens);
                    if (!itim.IsNull())
                    {
                        return(itim);
                    }
                }

                // try to build Timeable from clock only
                if (ContainSemicolons(tokens))
                {
                    ITimeable itim = BuildFromClock(tokens);
                    if (!itim.IsNull())
                    {
                        return(itim);
                    }
                }
            }

            return(null);
        }
        public static ICommand Build(List <Token> tokens)
        {
            string name = tokens[0].GetContent();

            tokens.RemoveAt(0);
            tokens.RemoveAt(0);

            if (tokens.Count == 0)
            {
                throw new SyntaxErrorException("ERROR! Variable " + name + " has no assigned value.");
            }

            if (name.Contains('.'))
            {
                return(BuildWithPoint(tokens, name));
            }

            if (!InterVariables.GetInstance().Contains(name))
            {
                IListable value = ListableBuilder.Build(tokens);
                if (value.IsNull())
                {
                    throw new SyntaxErrorException("ERROR! There are is something wrong with assigning value to variable " + name + ".");
                }

                if (value is IBoolable)
                {
                    InterVariables.GetInstance().Add(name, InterVarType.Bool);
                    return(new BoolDeclaration(name, (IBoolable)value));
                }
                else if (value is INumerable)
                {
                    InterVariables.GetInstance().Add(name, InterVarType.Number);
                    return(new NumericDeclaration(name, (INumerable)value));
                }
                else if (value is ITimeable)
                {
                    InterVariables.GetInstance().Add(name, InterVarType.Time);
                    return(new TimeDeclaration(name, (ITimeable)value));
                }
                else if (value is IStringable)
                {
                    InterVariables.GetInstance().Add(name, InterVarType.String);
                    return(new StringDeclaration(name, (IStringable)value));
                }
                else
                {
                    InterVariables.GetInstance().Add(name, InterVarType.List);
                    return(new ListDeclaration(name, value));
                }
            }
            else
            {
                InterVar ivar = InterVariables.GetInstance().GetVar(name);

                if (ivar.IsBool())
                {
                    IBoolable value = BoolableBuilder.Build(tokens);
                    if (value.IsNull())
                    {
                        throw new SyntaxErrorException("ERROR! Value assigned to variable " + name + " must be logical.");
                    }
                    return(new BoolDeclaration(name, value));
                }
                else
                {
                    if (ivar.IsNumber())
                    {
                        INumerable value = NumerableBuilder.Build(tokens);
                        if (value.IsNull())
                        {
                            throw new SyntaxErrorException("ERROR! Value assigned to variable " + name + " must be numeric.");
                        }
                        return(new NumericDeclaration(name, value));
                    }
                    else if (ivar.IsTime())
                    {
                        ITimeable value = TimeableBuilder.Build(tokens);
                        if (value.IsNull())
                        {
                            throw new SyntaxErrorException("ERROR! Value assigned to variable " + name + " must be time.");
                        }
                        return(new TimeDeclaration(name, value));
                    }
                    else if (ivar.IsString())
                    {
                        IStringable value = StringableBuilder.Build(tokens);
                        if (value.IsNull())
                        {
                            throw new SyntaxErrorException("ERROR! Value assigned to variable " + name + " must be text.");
                        }
                        return(new StringDeclaration(name, value));
                    }
                    else
                    {
                        IListable value = ListableBuilder.Build(tokens);
                        if (value.IsNull())
                        {
                            throw new SyntaxErrorException("ERROR! Value assigned to variable " + name + " must be list.");
                        }
                        return(new ListDeclaration(name, value));
                    }
                }
            }
        }
        public static ICommand Build(List <Token> tokens)
        {
            TokenType type = tokens[0].GetTokenType();
            string    name = type.Equals(TokenType.Recreate) ? "recreate" : (type.Equals(TokenType.Remodify) ? "remodify" : "reaccess");

            tokens.RemoveAt(0);

            if (tokens.Where(t => t.GetTokenType().Equals(TokenType.To)).Count() > 1)
            {
                throw new SyntaxErrorException("ERROR! In " + name + " command keyword 'to' occurs too many times.");
            }

            List <Token> part1  = new List <Token>();
            List <Token> part2  = new List <Token>();
            bool         pastTo = false;

            foreach (Token tok in tokens)
            {
                if (tok.GetTokenType().Equals(TokenType.To))
                {
                    pastTo = true;
                }
                else
                {
                    if (pastTo)
                    {
                        part2.Add(tok);
                    }
                    else
                    {
                        part1.Add(tok);
                    }
                }
            }

            if (part2.Count == 0)
            {
                throw new SyntaxErrorException("ERROR! Command " + name + " is too short and do not contain all necessary information.");
            }

            ITimeable expression2 = TimeableBuilder.Build(part2);

            if (expression2.IsNull())
            {
                throw new SyntaxErrorException("ERROR! Second part of command " + name + " cannot be read as time.");
            }

            if (part1.Count == 0)
            {
                return(BuildSimple(type, expression2));
            }
            else
            {
                IListable expression1 = ListableBuilder.Build(part1);
                if (expression1.IsNull())
                {
                    throw new SyntaxErrorException("ERROR! First part of command " + name + " cannot be read as list.");
                }
                return(BuildComplex(type, expression1, expression2));
            }
        }
示例#13
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);
            }
        }