コード例 #1
0
        private int SkipCasts(ref int i, ref int nParen, ref List <int> parenLoc, ref List <int> curOpLevel)
        {
            int          j        = -1;
            List <Token> tempList = ReadBetweenParens(tokens, i, nParen, ref j);

            if (CastHelper.IsCast(tempList))
            {
                i = j;
                if (tokens[i].Type == TokenType.OpenParen)
                {
                    parenLoc.Add(i);
                    curOpLevel.Add(-1);
                    curOpLevel[nParen] = -1;                                    //impossible to remove
                    nParen++;                                                   //skip the first paren of the cast
                    SkipCasts(ref i, ref nParen, ref parenLoc, ref curOpLevel);
                }
            }
            return(j);
        }
コード例 #2
0
        public static int GetOperator(Expression expr, out int numArgs)
        {
            int foundOp = -1;

            numArgs = -1;
            //go through each level of operators looking for one in the current statement
            for (int level = 0; level < operators.Count; level++)
            {
                List <string> operatorLevel = operators[level];
                List <Token>  tokens        = expr.Tokens;
                //this determines the direction we look through the tokens
                //left to right means we start need move backwards through the tokens, and right to left vice versa
                bool leftToRight = GetOpAssoc(level);
                //current paren level
                int nParen = 0;
                for (int i = leftToRight ? tokens.Count - 1 : 0; leftToRight?i >= 0 : i < tokens.Count; i += leftToRight ? -1 : 1)
                {
                    int          j           = -1;
                    bool         isCast      = false;
                    Token        token       = tokens[i];
                    List <Token> parenTokens = null;
                    if (token.Type == TokenType.CloseParen)
                    {
                        nParen--;
                    }
                    else if (token.Type == TokenType.OpenParen)
                    {
                        if (level == CastLevel)
                        {
                            parenTokens = ReadBetweenParens(tokens, i, nParen, ref j);
                            isCast      = CastHelper.IsCast(parenTokens);
                            if (i > 0 && isCast)
                            {
                                isCast = tokens[i - 1].Type != TokenType.StringType && tokens[i - 1] != "sizeof";
                            }
                        }
                        if (!isCast)
                        {
                            nParen++;
                        }
                    }
                    //almost all code before this is just to check for the special cast operator to see if we found one
                    //casts are horrible horrible creatures and cause tons of problems
                    if (nParen == 0 && ((token.Type == TokenType.OperatorType && operatorLevel.Contains(token.Text)) ||
                                        isCast == true))
                    {
                        //if we've found something and this new operator is not a cast screw it, its almost gauranteed
                        //not to work. This is meant to fix issues like (#cast) #DoubleOp #var
                        if (foundOp >= 0 && !isCast)
                        {
                            continue;
                        }
                        numArgs = GetNumArgs(level, token);
                        //we've found an operator, now we need to see if its actually what we want
                        if (leftToRight)
                        {
                            //if its not a double op were good
                            if (!DoubleOps.Contains(token))
                            {
                                return(i);
                            }
                            //otherwise check if the context is valid. If so mark that we found it
                            if (i > 0 && (tokens[i - 1].Type != TokenType.OperatorType ||
                                          (token == "*" && tokens[i - 1] == "*" && numArgs == 1)))
                            {
                                foundOp = i;
                            }
                        }
                        else
                        {
                            if (isCast)
                            {
                                //if its a cast and its not at the very end were good
                                if (j < tokens.Count)
                                {
                                    return(i);
                                }
                            }
                            //same as above
                            else if (!DoubleOps.Contains(token))
                            {
                                return(i);
                            }
                            if (i + 1 < tokens.Count && (tokens[i + 1].Type != TokenType.OperatorType ||
                                                         (token == "*" && tokens[i + 1] == "*" && numArgs == 1)))
                            {
                                foundOp = i;
                            }
                        }
                    }
                    //skip over what we thought was a cast
                    if (isCast)
                    {
                        i += parenTokens.Count - 1;
                    }
                }
            }
            return(foundOp);
        }