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

It's not possible to unquote code to be part of an Ident #5914

Closed
Thunkar opened this issue Sep 4, 2024 · 0 comments · Fixed by #5928
Closed

It's not possible to unquote code to be part of an Ident #5914

Thunkar opened this issue Sep 4, 2024 · 0 comments · Fixed by #5928
Labels
bug Something isn't working

Comments

@Thunkar
Copy link
Contributor

Thunkar commented Sep 4, 2024

Aim

Using the unquote operator as part of identifiers

Expected Behavior

Maybe using an escape character or a dedicated method to make this possible?

Bug

let name: Quoted = func.name();
let struct_name_based_on_fn_name = quote { $name_suffix }; // this doesn't work, since the var name is now name_suffix
let struct_name_based_on_fn_name = name.append(quote {_suffix}); // this is parsed as separate tokens

Those examples should be allowed somehow

To Reproduce

No response

Workaround

Yes

Workaround Description

As a hacky workaround, I implemented the following builtin for the Quoted struct:

// fn join_tokens(another: Quoted) -> Quoted
fn quoted_join_tokens(arguments: Vec<(Value, Location)>, location: Location) -> IResult<Value> {
    let (self_value, other_value) = check_two_arguments(arguments, location)?;

    let self_quoted = get_quoted(self_value)?;
    let other_quoted = get_quoted(other_value)?;

    let result = [&self_quoted[..], &other_quoted[..]]
        .concat()
        .iter()
        .map(|token| token.to_string())
        .collect::<Vec<_>>()
        .join("");

    let (tokens, _) = Lexer::lex(result.as_str());

    let tokens: Vec<_> = tokens.0.iter().map(|token| token.clone().into_token()).collect();

    Ok(Value::Quoted(Rc::new(tokens[0..tokens.len() - 1].to_vec())))
}

Additional Context

No response

Project Impact

Blocker

Blocker Context

No response

Nargo Version

No response

NoirJS Version

No response

Proving Backend Tooling & Version

No response

Would you like to submit a PR for this Issue?

None

Support Needs

No response

@Thunkar Thunkar added the bug Something isn't working label Sep 4, 2024
github-merge-queue bot pushed a commit that referenced this issue Sep 4, 2024
# Description

## Problem

Resolves #5899
Resolves #5914

## Summary

Two things here:
1. When interpolating quotes values inside a format string, we do that
without producing the `quote {` and `}` parts, which is likely what a
user would expect (similar to unquoting those values).
2. In order to create identifiers (or any piece of code in general) by
joining severa quoted values you can use format strings together with
the new `fmtstr::contents` method, which returns a `Quoted` value with
the string contents (that is, without the leading and trailing double
quotes).

## Additional Context

I originally thought about a method like `fmtstr::as_identifier` that
would try to parse the string contents as an identifier (maybe an
`Ident`, or maybe a `Path`), returning `Option<Quoted>` and `None` in
case it couldn't be parsed to that. But I think in general it could be
more useful to just get the string contents as a `Quoted` value. After
all, if it isn't an identifier you'll learn it later on once the value
is unquoted or interpolated.

## Documentation

Check one:
- [ ] No documentation needed.
- [x] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

1 participant