@@ -30,7 +30,7 @@ namespace Structure
3030
3131/-! Recall that the `structure command syntax is
3232```
33- leading_parser (structureTk <|> classTk) >> declId >> many Term.bracketedBinder >> Term.optType >> optional «extends» >> optional (" := " >> optional structCtor >> structFields)
33+ leading_parser (structureTk <|> classTk) >> declId >> optDeclSig >> optional «extends» >> optional (" := " >> optional structCtor >> structFields)
3434```
3535-/
3636
@@ -206,10 +206,10 @@ private def expandCtor (structStx : Syntax) (structModifiers : Modifiers) (struc
206206 let ref := structStx[1 ].mkSynthetic
207207 addDeclarationRangesFromSyntax declName ref
208208 pure { ref, declId := ref, modifiers := default, declName }
209- if structStx[5 ].isNone then
209+ if structStx[4 ].isNone then
210210 useDefault
211211 else
212- let optCtor := structStx[5 ][1 ]
212+ let optCtor := structStx[4 ][1 ]
213213 if optCtor.isNone then
214214 useDefault
215215 else
@@ -278,12 +278,12 @@ def structFields := leading_parser many (structExplicitBinder <|> struct
278278```
279279-/
280280private def expandFields (structStx : Syntax) (structModifiers : Modifiers) (structDeclName : Name) : TermElabM (Array StructFieldView) := do
281- if structStx[5 ][0 ].isToken ":=" then
281+ if structStx[4 ][0 ].isToken ":=" then
282282 -- https://github.com/leanprover/lean4/issues/5236
283283 let cmd := if structStx[0 ].getKind == ``Parser.Command.classTk then "class" else "structure"
284- withRef structStx[0 ] <| Linter.logLintIf Linter.linter.deprecated structStx[5 ][0 ]
284+ withRef structStx[0 ] <| Linter.logLintIf Linter.linter.deprecated structStx[4 ][0 ]
285285 s! "{ cmd} ... :=' has been deprecated in favor of '{ cmd} ... where'."
286- let fieldBinders := if structStx[5 ].isNone then #[] else structStx[5 ][2 ][0 ].getArgs
286+ let fieldBinders := if structStx[4 ].isNone then #[] else structStx[4 ][2 ][0 ].getArgs
287287 fieldBinders.foldlM (init := #[]) fun (views : Array StructFieldView) fieldBinder => withRef fieldBinder do
288288 let mut fieldBinder := fieldBinder
289289 if fieldBinder.getKind == ``Parser.Command.structSimpleBinder then
@@ -342,14 +342,12 @@ private def expandFields (structStx : Syntax) (structModifiers : Modifiers) (str
342342 }
343343
344344/-
345- leading_parser (structureTk <|> classTk) >> declId >> many Term.bracketedBinder >> Term.optType >> optional «extends» >>
345+ leading_parser (structureTk <|> classTk) >> declId >> optDeclSig >> optional «extends» >>
346346 optional (("where" <|> ":=") >> optional structCtor >> structFields) >> optDeriving
347347
348348where
349349def structParent := leading_parser optional (atomic (ident >> " : ")) >> termParser
350- def «extends» := leading_parser " extends " >> sepBy1 structParent ", "
351- def typeSpec := leading_parser " : " >> termParser
352- def optType : Parser := optional typeSpec
350+ def «extends» := leading_parser " extends " >> sepBy1 structParent ", " >> optType
353351
354352def structFields := leading_parser many (structExplicitBinder <|> structImplicitBinder <|> structInstBinder)
355353def structCtor := leading_parser try (declModifiers >> ident >> " :: ")
@@ -361,24 +359,24 @@ def structureSyntaxToView (modifiers : Modifiers) (stx : Syntax) : TermElabM Str
361359 let declId := stx[1 ]
362360 let ⟨name, declName, levelNames⟩ ← Term.expandDeclId (← getCurrNamespace) (← Term.getLevelNames) declId modifiers
363361 addDeclarationRangesForBuiltin declName modifiers.stx stx
364- let binders := stx[2 ]
365- let (optType, exts) ←
362+ let (binders, type?) := expandOptDeclSig stx[2 ]
363+ let exts := stx[3 ]
364+ let type? ←
366365 -- Compatibility mode for `structure S extends P : Type` syntax
367- if stx[ 3 ] .isNone && !stx[ 4 ] .isNone && !stx[ 4 ] [0 ][2 ].isNone then
368- logWarningAt stx[ 4 ] [0 ][2 ][0 ] "\
366+ if type? .isNone && !exts .isNone && !exts [0 ][2 ].isNone then
367+ logWarningAt exts [0 ][2 ][0 ] "\
369368 The syntax is now 'structure S : Type extends P' rather than 'structure S extends P : Type'.\n\n \
370369 The purpose of this change is to accommodate 'structure S extends toP : P' syntax for naming parent projections."
371- pure (stx[ 4 ][ 0 ][2 ], stx[ 4 ])
370+ pure (some exts[ 0 ][2 ][ 0 ][ 1 ])
372371 else
373- if !stx[ 4 ] .isNone && !stx[ 4 ] [0 ][2 ].isNone then
374- logErrorAt stx[ 4 ] [0 ][2 ][0 ] "\
372+ if !exts .isNone && !exts [0 ][2 ].isNone then
373+ logErrorAt exts [0 ][2 ][0 ] "\
375374 Unexpected additional resulting type. \
376375 The syntax is now 'structure S : Type extends P' rather than 'structure S extends P : Type'.\n\n \
377376 The purpose of this change is to accommodate 'structure S extends toP : P' syntax for naming parent projections."
378- pure (stx[3 ], stx[4 ])
379- let parents ← expandParents exts
380- let derivingClasses ← getOptDerivingClasses stx[6 ]
381- let type? := if optType.isNone then none else some optType[0 ][1 ]
377+ pure type?
378+ let parents ← expandParents exts
379+ let derivingClasses ← getOptDerivingClasses stx[5 ]
382380 let ctor ← expandCtor stx modifiers declName
383381 let fields ← expandFields stx modifiers declName
384382 fields.forM fun field => do
0 commit comments