Skip to content

Conversation

harehare
Copy link
Owner

@harehare harehare commented Oct 5, 2025

Add support for the null coalescing operator (??) to the mq language. The operator provides a concise way to handle null/none values by returning the right operand when the left operand is null or none.

  • Add COALESCE constant to ast/constants.rs
  • Implement TokenKind::Coalesce in lexer
  • Add coalesce operator parsing with precedence level 6
  • Implement coalesce builtin function in eval/builtin.rs
  • Include comprehensive test cases for operator and function behavior
  • Add documentation for coalesce function

Add support for the null coalescing operator (??) to the mq language. The operator provides a concise way to handle null/none values by returning the right operand when the left operand is null or none.

- Add COALESCE constant to ast/constants.rs
- Implement TokenKind::Coalesce in lexer
- Add coalesce operator parsing with precedence level 6
- Implement coalesce builtin function in eval/builtin.rs
- Include comprehensive test cases for operator and function behavior
- Add documentation for coalesce function
Copy link

codspeed-hq bot commented Oct 5, 2025

CodSpeed Performance Report

Merging #706 will not alter performance

Comparing feat/add-coalesce-operator (65b102b) with main (52f108a)

Summary

✅ 14 untouched

@harehare harehare requested a review from Copilot October 6, 2025 11:51
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for the null coalescing operator (??) to the mq language, providing a concise way to handle null/none values by returning the right operand when the left operand is null or none.

Key changes:

  • Introduces TokenKind::Coalesce for lexer support of the ?? operator
  • Implements coalesce operator parsing with precedence level 6 (same as range operator)
  • Adds COALESCE builtin function that returns the first non-None value from two arguments

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
crates/mq-lang/src/lexer/token.rs Adds TokenKind::Coalesce variant and its display implementation, reorganizes enum variants alphabetically
crates/mq-lang/src/lexer.rs Adds coalesce token parser and includes it in punctuation parsing
crates/mq-lang/src/eval/builtin.rs Implements COALESCE builtin function with documentation and hash mapping
crates/mq-lang/src/eval.rs Adds comprehensive test cases for coalesce function behavior
crates/mq-lang/src/cst/parser.rs Adds coalesce binary operation support to CST parser with test case
crates/mq-lang/src/cst/node.rs Adds BinaryOp::Coalesce variant to node types
crates/mq-lang/src/ast/parser.rs Implements coalesce operator precedence and AST parsing with extensive test coverage
crates/mq-lang/src/ast/constants.rs Defines COALESCE constant for the operator name

alt((
and, or, l_paren, r_paren, l_brace, r_brace, comma, colon, semi_colon, l_bracket,
and, or, coalesce, l_paren, r_paren, l_brace, r_brace, comma, colon, semi_colon, l_bracket,
r_bracket, question, pipe,
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 parser should be ordered by precedence or token length. Since ?? is two characters and ? is one character, coalesce should come before question in the alt() parser to ensure proper tokenization of ?? before attempting to parse it as two separate ? tokens.

Suggested change
r_bracket, question, pipe,
r_bracket, pipe, question,

Copilot uses AI. Check for mistakes.

@harehare harehare requested a review from Copilot October 6, 2025 12:13
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

fn punctuations(input: Span) -> IResult<Span, Token> {
alt((
and, or, l_paren, r_paren, l_brace, r_brace, comma, colon, semi_colon, l_bracket,
and, or, coalesce, l_paren, r_paren, l_brace, r_brace, comma, colon, semi_colon, l_bracket,
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 parser should be placed after question in the alt() chain to ensure proper parsing precedence. Since ?? starts with ?, the single ? parser might consume the first character before ?? can be matched.

Copilot uses AI. Check for mistakes.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@harehare harehare requested a review from Copilot October 6, 2025 12:17
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@harehare harehare requested a review from Copilot October 6, 2025 12:24
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Comment on lines +2194 to +2200
[a, b] => {
if a.is_none() {
Ok(std::mem::take(b))
} else {
Ok(std::mem::take(a))
}
}
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.

@harehare harehare requested a review from Copilot October 6, 2025 12:41
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated no new comments.

@harehare harehare merged commit 0de7747 into main Oct 6, 2025
6 checks passed
@harehare harehare deleted the feat/add-coalesce-operator branch October 6, 2025 13:46
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.

1 participant