Kampfkarren / full-moon

A lossless Lua 5.1 parser
Mozilla Public License 2.0
371 stars 47 forks source link

2 last statements in single top level block doesn't error recover well #298

Open toxamin opened 4 months ago

toxamin commented 4 months ago

test case:

print("xd")
continue -- this can be replaced with break/return
print("xd2")

parsing this does not produce any errors (imo it should). however, even if this should be parsed, the location of the laststmt in question is ambiguous: if it's continue, then we keep parsing statements, so the continue statement "teleports" to the end if it's return, then the other statements get stripped (parser is no longer lossless) etc. etc.

of course it's possible to determine the position based on tokens, but it isn't very convenient

Kampfkarren commented 4 months ago

Your example does error for me. Is this on master or on the current crate?

toxamin commented 4 months ago

latest master, my code:

use full_moon::ast::AstResult;
use full_moon::{LuaVersion, parse_fallible};

fn main() {
    let code = "
        print(\"xd\")
        continue
        print(\"xd2\")
    ";
    let raw_result: AstResult = parse_fallible(&code, LuaVersion::luau());

    raw_result.errors().iter().for_each(|e| println!("{}", e));
    println!("done");
}
Kampfkarren commented 4 months ago

I'm surprised we didn't have a regression test for this. Good find

JohnnyMorganz commented 2 months ago

Note this is specific to the top-level block parsing, and does not occur when parsing inside of nested blocks (e.g., loops)

JohnnyMorganz commented 2 months ago

Reopening as the error recovery part isn't completely solved yet, as we lose data