feat(library/init/lean/parser/parser): add withPosition and checkColGe parsers
This commit is contained in:
parent
3ef8845163
commit
f95ed02999
1 changed files with 15 additions and 0 deletions
|
|
@ -745,6 +745,15 @@ let sym := sym.trim;
|
|||
@[inline] def symbol {k : ParserKind} (sym : String) (lbp : Nat) : Parser k :=
|
||||
symbolAux sym lbp
|
||||
|
||||
@[inline] def checkColGeFn (col : Nat) (errorMsg : String) : BasicParserFn :=
|
||||
fun c s =>
|
||||
let pos := c.fileMap.toPosition s.pos;
|
||||
if pos.column ≥ col then s
|
||||
else s.mkError errorMsg
|
||||
|
||||
@[inline] def checkColGe {k : ParserKind} (col : Nat) (errorMsg : String) : Parser k :=
|
||||
{ fn := fun _ => checkColGeFn col errorMsg }
|
||||
|
||||
partial def strAux (sym : String) (errorMsg : String) : Nat → BasicParserFn
|
||||
| j c s :=
|
||||
if sym.atEnd j then s
|
||||
|
|
@ -971,6 +980,12 @@ def anyOfFn {k : ParserKind} : List (Parser k) → ParserFn k
|
|||
| [p] a c s := p.fn a c s
|
||||
| (p::ps) a c s := orelseFn p.fn (anyOfFn ps) a c s
|
||||
|
||||
@[inline] def withPosition {k : ParserKind} (p : Position → Parser k) : Parser k :=
|
||||
{ info := (p { line := 1, column := 0 }).info,
|
||||
fn := fun a c s =>
|
||||
let pos := c.fileMap.toPosition s.pos;
|
||||
(p pos).fn a c s }
|
||||
|
||||
/-- A multimap indexed by tokens. Used for indexing parsers by their leading token. -/
|
||||
def TokenMap (α : Type) := RBMap Name (List α) Name.quickLt
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue