Fix expression parsing by bailing out upon a macro block being found

TODO: Fix this when the block like macro is in expression position

E.g. `test(test!{})` currently parses
This commit is contained in:
DJMcNab 2018-12-19 18:12:19 +00:00
parent 97e70bf50f
commit 7a8560ba38
2 changed files with 12 additions and 17 deletions

View file

@ -158,13 +158,14 @@ fn current_op(p: &Parser) -> (u8, Op) {
// Parses expression with binding power of at least bp. // Parses expression with binding power of at least bp.
fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> BlockLike { fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> BlockLike {
let mut lhs = match lhs(p, r) { let mut lhs = match lhs(p, r) {
(Some(lhs), blocklike) => { (Some(lhs), macro_blocklike) => {
// test stmt_bin_expr_ambiguity // test stmt_bin_expr_ambiguity
// fn foo() { // fn foo() {
// let _ = {1} & 2; // let _ = {1} & 2;
// {1} &2; // {1} &2;
// } // }
if r.prefer_stmt && (is_block(lhs.kind()) || blocklike == Some(BlockLike::Block)) { if r.prefer_stmt && (is_block(lhs.kind()) || macro_blocklike == Some(BlockLike::Block))
{
return BlockLike::Block; return BlockLike::Block;
} }
lhs lhs
@ -251,11 +252,11 @@ fn lhs(p: &mut Parser, r: Restrictions) -> (Option<CompletedMarker>, Option<Bloc
_ => { _ => {
let (lhs_marker, macro_block_like) = atom::atom_expr(p, r); 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 { if let Some(lhs_marker) = lhs_marker {
return ( return (Some(postfix_expr(p, r, lhs_marker)), macro_block_like);
Some(postfix_expr(p, r, lhs_marker, macro_block_like)),
macro_block_like,
);
} else { } else {
return (None, None); return (None, None);
} }
@ -265,14 +266,11 @@ fn lhs(p: &mut Parser, r: Restrictions) -> (Option<CompletedMarker>, Option<Bloc
(Some(m.complete(p, kind)), None) (Some(m.complete(p, kind)), None)
} }
fn postfix_expr( fn postfix_expr(p: &mut Parser, r: Restrictions, mut lhs: CompletedMarker) -> CompletedMarker {
p: &mut Parser, // Calls are disallowed if the type is a block and we prefer statements because the call cannot be disambiguated from a tuple
r: Restrictions, // E.g. `while true {break}();` is parsed as
mut lhs: CompletedMarker, // `while true {break}; ();`
macro_block_like: Option<BlockLike>, let mut allow_calls = !r.prefer_stmt || !is_block(lhs.kind());
) -> CompletedMarker {
let mut allow_calls =
!r.prefer_stmt || !is_block(lhs.kind()) || macro_block_like != Some(BlockLike::Block);
loop { loop {
lhs = match p.current() { lhs = match p.current() {
// test stmt_postfix_expr_ambiguity // test stmt_postfix_expr_ambiguity

View file

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