Skip to main content

sysml_v2_parser/ast/
requirement.rs

1use super::behavior::{AssignStmt, ForLoop, InOut, 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    /// Set when parsed as `in`/`out`/`inout item` in port def bodies.
133    pub direction: Option<InOut>,
134}
135
136/// Enumeration usage inside a definition body: `enum` name (`:` type)? body.
137#[derive(Debug, Clone, PartialEq, Eq)]
138pub struct EnumerationUsage {
139    pub name: String,
140    pub type_name: Option<String>,
141    pub multiplicity: Option<String>,
142    pub body: AttributeBody,
143}
144
145/// Dependency: `dependency` (Identification `from`)? client(s) `to` supplier(s) RelationshipBody.
146#[derive(Debug, Clone, PartialEq, Eq)]
147pub struct Dependency {
148    pub identification: Option<Identification>,
149    pub clients: Vec<String>,
150    pub suppliers: Vec<String>,
151    pub body: ConnectBody,
152}
153
154/// Framed concern member in requirement body: `frame` name (`;` or body).
155#[derive(Debug, Clone, PartialEq, Eq)]
156pub struct FrameMember {
157    pub name: String,
158    pub body: RequirementDefBody,
159}
160
161/// Concern usage at package level: `concern` name (`:` type)? RequirementBody.
162#[derive(Debug, Clone, PartialEq, Eq)]
163pub struct ConcernUsage {
164    pub name: String,
165    pub type_name: Option<String>,
166    pub body: RequirementDefBody,
167}
168
169/// Case definition: `case def` Identification body.
170#[derive(Debug, Clone, PartialEq, Eq)]
171pub struct CaseDef {
172    pub identification: Identification,
173    pub specializes: Option<String>,
174    pub specializes_span: Option<Span>,
175    pub body: UseCaseDefBody,
176}
177
178/// Case usage: `case` name (`:` type)? body.
179#[derive(Debug, Clone, PartialEq, Eq)]
180pub struct CaseUsage {
181    pub name: String,
182    pub type_name: Option<String>,
183    pub body: UseCaseDefBody,
184}
185
186/// Analysis case definition: `analysis def` Identification body.
187#[derive(Debug, Clone, PartialEq, Eq)]
188pub struct AnalysisCaseDef {
189    pub identification: Identification,
190    pub specializes: Option<String>,
191    pub specializes_span: Option<Span>,
192    pub body: UseCaseDefBody,
193}
194
195/// Analysis case usage: `analysis` name (`:` type)? body.
196#[derive(Debug, Clone, PartialEq, Eq)]
197pub struct AnalysisCaseUsage {
198    pub name: String,
199    pub type_name: Option<String>,
200    pub body: UseCaseDefBody,
201}
202
203/// Verification case definition: `verification def` Identification body.
204#[derive(Debug, Clone, PartialEq, Eq)]
205pub struct VerificationCaseDef {
206    pub identification: Identification,
207    pub specializes: Option<String>,
208    pub specializes_span: Option<Span>,
209    pub body: UseCaseDefBody,
210}
211
212/// Verification case usage: `verification` name (`:` type)? body.
213#[derive(Debug, Clone, PartialEq, Eq)]
214pub struct VerificationCaseUsage {
215    pub name: String,
216    pub type_name: Option<String>,
217    pub body: UseCaseDefBody,
218}
219
220/// Use case usage at package level: `use case` name (`:` type)? CaseBody.
221#[derive(Debug, Clone, PartialEq, Eq)]
222pub struct UseCaseUsage {
223    pub name: String,
224    pub type_name: Option<String>,
225    pub body: UseCaseDefBody,
226}
227
228// ---------------------------------------------------------------------------
229// Use Cases
230// ---------------------------------------------------------------------------
231
232/// Actor declaration: `actor` Identification `;`.
233#[derive(Debug, Clone, PartialEq, Eq)]
234pub struct ActorDecl {
235    pub identification: Identification,
236}
237
238/// Use Case definition: `use case def` Identification body.
239#[derive(Debug, Clone, PartialEq, Eq)]
240pub struct UseCaseDef {
241    pub identification: Identification,
242    pub specializes: Option<String>,
243    pub specializes_span: Option<Span>,
244    pub body: UseCaseDefBody,
245}
246
247#[derive(Debug, Clone, PartialEq, Eq)]
248pub enum UseCaseDefBody {
249    Semicolon,
250    Brace {
251        elements: Vec<Node<UseCaseDefBodyElement>>,
252    },
253}
254
255/// `first <name>;` inside a case/use-case body (used in SysML v2 release fixtures).
256#[derive(Debug, Clone, PartialEq, Eq)]
257pub struct FirstSuccession {
258    pub target: String,
259}
260
261/// `then done;` inside a case/use-case body.
262#[derive(Debug, Clone, PartialEq, Eq)]
263pub struct ThenDone {}
264
265/// `include <usecase> ...` inside a case/use-case body.
266#[derive(Debug, Clone, PartialEq, Eq)]
267pub struct IncludeUseCase {
268    pub name: String,
269    /// Optional multiplicity suffix like `[0..*]` captured as raw text including brackets.
270    pub multiplicity: Option<String>,
271    pub body: UseCaseDefBody,
272}
273
274/// `then include <usecase> ...` inside a case/use-case body.
275#[derive(Debug, Clone, PartialEq, Eq)]
276pub struct ThenIncludeUseCase {
277    pub include: Node<IncludeUseCase>,
278}
279
280/// `then use case <name> ...` inside a case/use-case body.
281#[derive(Debug, Clone, PartialEq, Eq)]
282pub struct ThenUseCaseUsage {
283    pub use_case: Node<UseCaseUsage>,
284}
285
286/// `subject;` shorthand used in SysML v2 release fixtures (subject of an enclosing case/use case).
287#[derive(Debug, Clone, PartialEq, Eq)]
288pub struct SubjectRef {}
289
290/// `actor :>> <name> = <expr>;` redefinition/assignment used in SysML v2 release fixtures.
291#[derive(Debug, Clone, PartialEq, Eq)]
292pub struct ActorRedefinitionAssignment {
293    pub name: String,
294    /// Raw RHS expression text up to `;` (we don't model the expression grammar here yet).
295    pub rhs: String,
296}
297
298/// `ref :>> <name> { ... }` redefinition used in SysML v2 release fixtures.
299#[derive(Debug, Clone, PartialEq, Eq)]
300pub struct RefRedefinition {
301    pub name: String,
302    /// Raw body text for now (balanced `{ ... }` including nested braces).
303    pub body: String,
304}
305
306/// `return ref <name><multiplicity?> { ... }` used in SysML v2 release libraries.
307#[derive(Debug, Clone, PartialEq, Eq)]
308pub struct ReturnRef {
309    pub name: String,
310    pub multiplicity: Option<String>,
311    /// Raw body text for now (balanced `{ ... }` including nested braces).
312    pub body: String,
313}
314
315#[derive(Debug, Clone, PartialEq, Eq)]
316pub enum UseCaseDefBodyElement {
317    Error(Node<ParseErrorNode>),
318    /// Unmodeled use-case / analysis-case body element captured as raw text (used for library parsing).
319    Other(String),
320    Annotation(Node<Annotation>),
321    MetadataKeywordUsage(Node<MetadataKeywordUsage>),
322    AttributeDef(Node<AttributeDef>),
323    Doc(Node<DocComment>),
324    SubjectDecl(Node<SubjectDecl>),
325    /// `subject;` shorthand.
326    SubjectRef(Node<SubjectRef>),
327    ActorUsage(Node<ActorUsage>),
328    ActorRedefinitionAssignment(Node<ActorRedefinitionAssignment>),
329    Objective(Node<Objective>),
330    FirstSuccession(Node<FirstSuccession>),
331    ThenIncludeUseCase(Node<ThenIncludeUseCase>),
332    ThenUseCaseUsage(Node<ThenUseCaseUsage>),
333    ThenDone(Node<ThenDone>),
334    IncludeUseCase(Node<IncludeUseCase>),
335    RefRedefinition(Node<RefRedefinition>),
336    ReturnRef(Node<ReturnRef>),
337    Assign(Node<AssignStmt>),
338    ForLoop(Node<ForLoop>),
339    ThenAction(Node<ThenAction>),
340}
341
342/// actor usage `actor pilot : Operator;`
343#[derive(Debug, Clone, PartialEq, Eq)]
344pub struct ActorUsage {
345    pub name: String,
346    pub type_name: String,
347}
348
349/// Objective `objective { doc ... }`
350#[derive(Debug, Clone, PartialEq, Eq)]
351pub struct Objective {
352    pub visibility: Option<Visibility>,
353    pub requirement: Node<RequirementUsage>,
354}
355
356// ---------------------------------------------------------------------------
357// State Machine
358// ---------------------------------------------------------------------------