.Net string parsing library or regular expression for parsing .Net code files

I would like to be able to parse vb.net code files, so I can examine the collection of Subs, Functions (and their contents, including comments), private variables, etc.

I can open the actual source code files.

So, for example, if I have:

Public Function FunctionOne(arg1 As String, arg2 as String) as Integer
   here is some code
   ''//here are some comments
End Function

Public Sub FunctionOne(arg1 As integer, arg2 as integer)
   here is some code
   ''//here are some comments
End Sub

I would like to be able to analyze all subsets and functions and all the code between the Public Function and the End Function (in fact, it would be nice to be able to include either just the code inside or the definition of the whole function.

It would seem that this will cause some sort of parsing library, as well as pretty decent regular expression skills.

Any suggestions?

UPDATE: , , , , , , , .

+3
7

, , :

Private _SourceCode As String = Nothing
Private ReadOnly Property SourceCode() As String
                Get
                    If _SourceCode = Nothing Then
                        Dim thisCodeFile As String = Server.MapPath("~").ToString & "\" & Type.GetType(Me.GetType.BaseType.FullName).ToString & ".aspx.vb"
                        _SourceCode = My.Computer.FileSystem.ReadAllText(thisCodeFile)
                    End If
                    Return _SourceCode
                End Get
End Property  

Private Function extractProcedureDefinition(ByVal procedureName As String) As String
   Return extractStringContents(Me.SourceCode, "Sub " & procedureName & "()", "End Sub", True)
End Function  

Private Function extractFunctionDefinition(ByVal procedureName As String) As String
   'TODO: This works now, but wouldn't if we wanted includeTags = False, as it does not properly handle the "As xxxxx" portion
   Return extractStringContents(Me.SourceCode, "Function " & procedureName, "End Sub", True)
End Function

    Private Function extractStringContents(ByVal body As String, ByVal openTag As String, ByVal closeTag As String, ByVal includeTags As Boolean) As String
                Dim iStart As Integer = body.IndexOf(openTag)
                Dim iEnd As Integer = body.IndexOf(closeTag, iStart)
                If includeTags Then
                    iEnd += closeTag.Length
                Else
                    iStart += openTag.Length
                End If
                Return body.Substring(iStart, iEnd - iStart)
    End Function  
+1

, ?

Microsoft , !

+6

NRefactory, SharpDevelop.

VB #. , ( ).

:

Imports System

Class MainClass
  Public Function FunctionOne(arg1 As String, arg2 As String) As Integer
    Return Int32.Parse(arg1) + Int32.Parse(arg2)
  End Function

  Public Sub FunctionOne(arg1 As Integer, arg2 As Integer)
    Return
  End Sub

End Class

( NRefactoryDemo ) alt text http://img15.imageshack.us/img15/3564/stackoverflownrefactory.png

+3

, Visual Basic.NET Flex Bison ( C/++) - Antlr ( .NET).

, .

+1

madgnome ! # , , . NRefactory NRefactoryDemo , , !

!

+1

, Microsoft.CSharp.CSharpCodeProvider, #. , .

MSDN: http://msdn.microsoft.com/en-us/library/microsoft.csharp.csharpcodeprovider.aspx

Edit:

, , , , , . ...

0

, Reflector. , Reflector - , , , , .NET. DLL EXE. . ,

  • ,
  • programmatically script with a reflex output - get a list of functions and a decompiled source associated with it.

An example .

This approach may not be satisfactory - since the source you get from Reflector is not a source source, but a decompiled source. Comments will disappear, and decompilation is not 100% true to the original. Functionally equivalent, but not 100% textual.

In any case, it's worth a look.

0
source

Source: https://habr.com/ru/post/1704288/


All Articles