Skip to content

Commit 479ec53

Browse files
[WIP]: KEBNF files are parsed and turn into object graph
1 parent bd24ed7 commit 479ec53

25 files changed

+6579
-0
lines changed

Resources/KerML-textual-bnf.kebnf

Lines changed: 1467 additions & 0 deletions
Large diffs are not rendered by default.

Resources/SysML-textual-bnf.kebnf

Lines changed: 1705 additions & 0 deletions
Large diffs are not rendered by default.

Resources/kebnf.g4

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Grammar
2+
3+
grammar kebnf;
4+
5+
specification : (NL)* rule_definition+ EOF ;
6+
7+
rule_definition
8+
: name=ID (params=parameter_list)? (COLON target_ast=ID)? ASSIGN rule_body=alternatives SEMICOLON? NL+
9+
;
10+
11+
parameter_list
12+
: LPAREN param_name=ID COLON param_type=ID RPAREN
13+
;
14+
15+
alternatives
16+
: alternative (PIPE alternative)*
17+
;
18+
19+
alternative
20+
: element*
21+
;
22+
23+
element
24+
: assignment
25+
| non_parsing_assignment
26+
| non_parsing_empty
27+
| cross_reference
28+
| group
29+
| terminal
30+
| non_terminal
31+
;
32+
33+
assignment
34+
: property=dotted_id op=(ASSIGN | ADD_ASSIGN | BOOL_ASSIGN) content=element_core (suffix=suffix_op)?
35+
;
36+
37+
non_parsing_assignment
38+
: LBRACE property=dotted_id op=(ASSIGN | ADD_ASSIGN) val=value_literal RBRACE
39+
;
40+
41+
non_parsing_empty
42+
: LBRACE RBRACE
43+
;
44+
45+
cross_reference
46+
: TILDE? LBRACK ref=ID RBRACK
47+
;
48+
49+
group
50+
: LPAREN alternatives RPAREN (suffix=suffix_op)?
51+
;
52+
53+
terminal
54+
: val=value_literal (suffix=suffix_op)?
55+
;
56+
57+
non_terminal
58+
: name=ID (suffix=suffix_op)?
59+
;
60+
61+
element_core
62+
: cross_reference
63+
| group
64+
| terminal
65+
| non_terminal
66+
;
67+
68+
dotted_id
69+
: ID (DOT ID)*
70+
;
71+
72+
suffix_op : '*' | '+' | '?' ;
73+
74+
value_literal : ID | 'true' | 'false' | 'this' | INT | STRING | '[QualifiedName]';
75+
76+
// Lexer
77+
ASSIGN : '::=' | '=' ;
78+
ADD_ASSIGN : '+=' ;
79+
BOOL_ASSIGN : '?=' ;
80+
PIPE : '|' ;
81+
COLON : ':' ;
82+
SEMICOLON : ';' ;
83+
COMMA : ',' ;
84+
LPAREN : '(' ;
85+
RPAREN : ')' ;
86+
LBRACK : '[' ;
87+
RBRACK : ']' ;
88+
LBRACE : '{' ;
89+
RBRACE : '}' ;
90+
DOT : '.' ;
91+
TILDE : '~' ;
92+
93+
ID : [a-zA-Z_][a-zA-Z0-9_]* ;
94+
INT : [0-9]+ ;
95+
STRING : '\'' ( ~['\\] | '\\' . )* '\'' ;
96+
97+
COMMENT : '//' ~[\r\n]* -> skip ;
98+
WS : [ \t]+ -> skip ;
99+
CONTINUATION : '\r'? '\n' [ \t]+ -> skip ;
100+
NL : '\r'? '\n' ;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// -------------------------------------------------------------------------------------------------
2+
// <copyright file="TextualNotationSpecificationVisitorTestFixture.cs" company="Starion Group S.A.">
3+
//
4+
// Copyright 2022-2026 Starion Group S.A.
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// </copyright>
19+
// ------------------------------------------------------------------------------------------------
20+
21+
namespace SysML2.NET.CodeGenerator.Tests.Grammar
22+
{
23+
using System;
24+
using System.IO;
25+
26+
using Antlr4.Runtime;
27+
28+
using NUnit.Framework;
29+
30+
using SysML2.NET.CodeGenerator.Grammar;
31+
using SysML2.NET.CodeGenerator.Grammar.Model;
32+
33+
[TestFixture]
34+
public class TextualNotationSpecificationVisitorTestFixture
35+
{
36+
[Test]
37+
[TestCase("KerML-textual-bnf.kebnf")]
38+
[TestCase("SysML-textual-bnf.kebnf")]
39+
public void VerifyCanParseGrammar(string modelName)
40+
{
41+
var filePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "datamodel",modelName );
42+
43+
var stream = CharStreams.fromPath(filePath);
44+
var lexer = new kebnfLexer(stream);
45+
var tokens = new CommonTokenStream(lexer);
46+
var parser = new kebnfParser(tokens);
47+
48+
var tree = parser.specification();
49+
var explorer = new TextualNotationSpecificationVisitor();
50+
var result = (TextualNotationSpecification)explorer.Visit(tree);
51+
var rules = result.Rules;
52+
53+
using (Assert.EnterMultipleScope())
54+
{
55+
Assert.That(rules, Is.Not.Null);
56+
Assert.That(rules, Is.Not.Empty);
57+
}
58+
59+
Console.WriteLine($"Found {rules.Count} rules");
60+
}
61+
}
62+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
token literal names:
2+
null
3+
'*'
4+
'+'
5+
'?'
6+
'true'
7+
'false'
8+
'this'
9+
'[QualifiedName]'
10+
null
11+
'+='
12+
'?='
13+
'|'
14+
':'
15+
';'
16+
','
17+
'('
18+
')'
19+
'['
20+
']'
21+
'{'
22+
'}'
23+
'.'
24+
'~'
25+
null
26+
null
27+
null
28+
null
29+
null
30+
null
31+
null
32+
33+
token symbolic names:
34+
null
35+
null
36+
null
37+
null
38+
null
39+
null
40+
null
41+
null
42+
ASSIGN
43+
ADD_ASSIGN
44+
BOOL_ASSIGN
45+
PIPE
46+
COLON
47+
SEMICOLON
48+
COMMA
49+
LPAREN
50+
RPAREN
51+
LBRACK
52+
RBRACK
53+
LBRACE
54+
RBRACE
55+
DOT
56+
TILDE
57+
ID
58+
INT
59+
STRING
60+
COMMENT
61+
WS
62+
CONTINUATION
63+
NL
64+
65+
rule names:
66+
specification
67+
rule_definition
68+
parameter_list
69+
alternatives
70+
alternative
71+
element
72+
assignment
73+
non_parsing_assignment
74+
non_parsing_empty
75+
cross_reference
76+
group
77+
terminal
78+
non_terminal
79+
element_core
80+
dotted_id
81+
suffix_op
82+
value_literal
83+
84+
85+
atn:
86+
[4, 1, 29, 149, 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, 2, 11, 7, 11, 2, 12, 7, 12, 2, 13, 7, 13, 2, 14, 7, 14, 2, 15, 7, 15, 2, 16, 7, 16, 1, 0, 5, 0, 36, 8, 0, 10, 0, 12, 0, 39, 9, 0, 1, 0, 4, 0, 42, 8, 0, 11, 0, 12, 0, 43, 1, 0, 1, 0, 1, 1, 1, 1, 3, 1, 50, 8, 1, 1, 1, 1, 1, 3, 1, 54, 8, 1, 1, 1, 1, 1, 1, 1, 3, 1, 59, 8, 1, 1, 1, 4, 1, 62, 8, 1, 11, 1, 12, 1, 63, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 3, 1, 3, 1, 3, 5, 3, 75, 8, 3, 10, 3, 12, 3, 78, 9, 3, 1, 4, 5, 4, 81, 8, 4, 10, 4, 12, 4, 84, 9, 4, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 3, 5, 93, 8, 5, 1, 6, 1, 6, 1, 6, 1, 6, 3, 6, 99, 8, 6, 1, 7, 1, 7, 1, 7, 1, 7, 1, 7, 1, 7, 1, 8, 1, 8, 1, 8, 1, 9, 3, 9, 111, 8, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 10, 1, 10, 1, 10, 1, 10, 3, 10, 121, 8, 10, 1, 11, 1, 11, 3, 11, 125, 8, 11, 1, 12, 1, 12, 3, 12, 129, 8, 12, 1, 13, 1, 13, 1, 13, 1, 13, 3, 13, 135, 8, 13, 1, 14, 1, 14, 1, 14, 5, 14, 140, 8, 14, 10, 14, 12, 14, 143, 9, 14, 1, 15, 1, 15, 1, 16, 1, 16, 1, 16, 0, 0, 17, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 0, 4, 1, 0, 8, 10, 1, 0, 8, 9, 1, 0, 1, 3, 2, 0, 4, 7, 23, 25, 154, 0, 37, 1, 0, 0, 0, 2, 47, 1, 0, 0, 0, 4, 65, 1, 0, 0, 0, 6, 71, 1, 0, 0, 0, 8, 82, 1, 0, 0, 0, 10, 92, 1, 0, 0, 0, 12, 94, 1, 0, 0, 0, 14, 100, 1, 0, 0, 0, 16, 106, 1, 0, 0, 0, 18, 110, 1, 0, 0, 0, 20, 116, 1, 0, 0, 0, 22, 122, 1, 0, 0, 0, 24, 126, 1, 0, 0, 0, 26, 134, 1, 0, 0, 0, 28, 136, 1, 0, 0, 0, 30, 144, 1, 0, 0, 0, 32, 146, 1, 0, 0, 0, 34, 36, 5, 29, 0, 0, 35, 34, 1, 0, 0, 0, 36, 39, 1, 0, 0, 0, 37, 35, 1, 0, 0, 0, 37, 38, 1, 0, 0, 0, 38, 41, 1, 0, 0, 0, 39, 37, 1, 0, 0, 0, 40, 42, 3, 2, 1, 0, 41, 40, 1, 0, 0, 0, 42, 43, 1, 0, 0, 0, 43, 41, 1, 0, 0, 0, 43, 44, 1, 0, 0, 0, 44, 45, 1, 0, 0, 0, 45, 46, 5, 0, 0, 1, 46, 1, 1, 0, 0, 0, 47, 49, 5, 23, 0, 0, 48, 50, 3, 4, 2, 0, 49, 48, 1, 0, 0, 0, 49, 50, 1, 0, 0, 0, 50, 53, 1, 0, 0, 0, 51, 52, 5, 12, 0, 0, 52, 54, 5, 23, 0, 0, 53, 51, 1, 0, 0, 0, 53, 54, 1, 0, 0, 0, 54, 55, 1, 0, 0, 0, 55, 56, 5, 8, 0, 0, 56, 58, 3, 6, 3, 0, 57, 59, 5, 13, 0, 0, 58, 57, 1, 0, 0, 0, 58, 59, 1, 0, 0, 0, 59, 61, 1, 0, 0, 0, 60, 62, 5, 29, 0, 0, 61, 60, 1, 0, 0, 0, 62, 63, 1, 0, 0, 0, 63, 61, 1, 0, 0, 0, 63, 64, 1, 0, 0, 0, 64, 3, 1, 0, 0, 0, 65, 66, 5, 15, 0, 0, 66, 67, 5, 23, 0, 0, 67, 68, 5, 12, 0, 0, 68, 69, 5, 23, 0, 0, 69, 70, 5, 16, 0, 0, 70, 5, 1, 0, 0, 0, 71, 76, 3, 8, 4, 0, 72, 73, 5, 11, 0, 0, 73, 75, 3, 8, 4, 0, 74, 72, 1, 0, 0, 0, 75, 78, 1, 0, 0, 0, 76, 74, 1, 0, 0, 0, 76, 77, 1, 0, 0, 0, 77, 7, 1, 0, 0, 0, 78, 76, 1, 0, 0, 0, 79, 81, 3, 10, 5, 0, 80, 79, 1, 0, 0, 0, 81, 84, 1, 0, 0, 0, 82, 80, 1, 0, 0, 0, 82, 83, 1, 0, 0, 0, 83, 9, 1, 0, 0, 0, 84, 82, 1, 0, 0, 0, 85, 93, 3, 12, 6, 0, 86, 93, 3, 14, 7, 0, 87, 93, 3, 16, 8, 0, 88, 93, 3, 18, 9, 0, 89, 93, 3, 20, 10, 0, 90, 93, 3, 22, 11, 0, 91, 93, 3, 24, 12, 0, 92, 85, 1, 0, 0, 0, 92, 86, 1, 0, 0, 0, 92, 87, 1, 0, 0, 0, 92, 88, 1, 0, 0, 0, 92, 89, 1, 0, 0, 0, 92, 90, 1, 0, 0, 0, 92, 91, 1, 0, 0, 0, 93, 11, 1, 0, 0, 0, 94, 95, 3, 28, 14, 0, 95, 96, 7, 0, 0, 0, 96, 98, 3, 26, 13, 0, 97, 99, 3, 30, 15, 0, 98, 97, 1, 0, 0, 0, 98, 99, 1, 0, 0, 0, 99, 13, 1, 0, 0, 0, 100, 101, 5, 19, 0, 0, 101, 102, 3, 28, 14, 0, 102, 103, 7, 1, 0, 0, 103, 104, 3, 32, 16, 0, 104, 105, 5, 20, 0, 0, 105, 15, 1, 0, 0, 0, 106, 107, 5, 19, 0, 0, 107, 108, 5, 20, 0, 0, 108, 17, 1, 0, 0, 0, 109, 111, 5, 22, 0, 0, 110, 109, 1, 0, 0, 0, 110, 111, 1, 0, 0, 0, 111, 112, 1, 0, 0, 0, 112, 113, 5, 17, 0, 0, 113, 114, 5, 23, 0, 0, 114, 115, 5, 18, 0, 0, 115, 19, 1, 0, 0, 0, 116, 117, 5, 15, 0, 0, 117, 118, 3, 6, 3, 0, 118, 120, 5, 16, 0, 0, 119, 121, 3, 30, 15, 0, 120, 119, 1, 0, 0, 0, 120, 121, 1, 0, 0, 0, 121, 21, 1, 0, 0, 0, 122, 124, 3, 32, 16, 0, 123, 125, 3, 30, 15, 0, 124, 123, 1, 0, 0, 0, 124, 125, 1, 0, 0, 0, 125, 23, 1, 0, 0, 0, 126, 128, 5, 23, 0, 0, 127, 129, 3, 30, 15, 0, 128, 127, 1, 0, 0, 0, 128, 129, 1, 0, 0, 0, 129, 25, 1, 0, 0, 0, 130, 135, 3, 18, 9, 0, 131, 135, 3, 20, 10, 0, 132, 135, 3, 22, 11, 0, 133, 135, 3, 24, 12, 0, 134, 130, 1, 0, 0, 0, 134, 131, 1, 0, 0, 0, 134, 132, 1, 0, 0, 0, 134, 133, 1, 0, 0, 0, 135, 27, 1, 0, 0, 0, 136, 141, 5, 23, 0, 0, 137, 138, 5, 21, 0, 0, 138, 140, 5, 23, 0, 0, 139, 137, 1, 0, 0, 0, 140, 143, 1, 0, 0, 0, 141, 139, 1, 0, 0, 0, 141, 142, 1, 0, 0, 0, 142, 29, 1, 0, 0, 0, 143, 141, 1, 0, 0, 0, 144, 145, 7, 2, 0, 0, 145, 31, 1, 0, 0, 0, 146, 147, 7, 3, 0, 0, 147, 33, 1, 0, 0, 0, 16, 37, 43, 49, 53, 58, 63, 76, 82, 92, 98, 110, 120, 124, 128, 134, 141]
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
T__0=1
2+
T__1=2
3+
T__2=3
4+
T__3=4
5+
T__4=5
6+
T__5=6
7+
T__6=7
8+
ASSIGN=8
9+
ADD_ASSIGN=9
10+
BOOL_ASSIGN=10
11+
PIPE=11
12+
COLON=12
13+
SEMICOLON=13
14+
COMMA=14
15+
LPAREN=15
16+
RPAREN=16
17+
LBRACK=17
18+
RBRACK=18
19+
LBRACE=19
20+
RBRACE=20
21+
DOT=21
22+
TILDE=22
23+
ID=23
24+
INT=24
25+
STRING=25
26+
COMMENT=26
27+
WS=27
28+
CONTINUATION=28
29+
NL=29
30+
'*'=1
31+
'+'=2
32+
'?'=3
33+
'true'=4
34+
'false'=5
35+
'this'=6
36+
'[QualifiedName]'=7
37+
'+='=9
38+
'?='=10
39+
'|'=11
40+
':'=12
41+
';'=13
42+
','=14
43+
'('=15
44+
')'=16
45+
'['=17
46+
']'=18
47+
'{'=19
48+
'}'=20
49+
'.'=21
50+
'~'=22

0 commit comments

Comments
 (0)