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