Java Code Examples for org.eclipse.xtext.GrammarUtil#containingAssignment()

The following examples show how to use org.eclipse.xtext.GrammarUtil#containingAssignment() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EntryPointFinder.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
protected boolean shouldUseParent(ICompositeNode result, int offset, ILeafNode leaf) {
	if (leaf.getTotalEndOffset() == offset) {
		return true;
	}
	if (result.getGrammarElement() instanceof RuleCall) {
		RuleCall rc = (RuleCall) result.getGrammarElement();
		if (!rc.getArguments().isEmpty()) {
			return true;
		}
		Assignment assignment = GrammarUtil.containingAssignment(rc);
		if (assignment != null
				&& (GrammarUtil.isMultipleCardinality(assignment) || (assignment.eContainer() instanceof AbstractElement && GrammarUtil
						.isMultipleCardinality((AbstractElement) assignment.eContainer())))) {
			return true;
		}
	}
	return false;
}
 
Example 2
Source File: AbstractEObjectRegion.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public EStructuralFeature getContainingFeature() {
	if(semanticElement.eContainer() == null) {
		return null;
	}
	String feature = null;
	if (grammarElement instanceof Action) {
		feature = ((Action) grammarElement).getFeature();
	} else {
		Assignment assignment = GrammarUtil.containingAssignment(getGrammarElement());
		if (assignment != null) {
			feature = assignment.getFeature();
		}
	}
	if (feature == null) {
		return null;
	}
	return semanticElement.eContainer().eClass().getEStructuralFeature(feature);
}
 
Example 3
Source File: FeatureFinderUtil.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @since 2.0
 */
public static EStructuralFeature getFeature(AbstractElement grammarElement, EClass owner) {
	Preconditions.checkNotNull(owner);
	if (grammarElement == null)
		return null;
	String featureName = null;
	if (grammarElement instanceof Action)
		featureName = ((Action) grammarElement).getFeature();
	else {
		Assignment ass = GrammarUtil.containingAssignment(grammarElement);
		if (ass != null)
			featureName = ass.getFeature();
	}
	if (featureName != null)
		return owner.getEStructuralFeature(featureName);
	return null;
}
 
Example 4
Source File: BaseContentAssistParser.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
protected AbstractElement getEntryGrammarElement(ICompositeNode entryPoint) {
	EObject grammarElement = entryPoint.getGrammarElement();
	if (grammarElement instanceof RuleCall) {
		AbstractRule rule = ((RuleCall) grammarElement).getRule();
		if (rule instanceof ParserRule) {
			if (!GrammarUtil.isMultipleCardinality(rule.getAlternatives())) {
				grammarElement = rule.getAlternatives();
			}
		}
	} else if (grammarElement instanceof ParserRule) {
		grammarElement = ((ParserRule) grammarElement).getAlternatives();
	} else if (grammarElement instanceof CrossReference) {
		grammarElement = GrammarUtil.containingAssignment(grammarElement);
	}
	AbstractElement result = (AbstractElement) grammarElement;
	if (result instanceof Action) {
		return getEntryGrammarElement((ICompositeNode) entryPoint.getFirstChild());
	}
	return result;
}
 
Example 5
Source File: DefaultLocationInFileProvider.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Returns the smallest node that covers all assigned values of the given object. It handles the semantics of {@link Action
 * actions} and {@link RuleCall unassigned rule calls}.
 * 
 * @return the minimal node that covers all assigned values of the given object.
 * @since 2.3
 */
protected ICompositeNode findNodeFor(EObject semanticObject) {
	ICompositeNode result = NodeModelUtils.getNode(semanticObject);
	if (result != null) {
		ICompositeNode node = result;
		while (GrammarUtil.containingAssignment(node.getGrammarElement()) == null && node.getParent() != null && !node.getParent().hasDirectSemanticElement()) {
			ICompositeNode parent = node.getParent();
			if (node.hasSiblings()) {
				for(INode sibling: parent.getChildren()) {
					EObject grammarElement = sibling.getGrammarElement();
					if (grammarElement != null && GrammarUtil.containingAssignment(grammarElement) != null) {
						result = parent;
					}
				}
			}
			node = parent;
		}
	}
	return result;
}
 
Example 6
Source File: StringSemanticRegion.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public EStructuralFeature getContainingFeature() {
	Assignment assignment = GrammarUtil.containingAssignment(getGrammarElement());
	if (assignment != null) {
		return getSemanticElement().eClass().getEStructuralFeature(assignment.getFeature());
	}
	return null;
}
 
Example 7
Source File: SequenceFeeder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected String getToken(RuleCall rc, Object value, INode node) {
	CrossReference crossRef = GrammarUtil.containingCrossReference(rc);
	Assignment assignment = GrammarUtil.containingAssignment(rc);
	if (crossRef != null)
		return provider.crossRefSerializer.serializeCrossRef(semanticObject, crossRef, (EObject) value, node,
				errorAcceptor);
	else if (GrammarUtil.isEObjectRuleCall(rc) || GrammarUtil.isBooleanAssignment(assignment))
		return null;
	else if (GrammarUtil.isEnumRuleCall(rc))
		return provider.enumLiteralSerializer.serializeAssignedEnumLiteral(semanticObject, rc, value, node,
				errorAcceptor);
	else
		return provider.valueSerializer.serializeAssignedValue(semanticObject, rc, value, node, errorAcceptor);
}
 
Example 8
Source File: NodeModelBasedRegionAccessBuilder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected EObject findGrammarElement(INode node, EObject obj) {
	INode current = node;
	String feature = obj.eContainingFeature().getName();
	while (current != null) {
		EObject grammarElement = current.getGrammarElement();
		Assignment assignment = GrammarUtil.containingAssignment(grammarElement);
		if (assignment != null && feature.equals(assignment.getFeature()))
			return grammarElement;
		if (grammarElement instanceof Action) {
			Action action = (Action) grammarElement;
			if (feature.equals(action.getFeature()))
				return grammarElement;
			else if (current == node && current instanceof ICompositeNode) {
				INode child = ((ICompositeNode) current).getFirstChild();
				while (child instanceof ICompositeNode) {
					EObject grammarElement2 = child.getGrammarElement();
					Assignment assignment2 = GrammarUtil.containingAssignment(grammarElement2);
					if (assignment2 != null && feature.equals(assignment2.getFeature()))
						return grammarElement2;
					// if (child.hasDirectSemanticElement() && child.getSemanticElement() != obj)
					// break;
					child = ((ICompositeNode) child).getFirstChild();
				}
			}
		}
		if (current.hasDirectSemanticElement() && current.getSemanticElement() != obj)
			return null;
		current = current.getParent();
	}
	return null;
}
 
Example 9
Source File: GrammarElementTitleSwitch.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected String addAssignemnt(String result, AbstractElement ele) {
	if (!showAssignment)
		return result;
	Assignment ass = GrammarUtil.containingAssignment(ele);
	result = ass != null ? ass.getFeature() + ass.getOperator() + result : result;
	return addQualified(result, ele);
}
 
Example 10
Source File: ValueSerializer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean equalsOrReplacesNode(EObject context, RuleCall ruleCall, Object value, INode node) {
	if (ruleCall != node.getGrammarElement())
		return false;
	Assignment ass = GrammarUtil.containingAssignment(ruleCall);
	if (GrammarUtil.isSingleAssignment(ass))
		return true;
	Object converted = converter.toValue(serialize(node), ruleCall.getRule().getName(), node);
	return converted != null && converted.equals(value);
}
 
Example 11
Source File: AbstractParseTreeConstructor.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected String serializeInternal(INode node) {
	if (type == null)
		return null;
	switch (type) {
		case CROSS_REFERENCE:
			String ref = crossRefSerializer.serializeCrossRef(eObjectConsumer.getEObject(),
					(CrossReference) element, (EObject) value, node);
			if (ref == null) {
				Assignment ass = GrammarUtil.containingAssignment(element);
				throw new XtextSerializationException("Could not serialize cross reference from "
						+ EmfFormatter.objPath(eObjectConsumer.getEObject()) + "." + ass.getFeature() + " to "
						+ EmfFormatter.objPath((EObject) value));
			}
			return ref;
		case KEYWORD:
			return keywordSerializer.serializeAssignedKeyword(eObjectConsumer.getEObject(),
					((Keyword) element), value, node);
		case TERMINAL_RULE_CALL:
			return valueSerializer.serializeAssignedValue(eObjectConsumer.getEObject(), (RuleCall) element,
					value, node);
		case ENUM_RULE_CALL:
			return enumLitSerializer.serializeAssignedEnumLiteral(eObjectConsumer.getEObject(),
					(RuleCall) element, value, node);
		case PARSER_RULE_CALL:
			return null;
		case DATATYPE_RULE_CALL:
			return valueSerializer.serializeAssignedValue(eObjectConsumer.getEObject(), (RuleCall) element,
					value, node);
		default:
			return null;
	}
}
 
Example 12
Source File: CheckProposalProvider.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Get the name assigned feature in an Assignment rule call.
 *
 * @param call
 *          a rule call
 * @return the assigned feature
 */
private String getAssignedFeature(final RuleCall call) {
  Assignment ass = GrammarUtil.containingAssignment(call);
  if (ass != null) {
    String result = ass.getFeature();
    if (result.equals(result.toLowerCase())) { // NOPMD
      result = Strings.toFirstUpper(result);
    }
    return result;
  }
  return null;
}
 
Example 13
Source File: ConcreteSyntaxConstraintProvider.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected void collectReachableRules(ParserRule pr, Set<ParserRule> rules, Set<ParserRule> visited) {
	if (!visited.add(pr))
		return;
	for (RuleCall rc : GrammarUtil.containedRuleCalls(pr))
		if (isParserRule(rc.getRule())) {
			if (GrammarUtil.containingAssignment(rc) != null)
				rules.add((ParserRule) rc.getRule());
			collectReachableRules((ParserRule) rc.getRule(), rules, visited);
		}
}
 
Example 14
Source File: TerminalsProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private String getAssignedFeature(RuleCall call) {
	Assignment ass = GrammarUtil.containingAssignment(call);
	if (ass != null) {
		String result = ass.getFeature();
		if (result.equals(result.toLowerCase()))
			result = Strings.toFirstUpper(result);
		return result;
	}
	return null;
}
 
Example 15
Source File: XtextCallHierarchyNodeLocationProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public ITextRegionWithLineInformation getTextRegion(EObject owner, EStructuralFeature feature, int indexInList) {
	Assignment assignment = GrammarUtil.containingAssignment(owner);
	if (assignment != null) {
		return getTextRegion(assignment);
	}
	return super.getTextRegion(owner, feature, indexInList);
}
 
Example 16
Source File: CodetemplatesProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private String getAssignedFeature(RuleCall call) {
	Assignment ass = GrammarUtil.containingAssignment(call);
	if (ass != null) {
		String result = ass.getFeature();
		if (result.equals(result.toLowerCase()))
			result = Strings.toFirstUpper(result);
		return result;
	}
	return null;
}
 
Example 17
Source File: NodeModelUtils.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * <p>Returns the node that covers all assigned values of the given object. It handles the semantics of {@link Action
 * actions} and {@link RuleCall unassigned rule calls}. The returned node will include unassigned surrounding leafs,
 * e.g. if you use something like {@code Parenthesized expressions} redundant parentheses will be part of the returned node.</p>
 * <p>Consider the following simple expression (a number literal): 
 * <pre>
 *   ((1))
 * </pre>
 * Assuming it was parsed from a grammar like this:
 * <pre>
 * Expression: Number | Parentheses;
 * Parentheses: '(' Expression ')';
 * Number: value=INT
 * </pre>
 * The actual node for the only semantic object that was produced from the input {@code ((1))} is the root node 
 * even though the minimal node would be the one with the text {@code 1}.
 * 
 * @param semanticObject the semantic object whose node should be provided.
 * @return the node that covers all assigned values of the given object.
 */
/* @Nullable */
public static ICompositeNode findActualNodeFor(/* @Nullable */ EObject semanticObject) {
	ICompositeNode node = getNode(semanticObject);
	if (node != null) {
		while(GrammarUtil.containingAssignment(node.getGrammarElement()) == null) {
			ICompositeNode parent = node.getParent();
			if (parent != null && !parent.hasDirectSemanticElement() && !GrammarUtil.isEObjectFragmentRuleCall(parent.getGrammarElement())) {
				node = parent;
			} else {
				break;
			}
		}
	}
	return node;
}
 
Example 18
Source File: Xtext2RailroadFactory.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public ISegmentFigure createNodeSegment(Keyword keyword) {
	NodeSegment nodeSegment = new NodeSegment(keyword, NodeType.RECTANGLE, keyword.getValue(), primitiveFactory,
			getTextRegion(keyword));
	Assignment containingAssignment = GrammarUtil.containingAssignment(keyword);
	return wrapCardinalitySegments(containingAssignment != null ? containingAssignment :keyword, nodeSegment);
}
 
Example 19
Source File: SequenceFeeder.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected Assignment getAssignment(AbstractElement ele) {
	Assignment ass = GrammarUtil.containingAssignment(ele);
	if (ass == null)
		throw new RuntimeException("Only Assigned " + ele.eClass().getName() + "s are allowed");
	return ass;
}
 
Example 20
Source File: AbstractSyntacticSequencer.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected String getUnassignedRuleCallToken(RuleCall ruleCall, INode node) {
	Assignment ass = GrammarUtil.containingAssignment(ruleCall);
	if (ass != null && !GrammarUtil.isBooleanAssignment(ass))
		throw new IllegalStateException("RuleCall is invalid; Can not determine token.");
	return getUnassignedRuleCallToken(contexts.peek().semanticObject, ruleCall, node);
}