Skip to content

Commit d5d6104

Browse files
committed
feat: macroScope parser (aka this 2.0)
1 parent e01003f commit d5d6104

File tree

9 files changed

+50
-3
lines changed

9 files changed

+50
-3
lines changed

src/Init/Meta.lean

+10-1
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,11 @@ abbrev CharLit := TSyntax charLitKind
271271
abbrev NameLit := TSyntax nameLitKind
272272
abbrev ScientificLit := TSyntax scientificLitKind
273273
abbrev NumLit := TSyntax numLitKind
274+
abbrev MacroScopeLit := TSyntax macroScopeKind
274275

275276
end Syntax
276277

277-
export Syntax (Term Command Prec Prio Ident StrLit CharLit NameLit ScientificLit NumLit)
278+
export Syntax (Term Command Prec Prio Ident StrLit CharLit NameLit ScientificLit NumLit MacroScopeLit)
278279

279280
namespace TSyntax
280281

@@ -921,6 +922,14 @@ def getChar (s : CharLit) : Char :=
921922
def getName (s : NameLit) : Name :=
922923
s.raw.isNameLit?.getD .anonymous
923924

925+
def getMacroScope (s : MacroScopeLit) : Name :=
926+
s.raw[0].getId
927+
928+
def mkIdent (s : MacroScopeLit) (val : Name) (canonical := false) : Ident :=
929+
let src := s.raw[0]
930+
let id := { extractMacroScopes src.getId with name := val.eraseMacroScopes }.review
931+
⟨Syntax.ident (SourceInfo.fromRef src canonical) (toString val).toSubstring id []⟩
932+
924933
namespace Compat
925934

926935
scoped instance : CoeTail (Array Syntax) (Syntax.TSepArray k sep) where

src/Init/Prelude.lean

+6
Original file line numberDiff line numberDiff line change
@@ -3721,6 +3721,12 @@ abbrev nameLitKind : SyntaxNodeKind := `name
37213721
/-- `fieldIdx` is the node kind of projection indices like the `2` in `x.2`. -/
37223722
abbrev fieldIdxKind : SyntaxNodeKind := `fieldIdx
37233723

3724+
/--
3725+
`macroScope` is the node kind of macro scopes, which are "invisible tokens"
3726+
which capture the hygiene information at the current point without parsing anything.
3727+
-/
3728+
abbrev macroScopeKind : SyntaxNodeKind := `macroScope
3729+
37243730
/--
37253731
`interpolatedStrLitKind` is the node kind of interpolated string literal
37263732
fragments like `"value = {` and `}"` in `s!"value = {x}"`.

src/Lean/Elab/Syntax.lean

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ open Lean.Parser.Command
260260

261261
private def declareSyntaxCatQuotParser (catName : Name) : CommandElabM Unit := do
262262
if let .str _ suffix := catName then
263-
let quotSymbol := "`(" ++ suffix ++ "|"
263+
let quotSymbol := "`(" ++ suffix ++ "| "
264264
let name := catName ++ `quot
265265
let cmd ← `(
266266
@[term_parser] def $(mkIdent name) : Lean.ParserDescr :=

src/Lean/Parser.lean

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ builtin_initialize
2727
register_parser_alias (kind := charLitKind) "char" charLit
2828
register_parser_alias (kind := nameLitKind) "name" nameLit
2929
register_parser_alias (kind := scientificLitKind) "scientific" scientificLit
30-
register_parser_alias (kind := identKind) "ident" ident
30+
register_parser_alias (kind := identKind) ident
31+
register_parser_alias (kind := macroScopeKind) macroScope
3132
register_parser_alias "colGt" checkColGt { stackSz? := some 0 }
3233
register_parser_alias "colGe" checkColGe { stackSz? := some 0 }
3334
register_parser_alias "colEq" checkColEq { stackSz? := some 0 }

src/Lean/Parser/Basic.lean

+12
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,18 @@ def identEq (id : Name) : Parser := {
10721072
info := mkAtomicInfo "ident"
10731073
}
10741074

1075+
def macroScopeFn : ParserFn := fun _ s =>
1076+
let pos := s.pos
1077+
let info := SourceInfo.original "".toSubstring pos "".toSubstring pos
1078+
let ident := mkIdent info "".toSubstring .anonymous
1079+
let stx := mkNode macroScopeKind #[ident]
1080+
s.pushSyntax stx
1081+
1082+
def macroScopeNoAntiquot : Parser := {
1083+
fn := macroScopeFn
1084+
info := nodeInfo macroScopeKind epsilonInfo
1085+
}
1086+
10751087
namespace ParserState
10761088

10771089
def keepTop (s : SyntaxStack) (startStackSize : Nat) : SyntaxStack :=

src/Lean/Parser/Extra.lean

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ attribute [run_builtin_parser_attribute_hooks]
3737
@[run_builtin_parser_attribute_hooks] def rawIdent : Parser :=
3838
withAntiquot (mkAntiquot "ident" identKind) rawIdentNoAntiquot
3939

40+
@[run_builtin_parser_attribute_hooks] def macroScope : Parser :=
41+
withAntiquot (mkAntiquot "macroScope" macroScopeKind) macroScopeNoAntiquot
42+
4043
@[run_builtin_parser_attribute_hooks] def numLit : Parser :=
4144
withAntiquot (mkAntiquot "num" numLitKind) numLitNoAntiquot
4245

src/Lean/PrettyPrinter/Formatter.lean

+1
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ def sepByNoAntiquot.formatter (p pSep : Formatter) : Formatter := do
489489
@[combinator_formatter skip] def skip.formatter : Formatter := pure ()
490490

491491
@[combinator_formatter pushNone] def pushNone.formatter : Formatter := goLeft
492+
@[combinator_formatter macroScopeNoAntiquot] def macroScopeNoAntiquot.formatter : Formatter := goLeft
492493

493494
@[combinator_formatter interpolatedStr]
494495
def interpolatedStr.formatter (p : Formatter) : Formatter := do

src/Lean/PrettyPrinter/Parenthesizer.lean

+1
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ def sepByNoAntiquot.parenthesizer (p pSep : Parenthesizer) : Parenthesizer := do
492492
@[combinator_parenthesizer skip] def skip.parenthesizer : Parenthesizer := pure ()
493493

494494
@[combinator_parenthesizer pushNone] def pushNone.parenthesizer : Parenthesizer := goLeft
495+
@[combinator_parenthesizer macroScopeNoAntiquot] def macroScopeNoAntiquot.parenthesizer : Parenthesizer := goLeft
495496

496497
@[combinator_parenthesizer interpolatedStr]
497498
def interpolatedStr.parenthesizer (p : Parenthesizer) : Parenthesizer := do

tests/lean/interactive/1265.lean.expected.out

+14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"position": {"line": 0, "character": 51}}
33
{"items":
44
[{"label": "getId", "kind": 3, "detail": "Lean.Ident → Lean.Name"},
5+
{"label": "getMacroScope",
6+
"kind": 3,
7+
"detail": "Lean.MacroScopeLit → Lean.Name"},
58
{"label": "getName", "kind": 3, "detail": "Lean.NameLit → Lean.Name"},
69
{"label": "expandInterpolatedStr",
710
"kind": 3,
@@ -13,6 +16,10 @@
1316
"kind": 3,
1417
"detail": "Lean.ScientificLit → Nat × Bool × Nat"},
1518
{"label": "getString", "kind": 3, "detail": "Lean.StrLit → String"},
19+
{"label": "mkIdent",
20+
"kind": 3,
21+
"detail":
22+
"Lean.MacroScopeLit → Lean.Name → optParam Bool false → Lean.Ident"},
1623
{"label": "raw",
1724
"kind": 5,
1825
"documentation":
@@ -23,6 +30,9 @@
2330
"position": {"line": 2, "character": 53}}
2431
{"items":
2532
[{"label": "getId", "kind": 3, "detail": "Lean.Ident → Lean.Name"},
33+
{"label": "getMacroScope",
34+
"kind": 3,
35+
"detail": "Lean.MacroScopeLit → Lean.Name"},
2636
{"label": "getName", "kind": 3, "detail": "Lean.NameLit → Lean.Name"},
2737
{"label": "expandInterpolatedStr",
2838
"kind": 3,
@@ -34,6 +44,10 @@
3444
"kind": 3,
3545
"detail": "Lean.ScientificLit → Nat × Bool × Nat"},
3646
{"label": "getString", "kind": 3, "detail": "Lean.StrLit → String"},
47+
{"label": "mkIdent",
48+
"kind": 3,
49+
"detail":
50+
"Lean.MacroScopeLit → Lean.Name → optParam Bool false → Lean.Ident"},
3751
{"label": "raw",
3852
"kind": 5,
3953
"documentation":

0 commit comments

Comments
 (0)