Reproachfully Presenting Resilient Recursive Descent Parsing
Posted by thunderseethe@reddit | programming | View on Reddit | 5 comments
Posted by thunderseethe@reddit | programming | View on Reddit | 5 comments
ToaruBaka@reddit
I think you have a typo in the
expectcode snippet, should thisContinuebe aBreak? Theatefunction returnsBreakon a matched token.Good article so far, I'll finish the back half after lunch.
thunderseethe@reddit (OP)
Thanks for checking it out! This code is working as intended albeit it sound like it's not very clear. The next part of expect after this let..else is error reporting, which we only want to do if
atedidn't see the expected token.When
atesees the correct token and returns break that causes our let else to return early and we dont do any error recovery, since we saw the expected token. Conversely, when ate does not see the right token it returns Continue which causes expect to drop down and begin error recovery.I think seeing the let Continue is probably confusing as the magic is all in the else which is implied to be taken on the break branch. I'll try making it an if let and see if thats clearer.
ToaruBaka@reddit
Ohhh I see, I missed the
elseat the end of the line - the updated version is more clear, thanks!Yeah, I think that particular idiom (
let-else-return) is easy to misread when paired withControlFlow, because (at least for me) I was thinking aboutcontinue/breaksemantics, notif/else(theelsewithoutifis what really did me in, I think).The explicit
ifversion is much clearer IMO.thunderseethe@reddit (OP)
Great! Im glad it helped. i appreciate you pointing it out. I write a lot of swift, where
guard let ... elseis idiomatic, so I have a blindspot for it in rustToaruBaka@reddit
There's nothing wrong per-say with
let-else[-return]- it's super powerful and used all over the place. I do the same in Zig forconst x = foo() catch return;andconst x = y orelse return;for try/catch and optional unwrapping. Here it's moreso about the intersection of multiple control flow models.Nice article - it was really helpful to see a fleshed out parser example that includes recovery. I was working on something a few months ago that would have benefited from a CST, so I'll be keeping that in my back pocket :)