use of org.eclipse.xtext.Assignment in project xtext-core by eclipse.
the class ConcreteSyntaxConstraintProvider method createElement.
protected ISyntaxConstraint createElement(EObject obj) {
if (!(obj instanceof AbstractElement))
return null;
AbstractElement ele = (AbstractElement) obj;
boolean multiple = false;
boolean optional = false;
EClass semanticType = null;
while (true) {
multiple = multiple || isMultipleCardinality(ele);
optional = optional || isOptionalCardinality(ele);
if (ele.eContainer() instanceof ParserRule && ((ParserRule) ele.eContainer()).getType().getClassifier() instanceof EClass)
semanticType = (EClass) ((ParserRule) ele.eContainer()).getType().getClassifier();
if (ele instanceof Assignment) {
return createElement(ConstraintType.ASSIGNMENT, ele, semanticType, multiple, optional);
} else if (ele instanceof Group || ele instanceof UnorderedGroup) {
CompoundElement comp = (CompoundElement) ele;
AbstractElement lastChild = null;
for (AbstractElement o : comp.getElements()) if (containsRelevantElement(o)) {
if (lastChild == null)
lastChild = o;
else {
List<AbstractElement> c = new ArrayList<AbstractElement>(comp.getElements());
List<ISyntaxConstraint> e = createSummarizedAssignments(comp, c, semanticType, optional);
if (e.size() == 1 && c.size() == 0)
return e.get(0);
return createElement(ConstraintType.GROUP, ele, c, e, semanticType, multiple, optional);
}
}
if (lastChild == null)
return null;
ele = lastChild;
continue;
} else if (ele instanceof Alternatives) {
int relevantChildren = 0;
AbstractElement lastChild = null;
for (AbstractElement o : ((CompoundElement) ele).getElements()) if (containsRelevantElement(o)) {
relevantChildren++;
lastChild = o;
}
if (relevantChildren < ((CompoundElement) ele).getElements().size())
optional = true;
if (relevantChildren > 1)
return createElement(ConstraintType.ALTERNATIVE, ele, semanticType, multiple, optional);
if (lastChild == null)
return null;
ele = lastChild;
continue;
} else if (ele instanceof Action) {
semanticType = (EClass) ((Action) ele).getType().getClassifier();
return createElement(ConstraintType.ACTION, ele, semanticType, multiple, optional);
} else if (ele instanceof RuleCall) {
AbstractRule rule = ((RuleCall) ele).getRule();
if (rule.getType().getClassifier() instanceof EClass) {
ele = rule.getAlternatives();
continue;
}
}
return null;
}
}
use of org.eclipse.xtext.Assignment in project xtext-core by eclipse.
the class CrossRefTest method testGetLinkedObjects.
@Test
public void testGetLinkedObjects() throws Exception {
XtextResource r = getResourceFromString("type TypeA extends TypeB type TypeB extends TypeA type AnotherType extends TypeA");
Main model = (Main) r.getParseResult().getRootASTElement();
ILeafNode leaf = NodeModelUtils.findLeafNodeAtOffset(r.getParseResult().getRootNode(), 6);
assertEquals(3, model.getTypes().size());
EObject context = model.getTypes().get(0);
Assignment asExtends = get(LangATestLanguageGrammarAccess.class).getTypeAccess().getExtendsAssignment_2_1();
CrossReference xref = (CrossReference) asExtends.getTerminal();
EReference ref = GrammarUtil.getReference(xref, context.eClass());
assertEquals(1, getLinkingService().getLinkedObjects(context, ref, leaf).size());
}
use of org.eclipse.xtext.Assignment in project xtext-core by eclipse.
the class XtextLinkerTest method testNamedParameterLinking.
@Test
public void testNamedParameterLinking() throws Exception {
StringConcatenation _builder = new StringConcatenation();
_builder.append("grammar test.Lang with org.eclipse.xtext.common.Terminals");
_builder.newLine();
_builder.append("generate test \'http://test\'");
_builder.newLine();
_builder.append("Root<MyArg>: rule=Rule<MyArg>;");
_builder.newLine();
_builder.append("Rule<MyParam>: name=ID child=Root<MyArg=MyParam>?;");
_builder.newLine();
final String grammarAsString = _builder.toString();
EObject _model = this.getModel(grammarAsString);
final Grammar grammar = ((Grammar) _model);
AbstractRule _head = IterableExtensions.<AbstractRule>head(grammar.getRules());
final ParserRule rootRule = ((ParserRule) _head);
AbstractRule _last = IterableExtensions.<AbstractRule>last(grammar.getRules());
final ParserRule lastRule = ((ParserRule) _last);
AbstractElement _alternatives = lastRule.getAlternatives();
AbstractElement _last_1 = IterableExtensions.<AbstractElement>last(((Group) _alternatives).getElements());
final Assignment lastAssignment = ((Assignment) _last_1);
AbstractElement _terminal = lastAssignment.getTerminal();
final RuleCall ruleCall = ((RuleCall) _terminal);
final NamedArgument argument = IterableExtensions.<NamedArgument>head(ruleCall.getArguments());
Assert.assertEquals(IterableExtensions.<Parameter>head(rootRule.getParameters()), argument.getParameter());
Condition _value = argument.getValue();
Assert.assertEquals(IterableExtensions.<Parameter>head(lastRule.getParameters()), ((ParameterReference) _value).getParameter());
}
use of org.eclipse.xtext.Assignment in project xtext-core by eclipse.
the class NodeModelUtilsTest method testFindActualSemanticObjectFor_06.
@Test
public void testFindActualSemanticObjectFor_06() throws Exception {
String grammarString = "grammar foo.Bar with org.eclipse.xtext.common.Terminals generate foo 'bar' Model: name=ID;";
Grammar grammar = (Grammar) getModel(grammarString);
ILeafNode leafNode = NodeModelUtils.findLeafNodeAtOffset(NodeModelUtils.getNode(grammar), grammarString.lastIndexOf(" "));
EObject object = NodeModelUtils.findActualSemanticObjectFor(leafNode);
assertTrue(object instanceof Assignment);
}
use of org.eclipse.xtext.Assignment in project xtext-core by eclipse.
the class NodeModelUtilsTest method testFindNodesForFeature_Cardinality_2.
@Test
public void testFindNodesForFeature_Cardinality_2() throws Exception {
Grammar grammar = (Grammar) getModel("grammar foo.Bar with org.eclipse.xtext.common.Terminals generate foo 'bar' Model: (name+='foo'*)?;");
Assignment assignment = (Assignment) grammar.getRules().get(0).getAlternatives();
List<INode> nodes = NodeModelUtils.findNodesForFeature(assignment, XtextPackage.eINSTANCE.getAbstractElement_Cardinality());
assertEquals(2, nodes.size());
assertEquals("*", nodes.get(0).getText());
assertEquals("?", nodes.get(1).getText());
}
Aggregations