Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse .. as a full pattern #1848

Merged
merged 4 commits into from
Sep 15, 2019
Merged
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
1 change: 1 addition & 0 deletions crates/ra_hir/src/expr/lower.rs
Original file line number Diff line number Diff line change
@@ -573,6 +573,7 @@ where
}

// FIXME: implement
ast::Pat::DotDotPat(_) => Pat::Missing,
ast::Pat::BoxPat(_) => Pat::Missing,
ast::Pat::LiteralPat(_) => Pat::Missing,
ast::Pat::SlicePat(_) | ast::Pat::RangePat(_) => Pat::Missing,
52 changes: 42 additions & 10 deletions crates/ra_parser/src/grammar/patterns.rs
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ use super::*;

pub(super) const PATTERN_FIRST: TokenSet = expressions::LITERAL_FIRST
.union(paths::PATH_FIRST)
.union(token_set![BOX_KW, REF_KW, MUT_KW, L_PAREN, L_BRACK, AMP, UNDERSCORE, MINUS]);
.union(token_set![BOX_KW, REF_KW, MUT_KW, L_PAREN, L_BRACK, AMP, UNDERSCORE, MINUS, DOT]);

pub(crate) fn pattern(p: &mut Parser) {
pattern_r(p, PAT_RECOVERY_SET);
@@ -73,6 +73,7 @@ fn atom_pat(p: &mut Parser, recovery_set: TokenSet) -> Option<CompletedMarker> {
_ if paths::is_use_path_start(p) => path_pat(p),
_ if is_literal_pat_start(p) => literal_pat(p),

T![.] if p.at(T![..]) => dot_dot_pat(p),
T![_] => placeholder_pat(p),
T![&] => ref_pat(p),
T!['('] => tuple_pat(p),
@@ -163,7 +164,9 @@ fn record_field_pat_list(p: &mut Parser) {
p.bump_any();
while !p.at(EOF) && !p.at(T!['}']) {
match p.current() {
// A trailing `..` is *not* treated as a DOT_DOT_PAT.
T![.] if p.at(T![..]) => p.bump(T![..]),

IDENT if p.nth(1) == T![:] => record_field_pat(p),
T!['{'] => error_block(p, "expected ident"),
T![box] => {
@@ -201,6 +204,39 @@ fn placeholder_pat(p: &mut Parser) -> CompletedMarker {
m.complete(p, PLACEHOLDER_PAT)
}

// test dot_dot_pat
// fn main() {
// let .. = ();
// //
// // Tuples
// //
// let (a, ..) = ();
// let (a, ..,) = ();
// let Tuple(a, ..) = ();
// let Tuple(a, ..,) = ();
// let (.., ..) = ();
// let Tuple(.., ..) = ();
// let (.., a, ..) = ();
// let Tuple(.., a, ..) = ();
// //
// // Slices
// //
// let [..] = ();
// let [head, ..] = ();
// let [head, tail @ ..] = ();
// let [head, .., cons] = ();
// let [head, mid @ .., cons] = ();
// let [head, .., .., cons] = ();
// let [head, .., mid, tail @ ..] = ();
// let [head, .., mid, .., cons] = ();
// }
fn dot_dot_pat(p: &mut Parser) -> CompletedMarker {
assert!(p.at(T![..]));
let m = p.start();
p.bump(T![..]);
m.complete(p, DOT_DOT_PAT)
}

// test ref_pat
// fn main() {
// let &a = ();
@@ -241,16 +277,12 @@ fn slice_pat(p: &mut Parser) -> CompletedMarker {

fn pat_list(p: &mut Parser, ket: SyntaxKind) {
while !p.at(EOF) && !p.at(ket) {
match p.current() {
T![.] if p.at(T![..]) => p.bump(T![..]),
_ => {
if !p.at_ts(PATTERN_FIRST) {
p.error("expected a pattern");
break;
}
pattern(p)
}
if !p.at_ts(PATTERN_FIRST) {
p.error("expected a pattern");
break;
}

pattern(p);
if !p.at(ket) {
p.expect(T![,]);
}
1 change: 1 addition & 0 deletions crates/ra_parser/src/syntax_kind/generated.rs
Original file line number Diff line number Diff line change
@@ -152,6 +152,7 @@ pub enum SyntaxKind {
BOX_PAT,
BIND_PAT,
PLACEHOLDER_PAT,
DOT_DOT_PAT,
PATH_PAT,
RECORD_PAT,
RECORD_FIELD_PAT_LIST,
37 changes: 35 additions & 2 deletions crates/ra_syntax/src/ast/generated.rs
Original file line number Diff line number Diff line change
@@ -532,6 +532,29 @@ impl AstNode for ContinueExpr {
}
impl ContinueExpr {}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct DotDotPat {
pub(crate) syntax: SyntaxNode,
}
impl AstNode for DotDotPat {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
DOT_DOT_PAT => true,
_ => false,
}
}
fn cast(syntax: SyntaxNode) -> Option<Self> {
if Self::can_cast(syntax.kind()) {
Some(Self { syntax })
} else {
None
}
}
fn syntax(&self) -> &SyntaxNode {
&self.syntax
}
}
impl DotDotPat {}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct DynTraitType {
pub(crate) syntax: SyntaxNode,
}
@@ -2128,6 +2151,7 @@ pub enum Pat {
BoxPat(BoxPat),
BindPat(BindPat),
PlaceholderPat(PlaceholderPat),
DotDotPat(DotDotPat),
PathPat(PathPat),
RecordPat(RecordPat),
TupleStructPat(TupleStructPat),
@@ -2156,6 +2180,11 @@ impl From<PlaceholderPat> for Pat {
Pat::PlaceholderPat(node)
}
}
impl From<DotDotPat> for Pat {
fn from(node: DotDotPat) -> Pat {
Pat::DotDotPat(node)
}
}
impl From<PathPat> for Pat {
fn from(node: PathPat) -> Pat {
Pat::PathPat(node)
@@ -2194,8 +2223,10 @@ impl From<LiteralPat> for Pat {
impl AstNode for Pat {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
REF_PAT | BOX_PAT | BIND_PAT | PLACEHOLDER_PAT | PATH_PAT | RECORD_PAT
| TUPLE_STRUCT_PAT | TUPLE_PAT | SLICE_PAT | RANGE_PAT | LITERAL_PAT => true,
REF_PAT | BOX_PAT | BIND_PAT | PLACEHOLDER_PAT | DOT_DOT_PAT | PATH_PAT
| RECORD_PAT | TUPLE_STRUCT_PAT | TUPLE_PAT | SLICE_PAT | RANGE_PAT | LITERAL_PAT => {
true
}
_ => false,
}
}
@@ -2205,6 +2236,7 @@ impl AstNode for Pat {
BOX_PAT => Pat::BoxPat(BoxPat { syntax }),
BIND_PAT => Pat::BindPat(BindPat { syntax }),
PLACEHOLDER_PAT => Pat::PlaceholderPat(PlaceholderPat { syntax }),
DOT_DOT_PAT => Pat::DotDotPat(DotDotPat { syntax }),
PATH_PAT => Pat::PathPat(PathPat { syntax }),
RECORD_PAT => Pat::RecordPat(RecordPat { syntax }),
TUPLE_STRUCT_PAT => Pat::TupleStructPat(TupleStructPat { syntax }),
@@ -2222,6 +2254,7 @@ impl AstNode for Pat {
Pat::BoxPat(it) => &it.syntax,
Pat::BindPat(it) => &it.syntax,
Pat::PlaceholderPat(it) => &it.syntax,
Pat::DotDotPat(it) => &it.syntax,
Pat::PathPat(it) => &it.syntax,
Pat::RecordPat(it) => &it.syntax,
Pat::TupleStructPat(it) => &it.syntax,
3 changes: 3 additions & 0 deletions crates/ra_syntax/src/grammar.ron
Original file line number Diff line number Diff line change
@@ -161,6 +161,7 @@ Grammar(
"BOX_PAT",
"BIND_PAT",
"PLACEHOLDER_PAT",
"DOT_DOT_PAT",
"PATH_PAT",
"RECORD_PAT",
"RECORD_FIELD_PAT_LIST",
@@ -532,6 +533,7 @@ Grammar(
traits: ["NameOwner"]
),
"PlaceholderPat": (),
"DotDotPat": (),
"PathPat": ( options: [ "Path" ] ),
"RecordPat": ( options: ["RecordFieldPatList", "Path"] ),
"RecordFieldPatList": (
@@ -559,6 +561,7 @@ Grammar(
"BoxPat",
"BindPat",
"PlaceholderPat",
"DotDotPat",
"PathPat",
"RecordPat",
"TupleStructPat",
Original file line number Diff line number Diff line change
@@ -82,7 +82,8 @@ SOURCE_FILE@[0; 103)
NAME_REF@[87; 90)
IDENT@[87; 90) "Bar"
L_PAREN@[90; 91) "("
DOTDOT@[91; 93) ".."
DOT_DOT_PAT@[91; 93)
DOTDOT@[91; 93) ".."
R_PAREN@[93; 94) ")"
WHITESPACE@[94; 95) " "
EQ@[95; 96) "="
Original file line number Diff line number Diff line change
@@ -27,7 +27,8 @@ SOURCE_FILE@[0; 39)
IDENT@[24; 25) "b"
COMMA@[25; 26) ","
WHITESPACE@[26; 27) " "
DOTDOT@[27; 29) ".."
DOT_DOT_PAT@[27; 29)
DOTDOT@[27; 29) ".."
R_BRACK@[29; 30) "]"
WHITESPACE@[30; 31) " "
EQ@[31; 32) "="
Original file line number Diff line number Diff line change
@@ -84,7 +84,8 @@ SOURCE_FILE@[0; 97)
UNDERSCORE@[78; 79) "_"
COMMA@[79; 80) ","
WHITESPACE@[80; 81) " "
DOTDOT@[81; 83) ".."
DOT_DOT_PAT@[81; 83)
DOTDOT@[81; 83) ".."
WHITESPACE@[83; 84) " "
COMMA@[84; 85) ","
WHITESPACE@[85; 86) " "
Original file line number Diff line number Diff line change
@@ -27,7 +27,8 @@ SOURCE_FILE@[0; 39)
IDENT@[24; 25) "b"
COMMA@[25; 26) ","
WHITESPACE@[26; 27) " "
DOTDOT@[27; 29) ".."
DOT_DOT_PAT@[27; 29)
DOTDOT@[27; 29) ".."
R_PAREN@[29; 30) ")"
WHITESPACE@[30; 31) " "
EQ@[31; 32) "="
25 changes: 25 additions & 0 deletions crates/ra_syntax/test_data/parser/inline/ok/0144_dot_dot_pat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
fn main() {
let .. = ();
//
// Tuples
//
let (a, ..) = ();
let (a, ..,) = ();
let Tuple(a, ..) = ();
let Tuple(a, ..,) = ();
let (.., ..) = ();
let Tuple(.., ..) = ();
let (.., a, ..) = ();
let Tuple(.., a, ..) = ();
//
// Slices
//
let [..] = ();
let [head, ..] = ();
let [head, tail @ ..] = ();
let [head, .., cons] = ();
let [head, mid @ .., cons] = ();
let [head, .., .., cons] = ();
let [head, .., mid, tail @ ..] = ();
let [head, .., mid, .., cons] = ();
}
456 changes: 456 additions & 0 deletions crates/ra_syntax/test_data/parser/inline/ok/0144_dot_dot_pat.txt

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion crates/ra_syntax/test_data/parser/ok/0035_weird_exprs.txt
Original file line number Diff line number Diff line change
@@ -1656,7 +1656,8 @@ SOURCE_FILE@[0; 3813)
PARAM@[2952; 2962)
TUPLE_PAT@[2952; 2956)
L_PAREN@[2952; 2953) "("
DOTDOT@[2953; 2955) ".."
DOT_DOT_PAT@[2953; 2955)
DOTDOT@[2953; 2955) ".."
R_PAREN@[2955; 2956) ")"
COLON@[2956; 2957) ":"
TUPLE_TYPE@[2957; 2962)