summaryrefslogtreecommitdiff
path: root/tools/Scanner.lean
blob: 81b418137d87406878725d0b81b93783b2e1869c (plain)
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
import Scanner.Protocol
import Scanner.Parser
import Scanner.Processor

open WaylandScanner

def parseRawProtocol (path : System.FilePath) : IO Raw.ProtocolDesc := do
  parse (← IO.FS.readFile path)
    |>.mapError (fun ε ↦ s!"{path}: {ε}")
    |> IO.ofExcept

def process (rootModule : String) (outPath : System.FilePath) (srcPath : System.FilePath) : IO UInt32 := do
  let rawProtocols ← (← srcPath.readDir)
    |>.map IO.FS.DirEntry.path
    |>.mapM parseRawProtocol
  let clientProtocols ← processProtocols rawProtocols
    |> IO.ofExcept
  let rootModuleComponents := rootModule.split "."
    |>.map String.Slice.toString
    |>.toArray
  let rootModulePath := rootModuleComponents.foldl (· / ·) outPath
  IO.println (reprStr clientProtocols)
  IO.FS.createDirAll rootModulePath
  let indexPath := rootModulePath.addExtension "lean"
  IO.FS.writeFile indexPath ""
  return 0

def usage : IO UInt32 := do
  IO.println "Usage:"
  IO.println "  lake exe WaylandScanner -- <rootModule> <outPath> <srcPath>"
  IO.println ""
  IO.println "Description:"
  IO.println "  Read all (XML) protocols from the <srcPath> directory and"
  IO.println "  generate bindings in the <rootModule> namespace, where the"
  IO.println "  root source path is located at <outPath>."
  return 1

def main (args : List String) : IO UInt32 :=
  match args with
  | [rootModule, outPath, srcPath] => process rootModule outPath srcPath
  | _ => usage