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