/*
* generated by Xtext
*/
package de.ugoe.cs.swe.bnftools.generator
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.IGenerator
import org.eclipse.xtext.generator.IFileSystemAccess
import de.ugoe.cs.swe.bnftools.ebnf.EtsiBnf
import de.ugoe.cs.swe.bnftools.ebnf.BnfEntry
import de.ugoe.cs.swe.bnftools.ebnf.Rule
import de.ugoe.cs.swe.bnftools.ebnf.DefinitionList
import de.ugoe.cs.swe.bnftools.ebnf.SingleDefinition
import de.ugoe.cs.swe.bnftools.ebnf.Term
import de.ugoe.cs.swe.bnftools.ebnf.GroupedSequence
import de.ugoe.cs.swe.bnftools.ebnf.Atom
import de.ugoe.cs.swe.bnftools.ebnf.RepeatedSequence
import de.ugoe.cs.swe.bnftools.ebnf.OptionalSequence
import de.ugoe.cs.swe.bnftools.ebnf.RuleReference
import de.ugoe.cs.swe.bnftools.ebnf.StringRule
/**
* Generates code from your model files on save.
*
* see http://www.eclipse.org/Xtext/documentation.html#TutorialCodeGeneration
*/
class EbnfGenerator implements IGenerator {
//generation of the file
override void doGenerate(Resource resource, IFileSystemAccess fsa) {
for (e : resource.allContents.toIterable.filter(EtsiBnf)) {
if (e.bnfEntry.size!=0) {
fsa.generateFile(e.name + ".fo", e.compile)
}
}
}
// compile the different entities of the Bnf-----------------------------------------------------------------------------
def compile(EtsiBnf bnf)
'''
«FOR bnfentry:bnf.bnfEntry»
«bnfentry.compile»
«ENDFOR»
'''
def compile(BnfEntry bnfEntry)'''
«IF bnfEntry.rule!= null»
«bnfEntry.rule.compile»
«ENDIF»
'''
def compile(Rule rule)'''«rule.name» ::= «rule.definitionList.compile»'''
def compile(DefinitionList dList)'''«FOR sDef:dList.singleDefinition»«sDef.compile»«IF !sDef.equals(dList.singleDefinition.last)» | «ENDIF»«ENDFOR»'''
def compile(SingleDefinition sDefinition)'''«FOR term:sDefinition.terms»«term.compile»«ENDFOR»'''
def compile(Term term)'''«IF term.termAtom!=null»«term.termAtom.compile»«ENDIF»«IF term.termOptionalSequence!=null»«term.termOptionalSequence.compile»«ENDIF»«IF term.termRepeatedSequence!=null»«term.termRepeatedSequence.compile»«ENDIF»«IF term.termGroupedSequence!=null»«term.termGroupedSequence.compile»«ENDIF»'''
def compile(GroupedSequence gSequ)'''«FOR d :gSequ.definitionList»«d.compile»«ENDFOR»'''
def compile(RepeatedSequence rSequ)'''{«FOR d:rSequ.definitions»«d.compile»«ENDFOR»}«IF rSequ.morethanonce»+«ENDIF»'''
def compile(OptionalSequence oSequ)'''(«FOR d:oSequ.definitionList»«d.compile»«ENDFOR»)'''
def compile(Atom atom)'''
«IF atom.atomRuleReference!= null»«atom.atomRuleReference.compile»«ENDIF»«IF atom.atomStringRule!= null»«atom.atomStringRule.compile»«ENDIF»'''
def compile(RuleReference rRef)'''«rRef.ruleref.name»'''
def compile(StringRule sRule)'''«IF sRule.colon!=null»«sRule.colon»«ENDIF»«IF sRule.literal!=null»«sRule.literal»«ENDIF»'''
}