source: default/trunk/de.ugoe.cs.swe.bnftools.ebnf.generator/src/extensions/SimpleBnf.ext @ 60

Last change on this file since 60 was 5, checked in by zeiss, 14 years ago
File size: 12.5 KB
RevLine 
[5]1import ebnf;
2import emf;
3
4create List transform(List s):
5        this.addAll(s.typeSelect(EtsiBnf).createEtsiBnf())
6;
7
8create EtsiBnf createEtsiBnf(EtsiBnf bnf):
9//      corebnf.addAll(bnf.corebnf)-> //TODO: checken, ob auskommentierung sinnvoll war
10        extRule.addAll(bnf.extRule)->
11        imports.addAll(bnf.imports)->
12        mergeRule.addAll(bnf.mergeRule)->
13//      package.addAll(bnf.package)-> //TODO: checken, ob auskommentierung sinnvoll war
14        rule.addAll(bnf.rule.createRule(this))->
15        setName(bnf.name)
16;
17
18create Rule createRule(Rule r, EtsiBnf bnf):
19        setName(r.name)->
20        r.definitionList.singleDefinition.size > 0 ? setDefinitionList(r.definitionList.createDefinitionList(bnf, this)) : null->
21        setRulenumber(r.rulenumber)->
22        setRulevariant(r.rulevariant)
23;
24
25create DefinitionList createDefinitionList(DefinitionList d, EtsiBnf bnf, Rule refRule):
26        setSingleDefinition(d.singleDefinition.createSingleDefinition(bnf, refRule))
27;
28
29create SingleDefinition createSingleDefinition(SingleDefinition s, EtsiBnf bnf, Rule refRule):
30        let termsList = {}:
31        s.terms.processTerms(termsList, bnf, refRule)->
32        setTerms(termsList)
33;
34
35create SingleDefinition createSingleDefinitionLeftRepetition(SingleDefinition s, Rule repeatRule, EtsiBnf bnf, Rule refRule, String id):
36        let termsList = {}:
37        termsList.add(createRuleReferenceTerm(repeatRule, id))->
38        s.terms.processTerms(termsList, bnf, refRule)->
39        setTerms(termsList)
40;
41
42create Atom createAtom(Atom a, EtsiBnf bnf, Rule refRule):
43        a.atomStringRule != null ? setAtomStringRule(a.atomStringRule) : null->
44        a.atomRuleReference != null ? setAtomRuleReference(a.atomRuleReference) : null
45;
46
47create GroupedSequence createGroupedSequence(GroupedSequence g, EtsiBnf bnf, Rule refRule):
48        setDefinitionList(g.definitionList)
49;
50
51create OptionalSequence createOptionalSequence(OptionalSequence g, EtsiBnf bnf, Rule refRule):
52        setDefinitionList(g.definitionList)
53;
54
55create RepeatedSequence createRepeatedSequence(RepeatedSequence r, EtsiBnf bnf, Rule refRule):
56        setDefinitions(r.definitions)->
57        setMorethanonce(r.morethanonce)
58;
59
60// ----------------------------------------------------------------------------------------------------
61// non-create rules
62// ----------------------------------------------------------------------------------------------------
63
64Term processTerms(Term t, List termsList, EtsiBnf bnf, Rule refRule):
65        let newTerm = new Term:
66        t.termAtom != null ? newTerm.setTermAtom(t.termAtom.createAtom(bnf, refRule)) : null->
67        t.termAtom != null ? termsList.add(newTerm) : null->
68        t.termGroupedSequence != null ? processGroupedSequence(t.termGroupedSequence, bnf, refRule, termsList) : null->
69        t.termOptionalSequence != null ? processOptionalSequence(t.termOptionalSequence, bnf, refRule, termsList) : null->
70        t.termRepeatedSequence != null ? processRepeatedSequence(t.termRepeatedSequence, bnf, refRule, termsList) : null
71;
72
73GroupedSequence processGroupedSequence(GroupedSequence o, EtsiBnf bnf, Rule refRule, List termsList):
74        listToHash(o.definitionList).existsRule() ? insertRuleReferenceInTermsList(getReplacementRule(refRule, o, bnf), termsList) : insertRuleReferenceInTermsList(addGroupedSequenceReplacementRule(refRule, o, bnf), termsList)
75;
76
77OptionalSequence processOptionalSequence(OptionalSequence o, EtsiBnf bnf, Rule refRule, List termsList):
78        listToHash(o.definitionList).existsRule() ? insertRuleReferenceInTermsList(getReplacementRule(refRule, o, bnf), termsList) : insertRuleReferenceInTermsList(addOptionalSequenceReplacementRule(refRule, o, bnf), termsList)
79;
80
81OptionalSequence processRepeatedSequence(RepeatedSequence o, EtsiBnf bnf, Rule refRule, List termsList):
82        listToHash(o.definitions).existsRule() ? insertRuleReferenceInTermsList(getReplacementRule(refRule, o, bnf), termsList) : insertRuleReferenceInTermsList(addRepeatedSequenceReplacementRule(refRule, o, bnf), termsList)
83;
84
85Rule getReplacementRule(Rule superRule, GroupedSequence o, EtsiBnf bnf):
86        getRule(listToHash(o.definitionList))
87;
88
89Rule getReplacementRule(Rule superRule, OptionalSequence o, EtsiBnf bnf):
90        getRule(listToHash(o.definitionList))
91;
92
93Rule getReplacementRule(Rule superRule, RepeatedSequence o, EtsiBnf bnf):
94        getRule(listToHash(o.definitions))
95;
96
97Rule addRepeatedSequenceReplacementRule(Rule superRule, RepeatedSequence o, EtsiBnf bnf):
98        let repetitionRule = addRepeatedSequenceReplacementRuleWithRepetition(superRule, o, bnf):
99        let plainRule = addRepeatedSequenceReplacementRuleWithoutRepetition(superRule, o, bnf):
100        let mainRule = addRepeatedSequenceReplacementRuleMain(superRule, plainRule, repetitionRule, o, bnf):
101        let hashValue = listToHash(o.definitions):
102        o.morethanonce ? addMoreThanOnceRepetitionRules(repetitionRule, plainRule, mainRule, bnf, hashValue): addNormalRepetitionRule(repetitionRule, bnf, hashValue)
103;
104
105Rule addNormalRepetitionRule(Rule repetitionRule, EtsiBnf bnf, String hashValue):
106        bnf.rule.add(repetitionRule)->
107        storeRule(hashValue, repetitionRule)->
108        repetitionRule
109;
110
111Rule addMoreThanOnceRepetitionRules(Rule repetitionRule, Rule plainRule, Rule mainRule, EtsiBnf bnf, String hashValue):
112        bnf.rule.add(repetitionRule)->
113        bnf.rule.add(plainRule)->
114        bnf.rule.add(mainRule)->
115        storeRule(hashValue, mainRule)->
116        mainRule
117;
118
119create Rule addRepeatedSequenceReplacementRuleWithRepetition(Rule superRule, RepeatedSequence o, EtsiBnf bnf):
120        let emptySingleDefinition = new SingleDefinition:
121        let newDefinitionList = new DefinitionList:
122        let singleDefinitions = {}:
123        let hashValue = listToHash(o.definitions):
124        let rsCopy = new RepeatedSequence:
125        rsCopy.setDefinitions(copyEObjectList(o.definitions))->
126        rsCopy.setMorethanonce(o.morethanonce)->
127        setName(superRule.name + "_gen_" + hashValue + "_rep")->
128        singleDefinitions.addAll(rsCopy.definitions.singleDefinition.createSingleDefinitionLeftRepetition(this, bnf, superRule, this.name))->
129        singleDefinitions.add(emptySingleDefinition)->
130        newDefinitionList.setSingleDefinition(singleDefinitions)->
131        setDefinitionList(newDefinitionList)
132;
133
134create Rule addRepeatedSequenceReplacementRuleWithoutRepetition(Rule superRule, RepeatedSequence o, EtsiBnf bnf):
135        let singleDefinitions = {}:
136        let newDefinitionList = new DefinitionList:
137        let hashValue = listToHash(o.definitions):
138        let rsCopy = new RepeatedSequence:
139        rsCopy.setDefinitions(copyEObjectList(o.definitions))->
140        rsCopy.setMorethanonce(o.morethanonce)->
141        setName(superRule.name + "_gen_" + hashValue + "_plain")->
142        singleDefinitions.addAll(rsCopy.definitions.singleDefinition.createSingleDefinition(bnf, superRule))->
143        newDefinitionList.setSingleDefinition(singleDefinitions)->
144        setDefinitionList(newDefinitionList)
145;
146
147create Rule this addRepeatedSequenceReplacementRuleMain(Rule superRule, Rule plainRule, Rule repetitionRule, RepeatedSequence o, EtsiBnf bnf):
148        let newDefinitionList = new DefinitionList:
149        let singleDefinitionList = {}:
150        let singleDefinition = new SingleDefinition:
151        let definitionsList = {}:
152        let hashValue = listToHash(o.definitions):
153        this.setName(superRule.name + "_gen_" + hashValue + "_main")->
154        singleDefinition.terms.add(plainRule.createRuleReferenceTerm(this.name))->
155        singleDefinition.terms.add(repetitionRule.createRuleReferenceTerm(this.name))->
156        newDefinitionList.singleDefinition.add(singleDefinition)->     
157        this.setDefinitionList(newDefinitionList)       
158;
159
160create Term this createRuleReferenceTerm(Rule rule, String id):
161        let newAtom = new Atom:
162        let newRuleReference = new RuleReference:
163        newRuleReference.setRuleref(rule)->
164        newAtom.setAtomRuleReference(newRuleReference)->
165        this.setTermAtom(newAtom)       
166;
167
168create SingleDefinition createRuleReferenceSingleDefinition(Rule rule, String id):
169        let newTerms = {}:
170        newTerms.add(createRuleReferenceTerm(rule, id))->
171        setTerms(newTerms)     
172;
173
174create Rule addGroupedSequenceReplacementRule(Rule superRule, GroupedSequence o, EtsiBnf bnf):
175        let newDefinitionList = new DefinitionList:
176        let singleDefinitions = {}:
177        let hashValue = listToHash(o.definitionList):
178        setName(superRule.name + "_gen_" + hashValue + "_group")->
179        singleDefinitions.addAll(o.definitionList.singleDefinition.createSingleDefinition(bnf, superRule))->
180        newDefinitionList.setSingleDefinition(singleDefinitions)->
181        setDefinitionList(newDefinitionList)-> 
182        storeRule(hashValue, this)->
183        bnf.rule.add(this)
184;
185
186create Rule addOptionalSequenceReplacementRule(Rule superRule, OptionalSequence o, EtsiBnf bnf):
187        let newDefinitionList = new DefinitionList:
188        let emptySingleDefinition = new SingleDefinition:
189        let singleDefinitions = {}:
190        let hashValue = listToHash(o.definitionList):
191        setName(superRule.name + "_gen_" + hashValue + "_opt")->
192        singleDefinitions.addAll(o.definitionList.singleDefinition.createSingleDefinition(bnf, superRule))->
193        singleDefinitions.add(emptySingleDefinition)->
194        newDefinitionList.setSingleDefinition(singleDefinitions)->
195        setDefinitionList(newDefinitionList)-> 
196        storeRule(hashValue, this)->
197        bnf.rule.add(this)
198;
199
200Void insertRuleReferenceInTermsList(Rule refRule, List termsList):
201        let newTerm = new Term:
202        let newRuleRef = new RuleReference:
203        let newAtom = new Atom:
204        newRuleRef.setRuleref(refRule)->
205        newAtom.setAtomRuleReference(newRuleRef)->
206        newTerm.setTermAtom(newAtom)->
207        termsList.add(newTerm)
208;
209
210// ----------------------------------------------------------------------------------------------------
211// Java Extensions
212// ----------------------------------------------------------------------------------------------------
213
214Void dump(String s):
215        JAVA de.ugoe.cs.swe.bnftools.ebnf.javaextensions.SimpleBNFHelper.dump(java.lang.String)
216;
217
218String definitionListToString(DefinitionList l):
219        JAVA de.ugoe.cs.swe.bnftools.ebnf.javaextensions.SimpleBNFHelper.definitionListToString(de.ugoe.cs.swe.bnftools.ebnf.DefinitionList)
220;
221
222String listToString(List l):
223        JAVA de.ugoe.cs.swe.bnftools.ebnf.javaextensions.SimpleBNFHelper.listToString(java.util.List)
224;
225
226String listToString(EObject l):
227        JAVA de.ugoe.cs.swe.bnftools.ebnf.javaextensions.SimpleBNFHelper.eobjectToString(org.eclipse.emf.ecore.EObject)
228;
229
230String listToHash(List l):
231        JAVA de.ugoe.cs.swe.bnftools.ebnf.javaextensions.SimpleBNFHelper.listToHash(java.util.List)
232;
233
234Void storeRule(String k, Rule r):
235        JAVA de.ugoe.cs.swe.bnftools.ebnf.javaextensions.SimpleBNFHelper.storeRule(java.lang.String, de.ugoe.cs.swe.bnftools.ebnf.Rule)
236;
237
238Boolean existsRule(String s):
239        JAVA de.ugoe.cs.swe.bnftools.ebnf.javaextensions.SimpleBNFHelper.existsRule(java.lang.String)
240;
241
242Rule getRule(String s):
243        JAVA de.ugoe.cs.swe.bnftools.ebnf.javaextensions.SimpleBNFHelper.getRule(java.lang.String)
244;
245
246EObject copyEObject(EObject o):
247        JAVA de.ugoe.cs.swe.bnftools.ebnf.javaextensions.SimpleBNFHelper.copyEObject(org.eclipse.emf.ecore.EObject)
248;
249
250List copyEObjectList(List l):
251        JAVA de.ugoe.cs.swe.bnftools.ebnf.javaextensions.SimpleBNFHelper.copyEObjectList(java.util.List)
252;
253
254// ----------------------------------------------------------------------------------------------------
255// duplication rules
256// ----------------------------------------------------------------------------------------------------
257
258create DefinitionList duplicateDefinitionList(DefinitionList d, EtsiBnf bnf, Rule refRule):
259        setSingleDefinition(d.singleDefinition.duplicateSingleDefinition(bnf, refRule))
260;
261
262create SingleDefinition duplicateSingleDefinition(SingleDefinition s, EtsiBnf bnf, Rule refRule):
263        setTerms(s.terms.duplicateTerms(bnf, refRule))
264;
265
266create Term duplicateTerms(Term t, EtsiBnf bnf, Rule refRule):
267        t.termAtom != null ? setTermAtom(t.termAtom.duplicateAtom(bnf, refRule)) : null->
268        t.termGroupedSequence != null ? setTermGroupedSequence(t.termGroupedSequence.duplicateGroupedSequence(bnf, refRule)) : null->
269        t.termOptionalSequence != null ? setTermOptionalSequence(t.termOptionalSequence.duplicateOptionalSequence(bnf, refRule)) : null->
270        t.termRepeatedSequence != null ? setTermRepeatedSequence(t.termRepeatedSequence.duplicateRepeatedSequence(bnf, refRule)) : null
271;
272
273create GroupedSequence duplicateGroupedSequence(GroupedSequence g, EtsiBnf bnf, Rule refRule):
274        setDefinitionList(g.definitionList)
275;
276
277create OptionalSequence duplicateOptionalSequence(OptionalSequence g, EtsiBnf bnf, Rule refRule):
278        setDefinitionList(g.definitionList)
279;
280
281create RepeatedSequence duplicateRepeatedSequence(RepeatedSequence r, EtsiBnf bnf, Rule refRule):
282        setDefinitions(r.definitions)->
283        setMorethanonce(r.morethanonce)
284;
285
286create Atom duplicateAtom(Atom a, EtsiBnf bnf, Rule refRule):
287        a.atomStringRule != null ? setAtomStringRule(a.atomStringRule) : null->
288        a.atomRuleReference != null ? setAtomRuleReference(a.atomRuleReference) : null
289;
290
291// ----------------------------------------------------------------------------------------------------
292// Extensions for templates
293// ----------------------------------------------------------------------------------------------------
294
295Rule firstNonGeneratedRule(EtsiBnf bnf):
296        bnf.rule.selectFirst(e|!e.name.contains("_gen_") )
297;
Note: See TracBrowser for help on using the repository browser.