Skip to content

Commit 0593bcb

Browse files
committed
Create an option for removing overridden rules #7
1 parent 13e1c53 commit 0593bcb

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

io.typefox.xtext2langium.tests/src/io/typefox/xtext2langium/tests/Xtext2LangiumMultiLangTest.xtend

+29-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,34 @@ class Xtext2LangiumMultiLangTest extends AbstractXtext2LangiumTest {
4040
terminal ID returns string:'^'? ('a' ..'z' | 'A' ..'Z' | '_' )('a' ..'z' | 'A' ..'Z' | '_' | '0' ..'9' )* ;
4141
terminal INT returns number:'0' ..'9' +;
4242
''')
43+
assertGeneratedFile('Terminals.langium', '''
44+
45+
terminal ID returns string:'^'? ('a' ..'z' | 'A' ..'Z' | '_' )('a' ..'z' | 'A' ..'Z' | '_' | '0' ..'9' )* ;
46+
terminal INT returns number:'0' ..'9' +;
47+
terminal STRING returns string:'"' ('\\' . | !('\\' | '"' ))*'"' | "'" ('\\' . | !('\\' | "'" ))*"'" ;
48+
hidden terminal ML_COMMENT returns string:'/*' -> '*/' ;
49+
hidden terminal SL_COMMENT returns string:'//' !('\n' | '\r' )('\r'? '\n' )? ;
50+
hidden terminal WS returns string:(' ' | '\t' | '\r' | '\n' )+;
51+
terminal ANY_OTHER returns string:.;
52+
''')
53+
}
54+
@Test
55+
def void testTerminals_02() {
56+
'''
57+
grammar io.typefox.xtext2langium.Test with org.eclipse.xtext.common.Terminals
58+
generate xtext2langiumTest 'http://Xtext2LangiumTest'
59+
import "http://www.eclipse.org/emf/2002/Ecore" as ecore
60+
61+
@Override
62+
terminal ID: '^'?('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*;
63+
@Override
64+
terminal INT returns ecore::EInt: ('0'..'9')+;
65+
'''.assertGeneratedLangium('''
66+
import 'Terminals'
67+
68+
terminal ID returns string:'^'? ('a' ..'z' | 'A' ..'Z' | '_' )('a' ..'z' | 'A' ..'Z' | '_' | '0' ..'9' )* ;
69+
terminal INT returns number:'0' ..'9' +;
70+
''', [removeOverridenRules = true])
4371
assertGeneratedFile('Terminals.langium', '''
4472

4573
terminal STRING returns string:'"' ('\\' . | !('\\' | '"' ))*'"' | "'" ('\\' . | !('\\' | "'" ))*"'" ;
@@ -85,7 +113,7 @@ class Xtext2LangiumMultiLangTest extends AbstractXtext2LangiumTest {
85113
greetings+=Greeting *
86114
;
87115

88-
''')
116+
''', [conf | conf.removeOverridenRules = true])
89117
assertGeneratedFile('uddl.langium', '''
90118
grammar Uddl
91119
import 'Terminals'

io.typefox.xtext2langium/src/io/typefox/xtext2langium/Xtext2LangiumFragment.xtend

+10-3
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,20 @@ class Xtext2LangiumFragment extends AbstractXtextGeneratorFragment {
7171
boolean useStringAsEnumRuleType = false
7272

7373
/**
74-
* If true, types from the ecore metamodel will also be generated.<br>
74+
* If <code>true</code>, types from the ecore metamodel will also be generated.<br>
7575
* If false, ecore data types will be replaced with Langium data types.
7676
* Types that are not convertable to Langium built in types will be generated as string.<br>
77-
* Default is false.<br>
77+
* Default is <code>false</code>.<br>
7878
*/
7979
@Accessors(PUBLIC_SETTER)
8080
boolean generateEcoreTypes = false
81+
/**
82+
* In case a rule from the imported grammar was overriden, Langium will report a duplicate error.
83+
* If <code>true</code>, the super grammar rules will be skipped in favor of the current grammar rules.<br>
84+
* Default is <code>false</code>.<br>
85+
*/
86+
@Accessors(PUBLIC_SETTER)
87+
boolean removeOverridenRules = false
8188

8289
static val INDENT = ' '
8390

@@ -117,7 +124,7 @@ class Xtext2LangiumFragment extends AbstractXtextGeneratorFragment {
117124
var entryRuleCreated = false
118125
for (rule : grammarToGenerate.rules.filter [ rule |
119126
/* Filter out rules that were overridden by the sub grammar */
120-
subGrammar === null || !subGrammar.rules.exists[name == rule.name]
127+
subGrammar === null || !removeOverridenRules || !subGrammar.rules.exists[name == rule.name]
121128
]) {
122129
if (rule.eClass === PARSER_RULE && !entryRuleCreated) {
123130
ctx.out.append('entry ')

0 commit comments

Comments
 (0)