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 | import de.ugoe.cs.swe.bnftools.ebnf.RepeatRange |
---|
25 | |
---|
26 | /** |
---|
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 |
|
---|
33 | |
---|
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); |
---|
43 | var String realPath = ResourcesPlugin.getWorkspace().root.findMember(relativePath).rawLocation.toOSString; |
---|
44 | var File file = new File(realPath); |
---|
45 | if (file.exists) { |
---|
46 | //true -> pdf, false -> rtf |
---|
47 | |
---|
48 | if(mode){ |
---|
49 | FoToPdfOrRtf.createRtfFromFo(realPath.substring(0, realPath.length - 3)); |
---|
50 | }else{ |
---|
51 | FoToPdfOrRtf.createPdfFromFo(realPath.substring(0, realPath.length - 3)); |
---|
52 | } |
---|
53 | |
---|
54 | // fsa.deleteFile(e.name + ".fo"); |
---|
55 | } |
---|
56 | } |
---|
57 | } |
---|
58 | } |
---|
59 |
|
---|
60 | override void doGenerate(Resource resource, IFileSystemAccess fsa) {
|
---|
61 | var String workspacePath = WorkspaceResolver.getWorkspace();
|
---|
62 | for (e : resource.allContents.toIterable.filter(EtsiBnf)) {
|
---|
63 | if (e.bnfEntry.size != 0) {
|
---|
64 | fsa.generateFile(e.name + ".fo", e.compile)
|
---|
65 | System.out.println("fo generated");
|
---|
66 | //generate pdf |
---|
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) { |
---|
72 | |
---|
73 | FoToPdfOrRtf.createPdfFromFo(fullUri.substring(0, fullUri.length - 3)); |
---|
74 | // fsa.deleteFile(e.name + ".fo");
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | // compile the different entities of the Bnf-----------------------------------------------------------------------------
|
---|
81 | def compile(EtsiBnf bnf) '''
|
---|
82 | <?xml version="1.0"?> |
---|
83 | <!-- fop «bnf.name».xml -rtf «bnf.name».rtf --> |
---|
84 | <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> |
---|
85 | |
---|
86 | <fo:layout-master-set> |
---|
87 | <fo:simple-page-master master-name="A4" page-width="210mm" page-height="297mm"> |
---|
88 | <fo:region-body region-name="xsl-region-body" margin="2cm"/> |
---|
89 | </fo:simple-page-master> |
---|
90 | </fo:layout-master-set> |
---|
91 | |
---|
92 | <fo:page-sequence master-reference="A4"> |
---|
93 | <fo:flow flow-name="xsl-region-body" font-family="Monospaced" font-size="8pt"> |
---|
94 | |
---|
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.
|
---|
96 | name»</fo:block> |
---|
97 | |
---|
98 | «FOR bnfentry : bnf.bnfEntry»
|
---|
99 | «bnfentry.compile» |
---|
100 | «ENDFOR»
|
---|
101 | </fo:flow> |
---|
102 | </fo:page-sequence> |
---|
103 | </fo:root>'''
|
---|
104 |
|
---|
105 | def compile(BnfEntry bnfEntry) '''
|
---|
106 | «IF bnfEntry.rule != null»
|
---|
107 | «bnfEntry.rule.compile» |
---|
108 | «ENDIF» |
---|
109 | «IF bnfEntry.sectionheader!= null» |
---|
110 | «bnfEntry.sectionheader.compile» |
---|
111 | «ENDIF»
|
---|
112 | ''' |
---|
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> |
---|
117 | |
---|
118 | '''
|
---|
119 |
|
---|
120 | def compile(Rule rule) '''<fo:block><fo:inline id="«rule.name»" color="black">«IF rule.rulenumber != 0»«rule.
|
---|
121 | rulenumber». «ENDIF»«rule.name»</fo:inline> ::= «rule.definitionList.compile»</fo:block>'''
|
---|
122 |
|
---|
123 | def compile(DefinitionList dList) '''«IF dList!= null»«FOR sDef : dList.singleDefinition»«sDef.compile+" "»«IF !sDef.equals(
|
---|
124 | dList.singleDefinition.last)»| «ENDIF»«ENDFOR»«ENDIF»'''
|
---|
125 |
|
---|
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» '''
|
---|
131 |
|
---|
132 | def compile(GroupedSequence gSequ) '''( «FOR d : gSequ.definitionList»«d.compile+" "»«ENDFOR» )'''
|
---|
133 |
|
---|
134 | def compile(RepeatedSequence rSequ) '''{ «FOR d : rSequ.definitions»«d.compile+" "»«ENDFOR» }«IF rSequ.morethanonce»+«ENDIF»«IF rSequ.range!=null»«rSequ.range.compile»«ENDIF»'''
|
---|
135 | |
---|
136 | def compile(RepeatRange rRange) '''#(«rRange.from»,«rRange.to»)''' |
---|
137 |
|
---|
138 | def compile(OptionalSequence oSequ) '''[ «FOR d : oSequ.definitionList»«d.compile+" "»«ENDFOR» ]'''
|
---|
139 |
|
---|
140 | def compile(Atom atom) '''
|
---|
141 | «IF atom.atomRuleReference != null»«atom.atomRuleReference.compile»«ENDIF»«IF atom.atomStringRule != null»«atom.
|
---|
142 | atomStringRule.compile»«ENDIF» '''
|
---|
143 |
|
---|
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> '''
|
---|
146 |
|
---|
147 | def compile(StringRule sRule) '''«IF sRule.colon != null»«sRule.colon»«ENDIF»«IF sRule.literal != null»«'"'+sRule.
|
---|
148 | literal.trim.replaceAll("&","&").replaceAll("<","<").replaceAll(">",">")+'"'» «ENDIF»'''
|
---|
149 |
|
---|
150 | }
|
---|