feat: add Exception.isMaxRecDepth
This commit is contained in:
parent
8896c9b06d
commit
868e95df1b
2 changed files with 23 additions and 3 deletions
|
|
@ -168,7 +168,12 @@ def liftMacroM {α} {m : Type → Type} [Monad m] [MonadMacroAdapter m] [MonadEn
|
|||
maxRecDepth := ← MonadRecDepth.getMaxRecDepth
|
||||
} { macroScope := (← MonadMacroAdapter.getNextMacroScope) } with
|
||||
| EStateM.Result.error Macro.Exception.unsupportedSyntax _ => throwUnsupportedSyntax
|
||||
| EStateM.Result.error (Macro.Exception.error ref msg) _ => throwErrorAt ref msg
|
||||
| EStateM.Result.error (Macro.Exception.error ref msg) _ =>
|
||||
if msg == maxRecDepthErrorMessage then
|
||||
-- Make sure we can detect exception using `Exception.isMaxRecDepth`
|
||||
throwMaxRecDepthAt ref
|
||||
else
|
||||
throwErrorAt ref msg
|
||||
| EStateM.Result.ok a s =>
|
||||
MonadMacroAdapter.setNextMacroScope s.macroScope
|
||||
s.traceMsgs.reverse.forM fun (clsName, msg) => trace clsName fun _ => msg
|
||||
|
|
|
|||
|
|
@ -81,11 +81,26 @@ instance [Monad m] [MonadRecDepth m] : MonadRecDepth (StateRefT' ω σ m) :=
|
|||
instance [BEq α] [Hashable α] [Monad m] [STWorld ω m] [MonadRecDepth m] : MonadRecDepth (MonadCacheT α β m) :=
|
||||
inferInstanceAs (MonadRecDepth (StateRefT' _ _ _))
|
||||
|
||||
def throwMaxRecDepthAt [MonadError m] (ref : Syntax) : m α :=
|
||||
throw <| Exception.error ref (MessageData.ofFormat (Std.Format.text maxRecDepthErrorMessage))
|
||||
|
||||
/--
|
||||
Return true if `ex` was generated by `throwMaxRecDepthAt`.
|
||||
This function is a bit hackish. The max rec depth exception should probably be an internal exception,
|
||||
but it is also produced by `MacroM` which implemented in the prelude, and internal exceptions have not
|
||||
been defined yet. -/
|
||||
def Exception.isMaxRecDepth (ex : Exception) : Bool :=
|
||||
match ex with
|
||||
| error _ (MessageData.ofFormat (Std.Format.text msg)) => msg == maxRecDepthErrorMessage
|
||||
| _ => false
|
||||
|
||||
@[inline] def withIncRecDepth [Monad m] [MonadError m] [MonadRecDepth m] (x : m α) : m α := do
|
||||
let curr ← MonadRecDepth.getRecDepth
|
||||
let max ← MonadRecDepth.getMaxRecDepth
|
||||
if curr == max then Lean.throwError maxRecDepthErrorMessage
|
||||
MonadRecDepth.withRecDepth (curr+1) x
|
||||
if curr == max then
|
||||
throwMaxRecDepthAt (← getRef)
|
||||
else
|
||||
MonadRecDepth.withRecDepth (curr+1) x
|
||||
|
||||
syntax "throwError " (interpolatedStr(term) <|> term) : term
|
||||
syntax "throwErrorAt " term:max (interpolatedStr(term) <|> term) : term
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue