-
Notifications
You must be signed in to change notification settings - Fork 546
/
Copy pathLoad.lean
34 lines (31 loc) · 1.2 KB
/
Load.lean
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
/-
Copyright (c) 2024 Mac Malone. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mac Malone
-/
import Lake.Toml.Elab
import Lake.Util.Message
open Lean Parser
namespace Lake.Toml
/-- Load a TOML table from some input. -/
def loadToml (ictx : InputContext) : EIO MessageLog Table := do
let env ←
match (← mkEmptyEnvironment.toBaseIO) with
| .ok env => pure env
| .error e => throw <| MessageLog.empty.add <| mkMessageNoPos ictx <|
m!"failed to initialize TOML environment: {e}"
let s := toml.fn.run ictx { env, options := {} } {} (mkParserState ictx.input)
if let some errorMsg := s.errorMsg then
throw <| MessageLog.empty.add <| mkParserErrorMessage ictx s errorMsg
else if ictx.input.atEnd s.pos then
let act := elabToml ⟨s.stxStack.back⟩
match (← act.run {fileName := ictx.fileName, fileMap := ictx.fileMap} {env} |>.toBaseIO) with
| .ok (t, s) =>
if s.messages.hasErrors then
throw s.messages
else
return t
| .error e =>
throw <| MessageLog.empty.add <| mkExceptionMessage ictx e
else
throw <| MessageLog.empty.add <| mkParserErrorMessage ictx s {expected := ["end of input"]}