Ejemplo n.º 1
0
        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));
            }
        }
Ejemplo n.º 2
0
        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));

            }
        }
Ejemplo n.º 3
0
        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;
        }
Ejemplo n.º 4
0
        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;
        }
Ejemplo n.º 5
0
        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;
        }