rust/crates/ra_syntax/src/grammar/expressions/atom.rs

455 lines
9.9 KiB
Rust
Raw Normal View History

2018-08-04 13:58:22 +00:00
use super::*;
// test expr_literals
// fn foo() {
// let _ = true;
// let _ = false;
// let _ = 1;
// let _ = 2.0;
// let _ = b'a';
// let _ = 'b';
// let _ = "c";
// let _ = r"d";
// let _ = b"e";
// let _ = br"f";
// }
pub(crate) const LITERAL_FIRST: TokenSet = token_set![
TRUE_KW,
FALSE_KW,
INT_NUMBER,
FLOAT_NUMBER,
BYTE,
CHAR,
STRING,
RAW_STRING,
BYTE_STRING,
RAW_BYTE_STRING
];
2018-08-04 13:58:22 +00:00
pub(crate) fn literal(p: &mut Parser) -> Option<CompletedMarker> {
2018-09-08 07:38:53 +00:00
if !p.at_ts(LITERAL_FIRST) {
2018-08-04 13:58:22 +00:00
return None;
}
let m = p.start();
p.bump();
Some(m.complete(p, LITERAL))
}
// E.g. for after the break in `if break {}`, this should not match
2019-01-24 22:01:49 +00:00
pub(super) const ATOM_EXPR_FIRST: TokenSet =
LITERAL_FIRST.union(paths::PATH_FIRST).union(token_set![
L_PAREN,
L_CURLY,
L_BRACK,
PIPE,
MOVE_KW,
IF_KW,
WHILE_KW,
MATCH_KW,
UNSAFE_KW,
RETURN_KW,
BREAK_KW,
CONTINUE_KW,
LIFETIME,
]);
2018-08-04 13:58:22 +00:00
const EXPR_RECOVERY_SET: TokenSet = token_set![LET_KW];
2018-08-28 08:12:42 +00:00
pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMarker, BlockLike)> {
if let Some(m) = literal(p) {
return Some((m, BlockLike::NotBlock));
2018-08-04 13:58:22 +00:00
}
2018-08-13 20:54:00 +00:00
if paths::is_path_start(p) || p.at(L_ANGLE) {
return Some(path_expr(p, r));
2018-08-04 13:58:22 +00:00
}
let la = p.nth(1);
let done = match p.current() {
L_PAREN => tuple_expr(p),
2018-08-05 15:24:56 +00:00
L_BRACK => array_expr(p),
2018-08-04 13:58:22 +00:00
PIPE => lambda_expr(p),
MOVE_KW if la == PIPE => lambda_expr(p),
IF_KW => if_expr(p),
2018-08-24 08:45:14 +00:00
LOOP_KW => loop_expr(p, None),
FOR_KW => for_expr(p, None),
WHILE_KW => while_expr(p, None),
LIFETIME if la == COLON => {
let m = p.start();
label(p);
match p.current() {
LOOP_KW => loop_expr(p, Some(m)),
FOR_KW => for_expr(p, Some(m)),
WHILE_KW => while_expr(p, Some(m)),
2018-09-08 06:18:42 +00:00
L_CURLY => block_expr(p, Some(m)),
2018-08-24 08:45:14 +00:00
_ => {
// test_err misplaced_label_err
2018-09-08 06:18:42 +00:00
// fn main() {
// 'loop: impl
// }
2018-08-24 08:45:14 +00:00
p.error("expected a loop");
2018-09-08 06:18:42 +00:00
m.complete(p, ERROR);
return None;
2018-08-24 08:45:14 +00:00
}
}
}
2018-08-04 13:58:22 +00:00
MATCH_KW => match_expr(p),
2018-09-08 06:18:42 +00:00
UNSAFE_KW if la == L_CURLY => {
let m = p.start();
p.bump();
block_expr(p, Some(m))
}
2018-09-08 06:18:42 +00:00
L_CURLY => block_expr(p, None),
2018-08-04 13:58:22 +00:00
RETURN_KW => return_expr(p),
2018-08-24 08:21:13 +00:00
CONTINUE_KW => continue_expr(p),
BREAK_KW => break_expr(p, r),
2018-08-04 13:58:22 +00:00
_ => {
2018-08-28 08:12:42 +00:00
p.err_recover("expected expression", EXPR_RECOVERY_SET);
return None;
2018-08-04 13:58:22 +00:00
}
};
let blocklike = match done.kind() {
IF_EXPR | WHILE_EXPR | FOR_EXPR | LOOP_EXPR | MATCH_EXPR | BLOCK_EXPR => BlockLike::Block,
_ => BlockLike::NotBlock,
};
Some((done, blocklike))
2018-08-04 13:58:22 +00:00
}
2018-08-05 14:19:03 +00:00
// test tuple_expr
// fn foo() {
// ();
// (1);
// (1,);
// }
2018-08-04 13:58:22 +00:00
fn tuple_expr(p: &mut Parser) -> CompletedMarker {
assert!(p.at(L_PAREN));
let m = p.start();
p.expect(L_PAREN);
2018-08-05 14:19:03 +00:00
let mut saw_comma = false;
let mut saw_expr = false;
while !p.at(EOF) && !p.at(R_PAREN) {
saw_expr = true;
2018-09-08 07:38:53 +00:00
if !p.at_ts(EXPR_FIRST) {
2018-09-08 07:35:05 +00:00
p.error("expected expression");
break;
}
2018-08-05 14:19:03 +00:00
expr(p);
if !p.at(R_PAREN) {
saw_comma = true;
p.expect(COMMA);
}
}
2018-08-04 13:58:22 +00:00
p.expect(R_PAREN);
m.complete(
p,
if saw_expr && !saw_comma {
PAREN_EXPR
} else {
TUPLE_EXPR
},
)
2018-08-04 13:58:22 +00:00
}
2018-08-05 15:24:56 +00:00
// test array_expr
// fn foo() {
// [];
// [1];
// [1, 2,];
// [1; 2];
// }
fn array_expr(p: &mut Parser) -> CompletedMarker {
assert!(p.at(L_BRACK));
let m = p.start();
p.bump();
if p.eat(R_BRACK) {
return m.complete(p, ARRAY_EXPR);
}
expr(p);
if p.eat(SEMI) {
expr(p);
p.expect(R_BRACK);
return m.complete(p, ARRAY_EXPR);
}
while !p.at(EOF) && !p.at(R_BRACK) {
p.expect(COMMA);
2018-09-08 07:13:32 +00:00
if p.at(R_BRACK) {
break;
2018-08-05 15:24:56 +00:00
}
2018-09-08 07:38:53 +00:00
if !p.at_ts(EXPR_FIRST) {
2018-09-08 07:13:32 +00:00
p.error("expected expression");
break;
}
expr(p);
2018-08-05 15:24:56 +00:00
}
p.expect(R_BRACK);
m.complete(p, ARRAY_EXPR)
}
2018-08-04 13:58:22 +00:00
// test lambda_expr
// fn foo() {
// || ();
// || -> i32 { 92 };
// |x| x;
// move |x: i32,| x;
// }
fn lambda_expr(p: &mut Parser) -> CompletedMarker {
assert!(p.at(PIPE) || (p.at(MOVE_KW) && p.nth(1) == PIPE));
let m = p.start();
p.eat(MOVE_KW);
params::param_list_opt_types(p);
2018-08-23 23:14:10 +00:00
if opt_fn_ret_type(p) {
2018-08-28 20:59:57 +00:00
if !p.at(L_CURLY) {
p.error("expected `{`");
}
2018-08-04 13:58:22 +00:00
}
2018-08-28 20:59:57 +00:00
expr(p);
2018-08-04 13:58:22 +00:00
m.complete(p, LAMBDA_EXPR)
}
// test if_expr
// fn foo() {
// if true {};
// if true {} else {};
2018-08-04 14:12:00 +00:00
// if true {} else if false {} else {};
// if S {};
2018-08-04 13:58:22 +00:00
// }
fn if_expr(p: &mut Parser) -> CompletedMarker {
assert!(p.at(IF_KW));
let m = p.start();
2018-08-04 22:03:22 +00:00
p.bump();
cond(p);
2018-08-04 13:58:22 +00:00
block(p);
if p.at(ELSE_KW) {
p.bump();
if p.at(IF_KW) {
if_expr(p);
} else {
block(p);
}
}
m.complete(p, IF_EXPR)
}
2018-08-24 08:45:14 +00:00
// test label
2018-08-04 22:03:22 +00:00
// fn foo() {
2018-08-24 08:45:14 +00:00
// 'a: loop {}
// 'b: while true {}
// 'c: for x in () {}
2018-08-04 22:03:22 +00:00
// }
2018-08-24 08:45:14 +00:00
fn label(p: &mut Parser) {
assert!(p.at(LIFETIME) && p.nth(1) == COLON);
2018-08-04 22:03:22 +00:00
let m = p.start();
2018-08-04 13:58:22 +00:00
p.bump();
2018-08-24 08:45:14 +00:00
p.bump();
m.complete(p, LABEL);
2018-08-04 22:03:22 +00:00
}
2018-08-05 15:16:52 +00:00
// test loop_expr
// fn foo() {
// loop {};
// }
2018-08-24 08:45:14 +00:00
fn loop_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
2018-08-05 15:16:52 +00:00
assert!(p.at(LOOP_KW));
2018-08-24 08:45:14 +00:00
let m = m.unwrap_or_else(|| p.start());
2018-08-05 15:16:52 +00:00
p.bump();
block(p);
m.complete(p, LOOP_EXPR)
}
2018-08-24 08:45:14 +00:00
// test while_expr
// fn foo() {
// while true {};
// while let Some(x) = it.next() {};
// }
fn while_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
assert!(p.at(WHILE_KW));
let m = m.unwrap_or_else(|| p.start());
p.bump();
cond(p);
block(p);
m.complete(p, WHILE_EXPR)
}
2018-08-05 15:24:56 +00:00
// test for_expr
// fn foo() {
// for x in [] {};
// }
2018-08-24 08:45:14 +00:00
fn for_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
2018-08-05 15:24:56 +00:00
assert!(p.at(FOR_KW));
2018-08-24 08:45:14 +00:00
let m = m.unwrap_or_else(|| p.start());
2018-08-05 15:24:56 +00:00
p.bump();
patterns::pattern(p);
p.expect(IN_KW);
expr_no_struct(p);
block(p);
m.complete(p, FOR_EXPR)
}
2018-08-04 22:03:22 +00:00
// test cond
// fn foo() { if let Some(_) = None {} }
fn cond(p: &mut Parser) {
2018-08-27 09:22:09 +00:00
let m = p.start();
2018-08-04 22:03:22 +00:00
if p.eat(LET_KW) {
patterns::pattern(p);
p.expect(EQ);
}
2018-08-27 09:22:09 +00:00
expr_no_struct(p);
m.complete(p, CONDITION);
2018-08-04 13:58:22 +00:00
}
// test match_expr
// fn foo() {
// match () { };
2018-08-04 14:12:00 +00:00
// match S {};
2018-08-04 13:58:22 +00:00
// }
fn match_expr(p: &mut Parser) -> CompletedMarker {
assert!(p.at(MATCH_KW));
let m = p.start();
p.bump();
2018-08-04 14:12:00 +00:00
expr_no_struct(p);
2018-08-24 16:27:30 +00:00
if p.at(L_CURLY) {
match_arm_list(p);
} else {
p.error("expected `{`")
}
m.complete(p, MATCH_EXPR)
}
pub(crate) fn match_arm_list(p: &mut Parser) {
2018-08-24 16:27:30 +00:00
assert!(p.at(L_CURLY));
let m = p.start();
2018-08-04 13:58:22 +00:00
p.eat(L_CURLY);
while !p.at(EOF) && !p.at(R_CURLY) {
2018-08-27 18:10:02 +00:00
if p.at(L_CURLY) {
error_block(p, "expected match arm");
continue;
}
2018-08-07 13:32:09 +00:00
// test match_arms_commas
// fn foo() {
// match () {
// _ => (),
// _ => {}
// _ => ()
// }
// }
if match_arm(p).is_block() {
p.eat(COMMA);
} else if !p.at(R_CURLY) {
2018-08-04 13:58:22 +00:00
p.expect(COMMA);
}
}
p.expect(R_CURLY);
2018-08-24 16:27:30 +00:00
m.complete(p, MATCH_ARM_LIST);
2018-08-04 13:58:22 +00:00
}
// test match_arm
// fn foo() {
// match () {
// _ => (),
// _ if Test > Test{field: 0} => (),
2018-08-04 13:58:22 +00:00
// X | Y if Z => (),
2018-10-03 20:47:03 +00:00
// | X | Y if Z => (),
// | X => (),
2018-08-04 13:58:22 +00:00
// };
// }
2018-08-07 13:32:09 +00:00
fn match_arm(p: &mut Parser) -> BlockLike {
2018-08-04 13:58:22 +00:00
let m = p.start();
2018-10-03 20:47:03 +00:00
p.eat(PIPE);
2019-01-18 08:02:30 +00:00
patterns::pattern_r(p, TokenSet::empty());
Fix yet another parser infinite loop This commit is an example of fixing a common parser error: infinite loop due to error recovery. This error typically happens when we parse a list of items and fail to parse a specific item at the current position. One choices is to skip a token and try to parse a list item at the next position. This is a good, but not universal, default. When parsing a list of arguments in a function call, you, for example, don't want to skip over `fn`, because it's most likely that it is a function declaration, and not a mistyped arg: ``` fn foo() { quux(1, 2 fn bar() { } ``` Another choice is to bail out of the loop immediately, but it isn't perfect either: sometimes skipping over garbage helps: ``` quux(1, foo:, 92) // should skip over `:`, b/c that's part of `foo::bar` ``` In general, parser tries to balance these two cases, though we don't have a definitive strategy yet. However, if the parser accidentally neither skips over a token, nor breaks out of the loop, then it becomes stuck in the loop infinitely (there's an internal counter to self-check this situation and panic though), and that's exactly what is demonstrated by the test. To fix such situation, first of all, add the test case to tests/data/parser/{err,fuzz-failures}. Then, run ``` RUST_BACKTRACE=short cargo test --package libsyntax2 ```` to verify that parser indeed panics, and to get an idea what grammar production is the culprit (look for `_list` functions!). In this case, I see ``` 10: libsyntax2::grammar::expressions::atom::match_arm_list at crates/libsyntax2/src/grammar/expressions/atom.rs:309 ``` and that's look like it might be a culprit. I verify it by adding `eprintln!("loopy {:?}", p.current());` and indeed I see that this is printed repeatedly. Diagnosing this a bit shows that the problem is that `pattern::pattern` function does not consume anything if the next token is `let`. That is a good default to make cases like ``` let let foo = 92; ``` where the user hasn't typed the pattern yet, to parse in a reasonable they correctly. For match arms, pretty much the single thing we expect is a pattern, so, for a fix, I introduce a special variant of pattern that does not do recovery.
2018-09-08 16:10:20 +00:00
while p.eat(PIPE) {
2018-08-04 13:58:22 +00:00
patterns::pattern(p);
}
2019-01-28 22:06:11 +00:00
if p.at(IF_KW) {
match_guard(p);
2018-08-04 13:58:22 +00:00
}
p.expect(FAT_ARROW);
2018-08-07 14:00:45 +00:00
let ret = expr_stmt(p);
2018-08-04 13:58:22 +00:00
m.complete(p, MATCH_ARM);
2018-08-07 13:32:09 +00:00
ret
2018-08-04 13:58:22 +00:00
}
2019-01-28 22:06:11 +00:00
// test match_guard
// fn foo() {
// match () {
// _ if foo => (),
// }
// }
fn match_guard(p: &mut Parser) -> CompletedMarker {
assert!(p.at(IF_KW));
let m = p.start();
p.bump();
expr(p);
m.complete(p, MATCH_GUARD)
}
2018-08-04 13:58:22 +00:00
// test block_expr
// fn foo() {
// {};
// unsafe {};
2018-09-08 06:18:42 +00:00
// 'label: {};
2018-08-04 13:58:22 +00:00
// }
2018-09-08 06:18:42 +00:00
fn block_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
assert!(p.at(L_CURLY));
let m = m.unwrap_or_else(|| p.start());
2018-08-24 16:27:30 +00:00
block(p);
2018-08-04 13:58:22 +00:00
m.complete(p, BLOCK_EXPR)
}
// test return_expr
// fn foo() {
// return;
// return 92;
// }
fn return_expr(p: &mut Parser) -> CompletedMarker {
assert!(p.at(RETURN_KW));
let m = p.start();
p.bump();
2018-09-08 07:38:53 +00:00
if p.at_ts(EXPR_FIRST) {
2018-08-04 13:58:22 +00:00
expr(p);
}
m.complete(p, RETURN_EXPR)
}
2018-08-24 08:21:13 +00:00
// test continue_expr
// fn foo() {
// loop {
// continue;
// continue 'l;
// }
// }
fn continue_expr(p: &mut Parser) -> CompletedMarker {
assert!(p.at(CONTINUE_KW));
let m = p.start();
p.bump();
p.eat(LIFETIME);
m.complete(p, CONTINUE_EXPR)
}
// test break_expr
// fn foo() {
// loop {
// break;
// break 'l;
// break 92;
// break 'l 92;
// }
// }
fn break_expr(p: &mut Parser, r: Restrictions) -> CompletedMarker {
2018-08-24 08:21:13 +00:00
assert!(p.at(BREAK_KW));
let m = p.start();
p.bump();
p.eat(LIFETIME);
// test break_ambiguity
// fn foo(){
// if break {}
// while break {}
// for i in break {}
// match break {}
// }
2018-12-20 12:28:59 +00:00
if p.at_ts(EXPR_FIRST) && !(r.forbid_structs && p.at(L_CURLY)) {
2018-08-24 08:21:13 +00:00
expr(p);
}
m.complete(p, BREAK_EXPR)
}