| 1 | /* |
|---|
| 2 | * generated by Xtext |
|---|
| 3 | */
|
|---|
| 4 | package de.ugoe.cs.swe.bnftools.generator
|
|---|
| 5 |
|
|---|
| 6 | import de.ugoe.cs.swe.bnftools.ebnf.Atom
|
|---|
| 7 | import de.ugoe.cs.swe.bnftools.ebnf.BnfEntry
|
|---|
| 8 | import de.ugoe.cs.swe.bnftools.ebnf.DefinitionList
|
|---|
| 9 | import de.ugoe.cs.swe.bnftools.ebnf.EtsiBnf
|
|---|
| 10 | import de.ugoe.cs.swe.bnftools.ebnf.GroupedSequence
|
|---|
| 11 | import de.ugoe.cs.swe.bnftools.ebnf.OptionalSequence
|
|---|
| 12 | import de.ugoe.cs.swe.bnftools.ebnf.RepeatedSequence
|
|---|
| 13 | import de.ugoe.cs.swe.bnftools.ebnf.Rule
|
|---|
| 14 | import de.ugoe.cs.swe.bnftools.ebnf.RuleReference
|
|---|
| 15 | import de.ugoe.cs.swe.bnftools.ebnf.SingleDefinition
|
|---|
| 16 | import de.ugoe.cs.swe.bnftools.ebnf.StringRule
|
|---|
| 17 | import de.ugoe.cs.swe.bnftools.ebnf.Term
|
|---|
| 18 | import java.io.File
|
|---|
| 19 | import org.eclipse.emf.ecore.resource.Resource
|
|---|
| 20 | import org.eclipse.xtext.generator.IFileSystemAccess
|
|---|
| 21 | import org.eclipse.xtext.generator.IFileSystemAccessExtension2
|
|---|
| 22 | import org.eclipse.xtext.generator.IGenerator
|
|---|
| 23 | import de.ugoe.cs.swe.bnftools.ebnf.SectionHeading
import org.eclipse.core.resources.ResourcesPlugin |
|---|
| 24 | |
|---|
| 25 | /** |
|---|
| 26 | * Generates code from your model files on save. |
|---|
| 27 | * |
|---|
| 28 | * see http://www.eclipse.org/Xtext/documentation.html#TutorialCodeGeneration |
|---|
| 29 | */
|
|---|
| 30 | class EbnfGenerator implements IGenerator {
|
|---|
| 31 |
|
|---|
| 32 | |
|---|
| 33 | def void doGenerate(Resource resource, IFileSystemAccess fsa,boolean mode) { |
|---|
| 34 | var String workspacePath = WorkspaceResolver.getWorkspace(); |
|---|
| 35 | for (e : resource.allContents.toIterable.filter(EtsiBnf)) { |
|---|
| 36 | if (e.bnfEntry.size != 0) { |
|---|
| 37 | fsa.generateFile(e.name + ".fo", e.compile) |
|---|
| 38 | |
|---|
| 39 | //generate pdf |
|---|
| 40 | var uri = (fsa as IFileSystemAccessExtension2).getURI(e.name + ".fo"); |
|---|
| 41 | var String relativePath = uri.path.substring(10); |
|---|
| 42 | var String realPath = ResourcesPlugin.getWorkspace().root.findMember(relativePath).rawLocation.toOSString; |
|---|
| 43 | var File file = new File(realPath); |
|---|
| 44 | if (file.exists) { |
|---|
| 45 | //true -> pdf, false -> rtf |
|---|
| 46 | |
|---|
| 47 | if(mode){ |
|---|
| 48 | FoToPdfOrRtf.createRtfFromFo(realPath.substring(0, realPath.length - 3)); |
|---|
| 49 | }else{ |
|---|
| 50 | FoToPdfOrRtf.createPdfFromFo(realPath.substring(0, realPath.length - 3)); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | // fsa.deleteFile(e.name + ".fo"); |
|---|
| 54 | } |
|---|
| 55 | } |
|---|
| 56 | } |
|---|
| 57 | } |
|---|
| 58 |
|
|---|
| 59 | override void doGenerate(Resource resource, IFileSystemAccess fsa) {
|
|---|
| 60 | var String workspacePath = WorkspaceResolver.getWorkspace();
|
|---|
| 61 | for (e : resource.allContents.toIterable.filter(EtsiBnf)) {
|
|---|
| 62 | if (e.bnfEntry.size != 0) {
|
|---|
| 63 | fsa.generateFile(e.name + ".fo", e.compile)
|
|---|
| 64 | System.out.println("fo generated");
|
|---|
| 65 | //generate pdf |
|---|
| 66 | var uri = (fsa as IFileSystemAccessExtension2).getURI(e.name + ".fo");
|
|---|
| 67 | var String fullUri = workspacePath + uri.path.substring(10, uri.path.length);
|
|---|
| 68 | var File file = new File(fullUri);
|
|---|
| 69 |
|
|---|
| 70 | if (file.exists) { |
|---|
| 71 | |
|---|
| 72 | FoToPdfOrRtf.createPdfFromFo(fullUri.substring(0, fullUri.length - 3)); |
|---|
| 73 | // fsa.deleteFile(e.name + ".fo");
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 | }
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | // compile the different entities of the Bnf-----------------------------------------------------------------------------
|
|---|
| 80 | def compile(EtsiBnf bnf) '''
|
|---|
| 81 | <?xml version="1.0"?> |
|---|
| 82 | <!-- fop «bnf.name».xml -rtf «bnf.name».rtf --> |
|---|
| 83 | <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> |
|---|
| 84 | |
|---|
| 85 | <fo:layout-master-set> |
|---|
| 86 | <fo:simple-page-master master-name="A4" page-width="210mm" page-height="297mm"> |
|---|
| 87 | <fo:region-body region-name="xsl-region-body" margin="2cm"/> |
|---|
| 88 | </fo:simple-page-master> |
|---|
| 89 | </fo:layout-master-set> |
|---|
| 90 | |
|---|
| 91 | <fo:page-sequence master-reference="A4"> |
|---|
| 92 | <fo:flow flow-name="xsl-region-body" font-family="Monospaced" font-size="8pt"> |
|---|
| 93 | |
|---|
| 94 | <fo:block span="all" text-align="center" text-indent="1em" font-family="Monospaced" font-size="20pt" font-weight="bold" background-color="#EEEEEE" line-height="20mm">«bnf.
|
|---|
| 95 | name»</fo:block> |
|---|
| 96 | |
|---|
| 97 | «FOR bnfentry : bnf.bnfEntry»
|
|---|
| 98 | «bnfentry.compile» |
|---|
| 99 | «ENDFOR»
|
|---|
| 100 | </fo:flow> |
|---|
| 101 | </fo:page-sequence> |
|---|
| 102 | </fo:root>'''
|
|---|
| 103 |
|
|---|
| 104 | def compile(BnfEntry bnfEntry) '''
|
|---|
| 105 | «IF bnfEntry.rule != null»
|
|---|
| 106 | «bnfEntry.rule.compile» |
|---|
| 107 | «ENDIF» |
|---|
| 108 | «IF bnfEntry.sectionheader!= null» |
|---|
| 109 | «bnfEntry.sectionheader.compile» |
|---|
| 110 | «ENDIF»
|
|---|
| 111 | ''' |
|---|
| 112 | |
|---|
| 113 | def compile(SectionHeading sHeading) ''' |
|---|
| 114 | |
|---|
| 115 | <fo:block role="H1" font-family="SansSerif" font-size="12pt" line-height="2em"> «sHeading.sectionHeader» </fo:block> |
|---|
| 116 | |
|---|
| 117 | '''
|
|---|
| 118 |
|
|---|
| 119 | def compile(Rule rule) '''<fo:block><fo:inline id="«rule.name»" color="black">«IF rule.rulenumber != 0»«rule.
|
|---|
| 120 | rulenumber». «ENDIF»«rule.name»</fo:inline> ::= «rule.definitionList.compile»</fo:block>'''
|
|---|
| 121 |
|
|---|
| 122 | def compile(DefinitionList dList) '''«IF dList!= null»«FOR sDef : dList.singleDefinition»«sDef.compile+" "»«IF !sDef.equals(
|
|---|
| 123 | dList.singleDefinition.last)»| «ENDIF»«ENDFOR»«ENDIF»'''
|
|---|
| 124 |
|
|---|
| 125 | def compile(SingleDefinition sDefinition) '''«FOR term : sDefinition.terms»«term.compile»«ENDFOR»'''
|
|---|
| 126 |
|
|---|
| 127 | def compile(Term term) '''«IF term.termAtom != null»«term.termAtom.compile»«ENDIF»«IF term.termOptionalSequence !=
|
|---|
| 128 | null»«term.termOptionalSequence.compile»«ENDIF»«IF term.termRepeatedSequence != null»«term.termRepeatedSequence.
|
|---|
| 129 | compile»«ENDIF»«IF term.termGroupedSequence != null»«term.termGroupedSequence.compile»«ENDIF» '''
|
|---|
| 130 |
|
|---|
| 131 | def compile(GroupedSequence gSequ) '''( «FOR d : gSequ.definitionList»«d.compile+" "»«ENDFOR» )'''
|
|---|
| 132 |
|
|---|
| 133 | def compile(RepeatedSequence rSequ) '''{ «FOR d : rSequ.definitions»«d.compile+" "»«ENDFOR» }«IF rSequ.morethanonce»+«ENDIF»'''
|
|---|
| 134 |
|
|---|
| 135 | def compile(OptionalSequence oSequ) '''[ «FOR d : oSequ.definitionList»«d.compile+" "»«ENDFOR» ]'''
|
|---|
| 136 |
|
|---|
| 137 | def compile(Atom atom) '''
|
|---|
| 138 | «IF atom.atomRuleReference != null»«atom.atomRuleReference.compile»«ENDIF»«IF atom.atomStringRule != null»«atom.
|
|---|
| 139 | atomStringRule.compile»«ENDIF» '''
|
|---|
| 140 |
|
|---|
| 141 | def compile(RuleReference rRef) '''<fo:basic-link internal-destination="«rRef.ruleref.name»" text-decoration="underline" color="blue">«rRef.
|
|---|
| 142 | ruleref.name»</fo:basic-link> '''
|
|---|
| 143 |
|
|---|
| 144 | def compile(StringRule sRule) '''«IF sRule.colon != null»«sRule.colon»«ENDIF»«IF sRule.literal != null»«'"'+sRule.
|
|---|
| 145 | literal.trim.replaceAll("&","&").replaceAll("<","<").replaceAll(">",">")+'"'» «ENDIF»'''
|
|---|
| 146 |
|
|---|
| 147 | }
|
|---|