Skip to main content

sysml_v2_parser/ast/
requirement.rs

1use super::behavior::{AssignStmt, ForLoop, ThenAction};
2use super::common::{ConnectBody, DocComment, Identification, Import, ParseErrorNode, Visibility};
3use super::structure::{Annotation, AttributeBody, AttributeDef, AttributeUsage};
4use super::view::ConstraintDefBodyElement;
5use crate::ast::core::{Expression, Node, Span};
6
7/// Requirement definition: `requirement def` Identification (`:>` specializes)? body.
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub struct RequirementDef {
10    pub identification: Identification,
11    /// Supertype after `:>`, e.g. Some("UserRequirement") for `requirement def Need :> UserRequirement`.
12    pub specializes: Option<String>,
13    /// Span of the `:> <type>` fragment (for semantic tokens), when present.
14    pub specializes_span: Option<Span>,
15    pub body: RequirementDefBody,
16}
17
18/// Body of an requirement definition: `;` or `{` RequirementDefBodyElement* `}`.
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub enum RequirementDefBody {
21    Semicolon,
22    Brace {
23        elements: Vec<Node<RequirementDefBodyElement>>,
24    },
25}
26
27#[derive(Debug, Clone, PartialEq, Eq)]
28pub enum RequirementDefBodyElement {
29    Error(Node<ParseErrorNode>),
30    /// Unmodeled requirement-body element captured as raw text (used for library parsing).
31    Other(String),
32    Annotation(Node<Annotation>),
33    Import(Node<Import>),
34    SubjectDecl(Node<SubjectDecl>),
35    RequirementActorDecl(Node<RequirementActorDecl>),
36    AttributeDef(Node<AttributeDef>),
37    AttributeUsage(Node<AttributeUsage>),
38    VerifyRequirement(Node<VerifyRequirementMember>),
39    RequireConstraint(Node<RequireConstraint>),
40    Frame(Node<FrameMember>),
41    Doc(Node<DocComment>),
42}
43
44/// Subject declaration: `subject` name `:` type `;`.
45#[derive(Debug, Clone, PartialEq, Eq)]
46pub struct SubjectDecl {
47    pub name: String,
48    pub type_name: String,
49}
50
51/// Actor parameter in a requirement body: `actor` name? `:` type `;`.
52#[derive(Debug, Clone, PartialEq, Eq)]
53pub struct RequirementActorDecl {
54    pub name: String,
55    pub type_name: String,
56}
57
58/// Require constraint: `require constraint { ... }`.
59#[derive(Debug, Clone, PartialEq, Eq)]
60pub struct RequireConstraint {
61    pub body: RequireConstraintBody,
62}
63
64/// Requirement verification usage in requirement/objective bodies:
65/// `verify requirement <...>` or shorthand `verify <qualified_name>;`.
66#[derive(Debug, Clone, PartialEq, Eq)]
67pub struct VerifyRequirementMember {
68    /// True for `verify requirement ...`; false for shorthand `verify ...;`.
69    pub explicit_requirement_keyword: bool,
70    /// Parsed requirement usage when explicit form is used.
71    pub requirement: Option<Node<RequirementUsage>>,
72    /// Shorthand verified requirement reference (`verify QualifiedName;`).
73    pub target: Option<String>,
74}
75
76/// Require constraint body: `;` or `{` ConstraintDefBodyElement* `}`.
77#[derive(Debug, Clone, PartialEq, Eq)]
78pub enum RequireConstraintBody {
79    Semicolon,
80    Brace {
81        elements: Vec<Node<ConstraintDefBodyElement>>,
82    },
83}
84
85/// Requirement usage / Satisfy. Example: `satisfy EnduranceReq by droneInstance;`
86#[derive(Debug, Clone, PartialEq, Eq)]
87pub struct Satisfy {
88    pub source: Node<Expression>,
89    pub target: Node<Expression>,
90    pub body: ConnectBody,
91}
92
93/// Bare requirement Usage.
94#[derive(Debug, Clone, PartialEq, Eq)]
95pub struct RequirementUsage {
96    pub name: String,
97    pub type_name: Option<String>,
98    pub subsets: Option<String>,
99    pub body: RequirementDefBody,
100}
101
102/// Item usage inside a part definition body: `item` name multiplicity? (`:` type)? body.
103#[derive(Debug, Clone, PartialEq, Eq)]
104pub struct ItemUsage {
105    pub name: String,
106    pub type_name: Option<String>,
107    pub multiplicity: Option<String>,
108    pub body: AttributeBody,
109}
110
111/// Enumeration usage inside a definition body: `enum` name (`:` type)? body.
112#[derive(Debug, Clone, PartialEq, Eq)]
113pub struct EnumerationUsage {
114    pub name: String,
115    pub type_name: Option<String>,
116    pub multiplicity: Option<String>,
117    pub body: AttributeBody,
118}
119
120/// Dependency: `dependency` (Identification `from`)? client(s) `to` supplier(s) RelationshipBody.
121#[derive(Debug, Clone, PartialEq, Eq)]
122pub struct Dependency {
123    pub identification: Option<Identification>,
124    pub clients: Vec<String>,
125    pub suppliers: Vec<String>,
126    pub body: ConnectBody,
127}
128
129/// Framed concern member in requirement body: `frame` name (`;` or body).
130#[derive(Debug, Clone, PartialEq, Eq)]
131pub struct FrameMember {
132    pub name: String,
133    pub body: RequirementDefBody,
134}
135
136/// Concern usage at package level: `concern` name (`:` type)? RequirementBody.
137#[derive(Debug, Clone, PartialEq, Eq)]
138pub struct ConcernUsage {
139    pub name: String,
140    pub type_name: Option<String>,
141    pub body: RequirementDefBody,
142}
143
144/// Case definition: `case def` Identification body.
145#[derive(Debug, Clone, PartialEq, Eq)]
146pub struct CaseDef {
147    pub identification: Identification,
148    pub specializes: Option<String>,
149    pub specializes_span: Option<Span>,
150    pub body: UseCaseDefBody,
151}
152
153/// Case usage: `case` name (`:` type)? body.
154#[derive(Debug, Clone, PartialEq, Eq)]
155pub struct CaseUsage {
156    pub name: String,
157    pub type_name: Option<String>,
158    pub body: UseCaseDefBody,
159}
160
161/// Analysis case definition: `analysis def` Identification body.
162#[derive(Debug, Clone, PartialEq, Eq)]
163pub struct AnalysisCaseDef {
164    pub identification: Identification,
165    pub specializes: Option<String>,
166    pub specializes_span: Option<Span>,
167    pub body: UseCaseDefBody,
168}
169
170/// Analysis case usage: `analysis` name (`:` type)? body.
171#[derive(Debug, Clone, PartialEq, Eq)]
172pub struct AnalysisCaseUsage {
173    pub name: String,
174    pub type_name: Option<String>,
175    pub body: UseCaseDefBody,
176}
177
178/// Verification case definition: `verification def` Identification body.
179#[derive(Debug, Clone, PartialEq, Eq)]
180pub struct VerificationCaseDef {
181    pub identification: Identification,
182    pub specializes: Option<String>,
183    pub specializes_span: Option<Span>,
184    pub body: UseCaseDefBody,
185}
186
187/// Verification case usage: `verification` name (`:` type)? body.
188#[derive(Debug, Clone, PartialEq, Eq)]
189pub struct VerificationCaseUsage {
190    pub name: String,
191    pub type_name: Option<String>,
192    pub body: UseCaseDefBody,
193}
194
195/// Use case usage at package level: `use case` name (`:` type)? CaseBody.
196#[derive(Debug, Clone, PartialEq, Eq)]
197pub struct UseCaseUsage {
198    pub name: String,
199    pub type_name: Option<String>,
200    pub body: UseCaseDefBody,
201}
202
203// ---------------------------------------------------------------------------
204// Use Cases
205// ---------------------------------------------------------------------------
206
207/// Actor declaration: `actor` Identification `;`.
208#[derive(Debug, Clone, PartialEq, Eq)]
209pub struct ActorDecl {
210    pub identification: Identification,
211}
212
213/// Use Case definition: `use case def` Identification body.
214#[derive(Debug, Clone, PartialEq, Eq)]
215pub struct UseCaseDef {
216    pub identification: Identification,
217    pub specializes: Option<String>,
218    pub specializes_span: Option<Span>,
219    pub body: UseCaseDefBody,
220}
221
222#[derive(Debug, Clone, PartialEq, Eq)]
223pub enum UseCaseDefBody {
224    Semicolon,
225    Brace {
226        elements: Vec<Node<UseCaseDefBodyElement>>,
227    },
228}
229
230/// `first <name>;` inside a case/use-case body (used in SysML v2 release fixtures).
231#[derive(Debug, Clone, PartialEq, Eq)]
232pub struct FirstSuccession {
233    pub target: String,
234}
235
236/// `then done;` inside a case/use-case body.
237#[derive(Debug, Clone, PartialEq, Eq)]
238pub struct ThenDone {}
239
240/// `include <usecase> ...` inside a case/use-case body.
241#[derive(Debug, Clone, PartialEq, Eq)]
242pub struct IncludeUseCase {
243    pub name: String,
244    /// Optional multiplicity suffix like `[0..*]` captured as raw text including brackets.
245    pub multiplicity: Option<String>,
246    pub body: UseCaseDefBody,
247}
248
249/// `then include <usecase> ...` inside a case/use-case body.
250#[derive(Debug, Clone, PartialEq, Eq)]
251pub struct ThenIncludeUseCase {
252    pub include: Node<IncludeUseCase>,
253}
254
255/// `then use case <name> ...` inside a case/use-case body.
256#[derive(Debug, Clone, PartialEq, Eq)]
257pub struct ThenUseCaseUsage {
258    pub use_case: Node<UseCaseUsage>,
259}
260
261/// `subject;` shorthand used in SysML v2 release fixtures (subject of an enclosing case/use case).
262#[derive(Debug, Clone, PartialEq, Eq)]
263pub struct SubjectRef {}
264
265/// `actor :>> <name> = <expr>;` redefinition/assignment used in SysML v2 release fixtures.
266#[derive(Debug, Clone, PartialEq, Eq)]
267pub struct ActorRedefinitionAssignment {
268    pub name: String,
269    /// Raw RHS expression text up to `;` (we don't model the expression grammar here yet).
270    pub rhs: String,
271}
272
273/// `ref :>> <name> { ... }` redefinition used in SysML v2 release fixtures.
274#[derive(Debug, Clone, PartialEq, Eq)]
275pub struct RefRedefinition {
276    pub name: String,
277    /// Raw body text for now (balanced `{ ... }` including nested braces).
278    pub body: String,
279}
280
281/// `return ref <name><multiplicity?> { ... }` used in SysML v2 release libraries.
282#[derive(Debug, Clone, PartialEq, Eq)]
283pub struct ReturnRef {
284    pub name: String,
285    pub multiplicity: Option<String>,
286    /// Raw body text for now (balanced `{ ... }` including nested braces).
287    pub body: String,
288}
289
290#[derive(Debug, Clone, PartialEq, Eq)]
291pub enum UseCaseDefBodyElement {
292    Error(Node<ParseErrorNode>),
293    /// Unmodeled use-case / analysis-case body element captured as raw text (used for library parsing).
294    Other(String),
295    AttributeDef(Node<AttributeDef>),
296    Doc(Node<DocComment>),
297    SubjectDecl(Node<SubjectDecl>),
298    /// `subject;` shorthand.
299    SubjectRef(Node<SubjectRef>),
300    ActorUsage(Node<ActorUsage>),
301    ActorRedefinitionAssignment(Node<ActorRedefinitionAssignment>),
302    Objective(Node<Objective>),
303    FirstSuccession(Node<FirstSuccession>),
304    ThenIncludeUseCase(Node<ThenIncludeUseCase>),
305    ThenUseCaseUsage(Node<ThenUseCaseUsage>),
306    ThenDone(Node<ThenDone>),
307    IncludeUseCase(Node<IncludeUseCase>),
308    RefRedefinition(Node<RefRedefinition>),
309    ReturnRef(Node<ReturnRef>),
310    Assign(Node<AssignStmt>),
311    ForLoop(Node<ForLoop>),
312    ThenAction(Node<ThenAction>),
313}
314
315/// actor usage `actor pilot : Operator;`
316#[derive(Debug, Clone, PartialEq, Eq)]
317pub struct ActorUsage {
318    pub name: String,
319    pub type_name: String,
320}
321
322/// Objective `objective { doc ... }`
323#[derive(Debug, Clone, PartialEq, Eq)]
324pub struct Objective {
325    pub visibility: Option<Visibility>,
326    pub requirement: Node<RequirementUsage>,
327}
328
329// ---------------------------------------------------------------------------
330// State Machine
331// ---------------------------------------------------------------------------