Beispiel #1
0
        public static IStringable BuildStrNumNum(string name, List <Argument> args)
        {
            if (args.Count != 3)
            {
                throw new SyntaxErrorException("ERROR! Function " + name + " has to have 3 arguments: one text and two numbers.");
            }

            IStringable istr = StringableBuilder.Build(args[0].tokens);
            INumerable  inu1 = NumerableBuilder.Build(args[1].tokens);
            INumerable  inu2 = NumerableBuilder.Build(args[2].tokens);

            if (istr.IsNull())
            {
                throw new SyntaxErrorException("ERROR! First argument of function " + name + " cannot be read as text.");
            }
            if (inu1.IsNull())
            {
                throw new SyntaxErrorException("ERROR! Second argument of function " + name + " cannot be read as number.");
            }
            if (inu2.IsNull())
            {
                throw new SyntaxErrorException("ERROR! Third argument of function " + name + " cannot be read as number.");
            }

            if (name.Equals("substring"))
            {
                return(new FuncSubstring__3args(istr, inu1, inu2));
            }
            throw new SyntaxErrorException("ERROR! Function " + name + " not identified.");
        }
Beispiel #2
0
        public static IStringable BuildStrStr(string name, List <Argument> args)
        {
            if (args.Count != 2)
            {
                throw new SyntaxErrorException("ERROR! Function " + name + " has to have 2 arguments: two texts.");
            }

            IStringable istr1 = StringableBuilder.Build(args[0].tokens);
            IStringable istr2 = StringableBuilder.Build(args[1].tokens);

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

            if (name.Equals("beforetext") || name.Equals("textbefore"))
            {
                return(new FuncBeforeText(istr1, istr2));
            }
            else if (name.Equals("aftertext") || name.Equals("textafter"))
            {
                return(new FuncAfterText(istr1, istr2));
            }
            throw new SyntaxErrorException("ERROR! Function " + name + " not identified.");
        }
Beispiel #3
0
        public static IStringable BuildStrNum(string name, List <Argument> args)
        {
            if (args.Count != 2)
            {
                throw new SyntaxErrorException("ERROR! Function " + name + " has to have 2 arguments: one text and one number.");
            }

            IStringable istr = StringableBuilder.Build(args[0].tokens);
            INumerable  inu  = NumerableBuilder.Build(args[1].tokens);

            if (istr.IsNull())
            {
                throw new SyntaxErrorException("ERROR! First argument of function " + name + " cannot be read as text.");
            }
            if (inu.IsNull())
            {
                throw new SyntaxErrorException("ERROR! Second argument of function " + name + " cannot be read as number.");
            }

            if (name.Equals("filled") || name.Equals("fill"))
            {
                return(new FuncFilled(istr, inu));
            }
            else if (name.Equals("repeat") || name.Equals("repeated"))
            {
                return(new FuncRepeat(istr, inu));
            }
            else if (name.Equals("substring"))
            {
                return(new FuncSubstring__2args(istr, inu));
            }
            throw new SyntaxErrorException("ERROR! Function " + name + " not identified.");
        }
        public static IBoolable BuildIn(List <Token> tokens)
        {
            int index = tokens.TakeWhile(x => !x.GetTokenType().Equals(TokenType.In)).Count();

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

            List <Token> leftTokens  = tokens.GetRange(0, index);
            List <Token> rightTokens = tokens.GetRange(index + 1, tokens.Count - index - 1);

            IStringable istr = StringableBuilder.Build(leftTokens);

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

            IListable ilis = ListableBuilder.Build(rightTokens);

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

            return(new In(istr, ilis));
        }
Beispiel #5
0
        private static INumerable BuildCountInside(List <Token> tokens)
        {
            int index = TokenGroups.IndexOfTokenOutsideBrackets(tokens, TokenType.Inside);

            if (index == tokens.Count - 1)
            {
                throw new SyntaxErrorException("ERROR! Expression 'count inside' do not contain information about referent location.");
            }

            if (index == 0)
            {
                throw new SyntaxErrorException("ERROR! Expression 'count inside' do not contain information about referent list of elements.");
            }

            IListable ilist = ListableBuilder.Build(tokens.Take(index).ToList());

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

            IStringable istr = StringableBuilder.Build(tokens.Skip(index + 1).ToList());

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

            return(new CountInside(ilist, istr));
        }
Beispiel #6
0
        public static IBoolable BuildLisStr(string name, List <Argument> args)
        {
            if (args.Count != 2)
            {
                throw new SyntaxErrorException("ERROR! Function " + name + " has to have 2 arguments: one list and one text.");
            }

            IListable   ilis = ListableBuilder.Build(args[0].tokens);
            IStringable istr = StringableBuilder.Build(args[1].tokens);

            if (ilis.IsNull())
            {
                throw new SyntaxErrorException("ERROR! First argument of function " + name + " cannot be read as list.");
            }
            if (istr.IsNull())
            {
                throw new SyntaxErrorException("ERROR! Second argument of function " + name + " cannot be read as text.");
            }

            if (name.Equals("contain") || name.Equals("contains"))
            {
                return(new FuncContain(ilis, istr));
            }
            throw new SyntaxErrorException("ERROR! Function " + name + " not identified.");
        }
Beispiel #7
0
        public static IStringable BuildStringTernary(List <Token> tokens)
        {
            IBoolable condition = BoolableBuilder.Build(GetTernaryCondition(tokens));

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

            IStringable confirmationCase = StringableBuilder.Build(GetTernaryConfirmation(tokens));

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

            IStringable negationCase = StringableBuilder.Build(GetTernaryNegation(tokens));

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

            return(new StringTernary(condition, confirmationCase, negationCase));
        }
Beispiel #8
0
        public static IBoolable BuildStrStr(string name, List <Argument> args)
        {
            if (args.Count != 2)
            {
                throw new SyntaxErrorException("ERROR! Function " + name + " has to have 2 text arguments.");
            }

            IStringable istr1 = StringableBuilder.Build(args[0].tokens);
            IStringable istr2 = StringableBuilder.Build(args[1].tokens);

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

            if (name.Equals("existinside") || name.Equals("existsinside"))
            {
                return(new FuncExistinside(istr1, istr2));
            }
            throw new SyntaxErrorException("ERROR! Function " + name + " not identified.");
        }
        public static IBoolable BuildLike(List <Token> tokens)
        {
            int index = tokens.TakeWhile(x => !x.GetTokenType().Equals(TokenType.Like)).Count();

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

            List <Token> leftTokens  = tokens.GetRange(0, index);
            List <Token> rightTokens = tokens.GetRange(index + 1, tokens.Count - index - 1);

            IStringable istr = StringableBuilder.Build(leftTokens);

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

            if (rightTokens.Count == 1 && rightTokens[0].GetTokenType().Equals(TokenType.StringConstant))
            {
                string phrase = rightTokens[0].GetContent();
                CheckLikePhraseCorrectness(phrase);
                return(new Like(istr, phrase));
            }
            else
            {
                return(null);
            }
        }
Beispiel #10
0
        public static ITimeable BuildStr(string name, List <Argument> args)
        {
            if (args.Count != 1)
            {
                throw new SyntaxErrorException("ERROR! Function " + name + " has to have 1 text argument.");
            }

            IStringable istr = StringableBuilder.Build(args[0].tokens);

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

            if (name.Equals("access"))
            {
                return(new FuncAccess(istr));
            }
            else if (name.Equals("creation"))
            {
                return(new FuncCreation(istr));
            }
            else if (name.Equals("modification"))
            {
                return(new FuncModification(istr));
            }

            throw new SyntaxErrorException("ERROR! Function " + name + " not identified.");
        }
Beispiel #11
0
        public static INumerable BuildStr(string name, List <Argument> args)
        {
            if (args.Count != 1)
            {
                throw new SyntaxErrorException("ERROR! Function " + name + " has to have 1 text argument.");
            }

            IStringable istr = StringableBuilder.Build(args[0].tokens);

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

            if (name.Equals("number"))
            {
                return(new FuncNumber(istr));
            }
            if (name.Equals("length"))
            {
                return(new FuncLength(istr));
            }
            if (name.Equals("year"))
            {
                return(new FuncYear(istr));
            }
            if (name.Equals("size"))
            {
                return(new FuncSize(istr));
            }
            throw new SyntaxErrorException("ERROR! Function " + name + " not identified.");
        }
        public static ICommand Build(List <Token> tokens, bool forced)
        {
            TokenType type = tokens[0].GetTokenType();

            tokens.RemoveAt(0);

            if (tokens.Where(t => t.GetTokenType().Equals(TokenType.To)).Count() > 1)
            {
                throw new SyntaxErrorException("ERROR! In " + GetName(type) + " 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 " + GetName(type) + " is too short and do not contain all necessary information.");
            }

            IStringable expression2 = StringableBuilder.Build(part2);

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

            if (part1.Count == 0)
            {
                return(BuildSimple(type, expression2, forced));
            }
            else
            {
                IListable expression1 = ListableBuilder.Build(part1);
                if (expression1.IsNull())
                {
                    throw new SyntaxErrorException("ERROR! First part of command " + GetName(type) + " cannot be read as list.");
                }
                return(BuildComplex(type, expression1, expression2, forced));
            }
        }
Beispiel #13
0
        public static ICommand Build(List <Token> tokens, bool forced)
        {
            TokenType type = tokens[0].GetTokenType();

            tokens.RemoveAt(0);

            int toIndex = TokenGroups.IndexOfTokenOutsideBrackets(tokens, TokenType.To);
            int asIndex = TokenGroups.IndexOfTokenOutsideBrackets(tokens, TokenType.As);

            if (asIndex < toIndex)
            {
                return(null);
            }
            if (toIndex == asIndex - 1)
            {
                throw new SyntaxErrorException("ERROR! Command " + GetName(type) + " do not have definition of destination directory.");
            }
            if (asIndex == tokens.Count - 1)
            {
                throw new SyntaxErrorException("ERROR! Command " + GetName(type) + " do not have definition of new name for file/directory.");
            }

            List <Token> listTokens        = tokens.Take(toIndex).ToList();
            List <Token> destinationTokens = tokens.GetRange(toIndex + 1, asIndex - toIndex - 1);
            List <Token> nameTokens        = tokens.Skip(asIndex + 1).ToList();

            IStringable destination = StringableBuilder.Build(destinationTokens);

            if (destination.IsNull())
            {
                throw new SyntaxErrorException("ERROR! In command " + GetName(type) + " definition of destination directory cannot be read as text.");
            }
            IStringable name = StringableBuilder.Build(nameTokens);

            if (name.IsNull())
            {
                throw new SyntaxErrorException("ERROR! In command " + GetName(type) + " definition of new name for file/directory cannot be read as text.");
            }

            if (listTokens.Count == 0)
            {
                return(BuildSimple(type, destination, name, forced));
            }
            else
            {
                IListable list = ListableBuilder.Build(listTokens);
                if (list.IsNull())
                {
                    throw new SyntaxErrorException("ERROR! In command " + GetName(type) + " definition of list of files and directories is not correct.");
                }
                return(BuildComplex(type, list, destination, name, forced));
            }
        }
Beispiel #14
0
        public static IStringable BuildStr(string name, List <Argument> args)
        {
            if (args.Count != 1)
            {
                throw new SyntaxErrorException("ERROR! Function " + name + " has to have 1 text argument.");
            }

            IStringable istr = StringableBuilder.Build(args[0].tokens);

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

            if (name.Equals("upper") || name.Equals("toupper"))
            {
                return(new FuncUpper(istr));
            }
            else if (name.Equals("lower") || name.Equals("tolower"))
            {
                return(new FuncLower(istr));
            }
            else if (name.Equals("digits"))
            {
                return(new FuncDigits(istr));
            }
            else if (name.Equals("letters"))
            {
                return(new FuncLetters(istr));
            }
            else if (name.Equals("trim"))
            {
                return(new FuncTrim(istr));
            }
            else if (name.Equals("name"))
            {
                return(new FuncName(istr));
            }
            else if (name.Equals("fullname"))
            {
                return(new FuncFullname(istr));
            }
            else if (name.Equals("extension"))
            {
                return(new FuncExtension(istr));
            }

            throw new SyntaxErrorException("ERROR! Function " + name + " not identified.");
        }
Beispiel #15
0
        // functions are grouped by their arguments
        // every set of arguments is one method below

        public static IBoolable BuildStr(string name, List <Argument> args)
        {
            if (args.Count != 1)
            {
                throw new SyntaxErrorException("ERROR! Function " + name + " has to have 1 text argument.");
            }

            IStringable istr = StringableBuilder.Build(args[0].tokens);

            if (istr.IsNull())
            {
                throw new SyntaxErrorException("ERROR! Argument of function " + name + " cannot be read as text.");
            }
            if (name.Equals("exist") || name.Equals("exists"))
            {
                return(new FuncExist(istr));
            }
            else if (name.Equals("empty") || name.Equals("emptydirectory"))
            {
                return(new FuncEmpty(istr));
            }
            else if (name.Equals("iscorrect"))
            {
                return(new FuncIscorrect(istr));
            }
            else if (name.Equals("isdirectory"))
            {
                return(new FuncIsdirectory(istr));
            }
            else if (name.Equals("isfile"))
            {
                return(new FuncIsfile(istr));
            }
            else if (name.Equals("hidden"))
            {
                return(new FuncHidden(istr));
            }
            else if (name.Equals("readonly"))
            {
                return(new FuncReadonly(istr));
            }

            throw new SyntaxErrorException("ERROR! Function " + name + " not identified.");
        }
Beispiel #16
0
        public static ICommand Build(List <Token> tokens)
        {
            string name = tokens[0].GetContent();

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

            List <Token> indexTokens = new List <Token>();

            while (tokens.First().GetTokenType() != TokenType.SquareBracketOff)
            {
                indexTokens.Add(tokens.First());
                tokens.RemoveAt(0);
            }
            tokens.RemoveAt(0);

            INumerable index = NumerableBuilder.Build(indexTokens);

            if (index.IsNull())
            {
                throw new SyntaxErrorException("ERROR! Index of element of list " + name + " cannot be read as number.");
            }
            if (tokens.First().GetTokenType() != TokenType.Equals)
            {
                return(null);
            }

            tokens.RemoveAt(0);

            IStringable newValue = StringableBuilder.Build(tokens);

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

            return(new ListElementDeclaration(name, newValue, index));
        }
Beispiel #17
0
        private static IListable BuildSmallArrowFunction(List <Token> tokens)
        {
            bool         unique    = false;
            List <Token> leftSide  = new List <Token>();
            List <Token> rightSide = new List <Token>();
            bool         pastTo    = false;

            foreach (Token tok in tokens)
            {
                if (tok.GetTokenType().Equals(TokenType.SmallArrow))
                {
                    pastTo = true;
                }
                else
                {
                    if (pastTo)
                    {
                        rightSide.Add(tok);
                    }
                    else
                    {
                        leftSide.Add(tok);
                    }
                }
            }

            if (leftSide.Count == 0)
            {
                throw new SyntaxErrorException("ERROR! Left side of Small Arrow Function is empty.");
            }
            if (rightSide.Count == 0)
            {
                throw new SyntaxErrorException("ERROR! Right side of Small Arrow Function is empty.");
            }

            if (rightSide.First().GetTokenType().Equals(TokenType.Unique))
            {
                unique = true;
                rightSide.RemoveAt(0);
                if (rightSide.Count == 0)
                {
                    throw new SyntaxErrorException("ERROR! Right side of Small Arrow Function contains only one word: unique.");
                }
            }

            IListable ilist = ListableBuilder.Build(leftSide);

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

            IStringable istr = StringableBuilder.Build(rightSide);

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

            return(new SmallArrow(ilist, istr, unique));
        }
        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));
                    }
                }
            }
        }
Beispiel #19
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 #20
0
        // string concatenation
        // considers numeric expressions inside with signs '+'
        public static IStringable BuildConcatenated(List <Token> tokens)
        {
            List <Token>       currentTokens = new List <Token>();
            List <Token>       reserve       = new List <Token>();
            List <IStringable> elements      = new List <IStringable>();
            int level = 0;

            for (int i = 0; i < tokens.Count; i++)
            {
                if (tokens[i].GetTokenType().Equals(TokenType.BracketOn))
                {
                    level++;
                }
                if (tokens[i].GetTokenType().Equals(TokenType.BracketOff))
                {
                    level--;
                }

                if (tokens[i].GetTokenType().Equals(TokenType.Plus) && level == 0)
                {
                    if (currentTokens.Count > 0)
                    {
                        IStringable ist = StringableBuilder.Build(currentTokens);

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

                        if (ist is INumerable || ist is IBoolable)
                        {
                            reserve.AddRange(currentTokens);
                            reserve.Add(tokens[i]);
                            currentTokens.Clear();
                        }
                        else
                        {
                            if (reserve.Count > 0)
                            {
                                reserve.RemoveAt(reserve.Count - 1);
                                elements.Add(NumerableBuilder.Build(reserve) as IStringable);
                                reserve.Clear();
                            }

                            elements.Add(ist);
                            currentTokens.Clear();
                        }
                    }
                }
                else
                {
                    currentTokens.Add(tokens[i]);
                }
            }

            if (currentTokens.Count > 0)
            {
                IStringable ist = StringableBuilder.Build(currentTokens);

                if (reserve.Count > 0)
                {
                    if (ist is INumerable || ist is IBoolable)
                    {
                        reserve.AddRange(currentTokens);
                        elements.Add(NumerableBuilder.Build(reserve) as IStringable);
                    }
                    else
                    {
                        reserve.RemoveAt(reserve.Count - 1);
                        elements.Add(NumerableBuilder.Build(reserve) as IStringable);
                        elements.Add(ist);
                    }
                }
                else
                {
                    if (ist.IsNull())
                    {
                        return(null);
                    }
                    else
                    {
                        elements.Add(ist);
                    }
                }
            }

            if (elements.Count > 0)
            {
                return(new StringExpression(elements));
            }

            return(null);
        }
Beispiel #21
0
        public static ICommand BuildCreateFrom(List <Token> tokens, bool forced, bool directory)
        {
            List <Token> part1    = new List <Token>();
            List <Token> part2    = new List <Token>();
            bool         pastFrom = false;

            foreach (Token tok in tokens)
            {
                if (tok.GetTokenType().Equals(TokenType.From))
                {
                    pastFrom = true;
                }
                else
                {
                    if (pastFrom)
                    {
                        part2.Add(tok);
                    }
                    else
                    {
                        part1.Add(tok);
                    }
                }
            }
            if (part2.Count == 0)
            {
                throw new SyntaxErrorException("ERROR! Source in " + (directory ? "directory" : "file")
                                               + " creation command is empty.");
            }

            IListable   ilist;
            IStringable istring = StringableBuilder.Build(part2);

            if (part1.Count == 0)
            {
                ilist = new StringVariableRefer("this");
            }
            else
            {
                ilist = ListableBuilder.Build(part1);
            }

            if (ilist.IsNull())
            {
                throw new SyntaxErrorException("ERROR! There are is something wrong with new " + (directory ? "directory" : "file") +
                                               " name in " + (directory ? "directory" : "file") + " creation command syntax.");
            }
            if (istring.IsNull())
            {
                throw new SyntaxErrorException("ERROR! There are is something wrong with source " + (directory ? "directory" : "file") +
                                               " name in " + (directory ? "directory" : "file") + " creation command syntax.");
            }

            if (directory)
            {
                return(new CreateDirectoryFrom(istring, ilist, forced));
            }
            else
            {
                return(new CreateFileFrom(istring, ilist, forced));
            }
        }
Beispiel #22
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.");
        }