1 | /* |
---|
2 | * generated by Xtext |
---|
3 | */
|
---|
4 | package de.ugoe.cs.swe.bnftools.generator
|
---|
5 |
|
---|
6 | import org.eclipse.emf.ecore.resource.Resource
|
---|
7 | import org.eclipse.xtext.generator.IGenerator
|
---|
8 | import org.eclipse.xtext.generator.IFileSystemAccess
|
---|
9 | import de.ugoe.cs.swe.bnftools.ebnf.EtsiBnf
|
---|
10 | import de.ugoe.cs.swe.bnftools.ebnf.BnfEntry
|
---|
11 | import de.ugoe.cs.swe.bnftools.ebnf.Rule
|
---|
12 | import de.ugoe.cs.swe.bnftools.ebnf.DefinitionList
|
---|
13 | import de.ugoe.cs.swe.bnftools.ebnf.SingleDefinition
|
---|
14 | import de.ugoe.cs.swe.bnftools.ebnf.Term
|
---|
15 | import de.ugoe.cs.swe.bnftools.ebnf.GroupedSequence
|
---|
16 | import de.ugoe.cs.swe.bnftools.ebnf.Atom
|
---|
17 | import de.ugoe.cs.swe.bnftools.ebnf.RepeatedSequence
|
---|
18 | import de.ugoe.cs.swe.bnftools.ebnf.OptionalSequence
|
---|
19 | import de.ugoe.cs.swe.bnftools.ebnf.RuleReference
|
---|
20 | import de.ugoe.cs.swe.bnftools.ebnf.StringRule
|
---|
21 | import java.io.File
|
---|
22 | import org.eclipse.xtext.generator.IFileSystemAccessExtension2
|
---|
23 | import org.eclipse.emf.common.util.URI;
|
---|
24 | import org.eclipse.xtext.generator.IFileSystemAccessExtension
|
---|
25 | import java.io.PrintWriter
|
---|
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 | //generation of the xsl-file
|
---|
35 | override void doGenerate(Resource resource, IFileSystemAccess fsa) {
|
---|
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 fullUri = workspacePath + uri.path.substring(10, uri.path.length);
|
---|
44 | System.out.println(workspacePath + uri.path.substring(10, uri.path.length));
|
---|
45 | var File file = new File(fullUri);
|
---|
46 | System.out.println(file.exists+ "," + fullUri.substring(0, fullUri.length - 3));
|
---|
47 |
|
---|
48 | if (file.exists) {
|
---|
49 | foToPdf.createPdfFromFo(fullUri.substring(0, fullUri.length - 3));
|
---|
50 | }
|
---|
51 | }
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | // compile the different entities of the Bnf-----------------------------------------------------------------------------
|
---|
56 | def compile(EtsiBnf bnf) '''
|
---|
57 | <?xml version="1.0"?> |
---|
58 | <!-- fop «bnf.name».xml -rtf «bnf.name».rtf --> |
---|
59 | <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> |
---|
60 | |
---|
61 | <fo:layout-master-set> |
---|
62 | <fo:simple-page-master master-name="A4" page-width="210mm" page-height="297mm"> |
---|
63 | <fo:region-body region-name="xsl-region-body" margin="2cm"/> |
---|
64 | </fo:simple-page-master> |
---|
65 | </fo:layout-master-set> |
---|
66 | |
---|
67 | <fo:page-sequence master-reference="A4"> |
---|
68 | <fo:flow flow-name="xsl-region-body" font-family="verdana"> |
---|
69 | |
---|
70 | <fo:block span="all" text-align="center" text-indent="1em" font-family="Arial" font-size="20pt" font-weight="bold" background-color="#EEEEEE" line-height="20mm">«bnf.
|
---|
71 | name»</fo:block> |
---|
72 | |
---|
73 | «FOR bnfentry : bnf.bnfEntry»
|
---|
74 | «bnfentry.compile» |
---|
75 | «ENDFOR»
|
---|
76 | </fo:flow> |
---|
77 | </fo:page-sequence> |
---|
78 | </fo:root>'''
|
---|
79 |
|
---|
80 | def compile(BnfEntry bnfEntry) '''
|
---|
81 | «IF bnfEntry.rule != null»
|
---|
82 | «bnfEntry.rule.compile» |
---|
83 | «ENDIF»
|
---|
84 | '''
|
---|
85 |
|
---|
86 | def compile(Rule rule) '''<fo:block><fo:inline id="«rule.name»" color="purple">«IF rule.rulenumber != 0»«rule.
|
---|
87 | rulenumber». «ENDIF»«rule.name»</fo:inline> ::= «rule.definitionList.compile»</fo:block>'''
|
---|
88 |
|
---|
89 | def compile(DefinitionList dList) '''«FOR sDef : dList.singleDefinition»«sDef.compile»«IF !sDef.equals(
|
---|
90 | dList.singleDefinition.last)» | «ENDIF»«ENDFOR»'''
|
---|
91 |
|
---|
92 | def compile(SingleDefinition sDefinition) '''«FOR term : sDefinition.terms»«term.compile»«ENDFOR»'''
|
---|
93 |
|
---|
94 | def compile(Term term) '''«IF term.termAtom != null»«term.termAtom.compile»«ENDIF»«IF term.termOptionalSequence !=
|
---|
95 | null»«term.termOptionalSequence.compile»«ENDIF»«IF term.termRepeatedSequence != null»«term.termRepeatedSequence.
|
---|
96 | compile»«ENDIF»«IF term.termGroupedSequence != null»«term.termGroupedSequence.compile»«ENDIF»'''
|
---|
97 |
|
---|
98 | def compile(GroupedSequence gSequ) '''«FOR d : gSequ.definitionList»«d.compile»«ENDFOR»'''
|
---|
99 |
|
---|
100 | def compile(RepeatedSequence rSequ) '''{«FOR d : rSequ.definitions»«d.compile»«ENDFOR»}«IF rSequ.morethanonce»+«ENDIF»'''
|
---|
101 |
|
---|
102 | def compile(OptionalSequence oSequ) '''(«FOR d : oSequ.definitionList»«d.compile»«ENDFOR»)'''
|
---|
103 |
|
---|
104 | def compile(Atom atom) '''
|
---|
105 | «IF atom.atomRuleReference != null»«atom.atomRuleReference.compile»«ENDIF»«IF atom.atomStringRule != null»«atom.
|
---|
106 | atomStringRule.compile»«ENDIF»'''
|
---|
107 |
|
---|
108 | def compile(RuleReference rRef) '''<fo:basic-link internal-destination="«rRef.ruleref.name»" text-decoration="underline" color="blue">«rRef.
|
---|
109 | ruleref.name»</fo:basic-link> '''
|
---|
110 |
|
---|
111 | def compile(StringRule sRule) '''«IF sRule.colon != null»«sRule.colon»«ENDIF»«IF sRule.literal != null»«sRule.
|
---|
112 | literal» «ENDIF»'''
|
---|
113 |
|
---|
114 | }
|
---|