Skip to content

Commit 384ed3a

Browse files
committed
fix: #help under the module system
1 parent 450ddfd commit 384ed3a

File tree

2 files changed

+28
-51
lines changed

2 files changed

+28
-51
lines changed

Batteries/Tactic/HelpCmd.lean

Lines changed: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -129,43 +129,6 @@ elab_rules : command
129129
| `(#help attr $(id)?) => elabHelpAttr id
130130
| `(#help attribute $(id)?) => elabHelpAttr id
131131

132-
/-- Gets the initial string token in a parser description. For example, for a declaration like
133-
`syntax "bla" "baz" term : tactic`, it returns `some "bla"`. Returns `none` for syntax declarations
134-
that don't start with a string constant. -/
135-
partial def getHeadTk (e : Expr) : Option String :=
136-
match e.getAppFnArgs with
137-
| (``ParserDescr.node, #[_, _, p])
138-
| (``ParserDescr.trailingNode, #[_, _, _, p])
139-
| (``ParserDescr.unary, #[.app _ (.lit (.strVal "withPosition")), p])
140-
| (``ParserDescr.unary, #[.app _ (.lit (.strVal "atomic")), p])
141-
| (``ParserDescr.unary, #[.app _ (.lit (.strVal "ppRealGroup")), p])
142-
| (``ParserDescr.unary, #[.app _ (.lit (.strVal "ppRealFill")), p])
143-
| (``Parser.ppRealFill, #[p])
144-
| (``Parser.withAntiquot, #[_, p])
145-
| (``Parser.leadingNode, #[_, _, p])
146-
| (``Parser.trailingNode, #[_, _, _, p])
147-
| (``Parser.group, #[p])
148-
| (``Parser.withCache, #[_, p])
149-
| (``Parser.withResetCache, #[p])
150-
| (``Parser.withPosition, #[p])
151-
| (``Parser.withOpen, #[p])
152-
| (``Parser.withPositionAfterLinebreak, #[p])
153-
| (``Parser.suppressInsideQuot, #[p])
154-
| (``Parser.ppRealGroup, #[p])
155-
| (``Parser.ppIndent, #[p])
156-
| (``Parser.ppDedent, #[p])
157-
=> getHeadTk p
158-
| (``ParserDescr.binary, #[.app _ (.lit (.strVal "andthen")), p, q])
159-
| (``HAndThen.hAndThen, #[_, _, _, _, p, .lam _ _ q _])
160-
=> getHeadTk p <|> getHeadTk q
161-
| (``ParserDescr.nonReservedSymbol, #[.lit (.strVal tk), _])
162-
| (``ParserDescr.symbol, #[.lit (.strVal tk)])
163-
| (``Parser.nonReservedSymbol, #[.lit (.strVal tk), _])
164-
| (``Parser.symbol, #[.lit (.strVal tk)])
165-
| (``Parser.unicodeSymbol, #[.lit (.strVal tk), _])
166-
=> pure tk
167-
| _ => none
168-
169132
/--
170133
The command `#help cats` shows all syntax categories that have been defined in the
171134
current environment.
@@ -223,24 +186,34 @@ name of the syntax (which you can also click to go to the definition), and the d
223186
syntax withPosition("#help " colGt &"cat" "+"? colGt ident
224187
(colGt ppSpace (Parser.rawIdent <|> str))?) : command
225188

189+
private def tokensToList (tks : Parser.FirstTokens) : List String :=
190+
match tks with
191+
| .epsilon | .unknown => []
192+
| .tokens tks | .optTokens tks => tks
193+
226194
private def elabHelpCat (more : Option Syntax) (catStx : Ident) (id : Option String) :
227195
CommandElabM Unit := do
228196
let mut decls : Std.TreeMap _ _ compare := {}
229197
let mut rest : Std.TreeMap _ _ compare := {}
230198
let catName := catStx.getId.eraseMacroScopes
231-
let some cat := (Parser.parserExtension.getState (← getEnv)).categories.find? catName
199+
let categories := (Parser.parserExtension.getState (← getEnv)).categories
200+
let some cat := categories.find? catName
232201
| throwErrorAt catStx "{catStx} is not a syntax category"
233202
liftTermElabM <| Term.addCategoryInfo catStx catName
234-
let env ← getEnv
235203
for (k, _) in cat.kinds do
236204
let mut used := false
237-
if let some tk := do getHeadTk (← (← env.find? k).value?) then
238-
let tk := tk.trimAscii
205+
try
206+
let (leading, parser) ← liftCoreM <| Parser.mkParserOfConstant categories k
207+
let tks := tokensToList parser.info.firstTokens
208+
let tks := tks.filter (· != "$") -- filter antiquotations
209+
let mainTk :: _ := tks | pure ()
239210
if let some id := id then
240-
if !tk.startsWith id then
211+
unless tks.any (·.startsWith id) do
241212
continue
242213
used := true
243-
decls := decls.insert tk.copy ((decls.getD tk.copy #[]).push k)
214+
decls := decls.insert mainTk ((decls.getD mainTk #[]).push (k, leading))
215+
catch _ =>
216+
pure ()
244217
if !used && id.isNone then
245218
rest := rest.insert (k.toString false) k
246219
let mut msg := MessageData.nil
@@ -272,8 +245,11 @@ private def elabHelpCat (more : Option Syntax) (catStx : Ident) (id : Option Str
272245
| _ => pure ()
273246
return msg ++ msg1 ++ (.line ++ .line : Format)
274247
for (name, ks) in decls do
275-
for k in ks do
276-
msg ← addMsg k msg m!"syntax {repr name}... [{mkConst k}]"
248+
for (k, leading) in ks do
249+
if leading then
250+
msg ← addMsg k msg m!"syntax {repr name}... [{mkConst k}]"
251+
else
252+
msg ← addMsg k msg m!"syntax ...{repr name}... [{mkConst k}]"
277253
for (_, k) in rest do
278254
msg ← addMsg k msg m!"syntax ... [{mkConst k}]"
279255
logInfo msg

BatteriesTest/help_cmd.lean

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
module
12
import Batteries.Tactic.HelpCmd
23

34
/-! The `#help` command
@@ -95,10 +96,10 @@ info:
9596
syntax "("... [«prec(_)»]
9697
Parentheses are used for grouping precedence expressions.
9798
98-
syntax "+"... [Lean.Parser.Syntax.addPrec]
99+
syntax ..."+"... [Lean.Parser.Syntax.addPrec]
99100
Addition of precedences. This is normally used only for offsetting, e.g. `max + 1`.
100101
101-
syntax "-"... [Lean.Parser.Syntax.subPrec]
102+
syntax ..."-"... [Lean.Parser.Syntax.subPrec]
102103
Subtraction of precedences. This is normally used only for offsetting, e.g. `max - 1`.
103104
104105
syntax "arg"... [precArg]
@@ -117,7 +118,7 @@ syntax "min"... [precMin]
117118
syntax "min1"... [precMin1]
118119
`(min+1)` (we can only write `min+1` after `Meta.lean`)
119120
120-
syntax ... [Lean.Parser.Syntax.numPrec]
121+
syntax "num"... [Lean.Parser.Syntax.numPrec]
121122
-/
122123
#guard_msgs in
123124
#help cat prec
@@ -129,11 +130,11 @@ syntax "("... [«prec(_)»]
129130
+ macro «_aux_Init_Notation___macroRules_prec(_)_1»
130131
Parentheses are used for grouping precedence expressions.
131132
132-
syntax "+"... [Lean.Parser.Syntax.addPrec]
133+
syntax ..."+"... [Lean.Parser.Syntax.addPrec]
133134
Addition of precedences. This is normally used only for offsetting, e.g. `max + 1`.
134135
+ macro Lean._aux_Init_Meta___macroRules_Lean_Parser_Syntax_addPrec_1
135136
136-
syntax "-"... [Lean.Parser.Syntax.subPrec]
137+
syntax ..."-"... [Lean.Parser.Syntax.subPrec]
137138
Subtraction of precedences. This is normally used only for offsetting, e.g. `max - 1`.
138139
+ macro Lean._aux_Init_Meta___macroRules_Lean_Parser_Syntax_subPrec_1
139140
@@ -164,7 +165,7 @@ syntax "min1"... [precMin1]
164165
+ macro _aux_Init_Notation___macroRules_precMin1_1
165166
`(min+1)` (we can only write `min+1` after `Meta.lean`)
166167
167-
syntax ... [Lean.Parser.Syntax.numPrec]
168+
syntax "num"... [Lean.Parser.Syntax.numPrec]
168169
-/
169170
#guard_msgs in
170171
#help cat+ prec

0 commit comments

Comments
 (0)