use of org.eclipse.xtext.Assignment in project xtext-core by eclipse.
the class NodeModelUtilsTest method testFindActualSemanticObjectFor_03.
@Test
public void testFindActualSemanticObjectFor_03() 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.indexOf('='));
EObject object = NodeModelUtils.findActualSemanticObjectFor(leafNode);
assertTrue(object instanceof Assignment);
}
use of org.eclipse.xtext.Assignment in project xtext-core by eclipse.
the class XtextParserTest method testParseCrossRef.
@Test
public void testParseCrossRef() throws Exception {
Grammar model = (Grammar) getModel("grammar foo with org.eclipse.xtext.common.Terminals " + "generate foo 'bar' as boo " + "Model returns boo::Model : 'a' stuff+=Stuff*; " + "Stuff returns boo::Stuff : 'stuff' name=ID refersTo=[boo::Stuff];");
AbstractRule rule = model.getRules().get(1);
Group group = (Group) rule.getAlternatives();
Assignment assignment = (Assignment) group.getElements().get(2);
CrossReference reference = (CrossReference) assignment.getTerminal();
assertEquals("boo", reference.getType().getMetamodel().getAlias());
// assertWithXtend("'boo'", "eAllContents.typeSelect(xtext::CrossReference).first().type.metamodel.alias", model);
assertEquals("Stuff", reference.getType().getClassifier().getName());
// assertWithXtend("'Stuff'", "eAllContents.typeSelect(xtext::CrossReference).first().type.classifier.name", model);
}
use of org.eclipse.xtext.Assignment in project xtext-core by eclipse.
the class AntlrGrammarGenUtil method getPredicatedElement.
/**
* @since 2.9
*/
public static AbstractElement getPredicatedElement(AbstractElement element) {
if (element.isPredicated()) {
return doGetPredicatedElement(element);
}
if (element instanceof Assignment) {
return getPredicatedElement(((Assignment) element).getTerminal());
}
if (element instanceof RuleCall) {
final RuleCall ruleCall = (RuleCall) element;
final AbstractRule calledRule = ruleCall.getRule();
Group group = (Group) calledRule.getAlternatives();
AbstractElement first = group.getElements().get(0);
AbstractElement result = getPredicatedElement(first);
if (ruleCall.getArguments().isEmpty()) {
return result;
}
EcoreUtil.Copier copier = new EcoreUtil.Copier() {
private static final long serialVersionUID = 1L;
@Override
public EObject copy(EObject eObject) {
if (eObject instanceof ParameterReference) {
ParameterReference paramRef = (ParameterReference) eObject;
Parameter parameter = paramRef.getParameter();
if (calledRule == GrammarUtil.containingRule(parameter)) {
for (NamedArgument givenArgument : ruleCall.getArguments()) {
if (givenArgument.getParameter() == parameter) {
EObject result = copy(givenArgument.getValue());
return result;
}
}
throw new IllegalStateException();
}
}
return super.copy(eObject);
}
};
AbstractElement clone = (AbstractElement) copier.copy(result);
copier.copyReferences();
return clone;
}
return doGetPredicatedElement(element);
}
use of org.eclipse.xtext.Assignment in project xtext-core by eclipse.
the class XtextScopingTest method testScope_01.
@Test
public void testScope_01() {
ParserRule rule = (ParserRule) GrammarUtil.findRuleForName(grammar, "AbstractCallOverridenParserRule");
assertNotNull(rule);
Group group = (Group) rule.getAlternatives();
Assignment ass = (Assignment) group.getElements().get(1);
assertNotNull(ass);
RuleCall call = (RuleCall) ass.getTerminal();
AbstractRule calledRule = call.getRule();
assertNotNull(calledRule);
XtextGrammarAccess ga = (XtextGrammarAccess) get(IGrammarAccess.class);
String text = get(ICrossReferenceSerializer.class).serializeCrossRef(call, ga.getRuleCallAccess().getRuleAbstractRuleCrossReference_0_0(), calledRule, null);
assertNotNull(text);
assertEquals(calledRule.getName(), text);
}
use of org.eclipse.xtext.Assignment in project xtext-core by eclipse.
the class XtextValidationTest method testRuleCalledSuper.
@Test
public void testRuleCalledSuper() throws Exception {
XtextResource resource = getResourceFromString("grammar com.acme.Bar with org.eclipse.xtext.common.Terminals\n" + "generate metamodel 'myURI'\n" + "Model: super=super;\n" + "super: name=ID;");
IResourceValidator validator = get(IResourceValidator.class);
List<Issue> issues = validator.validate(resource, CheckMode.FAST_ONLY, CancelIndicator.NullImpl);
assertEquals(issues.toString(), 1, issues.size());
assertEquals("Discouraged rule name 'super'", issues.get(0).getMessage());
Grammar grammar = (Grammar) resource.getContents().get(0);
AbstractRule model = grammar.getRules().get(0);
Assignment assignment = (Assignment) model.getAlternatives();
RuleCall ruleCall = (RuleCall) assignment.getTerminal();
assertSame(grammar.getRules().get(1), ruleCall.getRule());
}
Aggregations