Esempio n. 1
0
        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));
        }
Esempio n. 2
0
        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);
            }
        }
Esempio n. 3
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));
        }
Esempio n. 4
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));
        }
Esempio n. 5
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.");
        }
Esempio n. 6
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));
        }
Esempio n. 7
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);
        }