-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.fs
71 lines (55 loc) · 3.05 KB
/
Program.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
open System
open System.IO
open GetAST
open Microsoft.FSharp.Compiler.SourceCodeServices
let rec getAllFilePaths dir pattern =
seq { yield! Directory.EnumerateFiles(dir, pattern)
for d in Directory.EnumerateDirectories(dir) do
yield! getAllFilePaths d pattern }
let IsSubLet (letDecl : LetDeclaration) (list : List<LetDeclaration>) =
List.exists (fun (elem : LetDeclaration) -> (elem.FileName = letDecl.FileName)
&& (elem.StartLine <= letDecl.StartLine)
&& (elem.EndLine >= letDecl.EndLine)
&& (elem.StartColumn <= letDecl.StartColumn)
&& (elem.EndColumn >= letDecl.EndColumn)) list
let mutable AllTopLevelLets : List<LetDeclaration> = List.empty<LetDeclaration>
let extractSingle (filepath, outfile) =
let input = File.ReadAllText(filepath)
let tree = GetAST.getUntypedTree(filepath, "/home/user/dummy.fsx", input)
let test = GetAST.traverseTree tree
File.WriteAllText(outfile, GetAST.astOutput)
let extractLetModules filePath =
GetAST.reset ()
let input = File.ReadAllText(filePath)
let test = GetAST.traverseTree (GetAST.getUntypedTree(filePath, "/home/user/dummy.fsx", input))
for letDecl in GetAST.LetDeclarations do
if (IsSubLet letDecl AllTopLevelLets) = false then
AllTopLevelLets <- letDecl :: AllTopLevelLets
()
let extractAllLetDecl (inputpath, outfile) =
if Directory.Exists (inputpath) then
let fs = getAllFilePaths inputpath "*.fs"
let fsx = getAllFilePaths inputpath "*.fsx"
let allFSharpFilePaths = Seq.append fs fsx
for filePath in allFSharpFilePaths do extractLetModules (filePath)
else extractLetModules (inputpath)
let mutable outputString = ""
for topLevelLet in (List.sortBy (fun (elem : LetDeclaration) -> elem.SourceCode.Length) AllTopLevelLets) do
outputString <- outputString + "File: #(" + topLevelLet.FileName + ")#" + Environment.NewLine
outputString <- outputString + "Code: #(" + topLevelLet.SourceCode + ")#" + Environment.NewLine
outputString <- outputString + "AST: #(" + topLevelLet.ASTRep + ")#" + Environment.NewLine
outputString <- outputString + "ModuleLevel: #(" + topLevelLet.IsModuleLevel.ToString() + ")#" + Environment.NewLine
outputString <- outputString + Environment.NewLine
File.WriteAllText(outfile, outputString)
()
[<EntryPoint>]
let main argv =
let errorMessage = "Called with wrong parameters. Sample usage: 'FSharpASTExtractor.exe simple <inputFile> <outputFile>' or 'FSharpASTExtractor.exe extended <inputDirectoryOrFile> <outputFile>'"
if argv.Length < 3 then
printfn "%s" errorMessage
else
match argv.[0], argv.[1], argv.[2] with
| "extendedLet", path, outFile -> extractAllLetDecl (path, outFile)
| "simple", filepath, outFile -> extractSingle (filepath, outFile)
| _ -> printfn "%s" errorMessage
0