feat(library/init/lean/parser/parser): add fieldIdx parser

We should not use `numLit` for projections since it will parse
`p.1.2` as
```
Term.proj `p (numLit "1.2")
```
This commit is contained in:
Leonardo de Moura 2019-07-05 15:07:51 -07:00
parent 3285a9e77d
commit dca0ba60fa
2 changed files with 23 additions and 2 deletions

View file

@ -780,6 +780,20 @@ fun _ c s =>
{ fn := identFn,
info := mkAtomicInfo "ident" }
def fieldIdxFn : BasicParserFn :=
fun c s =>
let iniPos := s.pos;
let curr := c.input.get iniPos;
if curr.isDigit && curr != '0' then
let s := takeWhileFn (fun c => c.isDigit) c s;
mkNodeToken fieldIdxKind iniPos c s
else
s.mkErrorAt "expected field index" iniPos
@[inline] def fieldIdx {k : ParserKind} : Parser k :=
{ fn := fun _ => fieldIdxFn,
info := mkAtomicInfo "fieldIdx" }
instance string2basic {k : ParserKind} : HasCoe String (Parser k) :=
⟨symbol⟩

View file

@ -35,6 +35,7 @@ abbrev SyntaxNodeKind := Name
@[matchPattern] def nullKind : SyntaxNodeKind := `null
def strLitKind : SyntaxNodeKind := `strLit
def numLitKind : SyntaxNodeKind := `numLit
def fieldIdxKind : SyntaxNodeKind := `fieldIdx
/- Syntax AST -/
@ -316,9 +317,9 @@ else
else if c.isDigit then decodeDecimalLitAux s 0 0
else none
def isNatLit : Syntax → Option Nat
def isNatLitAux (nodeKind : SyntaxNodeKind) : Syntax → Option Nat
| (Syntax.node k args _) :=
if k == numLitKind && args.size == 1 then
if k == nodeKind && args.size == 1 then
match args.get 0 with
| (Syntax.atom _ val) => decodeNatLitVal val
| _ => none
@ -326,6 +327,12 @@ def isNatLit : Syntax → Option Nat
none
| _ := none
def isNatLit (s : Syntax) : Option Nat :=
isNatLitAux numLitKind s
def isFieldIdx (s : Syntax) : Option Nat :=
isNatLitAux fieldIdxKind s
def isIdOrAtom : Syntax → Option String
| (Syntax.atom _ val) := some val
| (Syntax.ident _ rawVal _ _ _) := some rawVal.toString