@@ -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/--
170133The command `#help cats` shows all syntax categories that have been defined in the
171134current environment.
@@ -223,24 +186,34 @@ name of the syntax (which you can also click to go to the definition), and the d
223186syntax 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+
226194private 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
0 commit comments