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