use of org.eclipse.xtext.Alternatives 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.Alternatives in project xtext-core by eclipse.
the class XtextLinkerTest method testGuardLinking.
@Test
public void testGuardLinking() 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>: <MyArg> name=ID | <!MyArg> name=STRING;");
_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);
AbstractElement _alternatives = rootRule.getAlternatives();
final Alternatives alternatives = ((Alternatives) _alternatives);
AbstractElement _head_1 = IterableExtensions.<AbstractElement>head(alternatives.getElements());
Condition _guardCondition = ((Group) _head_1).getGuardCondition();
final ParameterReference firstGuard = ((ParameterReference) _guardCondition);
Assert.assertEquals(IterableExtensions.<Parameter>head(rootRule.getParameters()), firstGuard.getParameter());
AbstractElement _last = IterableExtensions.<AbstractElement>last(alternatives.getElements());
Condition _guardCondition_1 = ((Group) _last).getGuardCondition();
final Negation secondGuard = ((Negation) _guardCondition_1);
Condition _value = secondGuard.getValue();
Assert.assertEquals(IterableExtensions.<Parameter>head(rootRule.getParameters()), ((ParameterReference) _value).getParameter());
}
use of org.eclipse.xtext.Alternatives in project xtext-core by eclipse.
the class NodeModelUtilsTest method testFindActualSemanticObjectFor_07.
@Test
public void testFindActualSemanticObjectFor_07() throws Exception {
String grammarString = "grammar foo.Bar with org.eclipse.xtext.common.Terminals generate foo 'bar' Model:(name=ID|name=STRING?)*;";
Grammar grammar = (Grammar) getModel(grammarString);
ILeafNode star = NodeModelUtils.findLeafNodeAtOffset(NodeModelUtils.getNode(grammar), grammarString.indexOf('*'));
EObject object = NodeModelUtils.findActualSemanticObjectFor(star);
assertTrue(object instanceof Alternatives);
ILeafNode questionMark = NodeModelUtils.findLeafNodeAtOffset(NodeModelUtils.getNode(grammar), grammarString.indexOf('?'));
object = NodeModelUtils.findActualSemanticObjectFor(questionMark);
assertTrue(object instanceof Assignment);
}
use of org.eclipse.xtext.Alternatives in project xtext-core by eclipse.
the class NodeModelUtilsTest method testFindNodesForFeature_Elements.
@Test
public void testFindNodesForFeature_Elements() throws Exception {
Grammar grammar = (Grammar) getModel("grammar foo.Bar with org.eclipse.xtext.common.Terminals generate foo 'bar' Model:name='foo'|name='bar';");
Alternatives body = (Alternatives) grammar.getRules().get(0).getAlternatives();
List<INode> nodes = NodeModelUtils.findNodesForFeature(body, XtextPackage.eINSTANCE.getCompoundElement_Elements());
assertEquals(2, nodes.size());
assertEquals("name='foo'", nodes.get(0).getText());
assertEquals("name='bar'", nodes.get(1).getText());
}
use of org.eclipse.xtext.Alternatives in project xtext-core by eclipse.
the class GrammarParserTest method testPrecedencies_01.
@Test
public void testPrecedencies_01() throws Exception {
Alternatives alternatives = (Alternatives) getModel("'keyword' | 'keyword' & 'keyword' | 'keyword'");
assertNotNull(alternatives);
assertEquals(3, alternatives.getElements().size());
}
Aggregations