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()); }
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); }
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); }
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); }
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); }
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; }
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); }
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); }
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); }
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); }
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); }
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); }
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()); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
public void Evalute1() { string text = "1"; Preprocessor expression = new Preprocessor(TestUtil.GetTextStream(text)); Assert.AreEqual(true, expression.EvaluateExpression()); }
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); }
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); }
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); }
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); }
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())); }