Esempio n. 1
0
        private string ResolveMacros(string source, IEnumerable <string> restrictedDefines, IEnumerable <PreprocessorDefine> args,
                                     FileContext serverContext, ContentType contentType)
        {
            var reader = new StringPreprocessorReader(source);
            var writer = new StringPreprocessorWriter();

            var parms = new PreprocessorParams(reader, writer, string.Empty, null, serverContext, contentType, null);

            parms.restrictedDefines = restrictedDefines;
            parms.args            = args;
            parms.resolvingMacros = true;

            var lastText = source;
            var counter  = 32;                  // To prevent infinite loop with recursive define

            while (Preprocess(parms).DocumentAltered&& counter <= 32)
            {
                var newText = writer.Text;
                if (newText == lastText)
                {
                    break;
                }
                lastText = newText;

                parms.result.DocumentAltered = false;
                parms.args   = null;                    // Only apply the arguments to the first round
                parms.reader = new StringPreprocessorReader(newText);
                parms.writer = writer = new StringPreprocessorWriter();

                counter++;
            }
            return(writer.Text);
        }
Esempio n. 2
0
        private ConditionResult EvaluateCondition(PreprocessorParams p, string conditionStr, string fileName, int pos)
        {
            // Run preprocessor on the condition string
            var reader = new StringPreprocessorReader(conditionStr);
            var writer = new StringPreprocessorWriter();
            var parms  = new PreprocessorParams(reader, writer, string.Empty, null, p.fileContext, ContentType.Condition, null);

            parms.allowDirectives = false;
            parms.args            = p.args;
            Preprocess(parms);

            // Evaluate the condition string
            var parser     = new CodeParser(writer.Text);
            var tokenGroup = PreprocessorTokens.GroupToken.Parse(null, parser, null);
            var finalValue = tokenGroup.Value;

            ConditionResult ret;

            if (finalValue.HasValue)
            {
                if (finalValue.Value != 0)
                {
                    ret = ConditionResult.Positive;
                }
                else
                {
                    ret = ConditionResult.Negative;
                }
            }
            else
            {
#if DEBUG
                Log.Debug("Condition returned indeterminate: {0}\r\n  File Name: {1}\r\n  Position: {2}\r\n  Resolved: {3}",
                          conditionStr, fileName, pos, writer.Text);
#endif
                ret = ConditionResult.Indeterminate;
            }
            return(ret);
        }