Change parsing of struct field patterns

This commit is contained in:
Marcus Klaas de Vries 2019-01-19 01:02:38 +01:00 committed by Aleksey Kladov
parent bcbfa2cc11
commit fa43ef30f4
5 changed files with 46 additions and 40 deletions

View file

@ -910,7 +910,11 @@ impl AstNode for FieldPatList {
impl FieldPatList {
pub fn pats(&self) -> impl Iterator<Item = &FieldPat> {
pub fn field_pats(&self) -> impl Iterator<Item = &FieldPat> {
super::children(self)
}
pub fn bind_pats(&self) -> impl Iterator<Item = &BindPat> {
super::children(self)
}
}

View file

@ -496,7 +496,12 @@ Grammar(
"PlaceholderPat": (),
"PathPat": ( options: [ "Path" ] ),
"StructPat": ( options: ["FieldPatList", "Path"] ),
"FieldPatList": ( collections: [["pats", "FieldPat"]] ),
"FieldPatList": (
collections: [
["field_pats", "FieldPat"],
["bind_pats", "BindPat"],
]
),
"FieldPat": (
traits: ["NameOwner"],
options: ["Pat"]

View file

@ -128,7 +128,11 @@ fn field_pat_list(p: &mut Parser) {
while !p.at(EOF) && !p.at(R_CURLY) {
match p.current() {
DOTDOT => p.bump(),
_ => field_pat(p),
IDENT if p.nth(1) == COLON => field_pat(p),
L_CURLY => error_block(p, "expected ident"),
_ => {
bind_pat(p, false);
}
}
if !p.at(R_CURLY) {
p.expect(COMMA);
@ -139,18 +143,13 @@ fn field_pat_list(p: &mut Parser) {
}
fn field_pat(p: &mut Parser) {
assert!(p.at(IDENT));
assert!(p.nth(1) == COLON);
let m = p.start();
match p.current() {
IDENT if p.nth(1) == COLON => {
name(p);
p.bump();
pattern(p);
}
L_CURLY => error_block(p, "expected ident"),
_ => {
bind_pat(p, false);
}
}
name(p);
p.bump();
pattern(p);
m.complete(p, FIELD_PAT);
}