Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lex,parse): Add stub try/catch implementation #34

Merged
merged 4 commits into from Dec 1, 2023

Conversation

lvcabral
Copy link
Collaborator

@lvcabral lvcabral commented Nov 29, 2023

From sjbarag#611

Implementing try/catch/throw takes a good bit of work in the interpreter, so I'm splitting that into two pull requests. This one includes parsing try/catch/end try (without throw), and always executes only the try block to maintain backwards compatibility. It's a weird partial state, but the alternative is a massive pull request that can't be reasonably reviewed or understood.

see sjbarag#554

Here's the feature working in action:

brs-try

sjbarag and others added 3 commits November 29, 2023 12:34
RBI 9.4 adds support for error handling via the `try`/`catch` model [1].
Interestingly, `try`, `catch`, and `endtry` are all valid identifiers!
That makes parsing a little bit tougher, but it's not unprecedented in
this language.  Detect `try`/`catch`/throw`/`end try` during lexing and
emit the proper lexemes.

[1] https://developer.roku.com/docs/references/brightscript/language/error-handling.html
see sjbarag#554
Throwing exceptions and catching them aren't yet supported in `brs`, so
executing only the `try` block seems to be a reasonable "bare minimum"
approach to try/catch support.  Handle `try`, `catch`, and `end try`
lexemes in the parser, emitting TryCatch statements for the interpreter
to execute naïvely.

see sjbarag#554
Copy link
Member

@TwitchBronBron TwitchBronBron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything looks great here too. I remember seeing most of this when we added try/catch support to brighterscript. I did add one comment about the de-indenting logic, but not gonna hold up the PR for it. I'll merge it, and you can decide if it matters or not for some future PR.

* @param {string} str - the string to unindent
* @return {string} `str`, but reformatted so that at least one line starts at column 0
*/
exports.deindent = function deindent(str) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not gonna hold up the PR for this, but you could consider using undent which is a project I wrote to do exactly this. it also supports tagged template literals so you can use it like:

console.log(undent`
    something
         somethingElse
      someThirdLine
`);

@TwitchBronBron TwitchBronBron merged commit b8e459a into master Dec 1, 2023
2 checks passed
@TwitchBronBron TwitchBronBron deleted the stub-try-catch branch December 1, 2023 13:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants