feat: add withResultOf parser combinator and unboxSingleton

This commit is contained in:
Leonardo de Moura 2020-09-17 12:03:16 -07:00
parent 56f8103e66
commit 4265348bed
3 changed files with 28 additions and 0 deletions

View file

@ -581,6 +581,26 @@ fun c s =>
{ info := sepBy1Info p.info sep.info,
fn := sepBy1Fn allowTrailingSep p.fn sep.fn unboxSingleton }
/- Apply `f` to the syntax object produced by `p` -/
@[inline] def withResultOfFn (p : ParserFn) (f : Syntax → Syntax) : ParserFn :=
fun c s =>
let s := p c s;
if s.hasError then s
else
let stx := s.stxStack.back;
s.popSyntax.pushSyntax (f stx)
@[noinline] def withResultOfInfo (p : ParserInfo) : ParserInfo :=
{ collectTokens := p.collectTokens,
collectKinds := p.collectKinds }
@[inline] def withResultOf (p : Parser) (f : Syntax → Syntax) : Parser :=
{ info := withResultOfInfo p.info,
fn := withResultOfFn p.fn f }
abbrev unboxSingleton (p : Parser) : Parser :=
withResultOf p fun stx => if stx.getNumArgs == 1 then stx.getArg 0 else stx
@[specialize] partial def satisfyFn (p : Char → Bool) (errorMsg : String := "unexpected character") : ParserFn
| c, s =>
let i := s.pos;

View file

@ -323,6 +323,10 @@ else
def optional.formatter (p : Formatter) : Formatter := do
concatArgs p
@[combinatorFormatter Parser.withResultOf]
def withResultOf.formatter (p : Formatter) (f : Syntax → Syntax) : Formatter := do
concatArgs p
@[combinatorFormatter sepBy]
def sepBy.formatter (p pSep : Formatter) : Formatter := do
stx ← getCur;

View file

@ -408,6 +408,10 @@ else
def optional.parenthesizer (p : Parenthesizer) : Parenthesizer := do
visitArgs p
@[combinatorParenthesizer Lean.Parser.withResultOf]
def withResultOf.parenthesizer (p : Parenthesizer) (f : Syntax → Syntax) : Parenthesizer := do
visitArgs p
@[combinatorParenthesizer Lean.Parser.sepBy]
def sepBy.parenthesizer (p pSep : Parenthesizer) : Parenthesizer := do
stx ← getCur;