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

fix(failed): show function #586

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

fix(failed): show function #586

wants to merge 1 commit into from

Conversation

Mte90
Copy link
Member

@Mte90 Mte90 commented Nov 12, 2024

On my daily check if I can resolve some bugs in Amber I tried with #210

I am not sure about meta.get_index() - 4 to get the function with the issue.

Using the amberr code:

import * from "std/text"

// Output
// 123

main {
    echo parse("123")
}

The meta is:

Context { index: 15, expr: [Tok[import 1:1], Tok[* 1:8], Tok[from 1:10], Tok["std/text" 1:15], Tok[// Output
 3:1], Tok[<new_line> 3:10], Tok[// 123
 4:1], Tok[<new_line> 4:7], Tok[main 6:1], Tok[<symbol: { > 6:6], Tok[echo 7:5], Tok[parse 7:10], Tok[<symbol: ( > 7:15], Tok["123" 7:16], Tok[<symbol: ) > 7:21], Tok[<symbol: } > 8:1]]

REDACTED

So I think that the actual index is always the next symbol where it should be the failed, in this case a }, followed by the closing parenthesis of the function, the parameters, the open parenthesis and finally the function name.
Right now amber doesn't share in the context what is the function or the line is processing, but in this way with - 4 we start from the end that it is more easy to be right so we can ignore the echo token.

Anyway I am not so skilled in the internals but let me know if needs some changes internally before to be able to close that ticket.

@@ -61,7 +61,7 @@ impl SyntaxModule<ParserMetadata> for Failed {
self.is_parsed = true;
return Ok(());
} else {
return error!(meta, tok, "Failed expression must be followed by a block or statement")
return error!(meta, tok, format!("The function '{}' requires a 'failed' block or statement to handle errors", meta.get_token_at(meta.get_index() - 4).unwrap().word))
Copy link
Contributor

Choose a reason for hiding this comment

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

Good rule of thumb: suspect any code with magic numbers, like "4" here; there will probably be some edge case not covered by it. For example:

hwalters@Ghostwheel ~/git/amber-lang (210) 
$ cat untrusted.ab 
$ echo 'Hello world' $
hwalters@Ghostwheel ~/git/amber-lang (210) 
$ amber untrusted.ab 
thread 'main' panicked at src/modules/condition/failed.rs:64:171:
called `Option::unwrap()` on a `None` value
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

I'll see if I can find a better way of doing this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah I wasn't sure too.
Maybe a check if there are parenthesis because probably with built-in doesn't work too.

Copy link
Contributor

Choose a reason for hiding this comment

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

In fact, the error message "The function '{}' requires..." implies that you expect the $...$ block to be run inside a function; but as my example shows, this is not always the case. I think you need to reword the error message so it doesn't reference a function name at all.

Copy link
Contributor

Choose a reason for hiding this comment

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

Which is exactly what we did before. So, based on the above comment, I would question whether we need this PR at all?

@Ph0enixKM
Copy link
Member

One way we could solve this is by including an fun_name: Option<string> in the struct of this syntax module and let the function invocation module set the function name. Then if fun_name has Some we can print the alternative error message.

If this sounds convoluted, I'll open my PR tomorrow to show what I mean

@@ -61,7 +61,7 @@ impl SyntaxModule<ParserMetadata> for Failed {
self.is_parsed = true;
return Ok(());
} else {
return error!(meta, tok, "Failed expression must be followed by a block or statement")
return error!(meta, tok, format!("The function '{}' requires a 'failed' block or statement to handle errors", meta.get_token_at(meta.get_index() - 4).unwrap().word))
Copy link
Member

Choose a reason for hiding this comment

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

This module cannot move back to get the function name. The function invocation should provide this module with the name

@Mte90
Copy link
Member Author

Mte90 commented Nov 14, 2024

You know better than anyone else so I think that your proposal is good.
The purpose of the PR was to close the issue with adding the error message that it was missing (yet).

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.

4 participants