syster-base 0.3.3-alpha

Core library for SysML v2 and KerML parsing, AST, and semantic analysis
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
//! Rowan-based formatter for SysML/KerML
//!
//! This module provides lossless formatting that preserves comments and trivia.
//! It uses Rowan for CST (Concrete Syntax Tree) representation and Logos for lexing.

mod lexer;
mod options;
mod syntax_kind;

#[cfg(test)]
mod tests;

use lexer::{Token, tokenize};
pub use options::FormatOptions;
use rowan::GreenNodeBuilder;
use syntax_kind::{SyntaxKind, SyntaxNode};
use tokio_util::sync::CancellationToken;

/// Format SysML/KerML source code with cancellation support.
/// Returns `None` if the cancellation token is signalled.
pub fn format_async(
    source: &str,
    options: &FormatOptions,
    cancel: &CancellationToken,
) -> Option<String> {
    let tokens = tokenize(source);
    let cst = parse_to_cst(&tokens, cancel)?;
    render(&cst, options, cancel)
}

/// Parse tokens into a CST with cancellation support
fn parse_to_cst(tokens: &[Token], cancel: &CancellationToken) -> Option<SyntaxNode> {
    let mut builder = GreenNodeBuilder::new();

    builder.start_node(SyntaxKind::SourceFile.into());

    let mut pos = 0;
    while pos < tokens.len() {
        if cancel.is_cancelled() {
            return None;
        }
        pos = parse_element(tokens, pos, &mut builder);
    }

    builder.finish_node();

    Some(SyntaxNode::new_root(builder.finish()))
}

/// Parse a single element (package, definition, usage, import, comment, etc.)
fn parse_element(tokens: &[Token], mut pos: usize, builder: &mut GreenNodeBuilder) -> usize {
    // Consume leading trivia
    pos = consume_trivia(tokens, pos, builder);

    if pos >= tokens.len() {
        return pos;
    }

    let token = &tokens[pos];

    match token.kind {
        SyntaxKind::PackageKw => parse_package(tokens, pos, builder),
        SyntaxKind::PartKw
        | SyntaxKind::AttributeKw
        | SyntaxKind::PortKw
        | SyntaxKind::ItemKw
        | SyntaxKind::ActionKw
        | SyntaxKind::StateKw
        | SyntaxKind::RequirementKw
        | SyntaxKind::ConstraintKw
        | SyntaxKind::ConnectionKw
        | SyntaxKind::AllocationKw
        | SyntaxKind::InterfaceKw
        | SyntaxKind::FlowKw
        | SyntaxKind::UseCaseKw
        | SyntaxKind::ViewKw
        | SyntaxKind::ViewpointKw
        | SyntaxKind::RenderingKw
        | SyntaxKind::MetadataKw
        | SyntaxKind::OccurrenceKw
        | SyntaxKind::AnalysisKw
        | SyntaxKind::VerificationKw
        | SyntaxKind::ConcernKw
        | SyntaxKind::EnumKw
        | SyntaxKind::CalcKw
        | SyntaxKind::CaseKw
        | SyntaxKind::IndividualKw => parse_definition_or_usage(tokens, pos, builder),
        SyntaxKind::AbstractKw | SyntaxKind::RefKw | SyntaxKind::ConstKw => {
            parse_definition_or_usage(tokens, pos, builder)
        }
        SyntaxKind::ImportKw => parse_import(tokens, pos, builder),
        SyntaxKind::AliasKw => parse_alias(tokens, pos, builder),
        SyntaxKind::DocKw | SyntaxKind::CommentKw => parse_annotation(tokens, pos, builder),
        _ => {
            // Unknown token, just add it and move on
            builder.token(token.kind.into(), token.text);
            pos + 1
        }
    }
}

/// Consume trivia (whitespace, comments) and add to tree
fn consume_trivia(tokens: &[Token], mut pos: usize, builder: &mut GreenNodeBuilder) -> usize {
    while pos < tokens.len() {
        let token = &tokens[pos];
        match token.kind {
            SyntaxKind::Whitespace | SyntaxKind::LineComment | SyntaxKind::BlockComment => {
                builder.token(token.kind.into(), token.text);
                pos += 1;
            }
            _ => break,
        }
    }
    pos
}

/// Parse a package declaration
fn parse_package(tokens: &[Token], mut pos: usize, builder: &mut GreenNodeBuilder) -> usize {
    builder.start_node(SyntaxKind::Package.into());

    // 'package' keyword
    builder.token(tokens[pos].kind.into(), tokens[pos].text);
    pos += 1;

    pos = consume_trivia(tokens, pos, builder);

    // Optional name
    if pos < tokens.len() && tokens[pos].kind == SyntaxKind::Identifier {
        builder.start_node(SyntaxKind::Name.into());
        builder.token(tokens[pos].kind.into(), tokens[pos].text);
        builder.finish_node();
        pos += 1;
    }

    pos = consume_trivia(tokens, pos, builder);

    // Body or semicolon
    if pos < tokens.len() {
        if tokens[pos].kind == SyntaxKind::LBrace {
            pos = parse_body(tokens, pos, builder);
        } else if tokens[pos].kind == SyntaxKind::Semicolon {
            builder.token(tokens[pos].kind.into(), tokens[pos].text);
            pos += 1;
        }
    }

    builder.finish_node();
    pos
}

/// Parse a block body { ... }
fn parse_body(tokens: &[Token], mut pos: usize, builder: &mut GreenNodeBuilder) -> usize {
    builder.start_node(SyntaxKind::Body.into());

    // Opening brace
    builder.token(tokens[pos].kind.into(), tokens[pos].text);
    pos += 1;

    // Parse elements until closing brace
    while pos < tokens.len() && tokens[pos].kind != SyntaxKind::RBrace {
        let prev_pos = pos;
        pos = parse_element(tokens, pos, builder);
        if pos == prev_pos {
            // Avoid infinite loop - consume unknown token
            builder.token(tokens[pos].kind.into(), tokens[pos].text);
            pos += 1;
        }
    }

    // Closing brace
    if pos < tokens.len() && tokens[pos].kind == SyntaxKind::RBrace {
        pos = consume_trivia(tokens, pos, builder);
        if pos < tokens.len() && tokens[pos].kind == SyntaxKind::RBrace {
            builder.token(tokens[pos].kind.into(), tokens[pos].text);
            pos += 1;
        }
    }

    builder.finish_node();
    pos
}

/// Parse a definition or usage (part def, part, attribute, etc.)
fn parse_definition_or_usage(
    tokens: &[Token],
    mut pos: usize,
    builder: &mut GreenNodeBuilder,
) -> usize {
    // Determine if this is a definition (has 'def' keyword) or usage
    let is_definition = has_def_keyword(tokens, pos);

    if is_definition {
        builder.start_node(SyntaxKind::Definition.into());
    } else {
        builder.start_node(SyntaxKind::Usage.into());
    }

    // Consume modifiers (abstract, ref, const)
    while pos < tokens.len() {
        match tokens[pos].kind {
            SyntaxKind::AbstractKw | SyntaxKind::RefKw | SyntaxKind::ConstKw => {
                builder.token(tokens[pos].kind.into(), tokens[pos].text);
                pos += 1;
                pos = consume_trivia(tokens, pos, builder);
            }
            _ => break,
        }
    }

    // Keyword (part, attribute, etc.)
    if pos < tokens.len() && is_element_keyword(&tokens[pos].kind) {
        builder.token(tokens[pos].kind.into(), tokens[pos].text);
        pos += 1;
        pos = consume_trivia(tokens, pos, builder);
    }

    // 'def' keyword if definition
    if pos < tokens.len() && tokens[pos].kind == SyntaxKind::DefKw {
        builder.token(tokens[pos].kind.into(), tokens[pos].text);
        pos += 1;
        pos = consume_trivia(tokens, pos, builder);
    }

    // Name
    if pos < tokens.len() && tokens[pos].kind == SyntaxKind::Identifier {
        builder.start_node(SyntaxKind::Name.into());
        builder.token(tokens[pos].kind.into(), tokens[pos].text);
        builder.finish_node();
        pos += 1;
    }

    pos = consume_trivia(tokens, pos, builder);

    // Relationships and type annotations (consume until { or ;)
    while pos < tokens.len() {
        match tokens[pos].kind {
            SyntaxKind::LBrace | SyntaxKind::Semicolon => break,
            _ => {
                builder.token(tokens[pos].kind.into(), tokens[pos].text);
                pos += 1;
            }
        }
    }

    // Body or semicolon
    if pos < tokens.len() {
        if tokens[pos].kind == SyntaxKind::LBrace {
            pos = parse_body(tokens, pos, builder);
        } else if tokens[pos].kind == SyntaxKind::Semicolon {
            builder.token(tokens[pos].kind.into(), tokens[pos].text);
            pos += 1;
        }
    }

    builder.finish_node();
    pos
}

/// Check if a sequence starting at pos has a 'def' keyword before { or ;
fn has_def_keyword(tokens: &[Token], mut pos: usize) -> bool {
    while pos < tokens.len() {
        match tokens[pos].kind {
            SyntaxKind::DefKw => return true,
            SyntaxKind::LBrace | SyntaxKind::Semicolon => return false,
            _ => pos += 1,
        }
    }
    false
}

/// Check if a kind is an element keyword
fn is_element_keyword(kind: &SyntaxKind) -> bool {
    matches!(
        kind,
        SyntaxKind::PartKw
            | SyntaxKind::AttributeKw
            | SyntaxKind::PortKw
            | SyntaxKind::ItemKw
            | SyntaxKind::ActionKw
            | SyntaxKind::StateKw
            | SyntaxKind::RequirementKw
            | SyntaxKind::ConstraintKw
            | SyntaxKind::ConnectionKw
            | SyntaxKind::AllocationKw
            | SyntaxKind::InterfaceKw
            | SyntaxKind::FlowKw
            | SyntaxKind::UseCaseKw
            | SyntaxKind::ViewKw
            | SyntaxKind::ViewpointKw
            | SyntaxKind::RenderingKw
            | SyntaxKind::MetadataKw
            | SyntaxKind::OccurrenceKw
            | SyntaxKind::AnalysisKw
            | SyntaxKind::VerificationKw
            | SyntaxKind::ConcernKw
            | SyntaxKind::EnumKw
            | SyntaxKind::CalcKw
            | SyntaxKind::CaseKw
            | SyntaxKind::IndividualKw
    )
}

/// Parse an import statement
fn parse_import(tokens: &[Token], mut pos: usize, builder: &mut GreenNodeBuilder) -> usize {
    builder.start_node(SyntaxKind::Import.into());

    // 'import' keyword
    builder.token(tokens[pos].kind.into(), tokens[pos].text);
    pos += 1;

    // Consume until semicolon
    while pos < tokens.len() && tokens[pos].kind != SyntaxKind::Semicolon {
        builder.token(tokens[pos].kind.into(), tokens[pos].text);
        pos += 1;
    }

    // Semicolon
    if pos < tokens.len() && tokens[pos].kind == SyntaxKind::Semicolon {
        builder.token(tokens[pos].kind.into(), tokens[pos].text);
        pos += 1;
    }

    builder.finish_node();
    pos
}

/// Parse an alias declaration
fn parse_alias(tokens: &[Token], mut pos: usize, builder: &mut GreenNodeBuilder) -> usize {
    builder.start_node(SyntaxKind::Alias.into());

    // 'alias' keyword
    builder.token(tokens[pos].kind.into(), tokens[pos].text);
    pos += 1;

    // Consume until semicolon
    while pos < tokens.len() && tokens[pos].kind != SyntaxKind::Semicolon {
        builder.token(tokens[pos].kind.into(), tokens[pos].text);
        pos += 1;
    }

    // Semicolon
    if pos < tokens.len() && tokens[pos].kind == SyntaxKind::Semicolon {
        builder.token(tokens[pos].kind.into(), tokens[pos].text);
        pos += 1;
    }

    builder.finish_node();
    pos
}

/// Parse a doc or comment annotation
fn parse_annotation(tokens: &[Token], mut pos: usize, builder: &mut GreenNodeBuilder) -> usize {
    builder.start_node(SyntaxKind::Annotation.into());

    // 'doc' or 'comment' keyword
    builder.token(tokens[pos].kind.into(), tokens[pos].text);
    pos += 1;

    // Consume until end of annotation (block comment or semicolon)
    while pos < tokens.len() {
        let kind = tokens[pos].kind;
        builder.token(tokens[pos].kind.into(), tokens[pos].text);
        pos += 1;

        if kind == SyntaxKind::BlockComment || kind == SyntaxKind::Semicolon {
            break;
        }
    }

    builder.finish_node();
    pos
}

/// Render the CST back to formatted source code with cancellation support
fn render(
    node: &SyntaxNode,
    options: &FormatOptions,
    cancel: &CancellationToken,
) -> Option<String> {
    let mut output = String::new();
    let mut indent_level: usize = 0;
    let mut at_line_start = true;

    render_node(
        node,
        options,
        &mut output,
        &mut indent_level,
        &mut at_line_start,
        cancel,
    )?;

    Some(output)
}

fn render_node(
    node: &SyntaxNode,
    options: &FormatOptions,
    output: &mut String,
    indent_level: &mut usize,
    at_line_start: &mut bool,
    cancel: &CancellationToken,
) -> Option<()> {
    // Collect children for lookahead
    let children: Vec<_> = node.children_with_tokens().collect();

    for (i, child) in children.iter().enumerate() {
        if cancel.is_cancelled() {
            return None;
        }

        match child {
            rowan::NodeOrToken::Token(token) => {
                let kind: SyntaxKind = token.kind();
                let text = token.text();

                // Look ahead to next non-whitespace token
                let next_significant = children[i + 1..].iter().find_map(|c| match c {
                    rowan::NodeOrToken::Token(t) if t.kind() != SyntaxKind::Whitespace => {
                        Some(t.kind())
                    }
                    _ => None,
                });

                match kind {
                    SyntaxKind::Whitespace => {
                        // Don't preserve newlines before opening brace - keep it on same line
                        if next_significant == Some(SyntaxKind::LBrace) {
                            // Just add a single space, brace will be on same line
                            if !*at_line_start && !output.ends_with(' ') && !output.is_empty() {
                                output.push(' ');
                            }
                        } else if text.contains('\n') {
                            // Preserve newlines for other cases
                            let newline_count = text.matches('\n').count();
                            for _ in 0..newline_count.min(2) {
                                output.push('\n');
                            }
                            *at_line_start = true;
                        } else if !*at_line_start && !output.ends_with(' ') && !output.is_empty() {
                            // Single space between tokens
                            output.push(' ');
                        }
                    }
                    SyntaxKind::LineComment => {
                        if *at_line_start {
                            output.push_str(&options.indent(*indent_level));
                            *at_line_start = false;
                        }
                        output.push_str(text);
                    }
                    SyntaxKind::BlockComment => {
                        if *at_line_start {
                            output.push_str(&options.indent(*indent_level));
                            *at_line_start = false;
                        }
                        output.push_str(text);
                    }
                    SyntaxKind::LBrace => {
                        // Ensure space before brace if not at line start
                        if !*at_line_start && !output.ends_with(' ') && !output.ends_with('\n') {
                            output.push(' ');
                        }
                        // If at line start but we want brace on same line, remove trailing newlines
                        if *at_line_start && !output.is_empty() {
                            // Remove trailing newlines to put brace on same line
                            while output.ends_with('\n') {
                                output.pop();
                            }
                            if !output.ends_with(' ') {
                                output.push(' ');
                            }
                            *at_line_start = false;
                        }
                        output.push('{');
                        *indent_level += 1;
                    }
                    SyntaxKind::RBrace => {
                        *indent_level = indent_level.saturating_sub(1);
                        if *at_line_start {
                            output.push_str(&options.indent(*indent_level));
                        }
                        output.push('}');
                        *at_line_start = false;
                    }
                    SyntaxKind::Semicolon => {
                        output.push(';');
                        *at_line_start = false;
                    }
                    SyntaxKind::Colon | SyntaxKind::ColonColon | SyntaxKind::Dot => {
                        // No space before colons and dots
                        output.push_str(text);
                        *at_line_start = false;
                    }
                    _ => {
                        if *at_line_start {
                            output.push_str(&options.indent(*indent_level));
                            *at_line_start = false;
                        }
                        // Don't add automatic spaces - let whitespace tokens handle spacing
                        output.push_str(text);
                    }
                }
            }
            rowan::NodeOrToken::Node(child_node) => {
                render_node(
                    child_node,
                    options,
                    output,
                    indent_level,
                    at_line_start,
                    cancel,
                )?;
            }
        }
    }
    Some(())
}