protected void collectMatchableElements(AbstractElement ele, Collection<AbstractElement> result) { switch (ele.eClass().getClassifierID()) { case XtextPackage.RULE_CALL: assertNoEObjectRuleCall((RuleCall) ele); case XtextPackage.KEYWORD: result.add(ele); break; case XtextPackage.CROSS_REFERENCE: collectMatchableElements(((CrossReference) ele).getTerminal(), result); break; case XtextPackage.ASSIGNMENT: collectMatchableElements(((Assignment) ele).getTerminal(), result); break; case XtextPackage.ALTERNATIVES: case XtextPackage.GROUP: case XtextPackage.UNORDERED_GROUP: for (AbstractElement child : ((CompoundElement) ele).getElements()) collectMatchableElements(child, result); break; } }
protected boolean isOptional(AbstractElement ele) { if (GrammarUtil.isOptionalCardinality(ele) || ele instanceof Action) return true; if (ele instanceof CompoundElement) { List<EObject> children = ele.eContents(); if (children.isEmpty() && getBuilder().filter(ele)) return true; if (ele instanceof Alternatives) { for (AbstractElement a : ((Alternatives) ele).getElements()) if (isOptional(a)) return true; return false; } for (EObject e : children) if (e instanceof AbstractElement && !isOptional((AbstractElement) e)) return false; return true; } else return false; }
@Test public void testNameClash_02() throws Exception { String grammarAsText = "grammar test with org.eclipse.xtext.common.Terminals\n" + "generate test 'http://test'\n" + "Class returns Class: {Class_} name=ID;\n"; Grammar grammar = (Grammar) getModel(grammarAsText); AbstractMetamodelDeclaration metamodelDeclaration = grammar.getMetamodelDeclarations().get(0); XtextValidator validator = get(XtextValidator.class); ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, true, false); CompoundElement element = (CompoundElement) grammar.getRules().get(0).getAlternatives(); messageAcceptor.expectedContext( grammar.getRules().get(0).getType(), ((Action) element.getElements().get(0)).getType() ); validator.setMessageAcceptor(messageAcceptor); validator.checkGeneratedPackage((GeneratedMetamodel) metamodelDeclaration, Diagnostician.INSTANCE, Collections.EMPTY_MAP); messageAcceptor.validate(); }
@Test public void testNameClash_03() throws Exception { String grammarAsText = "grammar test with org.eclipse.xtext.common.Terminals\n" + "generate test 'http://test'\n" + "Foo: myVars=ID my_vars=ID;\n"; Grammar grammar = (Grammar) getModel(grammarAsText); AbstractMetamodelDeclaration metamodelDeclaration = grammar.getMetamodelDeclarations().get(0); XtextValidator validator = get(XtextValidator.class); ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, true, false); CompoundElement element = (CompoundElement) grammar.getRules().get(0).getAlternatives(); messageAcceptor.expectedContext( grammar.getRules().get(0).getType(), element.getElements().get(0), element.getElements().get(1) ); validator.setMessageAcceptor(messageAcceptor); validator.checkGeneratedPackage((GeneratedMetamodel) metamodelDeclaration, Diagnostician.INSTANCE, Collections.EMPTY_MAP); messageAcceptor.validate(); }
@Test public void testPredicatedUnorderedGroup_01() throws Exception { String grammarAsText = "grammar test with org.eclipse.xtext.common.Terminals\n" + "generate test 'http://test'\n" + "Model: =>(name=ID & value=STRING);\n"; Grammar grammar = (Grammar) getModel(grammarAsText); ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, true, false); messageAcceptor.expectedContext( grammar.getRules().get(0).getAlternatives(), ((CompoundElement) grammar.getRules().get(0).getAlternatives()).getElements().get(0) ); PredicateUsesUnorderedGroupInspector inspector = new PredicateUsesUnorderedGroupInspector(messageAcceptor); inspector.inspect(grammar); messageAcceptor.validate(); }
/** * Returns the root element or its alternative branches if it is an {@link Alternatives} or an * {@link UnorderedGroup}. In case of an {@link UnorderedGroup} the result contains only the elements that are not * yet processed up to the relevant cursor position. */ protected Collection<AbstractElement> getElementsToParse(AbstractElement root, List<AbstractElement> handledUnorderedGroupElements) { if (root instanceof UnorderedGroup) { if (handledUnorderedGroupElements.isEmpty()) return ((CompoundElement) root).getElements(); List<AbstractElement> result = Lists.newArrayList(root); for (AbstractElement child : ((UnorderedGroup) root).getElements()) { if (!handledUnorderedGroupElements.contains(child)) { result.add(child); } } return result; } return getElementsToParse(root); }
/** * <!-- begin-user-doc --> * <!-- end-user-doc --> * @generated */ @SuppressWarnings("unchecked") @Override public void eSet(int featureID, Object newValue) { switch (featureID) { case FormatPackage.GROUP_BLOCK__GRAMMAR_ELEMENT: setGrammarElement((CompoundElement)newValue); return; case FormatPackage.GROUP_BLOCK__MATCHER_LIST: setMatcherList((MatcherList)newValue); return; case FormatPackage.GROUP_BLOCK__SUB_GROUP: setSubGroup((GroupBlock)newValue); return; case FormatPackage.GROUP_BLOCK__DIRECTIVES: getDirectives().clear(); getDirectives().addAll((Collection<? extends GrammarRuleDirective>)newValue); return; } super.eSet(featureID, newValue); }
/** * <!-- begin-user-doc --> * <!-- end-user-doc --> * @generated */ @Override public void eUnset(int featureID) { switch (featureID) { case FormatPackage.GROUP_BLOCK__GRAMMAR_ELEMENT: setGrammarElement((CompoundElement)null); return; case FormatPackage.GROUP_BLOCK__MATCHER_LIST: setMatcherList((MatcherList)null); return; case FormatPackage.GROUP_BLOCK__SUB_GROUP: setSubGroup((GroupBlock)null); return; case FormatPackage.GROUP_BLOCK__DIRECTIVES: getDirectives().clear(); return; } super.eUnset(featureID); }
private Collection<AbstractElement> getElementsToParse(FollowElement element) { AbstractElement root = element.getGrammarElement(); if (root instanceof UnorderedGroup) { List<AbstractElement> handled = element.getHandledUnorderedGroupElements(); if (handled.isEmpty()) return ((CompoundElement) root).getElements(); List<AbstractElement> result = Lists.newArrayList(root); for(AbstractElement child: ((UnorderedGroup) root).getElements()) { if (!handled.contains(child)) { result.add(child); } } return result; } return getElementsToParse(root); }
@Override public Boolean caseCompoundElement(CompoundElement object) { for(AbstractElement element: object.getElements()) if (!doSwitch(element)) return false; return true; }
@Override public EClassifier caseCompoundElement(CompoundElement object) { // since we do not allow unassigned rule calls and // actions, it is safe to use the same logic for // unordered groups as for groups if (object.getElements().size() != 1) return null; return doSwitch(object.getElements().get(0)); }
@Override public Boolean caseCompoundElement(CompoundElement object) { EClassifier wasType = currentType; for(AbstractElement element: object.getElements()) { if (doSwitch(element)) return true; } if (object == stopElement) return true; if (GrammarUtil.isOptionalCardinality(object)) currentType = getCompatibleType(currentType, wasType, object); return false; }
@Override public Boolean caseCompoundElement(CompoundElement object) { if (GrammarUtil.isOptionalCardinality(object)) return Boolean.FALSE; for(AbstractElement element: object.getElements()) { if (doSwitch(element)) return Boolean.TRUE; } return Boolean.FALSE; }
@Override protected void emit_ParenthesizedElement_LeftParenthesisKeyword_0_a(EObject semanticObject, ISynNavigable transition, List<INode> nodes) { if (semanticObject instanceof CompoundElement) { CompoundElement ele = (CompoundElement) semanticObject; if (!Strings.isNullOrEmpty(ele.getCardinality()) && ele.getElements().size() > 1) { Keyword kw = grammarAccess.getParenthesizedElementAccess().getLeftParenthesisKeyword_0(); acceptUnassignedKeyword(kw, kw.getValue(), nodes == null || nodes.isEmpty() ? null : (ILeafNode) nodes.get(0)); } } super.emit_ParenthesizedCondition_LeftParenthesisKeyword_0_a(semanticObject, transition, nodes); }
@Override public Pair<Boolean, Boolean> caseCompoundElement(CompoundElement object) { boolean valid = true; boolean instantiated = false; for(AbstractElement element: object.getElements()) { Pair<Boolean, Boolean> elementResult = doSwitch(element); instantiated |= elementResult.getSecond().booleanValue(); if (!instantiated) valid &= elementResult.getFirst().booleanValue(); } valid |= instantiated; instantiated &= !(GrammarUtil.isOptionalCardinality(object)); return Tuples.create(valid, instantiated); }
@Override public Boolean caseCompoundElement(CompoundElement object) { // Do not create duplicate errors if (shouldTraverse(object)) { pushChecked(object); for(AbstractElement child: object.getElements()) doSwitch(child); popChecked(object); } return Boolean.FALSE; }
protected String addQualified(String result, AbstractElement ele) { if (!showQualified && !showRule) return result; AbstractRule rule = GrammarUtil.containingRule(ele); if (rule == null) return "<AbstractElement not contained in AbstractRule!>:" + result; if (!showQualified) return result + ":" + rule.getName(); GrammarElementTitleSwitch others = copy(); others.showQualified = false; others.showRule = false; List<AbstractElement> elementsWithSameName = Lists.newArrayList(); for (AbstractElement candidate : EcoreUtil2.getAllContentsOfType(rule, ele.getClass())) if (candidate == ele || result.equals(others.doSwitch(candidate))) elementsWithSameName.add(candidate); if (elementsWithSameName.size() < 2) { if (showRule) return rule.getName() + ":" + result; return result; } Map<CompoundElement, Node> nodes = Maps.newHashMap(); for (AbstractElement collision : elementsWithSameName) { EObject current = EcoreUtil2.getContainerOfType(collision, CompoundElement.class); Node node = new Node(null, collision == ele ? result : ""); while (current instanceof CompoundElement) { CompoundElement container = (CompoundElement) current; Node cntNode = nodes.get(container); if (cntNode == null) nodes.put(container, cntNode = new Node(container, null)); if (!cntNode.children.contains(node)) cntNode.children.add(node); node = cntNode; current = current.eContainer(); } } if (showRule) return rule.getName() + ":" + nodes.get(rule.getAlternatives()); return nodes.get(rule.getAlternatives()).toString(); }
public List<AbstractElement> contentsAsList(final EObject it) { if (it instanceof UnorderedGroup) { return _contentsAsList((UnorderedGroup)it); } else if (it instanceof CompoundElement) { return _contentsAsList((CompoundElement)it); } else if (it instanceof ParserRule) { return _contentsAsList((ParserRule)it); } else if (it instanceof AbstractElement) { return _contentsAsList((AbstractElement)it); } else { throw new IllegalArgumentException("Unhandled parameter types: " + Arrays.<Object>asList(it).toString()); } }
@Test public void testDuplicateFeatures_02() throws Exception { String grammarAsText = "grammar test with org.eclipse.xtext.common.Terminals\n" + "generate test 'http://test'\n" + "A : (b+=B)*;\n" + "B : C | D;\n" + "C : 'c' name=ID ('e' e+=E)+;\n" + "E : name=ID;\n" + "F : C | E;\n" + "D : 'd' name=ID 'ref' ref=[F];"; Grammar grammar = (Grammar) getModel(grammarAsText); AbstractMetamodelDeclaration metamodelDeclaration = grammar.getMetamodelDeclarations().get(0); XtextValidator validator = get(XtextValidator.class); ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, true, false); messageAcceptor.expectedContext( grammar.getRules().get(1).getType(), grammar.getRules().get(2).getType(), grammar.getRules().get(4).getType(), ((CompoundElement) grammar.getRules().get(2).getAlternatives()).getElements().get(1), grammar.getRules().get(3).getAlternatives(), ((CompoundElement) grammar.getRules().get(5).getAlternatives()).getElements().get(1) ); validator.setMessageAcceptor(messageAcceptor); validator.checkGeneratedPackage((GeneratedMetamodel) metamodelDeclaration, Diagnostician.INSTANCE, Collections.EMPTY_MAP); messageAcceptor.validate(); }
@Test public void groupScoped() { ParserRule parserRuleA = (ParserRule) grammarA.getRules().get(0); CompoundElement group1 = findSemanticElementByString("(", CompoundElement.class, parserRuleA); // ('-' b=STRING | ('-'c=ID ('-'d=INT | '-'e=STRING)))? CompoundElement group11 = findSemanticElementByString("b", CompoundElement.class, group1);// '-' b=STRING CompoundElement group12 = findSemanticElementByString("(", CompoundElement.class, group1);// ('-'c=ID ('-'d=INT | '-'e=STRING)) CompoundElement group121 = findSemanticElementByString("(", CompoundElement.class, group12);// ('-'d=INT | '-'e=STRING) CompoundElement group1211 = findSemanticElementByString("d", CompoundElement.class, group121);// '-'d=INT CompoundElement group1212 = findSemanticElementByString("e", CompoundElement.class, group121);// '-'e=STRING GroupBlock groupRef1 = findSemanticElementByString("/*@G1*/", GroupBlock.class, formatA); GroupBlock groupRef12 = findSemanticElementByString("/*@G12*/", GroupBlock.class, formatA); GroupBlock groupRef121 = findSemanticElementByString("/*@G121*/", GroupBlock.class, formatA); GroupBlock groupRef1212 = findSemanticElementByString("/*@G1212*/", GroupBlock.class, formatA); assertScope(groupRef1, FormatPackage.Literals.GROUP_BLOCK__GRAMMAR_ELEMENT, TO_URI.apply(group1)); assertScope(groupRef12, FormatPackage.Literals.GROUP_BLOCK__GRAMMAR_ELEMENT, TO_URI.apply(group11), TO_URI.apply(group12)); assertScope(groupRef121, FormatPackage.Literals.GROUP_BLOCK__GRAMMAR_ELEMENT, TO_URI.apply(group121)); assertScope(groupRef1212, FormatPackage.Literals.GROUP_BLOCK__GRAMMAR_ELEMENT, TO_URI.apply(group1211), TO_URI.apply(group1212)); groupRef1 = findSemanticElementByString("/*@G1*/", GroupBlock.class, formatC); groupRef12 = findSemanticElementByString("/*@G12*/", GroupBlock.class, formatC); groupRef121 = findSemanticElementByString("/*@G121*/", GroupBlock.class, formatC); groupRef1212 = findSemanticElementByString("/*@G1212*/", GroupBlock.class, formatC); assertScope(groupRef1, FormatPackage.Literals.GROUP_BLOCK__GRAMMAR_ELEMENT, TO_URI.apply(group1)); assertScope(groupRef12, FormatPackage.Literals.GROUP_BLOCK__GRAMMAR_ELEMENT, TO_URI.apply(group11), TO_URI.apply(group12)); assertScope(groupRef121, FormatPackage.Literals.GROUP_BLOCK__GRAMMAR_ELEMENT, TO_URI.apply(group121)); assertScope(groupRef1212, FormatPackage.Literals.GROUP_BLOCK__GRAMMAR_ELEMENT, TO_URI.apply(group1211), TO_URI.apply(group1212)); }
/** * <!-- begin-user-doc --> * <!-- end-user-doc --> * @generated */ public void setGrammarElement(CompoundElement newGrammarElement) { CompoundElement oldGrammarElement = grammarElement; grammarElement = newGrammarElement; if (eNotificationRequired()) eNotify(new ENotificationImpl(this, Notification.SET, FormatPackage.GROUP_BLOCK__GRAMMAR_ELEMENT, oldGrammarElement, grammarElement)); }
/** * Returns all {@link CompoundElement} of type compoundType in a specific context. * * @param <T> * - Group, UnorderedGroup or Alternatives * @param parent * the context from which to return elements * @param compoundType * the specific type of the compound elements * @return List a list with elements of type compoundType */ @SuppressWarnings("unchecked") protected <T extends CompoundElement> List<T> getCompoundElements(final AbstractRule parent, final Class<T> compoundType) { AbstractElement alternatives = parent.getAlternatives(); if (alternatives instanceof Alternatives) { Alternatives group = (Alternatives) alternatives; return Lists.newArrayList(Iterables.filter(group.getElements(), compoundType)); } else if (alternatives instanceof CompoundElement) { return Lists.newArrayList(Iterables.filter(alternatives.eContents(), compoundType)); } else if (compoundType.isInstance(alternatives)) { return Lists.newArrayList((T) alternatives); } return Collections.emptyList(); }
protected ISyntaxConstraint createElement(ConstraintType type, AbstractElement ele, EClass semanticType, boolean multiple, boolean optional) { List<AbstractElement> ctns = ele instanceof CompoundElement ? ((CompoundElement) ele).getElements() : null; return createElement(type, ele, ctns, new ArrayList<ISyntaxConstraint>(), semanticType, multiple, optional); }
@Override public Void caseCompoundElement(CompoundElement object) { for(AbstractElement element: object.getElements()) doSwitch(element); return null; }
public Node(CompoundElement compound, String text) { super(); this.compound = compound; this.text = text; }
protected void collectOutgoing(AbstractElement element, Set<AbstractElement> visited, boolean isRuleCall, AbstractElement loopCenter) { if (element instanceof Group || element instanceof UnorderedGroup) { List<AbstractElement> elements = ((CompoundElement) element).getElements(); switch (builder.getDirection()) { case FORWARD: int j = 0; while (j < elements.size()) { addOutgoing(elements.get(j), visited, isRuleCall, loopCenter); if (!GrammarUtil.isOptionalCardinality(elements.get(j))) break; j++; } // add transitions to the next sibling of this element if all children are optional if (j >= elements.size()) collectOutgoingByContainer(element, visited, isRuleCall, loopCenter); break; case BACKWARD: int i = elements.size() - 1; while (i >= 0) { addOutgoing(elements.get(i), visited, isRuleCall, loopCenter); if (!GrammarUtil.isOptionalCardinality(elements.get(i))) break; i--; } // add transitions to the next sibling of this element if all children are optional if (i < 0) collectOutgoingByContainer(element, visited, isRuleCall, loopCenter); break; } } else if (element instanceof Alternatives) { boolean hasOptional = false; for (AbstractElement e : ((Alternatives) element).getElements()) { hasOptional |= GrammarUtil.isOptionalCardinality(e); addOutgoing(e, visited, isRuleCall, loopCenter); } if (hasOptional) collectOutgoingByContainer(element, visited, isRuleCall, loopCenter); } else if (element instanceof Assignment) addOutgoing(((Assignment) element).getTerminal(), visited, isRuleCall, loopCenter); else if (element instanceof CrossReference) addOutgoing(((CrossReference) element).getTerminal(), visited, isRuleCall, loopCenter); else if (GrammarUtil.isEObjectRuleCall(element)) { addOutgoing(((RuleCall) element).getRule().getAlternatives(), visited, true, loopCenter); collectOutgoingByContainer(element, visited, isRuleCall, loopCenter); } else { if (GrammarUtil.isMultipleCardinality(element) && !filter(element)) addOutgoing(element, visited, isRuleCall, element); collectOutgoingByContainer(element, visited, isRuleCall, loopCenter); } }
protected List<AbstractElement> _contentsAsList(final CompoundElement it) { final Function1<AbstractElement, List<AbstractElement>> _function = (AbstractElement it_1) -> { return this.contentsAsList(it_1); }; return IterableExtensions.<AbstractElement>toList(Iterables.<AbstractElement>concat(ListExtensions.<AbstractElement, List<AbstractElement>>map(it.getElements(), _function))); }
/** * Return the element itself or its components if it is an {@link Alternatives}. */ protected Collection<AbstractElement> getElementsToParse(AbstractElement root) { if (root instanceof Alternatives) return ((CompoundElement) root).getElements(); return Collections.singleton(root); }
private Collection<AbstractElement> getElementsToParse(AbstractElement root) { if (root instanceof Alternatives/* || root instanceof UnorderedGroup*/) return ((CompoundElement) root).getElements(); return Collections.singleton(root); }
/** * Returns the value of the '<em><b>Grammar Element</b></em>' reference. * <!-- begin-user-doc --> * <p> * If the meaning of the '<em>Grammar Element</em>' reference isn't clear, * there really should be more of a description here... * </p> * <!-- end-user-doc --> * @return the value of the '<em>Grammar Element</em>' reference. * @see #setGrammarElement(CompoundElement) * @see com.avaloq.tools.ddk.xtext.format.format.FormatPackage#getGroupBlock_GrammarElement() * @model * @generated */ CompoundElement getGrammarElement();
/** * Sets the value of the '{@link com.avaloq.tools.ddk.xtext.format.format.GroupBlock#getGrammarElement <em>Grammar Element</em>}' reference. * <!-- begin-user-doc --> * <!-- end-user-doc --> * @param value the new value of the '<em>Grammar Element</em>' reference. * @see #getGrammarElement() * @generated */ void setGrammarElement(CompoundElement value);
/** * <!-- begin-user-doc --> * <!-- end-user-doc --> * @generated */ public CompoundElement basicGetGrammarElement() { return grammarElement; }
/** * Returns all {@link CompoundElement} of type compoundType in a specific context. * * @param <T> * - Group, UnorderedGroup or Alternatives * @param parent * the context from which to return elements * @param compoundType * the specific type of the compound elements * @return List a list with elements of type compoundType */ protected <T extends CompoundElement> List<T> getCompoundElements(final CompoundElement parent, final Class<T> compoundType) { return Lists.newArrayList(Iterables.filter(parent.getElements(), compoundType)); }