From 6b88799f1913a6d7e88d2d78b0d3c1ed5592e9da Mon Sep 17 00:00:00 2001 From: Moritz Eysholdt Date: Tue, 20 Oct 2015 23:49:22 +0200 Subject: [PATCH] [serializer] only check parameters when needed only check the value of parameters when the same context eobject maps to different constraints. Signed-off-by: Moritz Eysholdt --- .../serializer/SerializerFragment2.xtend | 68 ++++++++++--------- 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/plugins/org.eclipse.xtext.xtext.generator/src/org/eclipse/xtext/xtext/generator/serializer/SerializerFragment2.xtend b/plugins/org.eclipse.xtext.xtext.generator/src/org/eclipse/xtext/xtext/generator/serializer/SerializerFragment2.xtend index 507dd0659..340328c7f 100644 --- a/plugins/org.eclipse.xtext.xtext.generator/src/org/eclipse/xtext/xtext/generator/serializer/SerializerFragment2.xtend +++ b/plugins/org.eclipse.xtext.xtext.generator/src/org/eclipse/xtext/xtext/generator/serializer/SerializerFragment2.xtend @@ -66,6 +66,8 @@ import static extension org.eclipse.xtext.GrammarUtil.* import static extension org.eclipse.xtext.serializer.analysis.SerializationContext.* import static extension org.eclipse.xtext.xtext.generator.model.TypeReference.* import static extension org.eclipse.xtext.xtext.generator.util.GenModelUtil2.* +import com.google.common.collect.Multimaps +import com.google.common.collect.Multimap class SerializerFragment2 extends AbstractXtextGeneratorFragment { @@ -283,10 +285,14 @@ class SerializerFragment2 extends AbstractXtextGeneratorFragment { private def StringConcatenationClient genMethodCreateSequenceCaseBody(Map superConstraints, EClass type) { val contexts = grammar.getGrammarConstraints(type).entrySet.sortBy[key.name] + val context2constraint = LinkedHashMultimap.create + for (e : contexts) + for (ctx : e.value) + context2constraint.put((ctx as SerializationContext).actionOrRule, e.key) ''' «IF contexts.size > 1» «FOR ctx : contexts.indexed» - «IF ctx.key > 0»else «ENDIF»if («genCondition(ctx.value.value, ctx.value.key)») { + «IF ctx.key > 0»else «ENDIF»if («genCondition(ctx.value.value, ctx.value.key, context2constraint)») { «genMethodCreateSequenceCall(superConstraints, type, ctx.value.key)» } «ENDFOR» @@ -299,13 +305,13 @@ class SerializerFragment2 extends AbstractXtextGeneratorFragment { ''' } - private def StringConcatenationClient genCondition(List contexts, IConstraint constraint) { + private def StringConcatenationClient genCondition(List contexts, IConstraint constraint, Multimap ctx2ctr) { val sorted = contexts.sort val index = LinkedHashMultimap.create sorted.forEach [ index.put(contextObject, it) ] - '''«FOR obj : index.keySet SEPARATOR "\n\t\t|| "»«obj.genObjectSelector»«obj.genParameterSelector(index.get(obj), constraint)»«ENDFOR»''' + '''«FOR obj : index.keySet SEPARATOR "\n\t\t|| "»«obj.genObjectSelector»«IF ctx2ctr.get(obj).size > 1»«obj.genParameterSelector(index.get(obj), constraint)»«ENDIF»«ENDFOR»''' } private def StringConcatenationClient genObjectSelector(EObject obj) { @@ -316,34 +322,34 @@ class SerializerFragment2 extends AbstractXtextGeneratorFragment { } private def StringConcatenationClient genParameterSelector(EObject obj, Set contexts, IConstraint constraint) { - val rule = GrammarUtil.containingParserRule(obj) - if (rule.parameters.isEmpty || !constraint.contexts.exists[!(it as SerializationContext).declaredParameters.isEmpty]) { - return '''''' - } - // figure out which scenarios are independent from the parameter values - val withParamsByRule = LinkedHashMultimap.create - contexts.forEach [ - val param = enabledBooleanParameters.head - if (param !== null) { - withParamsByRule.put(GrammarUtil.containingParserRule(param), it) - } - ] - val copy = newLinkedHashSet - copy.addAll(contexts) - - // and remove these - withParamsByRule.keySet.forEach [ - val entries = withParamsByRule.get(it) - if (entries.size === (1 << it.parameters.size) - 1) { - copy.removeAll(entries) - } - ] - - if (copy.exists [ !enabledBooleanParameters.isEmpty ]) { - // param configuration doesn't matter - return '''''' - } - return ''' && («FOR context : copy SEPARATOR "\n\t\t\t|| "»«context.genParameterCondition(constraint)»«ENDFOR»)''' +// val rule = GrammarUtil.containingParserRule(obj) +// if (rule.parameters.isEmpty || !constraint.contexts.exists[!(it as SerializationContext).declaredParameters.isEmpty]) { +// return '''''' +// } +// // figure out which scenarios are independent from the parameter values +// val withParamsByRule = LinkedHashMultimap.create +// contexts.forEach [ +// val param = enabledBooleanParameters.head +// if (param !== null) { +// withParamsByRule.put(GrammarUtil.containingParserRule(param), it) +// } +// ] +// val copy = newLinkedHashSet +// copy.addAll(contexts) +// +// // and remove these +// withParamsByRule.keySet.forEach [ +// val entries = withParamsByRule.get(it) +// if (entries.size === (1 << it.parameters.size) - 1) { +// copy.removeAll(entries) +// } +// ] +// +// if (copy.isEmpty || copy.exists [ !enabledBooleanParameters.isEmpty ]) { +// // param configuration doesn't matter +// return '''''' +// } + return ''' && («FOR context : contexts SEPARATOR "\n\t\t\t|| "»«context.genParameterCondition(constraint)»«ENDFOR»)''' }