Zulip Chat Archive

Stream: Is there code for X?

Topic: parse_failure command


Asei Inoue (Mar 12 2025 at 22:03):

here is my command which run parser

infix:60 " ⋄ " => Nat.mul

open Lean Parser in

def parse (cat : Name) (s : String) : MetaM Syntax := do
  ofExcept <| runParserCategory ( getEnv) cat s

#eval parse `term "1 ⋄ 2 ⋄ 3"

but I want to create #parse_failure command which succeed only when parse fails.

Robin Arnez (Mar 13 2025 at 16:16):

If you want this for testing, #guard_msgs might suffice:

import Lean

infix:60 " ⋄ " => Nat.mul

open Lean Parser in

def parse (cat : Name) (s : String) : MetaM Syntax := do
  ofExcept <| runParserCategory ( getEnv) cat s

/-- error: <input>:1:6: expected end of input -/
#guard_msgs in
#eval parse `term "1 ⋄ 2 ⋄ 3"

Asei Inoue (Mar 13 2025 at 23:30):

I know guard_msgs

Asei Inoue (Mar 13 2025 at 23:32):

I want to display my intension by command name, and to display in better form…

Aaron Liu (Mar 13 2025 at 23:32):

Using #guard_msgs is usually preferable to commands like #check_failure because you can guard the error message too.

Aaron Liu (Mar 13 2025 at 23:32):

Asei Inoue said:

I want to display my intension by command name, and to display in better form…

You can use a macro that expands into #guard_msgs.

Damiano Testa (Mar 14 2025 at 05:14):

By the way, mathlib has a #parse command in Mathlib.Util.ParseCommand that seems quite similar to your parse function.


Last updated: May 02 2025 at 03:31 UTC