Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 21 additions & 0 deletions crates/mq-formatter/src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,27 @@ catch:
handle_error()
"
)]
#[case::coalesce_operator("let x = a?? b", "let x = a ?? b")]
#[case::coalesce_operator_with_call(
"let result = get_value() ?? default()",
"let result = get_value() ?? default()"
)]
#[case::coalesce_operator_with_literal("let x = value ?? 42", "let x = value ?? 42")]
#[case::coalesce_operator_with_string(
"let s = str ?? \"default\"",
"let s = str ?? \"default\""
)]
#[case::coalesce_operator_chain("let x = a ?? b ?? c", "let x = a ?? b ?? c")]
#[case::coalesce_operator_in_if(
"if(a ?? b): do_something() else: do_other()",
"if (a ?? b): do_something() else: do_other()"
)]
#[case::coalesce_operator_in_array("[a ?? b, c]", "[a ?? b, c]")]
#[case::coalesce_operator_in_dict("{\"key\": a ?? b}", "{\"key\": a ?? b}")]
#[case::coalesce_operator_with_comment(
"let x = a ?? b # fallback",
"let x = a ?? b # fallback"
)]
fn test_format(#[case] code: &str, #[case] expected: &str) {
let result = Formatter::new(None).format(code);
assert_eq!(result.unwrap(), expected);
Expand Down
1 change: 1 addition & 0 deletions crates/mq-lang/src/ast/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ pub const NEGATE: &str = "negate";
pub const RANGE: &str = "range";

pub const BREAKPOINT: &str = "breakpoint";
pub const COALESCE: &str = "coalesce";
152 changes: 152 additions & 0 deletions crates/mq-lang/src/ast/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ impl<'a> Parser<'a> {
TokenKind::Plus | TokenKind::Minus => 4,
TokenKind::Asterisk | TokenKind::Slash | TokenKind::Percent => 5,
TokenKind::RangeOp => 6,
TokenKind::Coalesce => 6,
_ => 0,
}
}
Expand All @@ -135,6 +136,7 @@ impl<'a> Parser<'a> {
match kind {
TokenKind::And => constants::AND,
TokenKind::Asterisk => constants::MUL,
TokenKind::Coalesce => constants::COALESCE,
TokenKind::EqEq => constants::EQ,
TokenKind::Gte => constants::GTE,
TokenKind::Gt => constants::GT,
Expand Down Expand Up @@ -483,6 +485,7 @@ impl<'a> Parser<'a> {
TokenKind::And
| TokenKind::Asterisk
| TokenKind::EqEq
| TokenKind::Coalesce
| TokenKind::Gte
| TokenKind::Gt
| TokenKind::Lte
Expand Down Expand Up @@ -536,6 +539,7 @@ impl<'a> Parser<'a> {
| Some(TokenKind::SemiColon)
| Some(TokenKind::End)
| Some(TokenKind::Slash)
| Some(TokenKind::Coalesce)
| None
)
}
Expand Down Expand Up @@ -5069,6 +5073,154 @@ mod tests {
)),
})])
)]
#[rstest]
#[case::question_mark_after_call(
vec![
token(TokenKind::Ident(SmolStr::new("foo"))),
token(TokenKind::LParen),
token(TokenKind::RParen),
token(TokenKind::Question),
token(TokenKind::Eof),
],
Ok(vec![
Shared::new(Node {
token_id: 1.into(),
expr: Shared::new(Expr::Try(
Shared::new(Node {
token_id: 0.into(),
expr: Shared::new(Expr::Call(
IdentWithToken::new_with_token("foo", Some(Shared::new(token(TokenKind::Ident(SmolStr::new("foo")))))),
SmallVec::new(),
)),
}),
Shared::new(Node {
token_id: 0.into(),
expr: Shared::new(Expr::Literal(Literal::None)),
}),
)),
})
]))]
#[case::question_mark_after_call_with_args(
vec![
token(TokenKind::Ident(SmolStr::new("bar"))),
token(TokenKind::LParen),
token(TokenKind::StringLiteral("arg".to_owned())),
token(TokenKind::RParen),
token(TokenKind::Question),
token(TokenKind::Eof),
],
Ok(vec![
Shared::new(Node {
token_id: 1.into(),
expr: Shared::new(Expr::Try(
Shared::new(Node {
token_id: 1.into(),
expr: Shared::new(Expr::Call(
IdentWithToken::new_with_token("bar", Some(Shared::new(token(TokenKind::Ident(SmolStr::new("bar")))))),
smallvec![
Shared::new(Node {
token_id: 0.into(),
expr: Shared::new(Expr::Literal(Literal::String("arg".to_owned()))),
}),
],
)),
}),
Shared::new(Node {
token_id: 1.into(),
expr: Shared::new(Expr::Literal(Literal::None)),
}),
)),
})
]))]
#[case::question_mark_after_call_error(
vec![
token(TokenKind::Ident(SmolStr::new("foo"))),
token(TokenKind::Question),
token(TokenKind::Eof),
],
Err(ParseError::UnexpectedToken(token(TokenKind::Ident("foo".into())))))]
#[case::coalesce_simple(
vec![
token(TokenKind::StringLiteral("foo".to_owned())),
token(TokenKind::Coalesce),
token(TokenKind::StringLiteral("bar".to_owned())),
token(TokenKind::Eof)
],
Ok(vec![
Shared::new(Node {
token_id: 1.into(),
expr: Shared::new(Expr::Call(
IdentWithToken::new_with_token(constants::COALESCE, Some(Shared::new(token(TokenKind::Coalesce)))),
smallvec![
Shared::new(Node {
token_id: 0.into(),
expr: Shared::new(Expr::Literal(Literal::String("foo".to_owned()))),
}),
Shared::new(Node {
token_id: 2.into(),
expr: Shared::new(Expr::Literal(Literal::String("bar".to_owned()))),
}),
],
)),
})
]))]
#[case::coalesce_with_none(
vec![
token(TokenKind::None),
token(TokenKind::Coalesce),
token(TokenKind::StringLiteral("default".to_owned())),
token(TokenKind::Eof)
],
Ok(vec![
Shared::new(Node {
token_id: 1.into(),
expr: Shared::new(Expr::Call(
IdentWithToken::new_with_token(constants::COALESCE, Some(Shared::new(token(TokenKind::Coalesce)))),
smallvec![
Shared::new(Node {
token_id: 0.into(),
expr: Shared::new(Expr::Literal(Literal::None)),
}),
Shared::new(Node {
token_id: 2.into(),
expr: Shared::new(Expr::Literal(Literal::String("default".to_owned()))),
}),
],
)),
})
]))]
#[case::coalesce_with_identifiers(
vec![
token(TokenKind::Ident(SmolStr::new("x"))),
token(TokenKind::Coalesce),
token(TokenKind::Ident(SmolStr::new("y"))),
token(TokenKind::Eof)
],
Ok(vec![
Shared::new(Node {
token_id: 1.into(),
expr: Shared::new(Expr::Call(
IdentWithToken::new_with_token(constants::COALESCE, Some(Shared::new(token(TokenKind::Coalesce)))),
smallvec![
Shared::new(Node {
token_id: 0.into(),
expr: Shared::new(Expr::Ident(IdentWithToken::new_with_token("x", Some(Shared::new(token(TokenKind::Ident(SmolStr::new("x")))))))),
}),
Shared::new(Node {
token_id: 2.into(),
expr: Shared::new(Expr::Ident(IdentWithToken::new_with_token("y", Some(Shared::new(token(TokenKind::Ident(SmolStr::new("y")))))))),
}),
],
)),
})
]))]
#[case::coalesce_error_missing_rhs(
vec![
token(TokenKind::StringLiteral("foo".to_owned())),
token(TokenKind::Coalesce),
token(TokenKind::Eof)
],
Err(ParseError::UnexpectedEOFDetected(Module::TOP_LEVEL_MODULE_ID)))]
fn test_parse(#[case] input: Vec<Token>, #[case] expected: Result<Program, ParseError>) {
let arena = Arena::new(10);
let tokens: Vec<Shared<Token>> = input.into_iter().map(Shared::new).collect();
Expand Down
1 change: 1 addition & 0 deletions crates/mq-lang/src/cst/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ pub enum NodeKind {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum BinaryOp {
And,
Coalesce,
Division,
Equal,
Gt,
Expand Down
40 changes: 40 additions & 0 deletions crates/mq-lang/src/cst/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ impl<'a> Parser<'a> {
kind,
TokenKind::And
| TokenKind::Asterisk
| TokenKind::Coalesce
| TokenKind::EqEq
| TokenKind::Gte
| TokenKind::Gt
Expand Down Expand Up @@ -324,6 +325,11 @@ impl<'a> Parser<'a> {
kind: TokenKind::Asterisk,
..
} => NodeKind::BinaryOp(BinaryOp::Multiplication),
Token {
range: _,
kind: TokenKind::Coalesce,
..
} => NodeKind::BinaryOp(BinaryOp::Coalesce),
Token {
range: _,
kind: TokenKind::EqEq,
Expand Down Expand Up @@ -4002,6 +4008,40 @@ mod tests {
ErrorReporter::default()
)
)]
#[case::coalesce(
vec![
Shared::new(token(TokenKind::Ident("a".into()))),
Shared::new(token(TokenKind::Coalesce)),
Shared::new(token(TokenKind::Ident("b".into()))),
],
(
vec![
Shared::new(Node {
kind: NodeKind::BinaryOp(BinaryOp::Coalesce),
token: Some(Shared::new(token(TokenKind::Coalesce))),
leading_trivia: Vec::new(),
trailing_trivia: Vec::new(),
children: vec![
Shared::new(Node {
kind: NodeKind::Ident,
token: Some(Shared::new(token(TokenKind::Ident("a".into())))),
leading_trivia: Vec::new(),
trailing_trivia: Vec::new(),
children: Vec::new(),
}),
Shared::new(Node {
kind: NodeKind::Ident,
token: Some(Shared::new(token(TokenKind::Ident("b".into())))),
leading_trivia: Vec::new(),
trailing_trivia: Vec::new(),
children: Vec::new(),
}),
],
}),
],
ErrorReporter::default()
)
)]
#[case::lt(
vec![
Shared::new(token(TokenKind::Ident("a".into()))),
Expand Down
52 changes: 46 additions & 6 deletions crates/mq-lang/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4797,12 +4797,52 @@ mod tests {
Ok(vec![RuntimeValue::Bool(true)])
)]
#[case::is_nan_with_number(
vec![RuntimeValue::Number(42.0.into())],
vec![
ast_call("is_nan", SmallVec::new())
],
Ok(vec![RuntimeValue::Bool(false)])
)]
vec![RuntimeValue::Number(42.0.into())],
vec![
ast_call("is_nan", SmallVec::new())
],
Ok(vec![RuntimeValue::Bool(false)])
)]
#[case::coalesce_first_non_none(
vec![RuntimeValue::NONE],
vec![
ast_call("coalesce", smallvec![
ast_node(ast::Expr::Literal(ast::Literal::None)),
ast_node(ast::Expr::Literal(ast::Literal::String("first".to_string()))),
])
],
Ok(vec![RuntimeValue::String("first".to_string())])
)]
#[case::coalesce_second_non_none(
vec![RuntimeValue::NONE],
vec![
ast_call("coalesce", smallvec![
ast_node(ast::Expr::Literal(ast::Literal::None)),
ast_node(ast::Expr::Literal(ast::Literal::None)),
])
],
Ok(vec![RuntimeValue::NONE])
)]
#[case::coalesce_first_value_non_none(
vec![RuntimeValue::String("value".to_string())],
vec![
ast_call("coalesce", smallvec![
ast_node(ast::Expr::Literal(ast::Literal::String("value".to_string()))),
ast_node(ast::Expr::Literal(ast::Literal::String("other".to_string()))),
])
],
Ok(vec![RuntimeValue::String("value".to_string())])
)]
#[case::coalesce_array(
vec![RuntimeValue::Array(vec![RuntimeValue::NONE, RuntimeValue::String("foo".to_string())])],
vec![
ast_call("coalesce", smallvec![
ast_node(ast::Expr::Literal(ast::Literal::None)),
ast_node(ast::Expr::Literal(ast::Literal::String("bar".to_string()))),
])
],
Ok(vec![RuntimeValue::String("bar".to_string())])
)]
fn test_eval(
token_arena: Shared<SharedCell<Arena<Shared<Token>>>>,
#[case] runtime_values: Vec<RuntimeValue>,
Expand Down
24 changes: 24 additions & 0 deletions crates/mq-lang/src/eval/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2187,6 +2187,21 @@ define_builtin!(INFINITE, ParamNum::Fixed(0), |_, _, _| {
Ok(RuntimeValue::Number(number::INFINITE))
});

define_builtin!(
COALESCE,
ParamNum::Fixed(2),
|_, _, mut args| match args.as_mut_slice() {
[a, b] => {
if a.is_none() {
Ok(std::mem::take(b))
} else {
Ok(std::mem::take(a))
}
}
Comment on lines +2194 to +2200
Copy link
Preview

Copilot AI Oct 6, 2025

Choose a reason for hiding this comment

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

The coalesce function implementation lacks documentation explaining its null coalescing behavior. Add a doc comment describing that it returns the first non-None value from the two arguments.

Copilot uses AI. Check for mistakes.

_ => unreachable!(),
}
);

#[cfg(feature = "file-io")]
define_builtin!(
READ_FILE,
Expand Down Expand Up @@ -2231,6 +2246,7 @@ const HASH_BASE64: u64 = fnv1a_hash_64("base64");
const HASH_BASE64D: u64 = fnv1a_hash_64("base64d");
const HASH_CEIL: u64 = fnv1a_hash_64("ceil");
const HASH_COMPACT: u64 = fnv1a_hash_64("compact");
const HASH_COALESCE: u64 = fnv1a_hash_64("coalesce");
const HASH_DECREASE_HEADER_LEVEL: u64 = fnv1a_hash_64("decrease_header_level");
const HASH_DEL: u64 = fnv1a_hash_64("del");
const HASH_DICT: u64 = fnv1a_hash_64(constants::DICT);
Expand Down Expand Up @@ -2344,6 +2360,7 @@ pub fn get_builtin_functions_by_str(name_str: &str) -> Option<&'static BuiltinFu
HASH_BASE64D => Some(&BASE64D),
HASH_CEIL => Some(&CEIL),
HASH_COMPACT => Some(&COMPACT),
HASH_COALESCE => Some(&COALESCE),
HASH_DECREASE_HEADER_LEVEL => Some(&DECREASE_HEADER_LEVEL),
HASH_DEL => Some(&DEL),
HASH_DICT => Some(&DICT),
Expand Down Expand Up @@ -3522,6 +3539,13 @@ pub static BUILTIN_FUNCTION_DOC: LazyLock<FxHashMap<SmolStr, BuiltinFunctionDoc>
params: &[],
},
);
map.insert(
SmolStr::new("coalesce"),
BuiltinFunctionDoc {
description: "Returns the first non-None value from the two provided arguments.",
params: &["value1", "value2"],
},
);
map
},
);
Expand Down
Loading