fix: go to definition for macro_rules etc.

This commit is contained in:
Sebastian Ullrich 2021-11-24 11:21:05 +01:00
parent 17f99e353e
commit f7decd2d46
6 changed files with 28 additions and 14 deletions

View file

@ -28,5 +28,5 @@ def elabAuxDef : CommandElab
let id := id.replacePrefix ns Name.anonymous -- TODO: replace with def _root_.id
elabCommand <|
← `($[$doc?:docComment]? $[$attrs?:attributes]?
def $(mkIdent id):ident : $ty := $body)
def $(mkIdentFrom (mkNullNode suggestion) id):ident : $ty := $body)
| _ => throwUnsupportedSyntax

View file

@ -12,7 +12,7 @@ open Lean.Parser.Command
@[builtinMacro Lean.Parser.Command.macro] def expandMacro : Macro
| `($[$doc?:docComment]? $attrKind:attrKind
macro$[:$prec?]? $[(name := $name?)]? $[(priority := $prio?)]? $args:macroArg* :
macro%$tk$[:$prec?]? $[(name := $name?)]? $[(priority := $prio?)]? $args:macroArg* :
$cat => $rhs) => do
let prio ← evalOptPrio prio?
let (stxParts, patArgs) := (← args.mapM expandMacroArg).unzip
@ -24,16 +24,16 @@ open Lean.Parser.Command
So, we must include current namespace when we create a pattern for the following `macro_rules` commands. -/
let pat := mkNode ((← Macro.getCurrNamespace) ++ name) patArgs
let stxCmd ← `($[$doc?:docComment]? $attrKind:attrKind
syntax$[:$prec?]? (name := $(← mkIdentFromRef name)) (priority := $(quote prio)) $[$stxParts]* : $cat)
syntax%$tk$[:$prec?]? (name := $(← mkIdentFromRef name)) (priority := $(quote prio)) $[$stxParts]* : $cat)
let macroRulesCmd ←
if rhs.getArgs.size == 1 then
-- `rhs` is a `term`
let rhs := rhs[0]
`($[$doc?:docComment]? macro_rules | `($pat) => $rhs)
`($[$doc?:docComment]? macro_rules%$tk | `($pat) => $rhs)
else
-- `rhs` is of the form `` `( $body ) ``
let rhsBody := rhs[1]
`($[$doc?:docComment]? macro_rules | `($pat) => `($rhsBody))
`($[$doc?:docComment]? macro_rules%$tk | `($pat) => `($rhsBody))
return mkNullNode #[stxCmd, macroRulesCmd]
| _ => Macro.throwUnsupported

View file

@ -15,7 +15,7 @@ open Lean.Parser.Command
Remark: `k` is the user provided kind with the current namespace included.
Recall that syntax node kinds contain the current namespace.
-/
def elabMacroRulesAux (doc? : Option Syntax) (attrKind : Syntax) (k : SyntaxNodeKind) (alts : Array Syntax) : CommandElabM Syntax := do
def elabMacroRulesAux (doc? : Option Syntax) (attrKind tk : Syntax) (k : SyntaxNodeKind) (alts : Array Syntax) : CommandElabM Syntax := do
let alts ← alts.mapM fun alt => match alt with
| `(matchAltExpr| | $pats,* => $rhs) => do
let pat := pats.elemsAndSeps[0]
@ -36,19 +36,19 @@ def elabMacroRulesAux (doc? : Option Syntax) (attrKind : Syntax) (k : SyntaxNode
throwErrorAt alt "invalid macro_rules alternative, unexpected syntax node kind '{k'}'"
| _ => throwUnsupportedSyntax
`($[$doc?:docComment]? @[$attrKind:attrKind macro $(Lean.mkIdent k)]
aux_def macroRules $(mkIdent k) : Macro :=
aux_def macroRules $(mkIdentFrom tk k) : Macro :=
fun $alts:matchAlt* | _ => throw Lean.Macro.Exception.unsupportedSyntax)
@[builtinCommandElab «macro_rules»] def elabMacroRules : CommandElab :=
adaptExpander fun stx => match stx with
| `($[$doc?:docComment]? $attrKind:attrKind macro_rules $alts:matchAlt*) =>
| `($[$doc?:docComment]? $attrKind:attrKind macro_rules%$tk $alts:matchAlt*) =>
expandNoKindMacroRulesAux alts "macro_rules" fun kind? alts =>
`($[$doc?:docComment]? $attrKind:attrKind macro_rules $[(kind := $(mkIdent <$> kind?))]? $alts:matchAlt*)
| `($[$doc?:docComment]? $attrKind:attrKind macro_rules (kind := $kind) | $x:ident => $rhs) =>
`($[$doc?:docComment]? $attrKind:attrKind macro_rules%$tk $[(kind := $(mkIdent <$> kind?))]? $alts:matchAlt*)
| `($[$doc?:docComment]? $attrKind:attrKind macro_rules%$tk (kind := $kind) | $x:ident => $rhs) =>
`($[$doc?:docComment]? @[$attrKind:attrKind macro $kind]
aux_def macroRules $kind : Macro := fun $x:ident => $rhs)
| `($[$doc?:docComment]? $attrKind:attrKind macro_rules (kind := $kind) $alts:matchAlt*) =>
do elabMacroRulesAux doc? attrKind (← resolveSyntaxKind kind.getId) alts
aux_def $(mkIdentFrom tk kind.getId) $kind : Macro := fun $x:ident => $rhs)
| `($[$doc?:docComment]? $attrKind:attrKind macro_rules%$tk (kind := $kind) $alts:matchAlt*) =>
do elabMacroRulesAux doc? attrKind tk (← resolveSyntaxKind kind.getId) alts
| _ => throwUnsupportedSyntax
end Lean.Elab.Command

View file

@ -52,3 +52,7 @@ example : Nat :=
--^ textDocument/definition
where
b := 2
macro_rules | `(test) => `(3)
#check test
--^ textDocument/definition

View file

@ -105,3 +105,13 @@
{"start": {"line": 53, "character": 2}, "end": {"line": 53, "character": 3}},
"originSelectionRange":
{"start": {"line": 50, "character": 6}, "end": {"line": 50, "character": 7}}}]
{"textDocument": {"uri": "file://goTo.lean"},
"position": {"line": 56, "character": 7}}
[{"targetUri": "file://goTo.lean",
"targetSelectionRange":
{"start": {"line": 55, "character": 0}, "end": {"line": 55, "character": 11}},
"targetRange":
{"start": {"line": 55, "character": 0}, "end": {"line": 55, "character": 29}},
"originSelectionRange":
{"start": {"line": 56, "character": 7},
"end": {"line": 56, "character": 11}}}]

View file

@ -2,7 +2,7 @@
scopedMacros.lean:11:7-11:11: error: unknown identifier 'foo!'
10 + 1 : Nat
scopedMacros.lean:19:0-19:50: error: scoped attributes must be used inside namespaces
scopedMacros.lean:19:0-19:50: error: invalid syntax node kind 'termBla!_'
scopedMacros.lean:19:7-19:50: error: invalid syntax node kind 'termBla!_'
scopedMacros.lean:29:7-29:11: error: unknown identifier 'bla!'
scopedMacros.lean:36:0-36:45: error: scoped attributes must be used inside namespaces
10 + 20 : Nat