Procedure Extractor

Search

 by Remas Wojciechowski

The Procedure Extractor (okay, don't laugh about the name) displays the source code of a given procedure (Sub or Function). It takes the name of the procedure and the physical path to the file to be searched as arguments. In version 1.1 the function has been extended by the functionality to include comment lines directly preceding the function body. The 3rd argument specifies whether or not the comments should be included.
Below you see the source code of that procedure. Interestingly enough, this very procedure is being used to retrieve the data.
Note: The color-coding bit is still very alpha.
' ================================================================
' = This procedure extracts the source code of a given procedure =
' = Version 1.1
' ================================================================
Function GetProcedureText(ByVal strProcName, ByVal strPathLib, ByVal bIncludeComments)
   Dim strTemp
   Dim objRE
   Dim objFSO
   Dim oF
   Dim strFileText
   Dim objMatchItem
   Dim objMatches
   Dim strPattern
  
   Set objRE = New RegExp
   Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
   If Not objFSO.FileExists(strPathLib) Then
      strTemp = "{Error: File not found. Please provide a valid PHYSICAL path!}"
   Else
      On Error Resume Next
      Set oF = objFSO.OpenTextFile(strPathLib)
      strFileText = oF.ReadAll
      oF.Close
      Set oF = Nothing
      If bIncludeComments Then
         strPattern = "(?:^[ \t]*?'(?:.|\n(?=\s*'|\n|"
         strPattern = strPattern & "(?:Function|Sub|Class)[ \t]*?" & strProcName & "))*?|^)"
      Else
         strPattern = "^"
      End If
      strPattern = strPattern & "[ \t]*?(Function|Sub|Class)[ \t]*?" & strProcName
      strPattern = strPattern & "(?:.|\n)*?^End\s*?\1"
      objRE.Pattern = strPattern
      objRE.IgnoreCase = True
      objRE.Global = False
      objRE.MultiLine = True
      Set objMatches = objRE.Execute(strFileText)
      
      If objMatches.Count > 0 Then
         strTemp = objMatches(0).Value
      Else
         strTemp = "Error: {No matches found for " & strProcName & "}"
      End If
      If Err.Number > 0 Then
         If Err.Number = 438 Then
            strTemp = "Error: {This procedure requires VBScript version 5.5 or later}"
         Else
            strTemp = "Error: {" & Err.Description & "}"
         End If
      End If
      Err.Clear
   End If
   Set objFSO = Nothing
   GetProcedureText = strTemp
End Function