public void TestFindAllCharTokens() { var testInput = @"open something named{ accessMod type name { someStatement; someOtherStatement; accessMod type name(someArg){someBody;} somefinalStatement; }}"; var testSubject = new NoFuture.Tokens.XDocFrame(); var testResults = testSubject.FindAllCharTokens(testInput, '{', '}'); System.Diagnostics.Debug.WriteLine(string.Join(",",testResults.Item1)); System.Diagnostics.Debug.WriteLine(string.Join(",",testResults.Item2)); }
public void TestApplyXDocFrame() { var testInput = System.IO.File.ReadAllText(@"C:\Projects\31g\trunk\Code\NoFuture.Tests\Tokens\TokensTestFile.txt"); var testSubject = new XDocFrame('{', '}') { MinTokenSpan = 1 }; var testResults = testSubject.GetXDocFrame(testInput); testSubject.ApplyXDocFrame(testResults, testInput); var testResultsXml = testResults.ToString(); System.IO.File.WriteAllBytes(@"C:\Projects\31g\trunk\Code\NoFuture.Tests\Tokens\ApplyXDocFrameResults.xml", System.Text.Encoding.UTF8.GetBytes(testResultsXml)); }
public void TestFindEnclosingTokens_Char() { var testInput = @"open something named{ accessMod type name { someStatement; someOtherStatement; accessMod type name(someArg){someBody;} somefinalStatement; }}"; var testSubject = new NoFuture.Tokens.XDocFrame(); var testResults = testSubject.FindEnclosingTokens(testInput, '{', '}'); Assert.IsNotNull(testResults); Assert.AreNotEqual(0, testResults.Count); Assert.AreEqual(3, testResults.Count); foreach (var t in testResults) { System.Diagnostics.Debug.WriteLine(string.Format("{0},{1}",t.Start, t.End)); } }
public void TestAppendHeadToken() { var testTokens = new List<NoFuture.Tokens.Token> { new TokenPair(20, 141), new TokenPair(42, 140), new TokenPair(107, 118) }; var testInput = @"open something named{ accessMod type name { someStatement; someOtherStatement; accessMod type name(someArg){someBody;} somefinalStatement; }}"; var testSubject = new NoFuture.Tokens.XDocFrame(); var testResults = testSubject.AppendHeadToken(testTokens, testInput); Assert.IsNotNull(testResults); Assert.AreNotEqual(0,testResults.Count); Assert.AreNotEqual(3, testResults.Count); foreach (var t in testResults) { System.Diagnostics.Debug.WriteLine(string.Format("{0},{1}", t.Start, t.End)); } }
public void TestFindEnclosingTokens_Word() { var testWordStart = "begin"; var testWordEnd = "end"; var testInput = @"some statement; begin somekindOfEnclosure begin firstStatement; secondStatement; end anotherStatement; end more stuff"; var testSubject = new NoFuture.Tokens.XDocFrame(); var testResults = testSubject.FindEnclosingTokens(testInput, testWordStart, testWordEnd); Assert.IsNotNull(testResults); Assert.AreNotEqual(0,testResults.Count); foreach (var t in testResults) { System.Diagnostics.Debug.WriteLine(string.Format("{0},{1}", t.Start, t.End)); System.Diagnostics.Debug.WriteLine(testInput.Substring(t.Start, testWordStart.Length)); System.Diagnostics.Debug.WriteLine(testInput.Substring(t.End - testWordEnd.Length, testWordEnd.Length)); } }
public void TestSetTabCount() { var testTokens = new List<Token> { new TokenPair(0, 143) {Register = 0, ChildTo = 0}, new TokenPair(20, 142) {Register = 1, ChildTo = 0}, new TokenPair(42, 141) {Register = 2, ChildTo = 1}, new TokenPair(107, 120) {Register = 3, ChildTo = 2} }; var testSubject = new NoFuture.Tokens.XDocFrame(); var testResults = testSubject.SetTabCount(testTokens); Assert.IsNotNull(testResults); foreach (var t in testResults) { System.Diagnostics.Debug.WriteLine(string.Format("{0},{1},{2},{3},{4}", t.Start, t.End, t.Register, t.ChildTo, t.Tab)); } }
public void TestInterlaceTokens() { var testTokens = new List<NoFuture.Tokens.Token> { new TokenPair(0,142), new TokenPair(20, 141), new TokenPair(42, 140), new TokenPair(107, 119) }; var testSubject = new NoFuture.Tokens.XDocFrame(); var testResults = testSubject.InterlaceTokens(testTokens); Assert.IsNotNull(testResults); foreach (var t in testResults) { System.Diagnostics.Debug.WriteLine(string.Format("{0},{1},{2},{3}", t.Start, t.End, t.Register, t.ChildTo)); } }
public void TestGetTokens() { var testInput = @"open something named{ accessMod type name { someStatement; someOtherStatement; accessMod type name(someArg){someBody;} somefinalStatement; }}"; var testSubject = new XDocFrame('{', '}'); var testResults = testSubject.GetTokens(testInput); Assert.IsNotNull(testResults); Assert.AreNotEqual(0, testResults.Count); Assert.AreEqual(4,testResults.Count); Assert.AreEqual("0, 142, 142, 0, 0", string.Format("{0}, {1}, {2}, {3}, {4}", testResults[0].Start, testResults[0].End, testResults[0].Span, testResults[0].Register, testResults[0].Tab)); Assert.AreEqual("20, 141, 121, 1, 0", string.Format("{0}, {1}, {2}, {3}, {4}", testResults[1].Start, testResults[1].End, testResults[1].Span, testResults[1].Register, testResults[1].Tab)); Assert.AreEqual("42, 140, 98, 2, 1", string.Format("{0}, {1}, {2}, {3}, {4}", testResults[2].Start, testResults[2].End, testResults[2].Span, testResults[2].Register, testResults[2].Tab)); Assert.AreEqual("107, 118, 11, 3, 2", string.Format("{0}, {1}, {2}, {3}, {4}", testResults[3].Start, testResults[3].End, testResults[3].Span, testResults[3].Register, testResults[3].Tab)); foreach (var t in testResults) { System.Diagnostics.Debug.WriteLine(string.Format("{0}, {1}, {2}, {3}, {4}", t.Start, t.End, t.Span, t.Register, t.Tab)); } }
public void TestGetGaps() { var testInput = @"open something named{ accessMod type name { someStatement; someOtherStatement; accessMod type name(someArg){someBody;} somefinalStatement; }}"; var testSubject = new XDocFrame('{', '}'); //technically this is no a valid unit test... var contrivedTestInput = testSubject.GetTokens(testInput); var testResults = testSubject.GetGaps(contrivedTestInput); Assert.IsNotNull(testResults); Assert.AreNotEqual(0, testResults); foreach (var t in testResults) { System.Diagnostics.Debug.WriteLine(string.Format("{0}, {1}, {2}, {3}", t.Start, t.End, t.Span, t.ChildTo)); } }
public void TestFindTokenMatch() { var testArr0 = new List<int> {20, 42, 107}; var testArr1 = new List<int> {117, 139, 140}; var testSubject = new NoFuture.Tokens.XDocFrame(); var testResults = testSubject.FindTokenMatch(testArr0[0], testArr0, testArr1); Assert.IsNotNull(testResults); Assert.AreNotEqual(0,testResults); }
public bool TryFindLastLineInClass(string typename, string[] srcFileLines, out int lastLine) { lastLine = int.MaxValue; if (srcFileLines == null) return false; var srcFile = srcFileLines.ToArray(); if (String.IsNullOrWhiteSpace(typename)) return false; if (srcFile.Length <= 0) return false; lastLine = srcFile.Length; //these preserve all lines srcFile = RemoveLineComments(srcFile, LINE_COMMENT_CHAR_SEQ); srcFile = RemoveBlockComments(srcFile); var joinedContent = String.Join("\n", srcFile); var myXDocFrame = new XDocFrame(C_OPEN_CURLY, C_CLOSE_CURLY); var mytokens = myXDocFrame.FindEnclosingTokens(joinedContent); if (mytokens == null || mytokens.Count < 1) return false; var targetToken = NfTypeName.GetTypeNameWithoutNamespace(typename) == typename ? mytokens[0] : mytokens[1]; var joinedContentAsChars = joinedContent.ToCharArray(); //need to count the number of newlines from the bottom up to token's end for (var i = joinedContent.Length - 1; i >= targetToken.End; i--) { if (joinedContentAsChars[i] != '\n') continue; lastLine -= 1; } //add one more if (lastLine < srcFile.Length) lastLine -= 1; return lastLine < srcFile.Length; }
public bool TryFindFirstLineInClass(string typename, string[] srcFileLines, out int firstLine) { firstLine = 1; if (srcFileLines == null) return false; var srcFile = srcFileLines.ToArray(); if (String.IsNullOrWhiteSpace(typename)) return false; if (srcFile.Length <= 0) return false; //these preserve all lines srcFile = RemoveLineComments(srcFile, LINE_COMMENT_CHAR_SEQ); srcFile = RemoveBlockComments(srcFile); var joinedContent = String.Join("\n", srcFile); var myXDocFrame = new XDocFrame(C_OPEN_CURLY, C_CLOSE_CURLY); var mytokens = myXDocFrame.FindEnclosingTokens(joinedContent); if (mytokens == null || mytokens.Count < 1) return false; var targetToken = NfTypeName.GetTypeNameWithoutNamespace(typename) == typename ? mytokens[0] : mytokens[1]; var joinedContentAsChars = joinedContent.ToCharArray(); //need to count the number of newlines up to the token's start for (var i = 0; i <= targetToken.Start; i++) { if (joinedContentAsChars[i] != '\n') continue; firstLine += 1; } return firstLine > 1; }
public static Ctor DisassembleConstructor(string signature) { var psSignature = new Ctor {RawSignature = signature}; var ws = signature; //remove string literals if (ws.Contains('"')) { var nfXDoc = new XDocFrame('"', '"'); var xdocItems = (nfXDoc.FindEnclosingTokens(signature)); if (xdocItems != null) { for (var i = 0; i < xdocItems.Count; i++) { var strToken = xdocItems[i]; psSignature.StringLiterals.Add(new StringLiteral() { Index = i, Value = signature.Substring(strToken.Start, strToken.Span) }); ws = ws.Replace((signature.Substring(strToken.Start, strToken.Span)), string.Format("%{0}", i)); } psSignature.AfterStringRem = ws; } } //terminate at end of first statement if (ws.Contains(";")) { ws = ws.Substring(0, (ws.IndexOf(";", StringComparison.Ordinal))); psSignature.AfterFirstTerminator = ws; } //extract assignment if (ws.Contains("=")) { var assign = ws.Substring(0, (ws.IndexOf("=", StringComparison.Ordinal))).Trim(); psSignature.LeftSplit = assign; ws = ws.Substring(ws.IndexOf("=", StringComparison.Ordinal), ws.Length - ws.IndexOf("=", StringComparison.Ordinal)).Trim(); psSignature.RightSplit = ws; if (assign.Contains(" ")) { assign = assign.Substring(assign.LastIndexOf(" ", StringComparison.Ordinal), (assign.Length - assign.LastIndexOf(" ", StringComparison.Ordinal))).Trim(); psSignature.Declaration = assign; } } //back the working sig up to open parenth. if (ws.IndexOf("(", StringComparison.Ordinal) != -1) { var ctorArgs = ws.Substring(ws.IndexOf("(", StringComparison.Ordinal), (ws.Length - ws.IndexOf("(", StringComparison.Ordinal))).Trim(); psSignature.ConstructorArgs = ctorArgs; ws = ws.Substring(0, (ws.IndexOf("(", StringComparison.Ordinal))); psSignature.InstanceType = ws; } //determine namespace if (ws.Contains(".")) { var ns = (ws.Substring(0, ws.LastIndexOf(".", StringComparison.Ordinal))); if (ns.Contains(" ")) { ns = ns.Substring(ns.LastIndexOf(" ", StringComparison.Ordinal), (ns.Length - ns.LastIndexOf(" ", StringComparison.Ordinal))).Trim(); } else { ns = ns.Replace("=", string.Empty); } psSignature.Namespace = ns; ws = ws.Substring(ws.LastIndexOf(".", StringComparison.Ordinal), (ws.Length - ws.LastIndexOf(".", StringComparison.Ordinal))).Trim(); if (ws.StartsWith(".")) { ws = ws.Substring(1, ws.Length - 1); } psSignature.TType = ws; } else { if (ws.Contains(" ")) { ws = ws.Substring(ws.LastIndexOf(" ", StringComparison.Ordinal), (ws.Length - ws.LastIndexOf(" ", StringComparison.Ordinal))).Trim(); } else { ws = ws.Replace("=", ""); } psSignature.TType = ws; } return psSignature; }