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
9 changes: 9 additions & 0 deletions crates/celox-sv-syntax/src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,14 @@ pub struct VariableDeclaration {
pub tokens: TokenRange,
}

/// A named constraint block. Unsupported constraint forms retain their name
/// but no executable expressions, so randomization cannot silently ignore them.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ClassConstraint {
pub name: Name,
pub expressions: Option<Vec<ExprId>>,
}

/// A class declaration (IEEE 1800-2023 8.3). Its body also remains an
/// unconverted body item covering the same tokens.
#[derive(Clone, Debug, Eq, PartialEq)]
Expand All @@ -976,6 +984,7 @@ pub struct ClassDeclaration {
pub interfaces: Vec<TypeId>,
/// Whether the class declares constraints, including prototypes.
pub has_constraints: bool,
pub constraints: Vec<ClassConstraint>,
/// The parameter port list `#(...)`, after which the parameters the
/// body declares are local (8.25).
pub parameter_ports: Option<ParameterDeclarationId>,
Expand Down
1 change: 1 addition & 0 deletions crates/celox-sv-syntax/src/parser/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ impl Parser<'_> {
base,
interfaces,
has_constraints: false,
constraints: Vec::new(),
parameter_ports,
parameters: Vec::new(),
type_aliases: Vec::new(),
Expand Down
48 changes: 41 additions & 7 deletions crates/celox-sv-syntax/src/parser/constraints.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Class constraint syntax validation. Constraints retain their opaque class
//! owner; no randomization or solver semantics are introduced here.
//! owner. Simple expression blocks also retain their expression nodes.
use super::*;

enum Job {
Expand Down Expand Up @@ -32,10 +32,26 @@ impl Parser<'_> {
class.has_constraints = true;
}
let prototype = self.eat("extern") || self.eat("pure");
self.eat("static");
let is_static = self.eat("static");
self.expect("constraint")?;
self.name()?;
let name = self.name()?;
if self.class_stack.last().is_some_and(|class| {
class
.constraints
.iter()
.any(|block| block.name.spelling == name.spelling)
}) {
return Err(self.error("duplicate constraint block"));
}
let mut expressions = Vec::new();
let mut supported = !is_static;
if self.eat(";") {
if let Some(class) = self.class_stack.last_mut() {
class.constraints.push(ClassConstraint {
name,
expressions: None,
});
}
return Ok(());
}
if prototype {
Expand All @@ -52,6 +68,7 @@ impl Parser<'_> {
}
jobs.push(Job::Block(solve));
if solve && self.eat("solve") {
supported = false;
self.constraint_primary_list()?;
self.expect("before")?;
self.constraint_primary_list()?;
Expand All @@ -74,32 +91,41 @@ impl Parser<'_> {
}
Job::Expression => {
if self.eat("if") {
supported = false;
self.expect("(")?;
self.expression(0)?;
self.expect(")")?;
jobs.push(Job::Else);
jobs.push(Job::Set);
} else if self.eat("foreach") {
supported = false;
self.foreach_header()?;
jobs.push(Job::Set);
} else if self.eat("disable") {
supported = false;
self.expect("soft")?;
self.constraint_primary()?;
self.expect(";")?;
} else if self.eat("unique") {
supported = false;
self.constraint_ranges(false)?;
self.expect(";")?;
} else {
let soft = self.eat("soft");
if soft {
supported = false;
self.expression(0)?;
} else {
self.constraint_condition()?;
let (expression, simple) = self.constraint_condition()?;
expressions.push(expression);
supported &= simple;
}
if !soft && self.eat("->") {
supported = false;
jobs.push(Job::Set);
} else {
if self.eat("dist") {
supported = false;
self.constraint_ranges(true)?;
}
self.expect(";")?;
Expand All @@ -108,17 +134,25 @@ impl Parser<'_> {
}
}
}
if let Some(class) = self.class_stack.last_mut() {
class.constraints.push(ClassConstraint {
name,
expressions: supported.then_some(expressions),
});
}
Ok(())
}

fn constraint_condition(&mut self) -> Result<()> {
fn constraint_condition(&mut self) -> Result<(ExprId, bool)> {
// Keep the constraint-set arrow for the outer continuation. Equivalence
// shares its expression precedence, so consume it explicitly here.
self.expression(2)?;
let expression = self.expression(2)?;
let mut simple = true;
while self.eat("<->") {
simple = false;
self.expression(2)?;
}
Ok(())
Ok((expression, simple))
}

fn constraint_ranges(&mut self, distribution: bool) -> Result<()> {
Expand Down
11 changes: 11 additions & 0 deletions crates/celox-sv-syntax/src/parser/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3043,3 +3043,14 @@ fn malformed_array_method_with_is_rejected() {
);
}
}

#[test]
fn constraints_retain_simple_expressions_and_mark_other_forms() {
let tree = parse(
"class C; rand int x; constraint a { x > 0; x < 3; } constraint b { if (x) x == 1; } endclass",
);
let blocks = &tree.classes[0].constraints;
assert_eq!(blocks[0].name.spelling, "a");
assert_eq!(blocks[0].expressions.as_ref().unwrap().len(), 2);
assert!(blocks[1].expressions.is_none());
}
Loading