Skip to content
Open
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixed

- A unary operator takes another unary expression as its operand (KerML 8.2.5.8.1: the operand is an `ArgumentMember`, so any expression), so `- -x`, `~~x` and `not not b` parse; the Pilot's Xtext narrows the operand to an `ExtentExpression`. `fmt`, which prints `-(-x)` as `--x`, no longer refuses such a file.

## v0.11.3

### Added
Expand Down
30 changes: 30 additions & 0 deletions crates/sysmlv2-parser/tests/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,36 @@ fn unary_binds_tighter_than_exponentiation() {
));
}

#[test]
fn unary_operators_nest() {
// KerML 8.2.5.8.1: the operand of a unary operator is any expression.
for (src, outer, inner) in [
("- -x", UnaryOp::Minus, UnaryOp::Minus),
("~~x", UnaryOp::Tilde, UnaryOp::Tilde),
("not not x", UnaryOp::Not, UnaryOp::Not),
("-~x", UnaryOp::Minus, UnaryOp::Tilde),
] {
let ExprKind::Unary { op, operand } = value_expr(src).kind else {
panic!("{src}")
};
assert_eq!(op, outer, "{src}");
assert!(
matches!(operand.kind, ExprKind::Unary { op, .. } if op == inner),
"{src}"
);
}
// - -2 ** 3 parses as (-(-2)) ** 3, as -2 ** 3 does.
let ExprKind::Binary {
op: BinaryOp::Pow,
lhs,
..
} = value_expr("- -2 ** 3").kind
else {
panic!()
};
assert!(matches!(lhs.kind, ExprKind::Unary { .. }));
}

#[test]
fn logical_chain() {
// a or b and c => or(a, and(b, c)) — `and` binds tighter.
Expand Down
5 changes: 4 additions & 1 deletion crates/sysmlv2-syntax/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5262,7 +5262,10 @@ impl<'s> Parser<'s> {
if let Some(op) = op {
let start = self.cur().span;
self.bump();
let operand = self.parse_extent()?;
// KerML 8.2.5.8.1: the operand is an ArgumentMember, so any
// expression, and `- -x` and `not not b` nest. The recursion
// is charged as one level deeper.
let operand = self.branch(|p| p.descend(Self::parse_unary))?;
let span = start.join(operand.span);
return Some(Expr {
kind: ExprKind::Unary {
Expand Down