blob: 7a68901fcf3aff085410ee19d434f4f69715e1d5 [file] [log] [blame]
Ed Willinka55dbe42015-02-24 21:49:23 +00001/*******************************************************************************
Ed Willinkb79677d2017-05-23 19:50:22 +01002 * Copyright (c) 2015, 2017 Willink Transformations and others.
Ed Willinka55dbe42015-02-24 21:49:23 +00003 * All rights reserved. This program and the accompanying materials
Ed Willink2b4d8be2018-06-13 11:53:02 +01004 * are made available under the terms of the Eclipse Public License v2.0
Ed Willinka55dbe42015-02-24 21:49:23 +00005 * which accompanies this distribution, and is available at
Ed Willink2b4d8be2018-06-13 11:53:02 +01006 * http://www.eclipse.org/legal/epl-v20.html
Ed Willinka55dbe42015-02-24 21:49:23 +00007 *
8 * Contributors:
9 * E.D.Willink - initial API and implementation
10 *******************************************************************************/
11package org.eclipse.ocl.examples.build.xtend
12
13import com.google.inject.Inject
14import com.google.inject.Provider
15import com.google.inject.Singleton
16import java.util.List
17import org.eclipse.emf.common.util.URI
18import org.eclipse.emf.ecore.EClassifier
19import org.eclipse.emf.ecore.EObject
20import org.eclipse.emf.ecore.EPackage
21import org.eclipse.emf.ecore.resource.Resource
22import org.eclipse.jdt.annotation.NonNull
23import org.eclipse.ocl.xtext.base.utilities.AbstractGrammarResource
24import org.eclipse.xtext.AbstractElement
25import org.eclipse.xtext.AbstractMetamodelDeclaration
26import org.eclipse.xtext.AbstractRule
27import org.eclipse.xtext.Action
28import org.eclipse.xtext.Alternatives
29import org.eclipse.xtext.Assignment
30import org.eclipse.xtext.CharacterRange
31import org.eclipse.xtext.CrossReference
32import org.eclipse.xtext.Grammar
33import org.eclipse.xtext.Group
34import org.eclipse.xtext.Keyword
35import org.eclipse.xtext.NegatedToken
36import org.eclipse.xtext.ParserRule
37import org.eclipse.xtext.ReferencedMetamodel
38import org.eclipse.xtext.RuleCall
39import org.eclipse.xtext.TerminalRule
40import org.eclipse.xtext.TypeRef
41import org.eclipse.xtext.UntilToken
42import org.eclipse.xtext.Wildcard
43import org.eclipse.xtext.resource.XtextResourceSet
Adolfo SBHf23d81e2015-04-27 18:03:17 +010044import org.eclipse.xtext.EnumRule
45import org.eclipse.xtext.EnumLiteralDeclaration
46import org.eclipse.emf.ecore.EEnumLiteral
47import org.eclipse.xtext.UnorderedGroup
Ed Willinkea1376a2018-05-20 19:15:41 +010048import java.util.GregorianCalendar
Ed Willinka55dbe42015-02-24 21:49:23 +000049
Ed Willink4f0cefa2018-06-17 08:26:42 +010050 class GenerateGrammarXtend extends GenerateGrammar
Ed Willinka55dbe42015-02-24 21:49:23 +000051{
Ed Willink70051092015-12-23 19:11:07 +000052 /*@NonNull*/ protected override String generate(/*@NonNull*/ Resource grammarResource) {
Ed Willinkea1376a2018-05-20 19:15:41 +010053 var year = new GregorianCalendar().get(GregorianCalendar.YEAR);
Ed Willinkcfdb4972020-06-12 12:03:25 +010054 var boolean hasEnumRules = hasRules(grammarResource, EnumRule);
Ed Willinka55dbe42015-02-24 21:49:23 +000055 '''
56 /*******************************************************************************
Ed Willinkea1376a2018-05-20 19:15:41 +010057 * Copyright (c) 2015, «year» Willink Transformations and others.
Ed Willinka55dbe42015-02-24 21:49:23 +000058 * All rights reserved. This program and the accompanying materials
Ed Willink2b4d8be2018-06-13 11:53:02 +010059 * are made available under the terms of the Eclipse Public License v2.0
Ed Willinka55dbe42015-02-24 21:49:23 +000060 * which accompanies this distribution, and is available at
Ed Willink2b4d8be2018-06-13 11:53:02 +010061 * http://www.eclipse.org/legal/epl-v20.html
Ed Willinka55dbe42015-02-24 21:49:23 +000062 *
63 * Contributors:
64 * E.D.Willink - initial API and implementation
65 *******************************************************************************
66 * This code is 100% auto-generated
67 * from: «sourceFile»
68 * by: org.eclipse.ocl.examples.build.xtend.generateGrammar.xtend
69 *
70 * Do not edit it.
71 *******************************************************************************/
72 package «javaPackageName»;
73
74 import «List.getName()»;
75
76 import «URI.getName()»;
77 import «NonNull.getName()»;
78 import «EObject.getName()»;
79 import «AbstractGrammarResource.getName()»;
80 import «AbstractMetamodelDeclaration.getName()»;
81 import «AbstractRule.getName()»;
Ed Willinkcfdb4972020-06-12 12:03:25 +010082 «IF hasEnumRules»
Adolfo SBHf23d81e2015-04-27 18:03:17 +010083 import «EnumRule.getName()»;
Ed Willinkcfdb4972020-06-12 12:03:25 +010084 «ENDIF»
Ed Willinka55dbe42015-02-24 21:49:23 +000085 import «Grammar.getName()»;
86 import «ParserRule.getName()»;
87 import «ReferencedMetamodel.getName()»;
88 import «TerminalRule.getName()»;
89 import «XtextResourceSet.getName()»;
90
91 import «Inject.getName()»;
92 import «Provider.getName()»;
93 import «Singleton.getName()»;
94
95 /**
96 * «grammarFileStem»GrammarResource provides a programmatically initialized «languageName» Grammar model avoiding
97 * the speed limitations of the pre-Xtext 2.4 *.xmi models and the binary incompatibilities between differing *.xtextbin versions.
98 * <p>
99 * The grammar is immutable and is available as static INSTANCE and GRAMMAR fields.
100 */
Ed Willinka55dbe42015-02-24 21:49:23 +0000101 public class «grammarFileStem»GrammarResource extends AbstractGrammarResource
102 {
103 «FOR grammar : grammarResource.getContents()»
104 private static final @NonNull Grammar «emit(grammar as Grammar, grammar as Grammar)» = createGrammar(«emitValue((grammar as Grammar).getName())»);
105 «ENDFOR»
106
107 /**
108 * The shared immutable instance of the «languageName» Grammar resource.
109 */
110 public static final @NonNull «grammarFileStem»GrammarResource INSTANCE = new «grammarFileStem»GrammarResource();
111
112 /**
113 * The shared immutable instance of the «languageName» Grammar model.
114 */
Ed Willinkc86f5732016-04-19 09:42:58 +0100115 public static final @NonNull Grammar GRAMMAR = (Grammar)INSTANCE.getContents().get(0);
Ed Willinka55dbe42015-02-24 21:49:23 +0000116
117 /**
118 * The name of the language supported by this grammar.
119 */
120 public static final @NonNull String LANGUAGE_NAME = "«languageName»";
121
122 protected «grammarFileStem»GrammarResource() {
123 super(URI.createURI(LANGUAGE_NAME));
124 List<EObject> contents = getContents();
125 «FOR grammar : grammarResource.getContents()»
126 contents.add(«getGrammarPackageName(grammar as Grammar)».initGrammar());
127 «ENDFOR»
128 }
129
130 /*
131 * This class should be bound to org.eclipse.xtext.service.GrammarProvider.
132 */
133 @Singleton
134 public static class GrammarProvider extends org.eclipse.xtext.service.GrammarProvider
135 {
136 @Inject
137 public GrammarProvider(Provider<XtextResourceSet> resourceSetProvider) {
138 super(LANGUAGE_NAME, resourceSetProvider);
139 }
140
141 @Override
142 public Grammar getGrammar(Object requestor) {
143 return «grammarFileStem»GrammarResource.GRAMMAR;
144 }
145 }
146 «FOR grammar : grammarResource.getContents()»
147 «generateGrammarPackage(grammar as Grammar)»
148 «ENDFOR»
149 }
150 '''
151 }
152
Ed Willink70051092015-12-23 19:11:07 +0000153 /*@NonNull*/ protected def String generateEnumRules(/*@NonNull*/ Grammar grammar, /*@NonNull*/ Iterable<EnumRule> eObjects) {
Adolfo SBHf23d81e2015-04-27 18:03:17 +0100154 '''
155 «FOR eObject : eObjects»
156 private static final @NonNull EnumRule ER_«eObject.getName()» = createEnumRule(«emitValue(eObject.getName())», «emit(grammar, eObject.getType())»);
157 «ENDFOR»
158
159 private static void initEnumRules() {
160 «FOR eObject : eObjects»
Ed Willink230210d2020-06-13 10:03:14 +0100161 «emit(grammar, eObject)».setAlternatives(«pushIndent»«emitIndent»«emit(grammar, eObject.alternatives)»«popIndent»);
Adolfo SBHf23d81e2015-04-27 18:03:17 +0100162 «ENDFOR»
163 }
164
165 '''
166 }
167
Ed Willink70051092015-12-23 19:11:07 +0000168 /*@NonNull*/ protected def String generateGrammarPackage(/*@NonNull*/ Grammar grammar) {
Adolfo SBHf23d81e2015-04-27 18:03:17 +0100169 var List<TerminalRule> terminalRules = getSortedAbstractRules(grammar, TerminalRule);
170 var List<EnumRule> enumRules = getSortedAbstractRules(grammar, EnumRule);
Ed Willinka55dbe42015-02-24 21:49:23 +0000171 '''
172
173 private static class «getGrammarPackageName(grammar)»
174 {
175 «generateReferencedMetamodels(grammar, getSortedReferencedMetamodels(grammar))»
176 «IF terminalRules.size() > 0»
177 «generateTerminalRules(grammar, terminalRules)»
178 «ENDIF»
Adolfo SBHf23d81e2015-04-27 18:03:17 +0100179 «IF enumRules.size() > 0»
180 «generateEnumRules(grammar, enumRules)»
181 «ENDIF»
182 «generateParserRules(grammar, getSortedAbstractRules(grammar, ParserRule))»
183 «generateInitGrammar(grammar, terminalRules.size() > 0, enumRules.size() > 0)»
Ed Willinka55dbe42015-02-24 21:49:23 +0000184 }
185 '''
186 }
187
Ed Willink70051092015-12-23 19:11:07 +0000188 /*@NonNull*/ protected def String generateInitGrammar(/*@NonNull*/ Grammar grammar, boolean hasTerminals, boolean hasEnumRules) {
Ed Willinka55dbe42015-02-24 21:49:23 +0000189 '''
190 private static @NonNull Grammar initGrammar() {
191 «IF hasTerminals»
192 initTerminalRules();
193 «ENDIF»
Adolfo SBHf23d81e2015-04-27 18:03:17 +0100194 «IF hasEnumRules»
195 initEnumRules();
196 «ENDIF»
Ed Willinka55dbe42015-02-24 21:49:23 +0000197 initParserRules();
198 Grammar grammar = «emit(grammar, grammar)»;
199 «IF grammar.isDefinesHiddenTokens()»
200 grammar.setDefinesHiddenTokens(true);
201 «ENDIF»
202 «IF grammar.getMetamodelDeclarations().size() > 0»
203 {
204 List<AbstractMetamodelDeclaration> metamodelDeclarations = grammar.getMetamodelDeclarations();
205 «FOR element : grammar.getMetamodelDeclarations()»
206 metamodelDeclarations.add(«emit(grammar, element)»);
207 «ENDFOR»
208 }
209 «ENDIF»
210 «IF grammar.getRules().size() > 0»
211 {
212 List<AbstractRule> rules = grammar.getRules();
213 «FOR element : grammar.getRules()»
214 rules.add(«emit(grammar, element)»);
215 «ENDFOR»
216 }
217 «ENDIF»
218 «IF grammar.getUsedGrammars().size() > 0»
219 {
220 List<Grammar> usedGrammars = grammar.getUsedGrammars();
221 «FOR element : grammar.getUsedGrammars()»
222 usedGrammars.add(«emit(grammar, element)»);
223 «ENDFOR»
224 }
225 «ENDIF»
226 «IF grammar.getHiddenTokens().size() > 0»
227 {
228 List<AbstractRule> hiddenTokens = grammar.getHiddenTokens();
229 «FOR element : grammar.getHiddenTokens()»
230 hiddenTokens.add(«emit(grammar, element)»);
231 «ENDFOR»
232 }
233 «ENDIF»
234 return grammar;
235 }
236 '''
237 }
238
Ed Willink70051092015-12-23 19:11:07 +0000239 /*@NonNull*/ protected def String generateParserRules(/*@NonNull*/ Grammar grammar, /*@NonNull*/ Iterable<ParserRule> eObjects) {
Ed Willinka55dbe42015-02-24 21:49:23 +0000240 '''
241 «FOR eObject : eObjects»
242 private static final @NonNull ParserRule PR_«eObject.getName()» = createParserRule(«emitValue(eObject.getName())», «emit(grammar, eObject.getType())»);
243 «ENDFOR»
244
245 private static void initParserRules() {
246 «FOR eObject : eObjects»
Ed Willink230210d2020-06-13 10:03:14 +0100247 «emit(grammar, eObject)».setAlternatives(«pushIndent»«emitIndent»«emit(grammar, eObject.alternatives)»«popIndent»);
Ed Willinkdf96dd22020-10-18 16:09:00 +0100248 «FOR annotation : eObject.annotations»
249 addAnnotation(«emit(grammar, eObject)», "«annotation.name»");
250 «ENDFOR»
Ed Willinka55dbe42015-02-24 21:49:23 +0000251 «ENDFOR»
252 }
253
254 '''
255 }
256
Ed Willink70051092015-12-23 19:11:07 +0000257 /*@NonNull*/ protected def String generateReferencedMetamodels(/*@NonNull*/ Grammar grammar, /*@NonNull*/ Iterable<ReferencedMetamodel> eObjects) {
Ed Willinka55dbe42015-02-24 21:49:23 +0000258 '''
259 «FOR eObject : eObjects»
260 private static final @NonNull ReferencedMetamodel «emit(grammar, eObject)» = createReferencedMetamodel(«emit(grammar, eObject.getEPackage())», «emitValue(eObject.getAlias())»); // «eObject.getEPackage().getNsURI()»
261 «ENDFOR»
262
263 '''
264 }
265
Ed Willink70051092015-12-23 19:11:07 +0000266 /*@NonNull*/ protected def String generateTerminalRules(/*@NonNull*/ Grammar grammar, /*@NonNull*/ Iterable<TerminalRule> eObjects) {
Ed Willinka55dbe42015-02-24 21:49:23 +0000267 '''
268 «FOR eObject : eObjects»
269 private static final @NonNull TerminalRule TR_«eObject.getName()» = createTerminalRule(«emitValue(eObject.getName())», «emit(grammar, eObject.getType())»);
270 «ENDFOR»
271
272 private static void initTerminalRules() {
273 «FOR eObject : eObjects»
274 «IF eObject.isFragment()»
275 «emit(grammar, eObject)».setFragment(true);
276 «ENDIF»
Ed Willink230210d2020-06-13 10:03:14 +0100277 «emit(grammar, eObject)».setAlternatives(«pushIndent»«emitIndent»«emit(grammar, eObject.alternatives)»«popIndent»);
Ed Willinka55dbe42015-02-24 21:49:23 +0000278 «ENDFOR»
279 }
280
281 '''
282 }
283
284 /*
285 * Emit the eObject term inline within a grammar, either as a direct construction or as a name reference to a previous construction.
286 */
Ed Willink70051092015-12-23 19:11:07 +0000287 /*@NonNull*/ protected def String emit(/*@NonNull*/ Grammar grammar, /*@NonNull*/ EObject eObject) {
Ed Willinka55dbe42015-02-24 21:49:23 +0000288 switch eObject {
289 Action: return emitAction(grammar, eObject)
290 Alternatives: return emitAlternatives(grammar, eObject)
291 Assignment: return emitAssignment(grammar, eObject)
292 CharacterRange: return emitCharacterRange(grammar, eObject)
293 CrossReference: return emitCrossReference(grammar, eObject)
294 EClassifier: return emitEClassifierLiteral(eObject)
Adolfo SBHf23d81e2015-04-27 18:03:17 +0100295 EPackage: return emitEPackageLiteral(eObject)
296 EEnumLiteral : return emitEEnumLiteral(eObject)
297 EnumRule: return emitEnumRuleLiteral(grammar, eObject)
298 EnumLiteralDeclaration: return emitEnumLiteralDeclaration(grammar, eObject)
Ed Willinka55dbe42015-02-24 21:49:23 +0000299 Grammar: return "G" + getGrammarPackageName(eObject)
300 Group: return emitGroup(grammar, eObject)
301 Keyword: return emitKeyword(eObject)
302 NegatedToken: return emitNegatedToken(grammar, eObject)
Adolfo SBHf23d81e2015-04-27 18:03:17 +0100303 ParserRule: return emitParserRuleLiteral(grammar, eObject)
Ed Willinka55dbe42015-02-24 21:49:23 +0000304 ReferencedMetamodel: return emitReferencedMetamodelName(grammar, eObject)
305 RuleCall: return emitRuleCall(grammar, eObject)
306 TerminalRule: return emitTerminalRuleLiteral(grammar, eObject)
307 TypeRef: return emitTypeRef(grammar, eObject)
308 UntilToken: return emitUntilToken(grammar, eObject)
Adolfo SBHf23d81e2015-04-27 18:03:17 +0100309 UnorderedGroup: return emitUnorderedGroup(grammar, eObject)
Ed Willinka55dbe42015-02-24 21:49:23 +0000310 Wildcard: return emitWildcard(grammar, eObject)
311 default: return emitSymbol(eObject.eClass(), eObject)
312 }
313 }
314
Ed Willink70051092015-12-23 19:11:07 +0000315 /*@NonNull*/ protected def String emitAction(/*@NonNull*/ Grammar grammar, /*@NonNull*/ Action eObject) {
Ed Willinka55dbe42015-02-24 21:49:23 +0000316 return wrapCardinality(eObject, wrapFirstSetPredicated(eObject, wrapPredicated(eObject,
317 '''createAction(«emitValue(eObject.getFeature())», «emitValue(eObject.getOperator())», «emit(grammar, eObject.getType())»)''')));
318 }
319
Ed Willink70051092015-12-23 19:11:07 +0000320 /*@NonNull*/ protected def String emitAlternatives(/*@NonNull*/ Grammar grammar, /*@NonNull*/ Alternatives eObject) {
Ed Willinka55dbe42015-02-24 21:49:23 +0000321 return wrapCardinality(eObject, wrapFirstSetPredicated(eObject, wrapPredicated(eObject,
Ed Willink230210d2020-06-13 10:03:14 +0100322 '''createAlternatives(«pushIndent()»«FOR element : eObject.getElements() SEPARATOR ", "»«emitIndent()»«emit(grammar, element)»«ENDFOR»«popIndent()»)''')));
Ed Willinka55dbe42015-02-24 21:49:23 +0000323 }
324
Ed Willink70051092015-12-23 19:11:07 +0000325 /*@NonNull*/ protected def String emitAssignment(/*@NonNull*/ Grammar grammar, /*@NonNull*/ Assignment eObject) {
Ed Willinka55dbe42015-02-24 21:49:23 +0000326 return wrapCardinality(eObject, wrapFirstSetPredicated(eObject, wrapPredicated(eObject,
327 '''createAssignment(«emitValue(eObject.getFeature())», «emitValue(eObject.getOperator())», «emit(grammar, eObject.getTerminal())»)''')));
328 }
329
Ed Willink70051092015-12-23 19:11:07 +0000330 /*@NonNull*/ protected def String emitCharacterRange(/*@NonNull*/ Grammar grammar, /*@NonNull*/ CharacterRange eObject) {
Ed Willinka55dbe42015-02-24 21:49:23 +0000331 return wrapCardinality(eObject, wrapFirstSetPredicated(eObject, wrapPredicated(eObject,
332 '''createCharacterRange(«emit(grammar, eObject.getLeft)», «emit(grammar, eObject.getRight)»)''')));
333 }
334
Ed Willink70051092015-12-23 19:11:07 +0000335 /*@NonNull*/ protected def String emitCrossReference(/*@NonNull*/ Grammar grammar, /*@NonNull*/ CrossReference eObject) {
Ed Willinka55dbe42015-02-24 21:49:23 +0000336 return wrapCardinality(eObject, wrapFirstSetPredicated(eObject, wrapPredicated(eObject,
Ed Willink230210d2020-06-13 10:03:14 +0100337 '''createCrossReference(«pushIndent()»«emitIndent()»«emit(grammar, eObject.getType())», «emit(grammar, eObject.getTerminal())»«popIndent()»)''')));
Ed Willinka55dbe42015-02-24 21:49:23 +0000338 }
339
Ed Willink70051092015-12-23 19:11:07 +0000340 /*@NonNull*/ protected def String emitEnumLiteralDeclaration(/*@NonNull*/ Grammar grammar, /*@NonNull*/ EnumLiteralDeclaration eObject) {
Adolfo SBHf23d81e2015-04-27 18:03:17 +0100341 return wrapCardinality(eObject, wrapFirstSetPredicated(eObject, wrapPredicated(eObject,
342 '''createEnumLiteral(«emit(grammar, eObject.literal)», «emit(grammar, eObject.enumLiteral)»)''')));
343
344
345 }
Ed Willink70051092015-12-23 19:11:07 +0000346 /*@NonNull*/ protected def String emitGroup(/*@NonNull*/ Grammar grammar, /*@NonNull*/ Group eObject) {
Ed Willinka55dbe42015-02-24 21:49:23 +0000347 return wrapCardinality(eObject, wrapFirstSetPredicated(eObject, wrapPredicated(eObject,
Ed Willink230210d2020-06-13 10:03:14 +0100348 '''createGroup(«pushIndent()»«FOR element : eObject.getElements() SEPARATOR ", "»«emitIndent()»«emit(grammar, element)»«ENDFOR»«popIndent()»)''')));
Ed Willinka55dbe42015-02-24 21:49:23 +0000349 }
350
Ed Willink70051092015-12-23 19:11:07 +0000351 /*@NonNull*/ protected def String emitKeyword(/*@NonNull*/ Keyword eObject) {
Ed Willinka55dbe42015-02-24 21:49:23 +0000352 return wrapCardinality(eObject, wrapFirstSetPredicated(eObject, wrapPredicated(eObject,
353 '''createKeyword(«emitValue(eObject.getValue())»)''')));
354 }
355
Ed Willink70051092015-12-23 19:11:07 +0000356 /*@NonNull*/ protected def String emitNegatedToken(/*@NonNull*/ Grammar grammar, /*@NonNull*/ NegatedToken eObject) {
Ed Willinka55dbe42015-02-24 21:49:23 +0000357 return wrapCardinality(eObject, wrapFirstSetPredicated(eObject, wrapPredicated(eObject,
358 '''createNegatedToken(«emit(grammar, eObject.getTerminal())»)''')));
359 }
360
Ed Willink70051092015-12-23 19:11:07 +0000361 /*@NonNull*/ protected def String emitRuleCall(/*@NonNull*/ Grammar grammar, /*@NonNull*/ RuleCall eObject) {
Ed Willinka55dbe42015-02-24 21:49:23 +0000362 return wrapCardinality(eObject, wrapFirstSetPredicated(eObject, wrapPredicated(eObject,
363 '''createRuleCall(«emit(grammar, eObject.getRule())»)''')));
364 }
365
Ed Willink70051092015-12-23 19:11:07 +0000366 /*@NonNull*/ protected def String emitTypeRef(/*@NonNull*/ Grammar grammar, /*@NonNull*/ TypeRef eObject) {
Ed Willinka55dbe42015-02-24 21:49:23 +0000367 return '''createTypeRef(«emit(grammar, eObject.getMetamodel())», «emit(grammar, eObject.getClassifier())»)''';
368 }
369
Ed Willink70051092015-12-23 19:11:07 +0000370 /*@NonNull*/ protected def String emitUnorderedGroup(/*@NonNull*/ Grammar grammar, /*@NonNull*/ UnorderedGroup eObject) {
Adolfo SBHf23d81e2015-04-27 18:03:17 +0100371 return wrapCardinality(eObject, wrapFirstSetPredicated(eObject, wrapPredicated(eObject,
Ed Willink230210d2020-06-13 10:03:14 +0100372 '''createUnorderedGroup(«pushIndent()»«FOR element : eObject.getElements() SEPARATOR ", "»«emitIndent()»«emit(grammar, element)»«ENDFOR»«popIndent()»)''')));
Adolfo SBHf23d81e2015-04-27 18:03:17 +0100373 }
374
Ed Willink70051092015-12-23 19:11:07 +0000375 /*@NonNull*/ protected def String emitUntilToken(/*@NonNull*/ Grammar grammar, /*@NonNull*/ UntilToken eObject) {
Ed Willinka55dbe42015-02-24 21:49:23 +0000376 return wrapCardinality(eObject, wrapFirstSetPredicated(eObject, wrapPredicated(eObject,
377 '''createUntilToken(«emit(grammar, eObject.getTerminal())»)''')));
378 }
379
Ed Willink70051092015-12-23 19:11:07 +0000380 /*@NonNull*/ protected def String emitWildcard(/*@NonNull*/ Grammar grammar, /*@NonNull*/ Wildcard eObject) {
Ed Willinka55dbe42015-02-24 21:49:23 +0000381 return wrapCardinality(eObject, '''createWildcard()''');
382 }
383
Ed Willink70051092015-12-23 19:11:07 +0000384 /*@NonNull*/ protected def String wrapCardinality(/*@NonNull*/ AbstractElement eObject, String generatedElement) {
Ed Willinka55dbe42015-02-24 21:49:23 +0000385 var String cardinality = eObject.getCardinality();
Ed Willink01526762018-01-13 16:50:52 +0000386 if (cardinality === null) {
Ed Willinka55dbe42015-02-24 21:49:23 +0000387 return generatedElement;
388 }
389 else {
390 return '''setCardinality(«emitValue(cardinality)», «generatedElement»)''';
391 }
392 }
393
Ed Willink70051092015-12-23 19:11:07 +0000394 /*@NonNull*/ protected def String wrapFirstSetPredicated(/*@NonNull*/ AbstractElement eObject, String generatedElement) {
Ed Willinka55dbe42015-02-24 21:49:23 +0000395 if (!eObject.isFirstSetPredicated()) {
396 return generatedElement;
397 }
398 else {
399 return '''setFirstSetPredicated(«generatedElement»)''';
400 }
401 }
402
Ed Willink70051092015-12-23 19:11:07 +0000403 /*@NonNull*/ protected def String wrapPredicated(/*@NonNull*/ AbstractElement eObject, String generatedElement) {
Ed Willinka55dbe42015-02-24 21:49:23 +0000404 if (!eObject.isPredicated()) {
405 return generatedElement;
406 }
407 else {
408 return '''setPredicated(«generatedElement»)''';
409 }
410 }
411}