forked from strata-org/Strata
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrataMain.lean
More file actions
203 lines (183 loc) · 7.02 KB
/
StrataMain.lean
File metadata and controls
203 lines (183 loc) · 7.02 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/-
Copyright Strata Contributors
SPDX-License-Identifier: Apache-2.0 OR MIT
-/
-- Executable with utilities for working with Strata files.
import Strata.DDM.Elab
import Strata.DDM.Ion
def exitFailure {α} (message : String) : IO α := do
IO.eprintln (message ++ "\n\nRun strata --help for additional help.")
IO.Process.exit 1
namespace Strata
def asText {m} [Monad m] [MonadExcept String m] (path : System.FilePath) (bytes : ByteArray) : m String :=
match String.fromUTF8? bytes with
| some s =>
pure s
| none =>
throw s!"{path} is not an Ion file and contains non UTF-8 data"
def mkErrorReport (path : System.FilePath) (errors : Array Lean.Message) : BaseIO String := do
let msg : String := s!"{errors.size} error(s) reading {path}:\n"
let msg ← errors.foldlM (init := msg) fun msg e =>
return s!"{msg} {e.pos.line}:{e.pos.column}: {← e.data.toString}\n"
return toString msg
inductive DialectOrProgram
| dialect (d : Dialect)
| program (pgm : Program)
end Strata
def readStrataText (fm : Strata.DialectFileMap) (input : System.FilePath) (bytes : ByteArray)
: IO (Strata.Elab.LoadedDialects × Strata.DialectOrProgram) := do
let leanEnv ← Lean.mkEmptyEnvironment 0
let contents ←
match Strata.asText input bytes with
| Except.ok c => pure c
| Except.error msg => exitFailure msg
let inputContext := Strata.Parser.stringInputContext input contents
let (header, errors, startPos) := Strata.Elab.elabHeader leanEnv inputContext
if errors.size > 0 then
exitFailure (← Strata.mkErrorReport input errors)
match header with
| .program stx dialect =>
let dialects ←
match ← Strata.Elab.loadDialect fm .builtin dialect with
| (dialects, .ok _) => pure dialects
| (_, .error msg) => exitFailure msg
match Strata.Elab.elabProgramRest dialects leanEnv inputContext stx dialect startPos with
| .ok program => pure (dialects, .program program)
| .error errors => exitFailure (← Strata.mkErrorReport input errors)
| .dialect stx dialect =>
let (loaded, d, s) ←
Strata.Elab.elabDialectRest fm .builtin #[] inputContext stx dialect startPos
if s.errors.size > 0 then
exitFailure (← Strata.mkErrorReport input s.errors)
pure (loaded.addDialect! d, .dialect d)
def fileReadError {α} (path : System.FilePath) (msg : String) : IO α := do
IO.eprintln s!"Error reading {path}:"
IO.eprintln s!" {msg}\n"
IO.eprintln s!"Either the file is invalid or there is a bug in Strata."
IO.Process.exit 1
def readStrataIon (fm : Strata.DialectFileMap) (path : System.FilePath) (bytes : ByteArray) : IO (Strata.Elab.LoadedDialects × Strata.DialectOrProgram) := do
let (hdr, frag) ←
match Strata.Ion.Header.parse bytes with
| .error msg =>
exitFailure msg
| .ok p =>
pure p
match hdr with
| .dialect dialect =>
match ← Strata.Elab.loadDialectFromIonFragment fm .builtin #[] dialect frag with
| (_, .error msg) =>
fileReadError path msg
| (dialects, .ok d) =>
pure (dialects, .dialect d)
| .program dialect => do
let dialects ←
match ← Strata.Elab.loadDialect fm .builtin dialect with
| (loaded, .ok _) => pure loaded
| (_, .error msg) => exitFailure msg
match Strata.Program.fromIonFragment frag dialects.dialects dialect with
| .ok pgm =>
pure (dialects, .program pgm)
| .error msg =>
fileReadError path msg
def readFile (fm : Strata.DialectFileMap) (path : System.FilePath) : IO (Strata.Elab.LoadedDialects × Strata.DialectOrProgram) := do
let bytes ←
match ← IO.FS.readBinFile path |>.toBaseIO with
| .error _ =>
exitFailure s!"Error reading {path}."
| .ok c => pure c
if bytes.startsWith Ion.binaryVersionMarker then
readStrataIon fm path bytes
else
readStrataText fm path bytes
structure Command where
name : String
args : List String
help : String
callback : Strata.DialectFileMap → Vector String args.length → IO Unit
def checkCommand : Command where
name := "check"
args := [ "file" ]
help := "Check a dialect or program file."
callback := fun fm v => do
let _ ← readFile fm v[0]
pure ()
def toIonCommand : Command where
name := "toIon"
args := [ "input", "output" ]
help := "Read a Strata text file and translate into Ion."
callback := fun searchPath v => do
let (_, pd) ← readFile searchPath v[0]
match pd with
| .dialect d =>
IO.FS.writeBinFile v[1] d.toIon
| .program pgm =>
IO.FS.writeBinFile v[1] pgm.toIon
def printCommand : Command where
name := "print"
args := [ "file" ]
help := "Write a Strata text or Ion file to standard output."
callback := fun searchPath v => do
let (ld, pd) ← readFile searchPath v[0]
match pd with
| .dialect d =>
IO.print <| d.format ld.dialects
| .program pgm =>
IO.print <| toString pgm
def diffCommand : Command where
name := "diff"
args := [ "file1", "file2" ]
help := "Check if two program files are syntactically equal."
callback := fun fm v => do
let ⟨_, p1⟩ ← readFile fm v[0]
let ⟨_, p2⟩ ← readFile fm v[1]
match p1, p2 with
| .program p1, .program p2 =>
if p1 == p2 then return ()
else exitFailure "Two programs are different"
| _, _ =>
exitFailure "Cannot compare dialect def with another dialect/program."
def commandList : List Command := [
checkCommand,
toIonCommand,
printCommand,
diffCommand,
]
def commandMap : Std.HashMap String Command :=
commandList.foldl (init := {}) fun m c => m.insert c.name c
def main (args : List String) : IO Unit := do
match args with
| ["--help"] => do
IO.println "Usage: strata <command> [flags]...\n"
for cmd in commandList do
let args := cmd.args.foldl (init := s!" {cmd.name}") fun s a => s!"{s} <{a}>"
IO.println s!" {args}: {cmd.help}"
IO.println "\nFlags:"
IO.println " --include path: Adds a path to Strata for searching for dialects."
| cmd :: args =>
match commandMap[cmd]? with
| none => exitFailure s!"Unknown command {cmd}"
| some cmd =>
let expectedArgs := cmd.args.length
let rec process (sp : Strata.DialectFileMap) args (cmdArgs : List String) : IO _ := do
match cmdArgs with
| cmd :: cmdArgs =>
match cmd with
| "--include" =>
let path :: cmdArgs := cmdArgs
| exitFailure s!"Expected path after --path."
match ← sp.add path |>.toBaseIO with
| .error msg => exitFailure msg
| .ok sp => process sp args cmdArgs
| _ =>
if cmd.startsWith "--" then
exitFailure s!"Unknown option {cmd}."
process sp (args.push cmd) cmdArgs
| [] =>
pure (sp, args)
let (sp, args) ← process {} #[] args
if p : args.size = cmd.args.length then
cmd.callback sp ⟨args, p⟩
else
exitFailure s!"{cmd.name} expects {expectedArgs} argument(s)."
| [] => do
exitFailure "Expected subcommand."