Beispiel #1
0
        public static IStringable BuildListElement(List <Token> tokens)
        {
            if (Brackets.ContainsIndependentBracketsPairs(tokens, BracketsType.Square))
            {
                return(null);
            }

            string name = tokens[0].GetContent();

            tokens.RemoveAt(tokens.Count - 1);
            tokens.RemoveAt(0);
            tokens.RemoveAt(0);

            if (!InterVariables.GetInstance().Contains(name, InterVarType.List))
            {
                throw new SyntaxErrorException("ERROR! List " + name + " not found. Impossible to take element from it.");
            }

            INumerable inu = NumerableBuilder.Build(tokens);

            if (inu.IsNull())
            {
                throw new SyntaxErrorException("ERROR! Impossible to take element from list " + name + ". Index identificator cannot be read as number.");
            }
            else
            {
                return(new ListElementRefer(name, inu));
            }
        }
Beispiel #2
0
        public static IStringable Build(List <Token> tokens)
        {
            if (Brackets.ContainsIndependentBracketsPairs(tokens, BracketsType.Normal))
            {
                return(null);
            }

            List <Token> tokensCopy = tokens.Select(t => t.Clone()).ToList();

            string name = tokensCopy[0].GetContent().ToLower();

            tokensCopy.RemoveAt(tokensCopy.Count - 1);
            tokensCopy.RemoveAt(0);
            tokensCopy.RemoveAt(0);

            List <Argument> args = ArgumentsExtractor.GetArguments(tokensCopy);

            if (name.Equals("hex") || name.Equals("binary") || name.Equals("month") ||
                name.Equals("weekday"))
            {
                return(BuildNum(name, args));
            }
            else if (name.Equals("substring"))
            {
                return(BuildSubstring(name, args));
            }
            else if (name.Equals("upper") || name.Equals("lower") || name.Equals("tolower") || name.Equals("toupper") ||
                     name.Equals("digits") || name.Equals("letters") || name.Equals("trim") || name.Equals("name") ||
                     name.Equals("fullname") || name.Equals("extension"))
            {
                return(BuildStr(name, args));
            }
            else if (name.Equals("commonbeginning") || name.Equals("commonending") || name.Equals("concatenate") ||
                     name.Equals("concatenated"))
            {
                return(BuildLis(name, args));
            }
            else if (name.Equals("filled") || name.Equals("fill") || name.Equals("repeat") || name.Equals("repeated"))
            {
                return(BuildStrNum(name, args));
            }
            else if (name.Equals("beforetext") || name.Equals("aftertext") || name.Equals("textbefore") || name.Equals("textafter"))
            {
                return(BuildStrStr(name, args));
            }

            return(null);
        }
Beispiel #3
0
        public static IBoolable Build(List <Token> tokens)
        {
            if (Brackets.ContainsIndependentBracketsPairs(tokens, BracketsType.Normal))
            {
                return(null);
            }

            List <Token> tokensCopy = tokens.Select(t => t.Clone()).ToList();

            string name = tokensCopy[0].GetContent().ToLower();

            tokensCopy.RemoveAt(tokensCopy.Count - 1);
            tokensCopy.RemoveAt(0);
            tokensCopy.RemoveAt(0);

            List <Argument> args = ArgumentsExtractor.GetArguments(tokensCopy);

            if (name.Equals("exist") || name.Equals("exists") || name.Equals("empty") ||
                name.Equals("emptydirectory") || name.Equals("iscorrect") ||
                name.Equals("isdirectory") || name.Equals("isfile") ||
                name.Equals("hidden") || name.Equals("readonly"))
            {
                return(BuildStr(name, args));
            }
            else if (name.Equals("existinside"))
            {
                return(BuildStrStr(name, args));
            }
            else if (name.Equals("emptylist") || name.Equals("listisempty"))
            {
                return(BuildLis(name, args));
            }
            else if (name.Equals("contain") || name.Equals("contains"))
            {
                return(BuildLisStr(name, args));
            }
            else if (name.Equals("samedate") || name.Equals("samedates") ||
                     name.Equals("thesamedate") || name.Equals("thesamedates") ||
                     name.Equals("sameclock") || name.Equals("sameclocks") ||
                     name.Equals("thesameclock") || name.Equals("thesameclocks"))
            {
                return(BuildTimTim(name, args));
            }

            return(null);
        }
Beispiel #4
0
        public static ITimeable Build(List <Token> tokens)
        {
            if (Brackets.ContainsIndependentBracketsPairs(tokens, BracketsType.Normal))
            {
                return(null);
            }

            List <Token> tokensCopy = tokens.Select(t => t.Clone()).ToList();

            string name = tokensCopy[0].GetContent().ToLower();

            tokensCopy.RemoveAt(tokensCopy.Count - 1);
            tokensCopy.RemoveAt(0);
            tokensCopy.RemoveAt(0);

            List <Argument> args = ArgumentsExtractor.GetArguments(tokensCopy);

            if (name.Equals("date"))
            {
                return(BuildNumNumNum(name, args));
            }
            else if (name.Equals("newyear") || name.Equals("christmas") || name.Equals("easter"))
            {
                return(BuildNum(name, args));
            }
            else if (name.Equals("access") || name.Equals("creation") || name.Equals("modification"))
            {
                return(BuildStr(name, args));
            }
            else if (name.Equals("tomorrow") || name.Equals("yesterday") || name.Equals("today"))
            {
                return(BuildWithArgs023(name, args));
            }

            return(null);
        }
Beispiel #5
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 IBoolable Build(List <Token> tokens)
        {
            /// INITIAL CHECKING

            // check is is empty
            if (tokens.Count == 0)
            {
                throw new SyntaxErrorException("ERROR! Variable declaration is empty.");
            }

            // check if contains not allowed tokens
            Token wwtok = TokenGroups.WrongTokenInExpression(tokens);

            if (!wwtok.GetTokenType().Equals(TokenType.Null))
            {
                return(null);
            }

            // check brackets
            if (!Brackets.CheckCorrectness(tokens))
            {
                return(null);
            }

            // 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;
            }

            // check is is empty again after removing brackets
            if (tokens.Count == 0)
            {
                throw new SyntaxErrorException("ERROR! Variable declaration is empty.");
            }

            /// BOOL BUILDING

            // try to build simple one-element Boolable
            if (tokens.Count == 1)
            {
                if (tokens[0].GetTokenType().Equals(TokenType.Variable))
                {
                    string str = tokens[0].GetContent();
                    if (InterVariables.GetInstance().Contains(str, InterVarType.Bool))
                    {
                        return(new BoolVariableRefer(str));
                    }
                    else
                    {
                        return(null);
                    }
                }
                if (tokens[0].GetTokenType().Equals(TokenType.BoolConstant))
                {
                    if (tokens[0].GetContent().Equals("true"))
                    {
                        return(new BoolConstant(true));
                    }
                    else
                    {
                        return(new BoolConstant(false));
                    }
                }
            }

            // try to build IN function
            if (TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.In))
            {
                IBoolable iboo = BuildIn(tokens);
                if (!iboo.IsNull())
                {
                    return(iboo);
                }
            }

            // try to build LIKE function
            if (TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.Like))
            {
                IBoolable iboo = BuildLike(tokens);
                if (!iboo.IsNull())
                {
                    return(iboo);
                }
            }

            // try to build BETWEEN function
            if (TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.And) &&
                TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.Between))
            {
                IBoolable iboo = BuildBetween(tokens);
                if (!iboo.IsNull())
                {
                    return(iboo);
                }
            }

            // try to build time comparison IS AFTER/IS BEFORE
            if (ContainsOneTimeComparingToken(tokens))
            {
                IBoolable iboo = BuildTimeComparison(tokens);
                if (!iboo.IsNull())
                {
                    return(iboo);
                }
            }

            // try to build comparison = != > < >= <=
            if (ContainsOneComparingToken(tokens))
            {
                IBoolable iboo = BuildComparison(tokens);
                if (!iboo.IsNull())
                {
                    return(iboo);
                }
            }

            // try to build bool ternary
            if (TernaryBuilder.IsPossibleTernary(tokens))
            {
                IBoolable iboo = TernaryBuilder.BuildBoolTernary(tokens);
                if (!iboo.IsNull())
                {
                    return(iboo);
                }
            }

            // try to build bool function
            if (Functions.IsPossibleFunction(tokens))
            {
                IBoolable iboo = BoolFunction.Build(tokens);
                if (!iboo.IsNull())
                {
                    return(iboo);
                }
            }

            // try to build expression: many elements with operators or, and, xor, not
            if (ContainsLogicTokens(tokens))
            {
                return(BuildExpression(tokens));
            }
            else
            {
                return(null);
            }
        }
Beispiel #7
0
        public static IListable Build(List <Token> tokens)
        {
            // try to build Stringable
            IStringable ist = StringableBuilder.Build(tokens);

            if (!ist.IsNull())
            {
                return(ist as IListable);
            }

            // 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 'empty list'
            if (tokens.Count == 2 && tokens[0].GetTokenType().Equals(TokenType.Variable) && tokens[1].GetTokenType().Equals(TokenType.Variable) &&
                tokens[0].GetContent().ToLower().Equals("empty") && tokens[1].GetContent().ToLower().Equals("list"))
            {
                return(new EmptyList());
            }

            // try to build small arrow function
            if (tokens.Where(t => t.GetTokenType().Equals(TokenType.SmallArrow)).Count() == 1)
            {
                IListable smallArrow = BuildSmallArrowFunction(tokens);
                if (!smallArrow.IsNull())
                {
                    return(smallArrow);
                }
            }

            string str = tokens[0].GetContent();

            // try to build list variable reference - just one word and it is a name
            if (tokens.Count == 1 && tokens[0].GetTokenType().Equals(TokenType.Variable) && InterVariables.GetInstance().Contains(str, InterVarType.List))
            {
                return(new ListVariableRefer(str));
            }

            // try to build list expression
            if (InterVariables.GetInstance().Contains(str, InterVarType.List))
            {
                IListable listEx = BuildListExpression(tokens, str);
                if (!listEx.IsNull())
                {
                    return(listEx);
                }
            }

            // try to build list ternary
            if (TernaryBuilder.IsPossibleTernary(tokens))
            {
                IListable ilist = TernaryBuilder.BuildListTernary(tokens);
                if (!ilist.IsNull())
                {
                    return(ilist);
                }
            }

            // try to build listed lists/strings: many Listables/Stringables divided by commas
            if (TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.Comma))
            {
                IListable listed = BuildListed(tokens);
                if (!listed.IsNull())
                {
                    return(listed);
                }
            }

            throw new SyntaxErrorException("ERROR! Unknown error in code syntax.");
        }
Beispiel #8
0
        public static INumerable Build(List <Token> tokens)
        {
            if (Brackets.ContainsIndependentBracketsPairs(tokens, BracketsType.Normal))
            {
                return(null);
            }

            List <Token> tokensCopy = tokens.Select(t => t.Clone()).ToList();

            string name = tokensCopy[0].GetContent().ToLower();

            tokensCopy.RemoveAt(tokensCopy.Count - 1);
            tokensCopy.RemoveAt(0);
            tokensCopy.RemoveAt(0);

            List <Argument> args = ArgumentsExtractor.GetArguments(tokensCopy);

            if (name.Equals("round") || name.Equals("floor") || name.Equals("ceil") || name.Equals("ceiling") ||
                name.Equals("sqrt") || name.Equals("ln") || name.Equals("log") ||
                name.Equals("log10") || name.Equals("kb") || name.Equals("mb") ||
                name.Equals("gb") || name.Equals("tb") || name.Equals("pb") || name.Equals("k") ||
                name.Equals("kk") || name.Equals("factorial"))
            {
                return(BuildNum(name, args));
            }
            else if (name.Equals("power") || name.Equals("pow"))
            {
                return(BuildNumNum(name, args));
            }
            else if (name.Equals("pi") || name.Equals("e") || name.Equals("goldenratio"))
            {
                return(BuildEmpty(name, args));
            }
            else if (name.Equals("number") || name.Equals("length") || name.Equals("year") || name.Equals("size"))
            {
                return(BuildStr(name, args));
            }
            else if (name.Equals("min") || name.Equals("max") || name.Equals("average") || name.Equals("avg") ||
                     name.Equals("mean") || name.Equals("sum") || name.Equals("product"))
            {
                return(BuildNums(name, args));
            }
            else if (name.Equals("lengthofshortest") || name.Equals("lengthoflongest"))
            {
                return(BuildLis(name, args));
            }
            else if (name.Equals("indexof"))
            {
                return(BuildStrStr(name, args));
            }
            else if (name.Equals("yearday") || name.Equals("dayofyear"))
            {
                return(BuildTim(name, args));
            }
            else if (name.Equals("yearsbetween") || name.Equals("monthsbetween") || name.Equals("daysbetween") ||
                     name.Equals("hoursbetween") || name.Equals("minutesbetween") || name.Equals("secondsbetween"))
            {
                return(BuildTimTim(name, args));
            }

            return(null);
        }
Beispiel #9
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);
            }
        }
Beispiel #10
0
        public static INumerable Build(List <Token> tokens)
        {
            // try to build Boolable
            IBoolable ibo = BoolableBuilder.Build(tokens);

            if (!ibo.IsNull())
            {
                return(ibo as INumerable);
            }

            // 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 Numerable
            if (tokens.Count == 1)
            {
                if (tokens[0].GetTokenType().Equals(TokenType.Variable))
                {
                    string str = tokens[0].GetContent();
                    if (InterVariables.GetInstance().Contains(str, InterVarType.Number))
                    {
                        return(new NumericVariableRefer(str));
                    }
                    else
                    {
                        // try to build reference to element of time variable
                        INumerable inum = BuildTimeVariableRefer(tokens[0]);
                        if (!inum.IsNull())
                        {
                            return(inum);
                        }
                    }
                }
                if (tokens[0].GetTokenType().Equals(TokenType.NumericConstant))
                {
                    return(new NumericConstant(tokens[0].GetNumericContent()));
                }
            }

            // try to build numeric ternary
            if (TernaryBuilder.IsPossibleTernary(tokens))
            {
                INumerable inum = TernaryBuilder.BuildNumericTernary(tokens);
                if (!inum.IsNull())
                {
                    return(inum);
                }
            }

            // try to build "count" and "count inside"
            if (tokens[0].GetTokenType().Equals(TokenType.Count))
            {
                INumerable inum = BuildCount(tokens);
                if (!inum.IsNull())
                {
                    return(inum);
                }
            }

            // try to build numeric function
            if (Functions.IsPossibleFunction(tokens))
            {
                INumerable inu = NumericFunction.Build(tokens);
                if (!inu.IsNull())
                {
                    return(inu);
                }
            }

            // try to build expression: many elements with operators +, -, *, /, %
            if (TokenGroups.ContainsArithmeticTokensOutsideBrackets(tokens))
            {
                return(BuildExpression(tokens));
            }
            else
            {
                return(null);
            }
        }