Revert "Revert to f6f7c5"

This approach is correct, but it needs an addition to Restrictions too

This reverts commit ad00d0c8a5f64142e6636e8b048204c8f8982f4a.
This commit is contained in:
DJMcNab 2018-12-19 20:02:37 +00:00
parent 4dce66ad31
commit def0a95d35
3 changed files with 36 additions and 22 deletions

View File

@ -158,18 +158,19 @@ fn current_op(p: &Parser) -> (u8, Op) {
// Parses expression with binding power of at least bp.
fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> BlockLike {
let mut lhs = match lhs(p, r) {
Some(lhs) => {
(Some(lhs), macro_blocklike) => {
// test stmt_bin_expr_ambiguity
// fn foo() {
// let _ = {1} & 2;
// {1} &2;
// }
if r.prefer_stmt && is_block(lhs.kind()) {
if r.prefer_stmt && (is_block(lhs.kind()) || macro_blocklike == Some(BlockLike::Block))
{
return BlockLike::Block;
}
lhs
}
None => return BlockLike::NotBlock,
(None, _) => return BlockLike::NotBlock,
};
loop {
@ -213,7 +214,7 @@ fn is_block(kind: SyntaxKind) -> bool {
atom::ATOM_EXPR_FIRST,
];
fn lhs(p: &mut Parser, r: Restrictions) -> Option<CompletedMarker> {
fn lhs(p: &mut Parser, r: Restrictions) -> (Option<CompletedMarker>, Option<BlockLike>) {
let m;
let kind = match p.current() {
// test ref_expr
@ -246,18 +247,29 @@ fn lhs(p: &mut Parser, r: Restrictions) -> Option<CompletedMarker> {
if p.at_ts(EXPR_FIRST) {
expr_bp(p, r, 2);
}
return Some(m.complete(p, RANGE_EXPR));
return (Some(m.complete(p, RANGE_EXPR)), None);
}
_ => {
let lhs = atom::atom_expr(p, r)?;
return Some(postfix_expr(p, r, lhs));
let (lhs_marker, macro_block_like) = atom::atom_expr(p, r);
if macro_block_like == Some(BlockLike::Block) {
return (lhs_marker, macro_block_like);
}
if let Some(lhs_marker) = lhs_marker {
return (Some(postfix_expr(p, r, lhs_marker)), macro_block_like);
} else {
return (None, None);
}
}
};
expr_bp(p, r, 255);
Some(m.complete(p, kind))
(Some(m.complete(p, kind)), None)
}
fn postfix_expr(p: &mut Parser, r: Restrictions, mut lhs: CompletedMarker) -> CompletedMarker {
// Calls are disallowed if the type is a block and we prefer statements because the call cannot be disambiguated from a tuple
// E.g. `while true {break}();` is parsed as
// `while true {break}; ();`
let mut allow_calls = !r.prefer_stmt || !is_block(lhs.kind());
loop {
lhs = match p.current() {
@ -406,21 +418,22 @@ fn arg_list(p: &mut Parser) {
// let _ = ::a::<b>;
// let _ = format!();
// }
fn path_expr(p: &mut Parser, r: Restrictions) -> CompletedMarker {
fn path_expr(p: &mut Parser, r: Restrictions) -> (CompletedMarker, Option<BlockLike>) {
assert!(paths::is_path_start(p) || p.at(L_ANGLE));
let m = p.start();
paths::expr_path(p);
match p.current() {
let res = match p.current() {
L_CURLY if !r.forbid_structs => {
named_field_list(p);
m.complete(p, STRUCT_LIT)
}
EXCL => {
items::macro_call_after_excl(p); // TODO: Use return type (BlockLike)
m.complete(p, MACRO_CALL)
let block_like = items::macro_call_after_excl(p); // TODO: Use return type (BlockLike)
return (m.complete(p, MACRO_CALL), Some(block_like));
}
_ => m.complete(p, PATH_EXPR),
}
};
(res, None)
}
// test struct_lit

View File

@ -61,12 +61,16 @@ pub(crate) fn literal(p: &mut Parser) -> Option<CompletedMarker> {
const EXPR_RECOVERY_SET: TokenSet = token_set![LET_KW];
pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<CompletedMarker> {
pub(super) fn atom_expr(
p: &mut Parser,
r: Restrictions,
) -> (Option<CompletedMarker>, Option<BlockLike>) {
if let Some(m) = literal(p) {
return Some(m);
return (Some(m), None);
}
if paths::is_path_start(p) || p.at(L_ANGLE) {
return Some(path_expr(p, r));
let path_expr = path_expr(p, r);
return (Some(path_expr.0), path_expr.1);
}
let la = p.nth(1);
let done = match p.current() {
@ -94,7 +98,7 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<CompletedMark
// }
p.error("expected a loop");
m.complete(p, ERROR);
return None;
return (None, None);
}
}
}
@ -111,10 +115,10 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<CompletedMark
BREAK_KW => break_expr(p),
_ => {
p.err_recover("expected expression", EXPR_RECOVERY_SET);
return None;
return (None, None);
}
};
Some(done)
(Some(done), None)
}
// test tuple_expr

View File

@ -43,7 +43,6 @@ fn parser_fuzz_tests() {
fn self_hosting_parsing() {
let empty_vec = vec![];
let dir = project_dir();
let mut count = 0u32;
for entry in walkdir::WalkDir::new(dir)
.into_iter()
.filter_entry(|entry| {
@ -64,7 +63,6 @@ fn self_hosting_parsing() {
!entry.path().is_dir() && (entry.path().extension() == Some(std::ffi::OsStr::new("rs")))
})
{
count += 1;
let text = read_text(entry.path());
let node = SourceFileNode::parse(&text);
let errors = node.errors();
@ -74,7 +72,6 @@ fn self_hosting_parsing() {
entry
);
}
panic!("{}", count)
}
/// Read file and normalize newlines.
///