Skip to content

Commit

Permalink
Create an option for removing overridden rules #7
Browse files Browse the repository at this point in the history
  • Loading branch information
dhuebner committed Feb 10, 2023
1 parent 13e1c53 commit 0593bcb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,34 @@ class Xtext2LangiumMultiLangTest extends AbstractXtext2LangiumTest {
terminal ID returns string:'^'? ('a' ..'z' | 'A' ..'Z' | '_' )('a' ..'z' | 'A' ..'Z' | '_' | '0' ..'9' )* ;
terminal INT returns number:'0' ..'9' +;
''')
assertGeneratedFile('Terminals.langium', '''

terminal ID returns string:'^'? ('a' ..'z' | 'A' ..'Z' | '_' )('a' ..'z' | 'A' ..'Z' | '_' | '0' ..'9' )* ;
terminal INT returns number:'0' ..'9' +;
terminal STRING returns string:'"' ('\\' . | !('\\' | '"' ))*'"' | "'" ('\\' . | !('\\' | "'" ))*"'" ;
hidden terminal ML_COMMENT returns string:'/*' -> '*/' ;
hidden terminal SL_COMMENT returns string:'//' !('\n' | '\r' )('\r'? '\n' )? ;
hidden terminal WS returns string:(' ' | '\t' | '\r' | '\n' )+;
terminal ANY_OTHER returns string:.;
''')
}
@Test
def void testTerminals_02() {
'''
grammar io.typefox.xtext2langium.Test with org.eclipse.xtext.common.Terminals
generate xtext2langiumTest 'http://Xtext2LangiumTest'
import "http://www.eclipse.org/emf/2002/Ecore" as ecore

@Override
terminal ID: '^'?('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*;
@Override
terminal INT returns ecore::EInt: ('0'..'9')+;
'''.assertGeneratedLangium('''
import 'Terminals'

terminal ID returns string:'^'? ('a' ..'z' | 'A' ..'Z' | '_' )('a' ..'z' | 'A' ..'Z' | '_' | '0' ..'9' )* ;
terminal INT returns number:'0' ..'9' +;
''', [removeOverridenRules = true])
assertGeneratedFile('Terminals.langium', '''

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

''')
''', [conf | conf.removeOverridenRules = true])
assertGeneratedFile('uddl.langium', '''
grammar Uddl
import 'Terminals'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,20 @@ class Xtext2LangiumFragment extends AbstractXtextGeneratorFragment {
boolean useStringAsEnumRuleType = false

/**
* If true, types from the ecore metamodel will also be generated.<br>
* If <code>true</code>, types from the ecore metamodel will also be generated.<br>
* If false, ecore data types will be replaced with Langium data types.
* Types that are not convertable to Langium built in types will be generated as string.<br>
* Default is false.<br>
* Default is <code>false</code>.<br>
*/
@Accessors(PUBLIC_SETTER)
boolean generateEcoreTypes = false
/**
* In case a rule from the imported grammar was overriden, Langium will report a duplicate error.
* If <code>true</code>, the super grammar rules will be skipped in favor of the current grammar rules.<br>
* Default is <code>false</code>.<br>
*/
@Accessors(PUBLIC_SETTER)
boolean removeOverridenRules = false

static val INDENT = ' '

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

0 comments on commit 0593bcb

Please sign in to comment.