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 arbitrarily complex box patterns. #1733

Merged
merged 6 commits into from
Aug 25, 2019
Merged
Show file tree
Hide file tree
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.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,7 @@ where
}

// FIXME: implement
ast::Pat::BoxPat(_) => Pat::Missing,
ast::Pat::LiteralPat(_) => Pat::Missing,
ast::Pat::SlicePat(_) | ast::Pat::RangePat(_) => Pat::Missing,
};
Expand Down
2 changes: 0 additions & 2 deletions crates/ra_parser/src/grammar/expressions/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,6 @@ pub(crate) fn match_arm_list(p: &mut Parser) {
// X | Y if Z => (),
// | X | Y if Z => (),
// | X => (),
// box X => (),
// Some(box X) => (),
// };
// }
fn match_arm(p: &mut Parser) -> BlockLike {
Expand Down
49 changes: 30 additions & 19 deletions crates/ra_parser/src/grammar/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,37 +56,33 @@ const PAT_RECOVERY_SET: TokenSet =
token_set![LET_KW, IF_KW, WHILE_KW, LOOP_KW, MATCH_KW, R_PAREN, COMMA];

fn atom_pat(p: &mut Parser, recovery_set: TokenSet) -> Option<CompletedMarker> {
let la0 = p.nth(0);
let la1 = p.nth(1);
if la0 == T![ref]
|| la0 == T![mut]
|| la0 == T![box]
|| (la0 == IDENT && !(la1 == T![::] || la1 == T!['('] || la1 == T!['{'] || la1 == T![!]))
{
return Some(bind_pat(p, true));
}
if paths::is_use_path_start(p) {
return Some(path_pat(p));
}
// Checks the token after an IDENT to see if a pattern is a path (Struct { .. }) or macro
// (T![x]).
let is_path_or_macro_pat =
|la1| la1 == T![::] || la1 == T!['('] || la1 == T!['{'] || la1 == T![!];

if is_literal_pat_start(p) {
return Some(literal_pat(p));
}
let m = match p.nth(0) {
T![box] => box_pat(p),
T![ref] | T![mut] | IDENT if !is_path_or_macro_pat(p.nth(1)) => bind_pat(p, true),

_ if paths::is_use_path_start(p) => path_pat(p),
_ if is_literal_pat_start(p) => literal_pat(p),

let m = match la0 {
T![_] => placeholder_pat(p),
T![&] => ref_pat(p),
T!['('] => tuple_pat(p),
T!['['] => slice_pat(p),

_ => {
p.err_recover("expected pattern", recovery_set);
return None;
}
};

Some(m)
}

fn is_literal_pat_start(p: &mut Parser) -> bool {
fn is_literal_pat_start(p: &Parser) -> bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

p.at(T![-]) && (p.nth(1) == INT_NUMBER || p.nth(1) == FLOAT_NUMBER)
|| p.at_ts(expressions::LITERAL_FIRST)
}
Expand Down Expand Up @@ -165,6 +161,9 @@ fn record_field_pat_list(p: &mut Parser) {
T![..] => p.bump(),
IDENT if p.nth(1) == T![:] => record_field_pat(p),
T!['{'] => error_block(p, "expected ident"),
T![box] => {
box_pat(p);
}
_ => {
bind_pat(p, false);
}
Expand Down Expand Up @@ -261,11 +260,9 @@ fn pat_list(p: &mut Parser, ket: SyntaxKind) {
// let ref mut d = ();
// let e @ _ = ();
// let ref mut f @ g @ _ = ();
// let box i = Box::new(1i32);
// }
fn bind_pat(p: &mut Parser, with_at: bool) -> CompletedMarker {
let m = p.start();
p.eat(T![box]);
p.eat(T![ref]);
p.eat(T![mut]);
name(p);
Expand All @@ -274,3 +271,17 @@ fn bind_pat(p: &mut Parser, with_at: bool) -> CompletedMarker {
}
m.complete(p, BIND_PAT)
}

// test box_pat
// fn main() {
// let box i = ();
// let box Outer { box i, j: box Inner(box &x) } = ();
// let box ref mut i = ();
// }
fn box_pat(p: &mut Parser) -> CompletedMarker {
assert!(p.at(T![box]));
let m = p.start();
p.bump();
pattern(p);
m.complete(p, BOX_PAT)
}
1 change: 1 addition & 0 deletions crates/ra_parser/src/syntax_kind/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ pub enum SyntaxKind {
IMPL_TRAIT_TYPE,
DYN_TRAIT_TYPE,
REF_PAT,
BOX_PAT,
BIND_PAT,
PLACEHOLDER_PAT,
PATH_PAT,
Expand Down
39 changes: 37 additions & 2 deletions crates/ra_syntax/src/ast/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,33 @@ impl BlockExpr {
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct BoxPat {
pub(crate) syntax: SyntaxNode,
}
impl AstNode for BoxPat {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
BOX_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 BoxPat {
pub fn pat(&self) -> Option<Pat> {
AstChildren::new(&self.syntax).next()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct BreakExpr {
pub(crate) syntax: SyntaxNode,
}
Expand Down Expand Up @@ -2063,6 +2090,7 @@ impl ParenType {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Pat {
RefPat(RefPat),
BoxPat(BoxPat),
BindPat(BindPat),
PlaceholderPat(PlaceholderPat),
PathPat(PathPat),
Expand All @@ -2078,6 +2106,11 @@ impl From<RefPat> for Pat {
Pat::RefPat(node)
}
}
impl From<BoxPat> for Pat {
fn from(node: BoxPat) -> Pat {
Pat::BoxPat(node)
}
}
impl From<BindPat> for Pat {
fn from(node: BindPat) -> Pat {
Pat::BindPat(node)
Expand Down Expand Up @@ -2126,14 +2159,15 @@ impl From<LiteralPat> for Pat {
impl AstNode for Pat {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
REF_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 | PATH_PAT | RECORD_PAT
| TUPLE_STRUCT_PAT | TUPLE_PAT | SLICE_PAT | RANGE_PAT | LITERAL_PAT => true,
_ => false,
}
}
fn cast(syntax: SyntaxNode) -> Option<Self> {
let res = match syntax.kind() {
REF_PAT => Pat::RefPat(RefPat { syntax }),
BOX_PAT => Pat::BoxPat(BoxPat { syntax }),
BIND_PAT => Pat::BindPat(BindPat { syntax }),
PLACEHOLDER_PAT => Pat::PlaceholderPat(PlaceholderPat { syntax }),
PATH_PAT => Pat::PathPat(PathPat { syntax }),
Expand All @@ -2150,6 +2184,7 @@ impl AstNode for Pat {
fn syntax(&self) -> &SyntaxNode {
match self {
Pat::RefPat(it) => &it.syntax,
Pat::BoxPat(it) => &it.syntax,
Pat::BindPat(it) => &it.syntax,
Pat::PlaceholderPat(it) => &it.syntax,
Pat::PathPat(it) => &it.syntax,
Expand Down
3 changes: 3 additions & 0 deletions crates/ra_syntax/src/grammar.ron
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ Grammar(
"DYN_TRAIT_TYPE",

"REF_PAT",
"BOX_PAT",
"BIND_PAT",
"PLACEHOLDER_PAT",
"PATH_PAT",
Expand Down Expand Up @@ -523,6 +524,7 @@ Grammar(
),

"RefPat": ( options: [ "Pat" ]),
"BoxPat": ( options: [ "Pat" ]),
"BindPat": (
options: [ "Pat" ],
traits: ["NameOwner"]
Expand Down Expand Up @@ -552,6 +554,7 @@ Grammar(
"Pat": (
enum: [
"RefPat",
"BoxPat",
"BindPat",
"PlaceholderPat",
"PathPat",
Expand Down
6 changes: 6 additions & 0 deletions crates/ra_syntax/test_data/parser/err/0034_bad_box_pattern.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn main() {
let ref box i = ();
let mut box i = ();
let ref mut box i = ();
}

95 changes: 95 additions & 0 deletions crates/ra_syntax/test_data/parser/err/0034_bad_box_pattern.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
SOURCE_FILE@[0; 91)
FN_DEF@[0; 89)
FN_KW@[0; 2) "fn"
WHITESPACE@[2; 3) " "
NAME@[3; 7)
IDENT@[3; 7) "main"
PARAM_LIST@[7; 9)
L_PAREN@[7; 8) "("
R_PAREN@[8; 9) ")"
WHITESPACE@[9; 10) " "
BLOCK@[10; 89)
L_CURLY@[10; 11) "{"
WHITESPACE@[11; 16) "\n "
LET_STMT@[16; 27)
LET_KW@[16; 19) "let"
WHITESPACE@[19; 20) " "
BIND_PAT@[20; 27)
REF_KW@[20; 23) "ref"
WHITESPACE@[23; 24) " "
ERROR@[24; 27)
BOX_KW@[24; 27) "box"
WHITESPACE@[27; 28) " "
EXPR_STMT@[28; 35)
BIN_EXPR@[28; 34)
PATH_EXPR@[28; 29)
PATH@[28; 29)
PATH_SEGMENT@[28; 29)
NAME_REF@[28; 29)
IDENT@[28; 29) "i"
WHITESPACE@[29; 30) " "
EQ@[30; 31) "="
WHITESPACE@[31; 32) " "
TUPLE_EXPR@[32; 34)
L_PAREN@[32; 33) "("
R_PAREN@[33; 34) ")"
SEMI@[34; 35) ";"
WHITESPACE@[35; 40) "\n "
LET_STMT@[40; 51)
LET_KW@[40; 43) "let"
WHITESPACE@[43; 44) " "
BIND_PAT@[44; 51)
MUT_KW@[44; 47) "mut"
WHITESPACE@[47; 48) " "
ERROR@[48; 51)
BOX_KW@[48; 51) "box"
WHITESPACE@[51; 52) " "
EXPR_STMT@[52; 59)
BIN_EXPR@[52; 58)
PATH_EXPR@[52; 53)
PATH@[52; 53)
PATH_SEGMENT@[52; 53)
NAME_REF@[52; 53)
IDENT@[52; 53) "i"
WHITESPACE@[53; 54) " "
EQ@[54; 55) "="
WHITESPACE@[55; 56) " "
TUPLE_EXPR@[56; 58)
L_PAREN@[56; 57) "("
R_PAREN@[57; 58) ")"
SEMI@[58; 59) ";"
WHITESPACE@[59; 64) "\n "
LET_STMT@[64; 79)
LET_KW@[64; 67) "let"
WHITESPACE@[67; 68) " "
BIND_PAT@[68; 79)
REF_KW@[68; 71) "ref"
WHITESPACE@[71; 72) " "
MUT_KW@[72; 75) "mut"
WHITESPACE@[75; 76) " "
ERROR@[76; 79)
BOX_KW@[76; 79) "box"
WHITESPACE@[79; 80) " "
EXPR_STMT@[80; 87)
BIN_EXPR@[80; 86)
PATH_EXPR@[80; 81)
PATH@[80; 81)
PATH_SEGMENT@[80; 81)
NAME_REF@[80; 81)
IDENT@[80; 81) "i"
WHITESPACE@[81; 82) " "
EQ@[82; 83) "="
WHITESPACE@[83; 84) " "
TUPLE_EXPR@[84; 86)
L_PAREN@[84; 85) "("
R_PAREN@[85; 86) ")"
SEMI@[86; 87) ";"
WHITESPACE@[87; 88) "\n"
R_CURLY@[88; 89) "}"
WHITESPACE@[89; 91) "\n\n"
error 24: expected a name
error 27: expected SEMI
error 48: expected a name
error 51: expected SEMI
error 76: expected a name
error 79: expected SEMI
2 changes: 0 additions & 2 deletions crates/ra_syntax/test_data/parser/inline/ok/0066_match_arm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,5 @@ fn foo() {
X | Y if Z => (),
| X | Y if Z => (),
| X => (),
box X => (),
Some(box X) => (),
};
}
Loading