Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/Lean/Parser/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,20 @@ def nameLitNoAntiquot : Parser := {
info := mkAtomicInfo "name"
}

def identFn : ParserFn := expectTokenFn identKind "identifier"
def identFn : ParserFn := fun c s =>
if c.forbiddenTks.isEmpty then
expectTokenFn identKind "identifier" c s
else
-- A forbidden token used as an identifier (e.g. a non-reserved clause keyword like
-- `invariant`) stops the enclosing term, mirroring `mkTokenAndFixPos`.
let iniSz := s.stackSize
let iniPos := s.pos
let s := expectTokenFn identKind "identifier" c s
if s.hasError then s
else match s.stxStack.back with
| .ident _ rawVal _ _ =>
if c.forbiddenTks.contains rawVal.toString then s.mkErrorAt "forbidden token" iniPos iniSz else s
| _ => s

def identNoAntiquot : Parser := {
fn := identFn
Expand Down
30 changes: 30 additions & 0 deletions tests/elab/forbiddenIdentifiers.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Lean
/-!
`withForbidden` stops a term at a non-reserved word used as a bare identifier, while the word
stays usable as an identifier (escaped, projected, or a plain declaration name).
-/
open Lean Elab Command Parser

namespace ForbiddenIdentTest

-- `#collect e sep e'` parses `e` with the non-reserved word `sep` forbidden, so a bare `sep`
-- ends `e` without `sep` being a reserved keyword.
@[command_parser]
def collectCmd : Parser := leading_parser
"#collect " >> withForbidden "sep" termParser >> nonReservedSymbol "sep" >> ppSpace >> termParser

@[command_elab collectCmd]
def elabCollect : CommandElab := fun _ => pure ()

-- A bare `sep` ends the first term (requires the forbidding; else `a sep` is an application).
#collect a sep b
-- A multi-token first term still stops at the bare `sep`.
#collect f a sep b
-- Escaped `«sep»` is not forbidden: it is consumed as the first term.
#collect «sep» sep b
-- A projection `.sep` (rawIdent path) is not forbidden.
#collect (id x).sep sep b
-- The word remains usable as an ordinary identifier.
def sep : Nat := 0

end ForbiddenIdentTest
Loading