Ejemplo n.º 1
0
        private List <Token> ReplaceMethodMacro(MethodMacro method, List <Token> args)
        {
            // First run the replacement
            List <Token> retList = method.Replace(args);

            // When creating the arguments for a macro, non-trivial arguments (1+2) come accross
            // as text macros.  For those items we need to reparse them here and put them back into the stream.
            // Have to do this after the above loop so that ## and # are processed correctly
            int i = 0;

            while (i < retList.Count)
            {
                Token cur = retList[i];
                if (cur.TokenType == TokenType.Text && args.IndexOf(cur) >= 0)
                {
                    retList.RemoveAt(i);
                    retList.InsertRange(i, Scanner.TokenizeText(cur.Value, _scanner.Options));
                }

                i += 1;
            }

            return(retList);
        }
Ejemplo n.º 2
0
        public bool TryEvaluate(string expr, out ExpressionValue result)
        {
            List <Token> list = Scanner.TokenizeText(expr, _opts);

            return(TryEvaluate(list, out result));
        }
Ejemplo n.º 3
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);
            }
        }