示例#1
0
        private void ReplaceDefinedTokens(PreprocessorLine line)
        {
            ThrowIfNull(line);

            int          i    = 0;
            List <Token> list = line.TokenList;

            while ((i < list.Count))
            {
                Token token = list[i];
                if (token.TokenType != TokenType.Word)
                {
                    i += 1;
                    continue;
                }

                Macro macro = null;
                if (_macroMap.TryGetValue(token.Value, out macro))
                {
                    // Remove the original token
                    list.RemoveAt(i);

                    List <Token> replaceList = null;
                    if (macro.IsMethod)
                    {
                        MethodMacro  method = (MethodMacro)macro;
                        List <Token> args   = ParseAndRemoveMacroMethodArguments(list, i);
                        if (args == null)
                        {
                            // Parse did not succeed, move to the next token
                            i += 1;
                        }
                        else
                        {
                            // Insert the tokens
                            replaceList = ReplaceMethodMacro(method, args);
                        }
                    }
                    else
                    {
                        // Use the scanner to create the replacement tokens
                        replaceList = Scanner.TokenizeText(macro.Value, CreateScannerOptions());
                    }

                    if (replaceList != null)
                    {
                        CollapseDoublePounds(replaceList);
                        list.InsertRange(i, replaceList);
                    }
                }
                else
                {
                    i += 1;
                }
            }

            // Do one more pass to check and see if we need a recursive replace
            bool needAnotherPass = false;

            foreach (Token cur in line.TokenList)
            {
                if (cur.TokenType == TokenType.Word && _macroMap.ContainsKey(cur.Value))
                {
                    needAnotherPass = true;
                    break;
                }
            }

            if (needAnotherPass)
            {
                ReplaceDefinedTokens(line);
            }
        }