/** * @param template * @return */ private static String getSourceStorageName(OperatorTemplate template) { if (template != null) { PropertyReference propRef = template.getStoreLeftSideTo(); if (propRef.getStrucfeature() != null) { return propRef.getStrucfeature().getName(); } else { return propRef.getName(); } } return null; }
/** * Adds the template. * * @param template the template * @throws SyntaxParsingException * @throws MetaModelLookupException */ public void addTemplate(OperatorTemplate template, RuleBodyBufferFactory ruleBodyBufferFactory) throws MetaModelLookupException, SyntaxElementException { VarStringBuffer rulebody = new VarStringBuffer(); // first add ANTLR rule to Rulebody, then Java elements Collection<Operator> ops = template.getOperators(); boolean allOperatorsPostFix = true; for (Operator operator : ops) { if (!operator.isPostfix()) { allOperatorsPostFix = false; } } if (allOperatorsPostFix) { // TODO: Understand and Support specific operator templates // throw new RuntimeException("Operator Template with all operators being postfix not // supported yet"); } try { ResolvedNameAndReferenceBean<Type> refbean = resolutionHelper.resolve(template); String returnDeclaration = concat("java.lang.String opName, Object left, org.antlr.runtime.Token firstToken"); String metaObjectListParam = namingHelper.getMetaTypeListParameter(refbean); StringBuilder initString = new StringBuilder(); // TODO cleanup string construction if (template.isIsReferenceOnly()) { initString.append( concat( "List<String> metaType=", metaObjectListParam, ";\n", ObservationDirectivesHelper.getEnterTemplateNotification(template), "IModelElementProxy ret=(getBacktrackingLevel()==0) ? createReferenceProxy(metaType) : null;\n\n")); } else { initString.append( concat( "List<String> metaType=", metaObjectListParam, ";\n", ObservationDirectivesHelper.getEnterTemplateNotification(template), "IModelElementProxy ret=(getBacktrackingLevel()==0) ? createModelElementProxy(metaType, ", template.isIsContext(), ", false")); ContextTags tags = template.getContextTags(); if (tags != null && tags.getTags() != null && tags.getTags().size() > 0) { initString.append(", new String[]{"); for (Iterator<String> iterator = tags.getTags().iterator(); iterator.hasNext(); ) { String tag = iterator.next(); initString.append("\"").append(tag).append("\""); if (iterator.hasNext()) { initString.append(", "); } } initString.append("}"); } initString.append(") : null;\n\n"); } if (template.getTemplateSequence() != null) { Sequence sequence = template.getTemplateSequence(); String rulefragment = ruleBodyBufferFactory.getNewRuleBodyFragment(sequence); rulebody.append(rulefragment); } rulebody.append("{\n"); String operatorStorageName = getOperatorStorageName(template); if (operatorStorageName != null) { rulebody.append("setProperty(ret,\"", operatorStorageName, "\" , opName);\n"); } else { rulebody.append("// discarding operator name instead of storing it here\n"); } rulebody.append("setProperty(ret,\"", getSourceStorageName(template), "\" , left);\n"); rulebody.append("ret2 = createOrResolve(ret, firstToken);\n"); rulebody.append("onRuleElementCreationCommited(ret2);\n"); if (template.isIsContext()) { rulebody.append("leaveContext();\n"); // leave context after sub-elements are done with add to context } rulebody.append("\n}"); writer.addRule( new OperatorTemplateProductionRule( namingHelper.getRuleName(template), returnDeclaration, "Object ret2", initString.toString(), rulebody.toString())); } catch (NameResolutionFailedException e) { throw new SyntaxElementException( "OperatorTemplate name could not be resolved: " + MessageHelper.getTemplateName(template), template, e); } }