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