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