Ejemplo n.º 1
0
        public void ConsumedCharsAreSpaced()
        {
            string text = "//j\r\n" +
                    "text";

              Preprocessor preprocessor = new Preprocessor(TestUtil.GetTextStream(text));
              StreamReader result = preprocessor.Process();

              Assert.AreEqual("   \r\ntext", result.ReadToEnd());
        }
Ejemplo n.º 2
0
        public void CommentBlockRemoved()
        {
            string text = "/* test */abc";

              Preprocessor preprocessor = new Preprocessor(TestUtil.GetTextStream(text));
              string result = preprocessor.Process().ReadToEnd();

              Assert.IsTrue(result.EndsWith("abc"));
              Assert.IsTrue(result.StartsWith(" "));
              Assert.AreEqual(text.Length, result.Length);
        }
Ejemplo n.º 3
0
        public void CommentBlockWithHalfCommentIsRemoved()
        {
            string text = "/*/ test \r\n" +
                    "*/abc";

              Preprocessor preprocessor = new Preprocessor(TestUtil.GetTextStream(text));
              string result = preprocessor.Process().ReadToEnd();

              Assert.IsTrue(result.EndsWith("abc"));
              Assert.IsFalse(result.Contains("test"));
              Assert.AreEqual(text.Length, result.Length);
        }
Ejemplo n.º 4
0
        public void DefinesAreRemoved()
        {
            string text = "#define ObjectIdFromObjectTypeAndInstanceId(cls,id)     (System::UInt32)((((System::UInt32)(cls)) << 22) | ((System::UInt32)(id)))\r\n" +
                    "text";

              Preprocessor preprocessor = new Preprocessor(TestUtil.GetTextStream(text));
              string result = preprocessor.Process().ReadToEnd();

              Assert.IsTrue(result.EndsWith("text"));
              Assert.IsFalse(result.Contains("System"));
              Assert.AreEqual(text.Length, result.Length);
        }
Ejemplo n.º 5
0
        public void BlockCommentInsideBlockCommentIsConsumed()
        {
            string text = "/* don't care \r\n" +
                    "don not care \r\n" +
                    "*/include";

              Preprocessor preprocessor = new Preprocessor(TestUtil.GetTextStream(text));
              string result = preprocessor.Process().ReadToEnd();

              Assert.IsTrue(result.Contains("include"));
              Assert.IsFalse(result.Contains("/"));
              Assert.IsFalse(result.Contains("*"));
              Assert.AreEqual(text.Length, result.Length);
        }
Ejemplo n.º 6
0
        public FileAnalyzer(StreamReader filestream, ICCMNotify callback, object context, bool suppressMethodSignatures, string filename,
            ParserSwitchBehavior switchBehavior = ParserSwitchBehavior.TraditionalInclude)
        {
            this.buffer = new char[filestream.BaseStream.Length];
              filestream.Read(this.buffer, 0, this.buffer.Length);

              var processStream = new StreamReader(new MemoryStream(Encoding.Default.GetBytes(this.buffer)));

              // run through preprocessor before setting up parser...
              Preprocessor preprocessor = new Preprocessor(processStream);
              StreamReader stream = preprocessor.Process();

              // this construct should be fixed and support OCP
              if (filename.ToLower().EndsWith(".js") || filename.ToLower().EndsWith(".ts"))
            this.parser = LookAheadLangParser.CreateJavascriptParser(stream);
              else
            this.parser = LookAheadLangParser.CreateCppParser(stream);

              this.callback = callback;
              this.context = context;
              this.suppressMethodSignatures = suppressMethodSignatures;
              this.filename = filename;
              this.switchBehavior = switchBehavior;
        }
Ejemplo n.º 7
0
        public void UnclosedQuotedTextInComment()
        {
            string text = "// not properly ' closed \r\n" +
                    "/* don't care \r\n" +
                    "*/include";

              Preprocessor preprocessor = new Preprocessor(TestUtil.GetTextStream(text));
              string result = preprocessor.Process().ReadToEnd();

              Assert.IsTrue(result.Contains("include"));
              Assert.IsFalse(result.Contains("/"));
              Assert.IsFalse(result.Contains("*"));
              Assert.AreEqual(text.Length, result.Length);
        }
Ejemplo n.º 8
0
        public void TestMultilineCppCommentWithExtraStarOnlyRemovesComment()
        {
            string text = "/* comment \r\n more comment\r\n text **/text";
              Preprocessor preprocessor = new Preprocessor(TestUtil.GetTextStream(text));
              string result = preprocessor.Process().ReadToEnd();

              Assert.AreEqual("text", result.Trim());
              Assert.AreEqual(text.Length, result.Length);
        }
Ejemplo n.º 9
0
        public void TestTabAfterEndIfDoesntThrowParserOff()
        {
            string text = "#ifdef false \r\n" +
                    "#ifdef false \r\n" +
                    " // something \r\n" +
                    "#else \r\n" +
                    " // something \r\n" +
                    "#endif\r\n" +
                    "#endif\t //comment\r\n" +
                    "text";

              Preprocessor preprocessor = new Preprocessor(TestUtil.GetTextStream(text));
              string result = preprocessor.Process().ReadToEnd();

              Assert.AreEqual("text", result.Trim());
              Assert.AreEqual(text.Length, result.Length);
        }
Ejemplo n.º 10
0
        public void PragmaRemoved()
        {
            string text = "#pragma warning(x: disable)\r\n" +
                    "text";

              Preprocessor preprocessor = new Preprocessor(TestUtil.GetTextStream(text));
              string result = preprocessor.Process().ReadToEnd();

              Assert.IsTrue(result.EndsWith("text"));
              Assert.IsFalse(result.Contains("disable"));
              Assert.AreEqual(text.Length, result.Length);
        }
Ejemplo n.º 11
0
        public void SingleLineCommentEmbeddedInBlock()
        {
            string text = "/*// don't care about this one...\r\n*/def";

              Preprocessor preprocessor = new Preprocessor(TestUtil.GetTextStream(text));
              string result = preprocessor.Process().ReadToEnd();

              Assert.IsTrue(result.Contains("def"));
              Assert.IsFalse(result.Contains("care"));
              Assert.AreEqual(text.Length, result.Length);
        }
Ejemplo n.º 12
0
        public void IfElseParanthesis()
        {
            string text = "#if(DEBUG)\r\n" +
                    " ddd \r\n" +
                    "#else \r\n" +
                    "uuuu\r\n" +
                    "#endif\r\n";

              Preprocessor preprocessor = new Preprocessor(TestUtil.GetTextStream(text));
              string result = preprocessor.Process().ReadToEnd();

              Assert.IsTrue(result.Contains("uuuu"));
              Assert.IsFalse(result.Contains("ddd"));
              Assert.AreEqual(text.Length, result.Length);
        }
Ejemplo n.º 13
0
        public void NoDirectives()
        {
            string text = "the lazy brown fox";

              Preprocessor preprocessor = new Preprocessor(TestUtil.GetTextStream(text));

              StreamReader result = preprocessor.Process();

              Assert.AreEqual(text, result.ReadToEnd());
        }
Ejemplo n.º 14
0
        public void DiffStylePartialConditional()
        {
            string text = "152,153d153 \r\n " +
                    "< #endif \r\n " +
                    " public override string ToString() { return 1.ToString(); } \r\n " +
                    "188,22";

              Preprocessor preprocessor = new Preprocessor(TestUtil.GetTextStream(text));
              string result = preprocessor.Process().ReadToEnd();

              Assert.IsTrue(result.StartsWith("152,153d153"));
              Assert.IsTrue(result.EndsWith("188,22"));
              Assert.AreEqual(text.Length, result.Length);
        }
Ejemplo n.º 15
0
        public void NestedIfDefsWithParentInclusive()
        {
            string text = "#if 1\r\n" +
                    "#if MY_MUTEX \r\n" +
                    "# define mutex() (1)\r\n" +
                    "#else\r\n" +
                    "static int mutex() {} \r\n" +
                    "#endif\r\n" +
                    "#endif\r\n";

              Preprocessor preprocessor = new Preprocessor(TestUtil.GetTextStream(text));
              string result = preprocessor.Process().ReadToEnd();

              Assert.IsTrue(result.Contains("mutex()"));
              Assert.AreEqual(text.Length, result.Length);
        }
Ejemplo n.º 16
0
        public void MultiLineDefineWithinIfdef()
        {
            string text = "#ifdef 1\r\n" +
                    "#define MCR one \\ \r\n" +
                    "  two \\ \r\n" +
                    "  three \r\n" +
                    "endif \r\n" +
                    "included";

              Preprocessor preprocessor = new Preprocessor(TestUtil.GetTextStream(text));
              string result = preprocessor.Process().ReadToEnd();

              Assert.IsTrue(result.EndsWith("included"));
              Assert.IsFalse(result.Contains("one"));
              Assert.IsFalse(result.Contains("two"));
              Assert.IsFalse(result.Contains("three"));
              Assert.AreEqual(text.Length, result.Length);
        }
Ejemplo n.º 17
0
        public void DiffStyledStreamRemovesBlock()
        {
            string text = "152,153d153 \r\n " +
                    "< #if DEBUG \r\n " +
                    "< 155,167d154 \r\n " +
                    " public override string ToString() { return 1.ToString(); } \r\n " +
                    " < #endif \r\n" +
                    "188,22";

              Preprocessor preprocessor = new Preprocessor(TestUtil.GetTextStream(text));
              string result = preprocessor.Process().ReadToEnd();

              Assert.IsTrue(result.StartsWith("152,153d153"));
              Assert.IsTrue(result.EndsWith("188,22"));
              Assert.IsFalse(result.Contains("ToString()"));
              Assert.AreEqual(text.Length, result.Length);
        }
Ejemplo n.º 18
0
        public void IfDef1()
        {
            string text = "#ifdef 1 \r\n" +
                    "the lazy brown fox \r\n" +
                    "#endif\r\n";

              string expect =
                    "         \r\n" +
                    "the lazy brown fox \r\n" +
                    "      \r\n";

              Preprocessor preprocessor = new Preprocessor(TestUtil.GetTextStream(text));
              string result = preprocessor.Process().ReadToEnd();

              Assert.AreEqual(expect, result);
              Assert.IsTrue(result.Contains("lazy brown fox"));
              Assert.AreEqual(text.Length, result.Length);
        }
Ejemplo n.º 19
0
        public void IfNotDefined()
        {
            string text = "#ifndef UNK_DEFINE \r\n" +
                    "#ifndef UNK2 \r\n" +
                    " the lazy brown fox \r\n" +
                    "#endif\r\n" +
                    "#else \r\n" +
                    " energizing bunny \r\n" +
                    "#endif\r\n";

              Preprocessor preprocessor = new Preprocessor(TestUtil.GetTextStream(text));
              string result = preprocessor.Process().ReadToEnd();

              Assert.IsTrue(result.Contains("the lazy brown fox"));
              Assert.AreEqual(text.Length, result.Length);
        }
Ejemplo n.º 20
0
        public void IfnDefNestedInsideIfdef1()
        {
            string text = "#ifndef 1\r\n" +
                    "#ifndef MYS \r\n" +
                    "FINDERS KEEP\r\n" +
                    "#else\r\n" +
                    "static int mutex() {} \r\n" +
                    "#endif\r\n" +
                    "#endif\r\n" +
                    "bunny";

              Preprocessor preprocessor = new Preprocessor(TestUtil.GetTextStream(text));
              string result = preprocessor.Process().ReadToEnd();

              Assert.IsTrue(result.Contains("bunny"));
              Assert.IsFalse(result.Contains("static int"));
              Assert.AreEqual(text.Length, result.Length);
        }
Ejemplo n.º 21
0
        public void Evalute1()
        {
            string text = "1";

              Preprocessor expression = new Preprocessor(TestUtil.GetTextStream(text));

              Assert.AreEqual(true, expression.EvaluateExpression());
        }
Ejemplo n.º 22
0
        public void IfDef0InsideText()
        {
            string text = "text" +
                    "#ifdef 0 \r\n" +
                    " the lazy brown fox \r\n" +
                    "#endif\r\n" +
                    "more text";

              Preprocessor preprocessor = new Preprocessor(TestUtil.GetTextStream(text));
              string result = preprocessor.Process().ReadToEnd();

              Assert.IsTrue(result.StartsWith("text"));
              Assert.IsTrue(result.EndsWith("more text"));
              Assert.IsFalse(result.Contains("if"));
              Assert.AreEqual(text.Length, result.Length);
        }
Ejemplo n.º 23
0
        public void MultipleIfDefsShouldExclude()
        {
            string text = "#ifdef _THE_DEFINE\r\n" +
                    "#ifdef _THE_DE2\r\n " +
                    "#ifdef ___DD\r\n" +
                    " the lazy brown fox \r\n" +
                    "#endif \r\n" +
                    "#endif \r\n" +
                    "#endif \r\n" +
                    "bunny";

              Preprocessor preprocessor = new Preprocessor(TestUtil.GetTextStream(text));
              string result = preprocessor.Process().ReadToEnd();

              Assert.IsTrue(result.EndsWith("bunny"));
              Assert.IsFalse(result.Contains("fox"));
              Assert.AreEqual(text.Length, result.Length);
        }
Ejemplo n.º 24
0
        public void IfDefsWithWhiteSpaceIsHandled()
        {
            string text = "# ifdef _THE_DEFINE\r\n" +
                    " yada \r\n " +
                    "# else  ___DD\r\n" +
                    "//comment\r\n" +
                    "bunny\r\n" +
                    "# endif\r\n";

              Preprocessor preprocessor = new Preprocessor(TestUtil.GetTextStream(text));
              string result = preprocessor.Process().ReadToEnd();

              Assert.IsTrue(result.Contains("bunny"));
              Assert.IsFalse(result.Contains("#"));
              Assert.IsFalse(result.Contains("/"));
              Assert.IsFalse(result.Contains("yada"));
              Assert.IsFalse(result.Contains("//"));
              Assert.IsFalse(result.Contains("comment"));
              Assert.AreEqual(text.Length, result.Length);
        }
Ejemplo n.º 25
0
        public void MultipleIfDefsWithElse()
        {
            string text = "#ifdef 1\r\n" +
                    "#ifdef UNK2\r\n" +
                    "  the lazy brown fox \r\n" +
                    "#endif \r\n" +
                    "#endif \r\n" +
                    "yesway2";

              Preprocessor preprocessor = new Preprocessor(TestUtil.GetTextStream(text));
              string result = preprocessor.Process().ReadToEnd();

              Assert.IsTrue(result.EndsWith("yesway2"));
              Assert.IsFalse(result.Contains("fox"));
              Assert.AreEqual(text.Length, result.Length);
        }
Ejemplo n.º 26
0
        public StreamReader Process()
        {
            try
            {
                Stack <bool> blockInclusion = new Stack <bool>();

                blockInclusion.Push(true); // start by including next block of text

                do
                {
                    if (Preprocessor.NextIsIfdef(this.parser))
                    {
                        ConsumeIfDef(this.parser);

                        // if we are currently excluding a block, i.e nested #if's, just push 0 onto the inclusion
                        if (!blockInclusion.Peek())
                        {
                            blockInclusion.Push(false);
                        }
                        else
                        {
                            blockInclusion.Push(EvaluateExpression());
                        }

                        MoveToNextLineSpaceOutput();
                    }
                    else if (Preprocessor.NextIsElse(this.parser) && blockInclusion.Count > 1)
                    {
                        // just reverse the inclusion book-keeping

                        bool blockShouldInclude = blockInclusion.Pop();

                        // check if parent inclusing says no
                        if (!blockInclusion.Peek())
                        {
                            blockInclusion.Push(false);
                        }
                        else
                        {
                            blockInclusion.Push(!blockShouldInclude);
                        }

                        MoveToNextLineSpaceOutput();
                    }
                    else if (Preprocessor.NextIsEndif(this.parser) && blockInclusion.Count > 1)
                    {
                        blockInclusion.Pop();
                        MoveToNextLineSpaceOutput();
                    }
                    else if (Preprocessor.NextIsIfndef(this.parser))
                    {
                        ConsumeIfndef(this.parser);

                        if (!blockInclusion.Peek())
                        {
                            blockInclusion.Push(false);
                        }
                        else
                        {
                            blockInclusion.Push(!EvaluateExpression());
                        }

                        MoveToNextLineSpaceOutput();
                    }
                    else if (Preprocessor.NextIsCommentBlock(this.parser))
                    {
                        ConsumeComments();
                    }
                    else if (this.parser.PeekNextKeyword().Equals("#"))
                    {
                        ConsumeDirectiveMultiLine();
                    }
                    else
                    {
                        if (blockInclusion.Peek())
                        {
                            this.sb.Append(this.parser.NextKeyword());
                        }
                        else
                        {
                            ConumseNextKeyWordSpaceOutput(); // move the stream ahead without including its data...
                        }
                    }
                }while (true);
            }
            catch (EndOfStreamException)
            {
            }
            catch (Exception)
            {
                throw new PreprocessorException();
            }

            return(Preprocessor.GetTextStream(this.sb.ToString()));
        }