Repository navigation
438 feature load sysml graphical bnfkgbnf into the code generator #443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
antoineatsoprasteria
merged 2 commits into
development
from
438-feature-load-sysml-graphical-bnfkgbnf-into-the-code-generator
Nov 13, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| // Grammar | ||
|
|
||
| grammar kgbnf; | ||
|
|
||
| specification : (NL)* rule_definition+ EOF ; | ||
|
|
||
| rule_definition | ||
| : name=GRAPHICAL_NAME op=(ASSIGN | PARTIAL_ASSIGN) rule_body=alternatives NL+ | ||
| ; | ||
|
|
||
| alternatives | ||
| : alternative (PIPE alternative)* | ||
| ; | ||
|
|
||
| alternative | ||
| : element* | ||
| ; | ||
|
|
||
| element | ||
| : group | ||
| | terminal | ||
| | image | ||
| | edge_end | ||
| | non_terminal | ||
| ; | ||
|
|
||
| group | ||
| : LPAREN alternatives RPAREN (suffix=suffix_op)? | ||
| ; | ||
|
|
||
| terminal | ||
| : val=SINGLE_QUOTED_STRING (suffix=suffix_op)? | ||
| ; | ||
|
|
||
| image | ||
| : IMAGE | ||
| ; | ||
|
|
||
| edge_end | ||
| : AMPERSAND name=GRAPHICAL_NAME (suffix=suffix_op)? | ||
| ; | ||
|
|
||
| non_terminal | ||
| : name=(GRAPHICAL_NAME | TEXTUAL_NAME) (suffix=suffix_op)? | ||
| ; | ||
|
|
||
| suffix_op : '*' | '+' | '?' ; | ||
|
|
||
| // Lexer | ||
| PARTIAL_ASSIGN : '=|' | '|=' ; | ||
| ASSIGN : '=' ; | ||
| PIPE : '|' ; | ||
| LPAREN : '(' ; | ||
| RPAREN : ')' ; | ||
| AMPERSAND : '&' ; | ||
| IMAGE : '<img' ~[>]* '>' ; | ||
|
|
||
| GRAPHICAL_NAME : [a-z]+ ('-' [a-z]+)* ; | ||
| TEXTUAL_NAME : [a-zA-Z_] [a-zA-Z0-9_]* ; | ||
| SINGLE_QUOTED_STRING : '\'' (~['\\] | '\\' .)* '\'' ; | ||
|
|
||
| COMMENT : '//' ~[\r\n]* -> skip ; | ||
| WS : [ \t]+ -> skip ; | ||
| CONTINUATION : '\r'? '\n' [ \t]+ -> skip ; | ||
| NL : '\r'? '\n' ; |
33 changes: 33 additions & 0 deletions
33
SysML2.NET.CodeGenerator.Tests/Extensions/GraphicalGrammarErrataTestFixture.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // ------------------------------------------------------------------------------------------------ | ||
| // <copyright file="GraphicalGrammarErrataTestFixture.cs" company="Starion Group S.A."> | ||
| // | ||
| // Copyright (C) 2022-2026 Starion Group S.A. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| // </copyright> | ||
| // ------------------------------------------------------------------------------------------------ | ||
|
|
||
| namespace SysML2.NET.CodeGenerator.Tests.Extensions | ||
| { | ||
| using System.IO; | ||
| using System.Linq; | ||
|
|
||
| using NUnit.Framework; | ||
|
|
||
| using SysML2.NET.CodeGenerator.Extensions; | ||
| using SysML2.NET.CodeGenerator.Grammar; | ||
|
|
||
| [TestFixture] | ||
| public class GraphicalGrammarErrataTestFixture | ||
| { | ||
| [Test] | ||
| public void VerifyQueryUnappliedErrata() | ||
| { | ||
| GrammarLoader.LoadGraphicalNotationSpecification(Path.Combine(TestContext.CurrentContext.TestDirectory, "datamodel", "SysML-graphical-bnf.kgbnf")); | ||
|
|
||
| var unapplied = GraphicalGrammarErrata.QueryUnappliedErrata(); | ||
|
|
||
| Assert.That(unapplied, Is.Empty, $"Errata matched nothing in the graphical BNF: {string.Join(", ", unapplied.Select(erratum => erratum.RuleName))}"); | ||
| } | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
SysML2.NET.CodeGenerator.Tests/Grammar/GraphicalNotationSpecificationVisitorTestFixture.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // ------------------------------------------------------------------------------------------------ | ||
| // <copyright file="GraphicalNotationSpecificationVisitorTestFixture.cs" company="Starion Group S.A."> | ||
| // | ||
| // Copyright (C) 2022-2026 Starion Group S.A. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| // </copyright> | ||
| // ------------------------------------------------------------------------------------------------ | ||
|
|
||
| namespace SysML2.NET.CodeGenerator.Tests.Grammar | ||
| { | ||
| using System.IO; | ||
|
|
||
| using Antlr4.Runtime; | ||
|
|
||
| using NUnit.Framework; | ||
|
|
||
| using SysML2.NET.CodeGenerator.Grammar; | ||
| using SysML2.NET.CodeGenerator.Grammar.Model; | ||
|
|
||
| [TestFixture] | ||
| public class GraphicalNotationSpecificationVisitorTestFixture | ||
| { | ||
| [Test] | ||
| public void VerifyCanParseGrammar() | ||
| { | ||
| var filePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "datamodel", "SysML-graphical-bnf.kgbnf"); | ||
|
|
||
| var parser = new kgbnfParser(new CommonTokenStream(new kgbnfLexer(CharStreams.fromPath(filePath)))); | ||
| var tree = parser.specification(); | ||
| var specification = (GraphicalNotationSpecification)new GraphicalNotationSpecificationVisitor().Visit(tree); | ||
|
|
||
| using (Assert.EnterMultipleScope()) | ||
| { | ||
| Assert.That(parser.NumberOfSyntaxErrors, Is.Zero); | ||
| Assert.That(specification.Rules, Is.Not.Empty); | ||
| } | ||
| } | ||
| } | ||
| } |
99 changes: 99 additions & 0 deletions
99
SysML2.NET.CodeGenerator/Extensions/GraphicalGrammarErrata.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| // ------------------------------------------------------------------------------------------------ | ||
| // <copyright file="GraphicalGrammarErrata.cs" company="Starion Group S.A."> | ||
| // | ||
| // Copyright (C) 2022-2026 Starion Group S.A. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| // </copyright> | ||
| // ------------------------------------------------------------------------------------------------ | ||
|
|
||
| namespace SysML2.NET.CodeGenerator.Extensions | ||
| { | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
|
|
||
| /// <summary> | ||
| /// Corrects known defects in the graphical BNF (<c>Resources/SysML-graphical-bnf.kgbnf</c>), at generation time. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// The grammar is OMG source and is never edited. An entry belongs here only when a reference names no production and exactly | ||
| /// one production is evidently meant; a reference with no evident target stays unresolved and is reported by | ||
| /// <c>GraphicalNotationSpecification.QueryUnresolvedReferences</c>. | ||
| /// </remarks> | ||
| public static class GraphicalGrammarErrata | ||
| { | ||
| /// <summary> | ||
| /// The production-text corrections applied to the grammar before it is parsed. | ||
| /// </summary> | ||
| private static readonly GrammarProductionErratum[] ProductionEntries = | ||
| [ | ||
| new("usage-cp", | ||
| "usage-cp = usageDeclaration ValuePart?", | ||
| "usage-cp = UsageDeclaration ValuePart?", | ||
| "SysML 8.2.3.6 writes 'usageDeclaration', which names no rule; every other reference in 8.2.3 writes the textual rule " + | ||
| "'UsageDeclaration' (SysML 8.2.2.6.2)."), | ||
| new("allocation-node", | ||
| "allocation-node =\n general-node\n | element-in-textual-compartment", | ||
| "allocation-node =\n general-node\n | element-inside-textual-compartment", | ||
| "SysML 8.2.3.15 references 'element-in-textual-compartment', which is never defined; the production is " + | ||
| "'element-inside-textual-compartment' (SysML 8.2.3.2)."), | ||
| new("exhibit-states-compartment-contents", | ||
| "exhibit-states-compartment-contents = exhibit-state-scompartment-element* '…'?\n\nexhibit-states-compartment-element-compartment = UsageDeclaration", | ||
| "exhibit-states-compartment-contents = exhibit-states-compartment-element* '…'?\n\nexhibit-states-compartment-element = UsageDeclaration", | ||
| "SysML 8.2.3.18 references 'exhibit-state-scompartment-element', which is never defined, and defines " + | ||
| "'exhibit-states-compartment-element-compartment', which is never referenced; every sibling compartment pairs " + | ||
| "'<x>-compartment-contents' with '<x>-compartment-element'."), | ||
| new("action-flow-view", | ||
| "(perform-action-swimlanes)?", | ||
| "(perform-actions-swimlanes)?", | ||
| "SysML 8.2.3.17 references 'perform-action-swimlanes', which is never defined; the production is 'perform-actions-swimlanes'.") | ||
| ]; | ||
|
|
||
| /// <summary> | ||
| /// The production rule names whose corrections have matched during this generator run. | ||
| /// </summary> | ||
| private static readonly HashSet<string> AppliedRuleNames = []; | ||
|
|
||
| /// <summary> | ||
| /// Applies every known production correction to the raw text of the graphical BNF. | ||
| /// </summary> | ||
| /// <param name="kgbnfSource">The grammar text as read from disk.</param> | ||
| /// <returns>The corrected grammar text with <c>\n</c> line endings, or <paramref name="kgbnfSource" /> when it is blank.</returns> | ||
| /// <remarks> | ||
| /// Line endings are normalised first because every <c>Original</c> is written with <c>\n</c>. | ||
| /// </remarks> | ||
| public static string ApplyProductions(string kgbnfSource) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(kgbnfSource)) | ||
| { | ||
| return kgbnfSource; | ||
| } | ||
|
|
||
| var normalisedSource = kgbnfSource.Replace("\r\n", "\n").Replace('\r', '\n'); | ||
|
|
||
| return ProductionEntries | ||
| .Where(erratum => normalisedSource.Contains(erratum.Original, StringComparison.Ordinal)) | ||
| .Aggregate(normalisedSource, (corrected, erratum) => | ||
| { | ||
| AppliedRuleNames.Add(erratum.RuleName); | ||
| return corrected.Replace(erratum.Original, erratum.Replacement, StringComparison.Ordinal); | ||
| }); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns the corrections that matched nothing during this generator run. | ||
| /// </summary> | ||
| /// <returns>The stale entries, which should be pruned.</returns> | ||
| /// <remarks> | ||
| /// Only meaningful once the graphical BNF has been loaded. | ||
| /// </remarks> | ||
| public static IReadOnlyList<(string RuleName, string Justification)> QueryUnappliedErrata() | ||
| => | ||
| [ | ||
| ..ProductionEntries | ||
| .Where(erratum => !AppliedRuleNames.Contains(erratum.RuleName)) | ||
| .Select(erratum => (erratum.RuleName, erratum.Justification)) | ||
| ]; | ||
| } | ||
| } | ||
56 changes: 56 additions & 0 deletions
56
...L2.NET.CodeGenerator/Grammar/AutoGenGrammar/SysML2/NET/CodeGenerator/Grammar/kgbnf.interp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| token literal names: | ||
| null | ||
| '*' | ||
| '+' | ||
| '?' | ||
| null | ||
| '=' | ||
| '|' | ||
| '(' | ||
| ')' | ||
| '&' | ||
| null | ||
| null | ||
| null | ||
| null | ||
| null | ||
| null | ||
| null | ||
| null | ||
|
|
||
| token symbolic names: | ||
| null | ||
| null | ||
| null | ||
| null | ||
| PARTIAL_ASSIGN | ||
| ASSIGN | ||
| PIPE | ||
| LPAREN | ||
| RPAREN | ||
| AMPERSAND | ||
| IMAGE | ||
| GRAPHICAL_NAME | ||
| TEXTUAL_NAME | ||
| SINGLE_QUOTED_STRING | ||
| COMMENT | ||
| WS | ||
| CONTINUATION | ||
| NL | ||
|
|
||
| rule names: | ||
| specification | ||
| rule_definition | ||
| alternatives | ||
| alternative | ||
| element | ||
| group | ||
| terminal | ||
| image | ||
| edge_end | ||
| non_terminal | ||
| suffix_op | ||
|
|
||
|
|
||
| atn: | ||
| [4, 1, 17, 88, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 2, 5, 7, 5, 2, 6, 7, 6, 2, 7, 7, 7, 2, 8, 7, 8, 2, 9, 7, 9, 2, 10, 7, 10, 1, 0, 5, 0, 24, 8, 0, 10, 0, 12, 0, 27, 9, 0, 1, 0, 4, 0, 30, 8, 0, 11, 0, 12, 0, 31, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 4, 1, 40, 8, 1, 11, 1, 12, 1, 41, 1, 2, 1, 2, 1, 2, 5, 2, 47, 8, 2, 10, 2, 12, 2, 50, 9, 2, 1, 3, 5, 3, 53, 8, 3, 10, 3, 12, 3, 56, 9, 3, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 3, 4, 63, 8, 4, 1, 5, 1, 5, 1, 5, 1, 5, 3, 5, 69, 8, 5, 1, 6, 1, 6, 3, 6, 73, 8, 6, 1, 7, 1, 7, 1, 8, 1, 8, 1, 8, 3, 8, 80, 8, 8, 1, 9, 1, 9, 3, 9, 84, 8, 9, 1, 10, 1, 10, 1, 10, 0, 0, 11, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 0, 3, 1, 0, 4, 5, 1, 0, 11, 12, 1, 0, 1, 3, 89, 0, 25, 1, 0, 0, 0, 2, 35, 1, 0, 0, 0, 4, 43, 1, 0, 0, 0, 6, 54, 1, 0, 0, 0, 8, 62, 1, 0, 0, 0, 10, 64, 1, 0, 0, 0, 12, 70, 1, 0, 0, 0, 14, 74, 1, 0, 0, 0, 16, 76, 1, 0, 0, 0, 18, 81, 1, 0, 0, 0, 20, 85, 1, 0, 0, 0, 22, 24, 5, 17, 0, 0, 23, 22, 1, 0, 0, 0, 24, 27, 1, 0, 0, 0, 25, 23, 1, 0, 0, 0, 25, 26, 1, 0, 0, 0, 26, 29, 1, 0, 0, 0, 27, 25, 1, 0, 0, 0, 28, 30, 3, 2, 1, 0, 29, 28, 1, 0, 0, 0, 30, 31, 1, 0, 0, 0, 31, 29, 1, 0, 0, 0, 31, 32, 1, 0, 0, 0, 32, 33, 1, 0, 0, 0, 33, 34, 5, 0, 0, 1, 34, 1, 1, 0, 0, 0, 35, 36, 5, 11, 0, 0, 36, 37, 7, 0, 0, 0, 37, 39, 3, 4, 2, 0, 38, 40, 5, 17, 0, 0, 39, 38, 1, 0, 0, 0, 40, 41, 1, 0, 0, 0, 41, 39, 1, 0, 0, 0, 41, 42, 1, 0, 0, 0, 42, 3, 1, 0, 0, 0, 43, 48, 3, 6, 3, 0, 44, 45, 5, 6, 0, 0, 45, 47, 3, 6, 3, 0, 46, 44, 1, 0, 0, 0, 47, 50, 1, 0, 0, 0, 48, 46, 1, 0, 0, 0, 48, 49, 1, 0, 0, 0, 49, 5, 1, 0, 0, 0, 50, 48, 1, 0, 0, 0, 51, 53, 3, 8, 4, 0, 52, 51, 1, 0, 0, 0, 53, 56, 1, 0, 0, 0, 54, 52, 1, 0, 0, 0, 54, 55, 1, 0, 0, 0, 55, 7, 1, 0, 0, 0, 56, 54, 1, 0, 0, 0, 57, 63, 3, 10, 5, 0, 58, 63, 3, 12, 6, 0, 59, 63, 3, 14, 7, 0, 60, 63, 3, 16, 8, 0, 61, 63, 3, 18, 9, 0, 62, 57, 1, 0, 0, 0, 62, 58, 1, 0, 0, 0, 62, 59, 1, 0, 0, 0, 62, 60, 1, 0, 0, 0, 62, 61, 1, 0, 0, 0, 63, 9, 1, 0, 0, 0, 64, 65, 5, 7, 0, 0, 65, 66, 3, 4, 2, 0, 66, 68, 5, 8, 0, 0, 67, 69, 3, 20, 10, 0, 68, 67, 1, 0, 0, 0, 68, 69, 1, 0, 0, 0, 69, 11, 1, 0, 0, 0, 70, 72, 5, 13, 0, 0, 71, 73, 3, 20, 10, 0, 72, 71, 1, 0, 0, 0, 72, 73, 1, 0, 0, 0, 73, 13, 1, 0, 0, 0, 74, 75, 5, 10, 0, 0, 75, 15, 1, 0, 0, 0, 76, 77, 5, 9, 0, 0, 77, 79, 5, 11, 0, 0, 78, 80, 3, 20, 10, 0, 79, 78, 1, 0, 0, 0, 79, 80, 1, 0, 0, 0, 80, 17, 1, 0, 0, 0, 81, 83, 7, 1, 0, 0, 82, 84, 3, 20, 10, 0, 83, 82, 1, 0, 0, 0, 83, 84, 1, 0, 0, 0, 84, 19, 1, 0, 0, 0, 85, 86, 7, 2, 0, 0, 86, 21, 1, 0, 0, 0, 10, 25, 31, 41, 48, 54, 62, 68, 72, 79, 83] |
25 changes: 25 additions & 0 deletions
25
...L2.NET.CodeGenerator/Grammar/AutoGenGrammar/SysML2/NET/CodeGenerator/Grammar/kgbnf.tokens
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| T__0=1 | ||
| T__1=2 | ||
| T__2=3 | ||
| PARTIAL_ASSIGN=4 | ||
| ASSIGN=5 | ||
| PIPE=6 | ||
| LPAREN=7 | ||
| RPAREN=8 | ||
| AMPERSAND=9 | ||
| IMAGE=10 | ||
| GRAPHICAL_NAME=11 | ||
| TEXTUAL_NAME=12 | ||
| SINGLE_QUOTED_STRING=13 | ||
| COMMENT=14 | ||
| WS=15 | ||
| CONTINUATION=16 | ||
| NL=17 | ||
| '*'=1 | ||
| '+'=2 | ||
| '?'=3 | ||
| '='=5 | ||
| '|'=6 | ||
| '('=7 | ||
| ')'=8 | ||
| '&'=9 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see this is a public static class holding a private static
AppliedRuleNamescollection. Is this intended to be used elsewhere or just by the generator (one-run)? Just commenting this to double check the intent of the implementation, not an issue itselfThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything there is really in the scope of code-generation and won't ever be used in another scope