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: Add Quoted::tokens #5942

Merged
merged 7 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ impl<'local, 'context> Interpreter<'local, 'context> {
"expr_resolve" => expr_resolve(self, arguments, location),
"is_unconstrained" => Ok(Value::Bool(true)),
"fmtstr_quoted_contents" => fmtstr_quoted_contents(interner, arguments, location),
"fmtstr_quoted" => fmtstr_quoted(interner, arguments, location),
"function_def_body" => function_def_body(interner, arguments, location),
"function_def_has_named_attribute" => {
function_def_has_named_attribute(interner, arguments, location)
Expand Down Expand Up @@ -1595,6 +1596,28 @@ fn fmtstr_quoted_contents(
Ok(Value::Quoted(Rc::new(tokens)))
}

// fn quoted(self) -> Quoted
fn fmtstr_quoted(
interner: &NodeInterner,
arguments: Vec<(Value, Location)>,
location: Location,
) -> IResult<Value> {
let self_argument = check_one_argument(arguments, location)?;
let (string, _) = get_format_string(interner, self_argument)?;
let (tokens, _) = Lexer::lex(&string);
let str = tokens
.0
.into_iter()
.filter(|token| !matches!(token.clone().into_token(), Token::EOF))
.map(|token| token.into_token().display(interner).to_string())
.collect::<Vec<_>>()
.join("");

let token = Token::Str(str);

Ok(Value::Quoted(Rc::new(vec![token])))
}

// fn body(self) -> Expr
fn function_def_body(
interner: &NodeInterner,
Expand Down
8 changes: 7 additions & 1 deletion docs/docs/noir/standard_library/fmtstr.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ title: fmtstr

#include_code quoted_contents noir_stdlib/src/meta/format_string.nr rust

Returns the format string contents (that is, without the leading and trailing double quotes) as a `Quoted` value.
Returns the format string contents (that is, without the leading and trailing double quotes) as a `Quoted` value.

### quoted

#include_code quoted noir_stdlib/src/meta/format_string.nr rust

Returns the format string (including the leading and trailing double quotes) as a `Quoted` value.
5 changes: 5 additions & 0 deletions noir_stdlib/src/meta/format_string.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ impl <let N: u32, T> fmtstr<N, T> {
// docs:start:quoted_contents
fn quoted_contents(self) -> Quoted {}
// docs:end:quoted_contents

#[builtin(fmtstr_quoted)]
// docs:start:quoted
fn quoted(self) -> Quoted {}
// docs:end:quoted
}
Loading