Advertisement
Guest User

Untitled

a guest
Apr 11th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 6.70 KB | None | 0 0
  1. // Learn more about F# at http://fsharp.org
  2. // See the 'F# Tutorial' project for more help.
  3. module Main
  4. open System;
  5. open Eto;
  6. open Eto.Forms;
  7. open Eto.Drawing;
  8. open System.IO;
  9. open System.Reflection
  10.  
  11. let RadioMenuItem = Menu.RadioMenuItem
  12. let CheckMenuItem = Menu.CheckMenuItem
  13.  
  14. let languages = [
  15.                 ("Java", null)
  16.                 ("C#", "CSharp")
  17.                 ("JavaScript", "JavaScript")
  18.                 ("Go", "Go")
  19.                 ("C++", "Cpp")
  20.                 ("Swift", "Swift")
  21.                 ]
  22. let mutable file : string = null
  23. let mutable language : string = null
  24. type CompilerOption = { Name:string; mutable Value:bool; ActiveFlag:string; InactiveFlag: string}
  25. let mutable options = [
  26.                          { Name="ATN"; Value=false ; ActiveFlag="-atn";InactiveFlag=String.Empty}
  27.                          { Name="Long Messages"; Value= false ; ActiveFlag= "-long-messages";InactiveFlag= String.Empty}
  28.                          { Name="Listener"; Value= true ; ActiveFlag= "-listener";InactiveFlag="-no-listener"}
  29.                          { Name="Visitor"; Value= false ; ActiveFlag= "-visitor";InactiveFlag= "-no-visitor"}
  30.                          { Name="Generate Dependencies"; Value= false; ActiveFlag= "-depend";InactiveFlag= String.Empty}
  31.                          { Name="Treat Errors as warnings"; Value=false; ActiveFlag="-Werror";InactiveFlag= String.Empty}
  32.                          { Name="Launch StringTemplate visualizer"; Value=false ; ActiveFlag= "-XdbgST";InactiveFlag= String.Empty}
  33.                          { Name="Wait StringTemplate visualizer before contiunuing"; Value=false; ActiveFlag="-XdbgSTWait";InactiveFlag=String.Empty}
  34.                          { Name="Force ATN Simulation"; Value=false; ActiveFlag="-Xforce-atn";InactiveFlag= String.Empty}
  35.                          { Name="Dump loggin info"; Value= false; ActiveFlag="-Xlog";InactiveFlag=String.Empty}
  36.                       ]                
  37.  
  38. let transform () =
  39.     let flags = options
  40.                 |> Seq.map(fun option -> if option.Value then option.ActiveFlag else option.InactiveFlag)
  41.                 |> Seq.toList
  42.     let strings = [file; (if language <> null then sprintf "-Dlanguage=%s" language else String.Empty)]
  43.     String.Join(" ", Seq.concat [strings;flags])
  44.  
  45. let antlrLocation = Path.Combine(Path.GetDirectoryName(Assembly.GetCallingAssembly().Location),
  46.                                 "antlr-4.7.2-complete.jar")
  47.  
  48. let startJavaProgram(jarLocation: string, programArguments: string) =
  49.     let javaProcessArguments =
  50.         sprintf "-jar \"%s\" %s"
  51.         <| antlrLocation
  52.         <| programArguments
  53.  
  54.     let startInfo = new Diagnostics.ProcessStartInfo(WorkingDirectory = Directory.GetCurrentDirectory(),
  55.                                                      FileName = "java",
  56.                                                      Arguments = javaProcessArguments,
  57.                                                      UseShellExecute = false,
  58.                                                      RedirectStandardOutput=true,
  59.                                                      CreateNoWindow = true)
  60.  
  61.     let osProcess = Diagnostics.Process.Start(startInfo);
  62.     osProcess
  63.  
  64. let generate _ =
  65.     let antlrArguments = transform ()
  66.     let antlrJavaProcess = startJavaProgram (antlrLocation, antlrArguments)
  67.     antlrJavaProcess.WaitForExit();
  68.     let processOutput = antlrJavaProcess.StandardOutput.ReadToEnd()
  69.     printfn "%s" <| processOutput
  70.  
  71. let mutable webView : WebView = null
  72.  
  73. let buildSvgHtml(svg : string)=
  74.     let builder = new System.Text.StringBuilder()
  75.  
  76.     builder.Append("<html>") |> ignore
  77.  
  78.     builder.Append("<head>") |> ignore
  79.  
  80.     //a SVG will otherwise not show on Windows.
  81.     builder.Append("<meta http-equiv=\"X-UA-Compatible\" content=\"IE=9\"/>") |> ignore
  82.     builder.Append("</head>") |> ignore
  83.  
  84.     builder.Append("<body>") |> ignore
  85.     builder.Append(svg) |> ignore
  86.     builder.Append("</body>") |> ignore
  87.  
  88.     builder.Append("</html>") |> ignore
  89.  
  90.     builder.ToString()
  91.  
  92. let readGrammar name =
  93.     use writer = new System.IO.StringWriter()
  94.     printfn "%s" name
  95.     file <- name
  96.     let parsedAntlr = ANTLRStudio.Parser.AntlrParser.ParseFile file
  97.     parsedAntlr.writeSvg(writer)
  98.     webView.LoadHtml(buildSvgHtml(writer.ToString()))
  99.     ()
  100.    
  101. let openGrammar (form:Form) =
  102.     let dir = Directory.GetCurrentDirectory() // Eto changes the directory?!
  103.     use dialog = new OpenFileDialog(MultiSelect = false,
  104.                                     Title = "Select Grammar",
  105.                                     CheckFileExists = true,
  106.                                     Directory = new Uri(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)))
  107.     let mutable fileName = null
  108.     match dialog.ShowDialog(form.ParentWindow) with
  109.     | DialogResult.Ok -> if dialog.FileName <> null then
  110.                             fileName <- dialog.FileName
  111.     | v -> printf "User pressed %O" v
  112.     if fileName <> null then readGrammar fileName
  113.     Directory.SetCurrentDirectory(dir) // Eto changes the directory?!
  114. let insertMenus (app:Application) (form:Form) =
  115.     let transformCheckItem option =
  116.         let item = CheckMenuItem(option.Name)
  117.                    |> action (fun _ -> option.Value <- not option.Value )
  118.         if not option.Value
  119.             then item
  120.         else (item |> check)
  121.  
  122.     let menu = new MenuBar()
  123.     let menus = [
  124.                 SubMenu("File",
  125.                     [
  126.                     ActionMenuItem("Open") |> action (fun _ -> openGrammar form)                                        
  127.                     ActionMenuItem("Quit") |> action (fun _ -> app.Quit())
  128.                     ])
  129.                 SubMenu("Language", languages
  130.                                     |> Seq.map(fun (name,value) -> RadioMenuItem("Languages",name)
  131.                                                                    |> action (fun _ -> language <- value)))
  132.                 SubMenu("Options",  options |> Seq.map transformCheckItem)
  133.                 ActionMenuItem("Generate")  |> action generate
  134.                 ]
  135.     menus |> Seq.iter (menu.Items.Add << makeMenu)
  136.     form.Menu <- menu
  137.     form
  138.  
  139. let railwayForm (app:Application) (form:Form) =
  140.     openGrammar form
  141.     form.Content <- webView
  142.     form
  143.  
  144. [<STAThread>]//Windows UI needs this for UI thread
  145. [<EntryPoint>]
  146. let main argv =
  147.     let progName = "ANTLRStudio"
  148.     Console.Title <- progName
  149.     use app = new Application ()
  150.     webView <- new WebView()
  151.     app.UnhandledException.Add(fun _ -> ())
  152.     use form = new Form (Title = progName, Size =Size(Screen.PrimaryScreen.Bounds.Size))
  153.  
  154.     railwayForm app form
  155.         |> insertMenus app
  156.         |> app.Run
  157.     0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement