Java Code Examples for org.eclipse.xtext.Assignment#getTerminal()

The following examples show how to use org.eclipse.xtext.Assignment#getTerminal() . 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: XtextValidationTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@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());
}
 
Example 2
Source File: Xtext2EcoreInterpretationContext.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
public void addFeature(Assignment assignment) throws TransformationException {
	final String featureName = assignment.getFeature();
	boolean isMultivalue = GrammarUtil.isMultipleAssignment(assignment);
	boolean isContainment = true;
	EClassifierInfo featureTypeInfo;

	if (GrammarUtil.isBooleanAssignment(assignment)) {
		checkNoFragmentRuleCall(assignment.getTerminal());
		EDataType eBoolean = GrammarUtil.findEBoolean(GrammarUtil.getGrammar(assignment));
		featureTypeInfo = getEClassifierInfoOrThrowException(eBoolean, assignment);
		isMultivalue = false;
	}
	else {
		AbstractElement terminal = assignment.getTerminal();
		if (terminal == null) {
			throw new TransformationException(TransformationErrorCode.NoSuchTypeAvailable, "Cannot derive type from incomplete assignment.", assignment);
		}
		checkNoFragmentRuleCall(terminal);
		EClassifier type = getTerminalType(terminal);
		isContainment = isContainmentAssignment(assignment);
		featureTypeInfo = getEClassifierInfoOrThrowException(type, assignment);
	}
	addFeature(featureName, featureTypeInfo, isMultivalue, isContainment, assignment);
}
 
Example 3
Source File: XtextProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Do not propose terminal fragments in hidden token sections.
 */
protected void completeHiddenTokens(Assignment assignment, final ContentAssistContext context,
		ICompletionProposalAcceptor acceptor) {
	CrossReference crossReference = (CrossReference) assignment.getTerminal();
	lookupCrossReference(crossReference, context, acceptor, new Predicate<IEObjectDescription>() {
		@Override
		public boolean apply(IEObjectDescription input) {
			if (input.getEClass() == XtextPackage.Literals.TERMINAL_RULE) {
				EObject object = resolve(input, context);
				if (object instanceof TerminalRule)
					return !((TerminalRule) object).isFragment();
			}
			return false;
		}
	});
}
 
Example 4
Source File: XtextScopingTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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);
}
 
Example 5
Source File: XtextLinkerTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testImplicitNamedParameterLinking_01() 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<MyParam>: rule=Rule<MyParam>;");
  _builder.newLine();
  _builder.append("Rule<MyParam>: name=ID child=Root<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());
}
 
Example 6
Source File: XtextLinkerTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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());
}
 
Example 7
Source File: AbstractSubCodeBuilderFragment.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Replies the rule used for defining the members of the given element.
 *
 * @param description description of the container.
 * @return the rule that is defining the members.
 */
protected AbstractRule getMemberRule(CodeElementExtractor.ElementDescription description) {
	for (final Assignment assignment : GrammarUtil.containedAssignments(description.getGrammarComponent())) {
		if (Objects.equals(getCodeBuilderConfig().getMemberCollectionExtensionGrammarName(), assignment.getFeature())) {
			if (assignment.getTerminal() instanceof RuleCall) {
				return ((RuleCall) assignment.getTerminal()).getRule();
			}
		}
	}
	return null;
}
 
Example 8
Source File: AbstractXtendProposalProvider.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public void completeMember_Modifiers(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
	if (assignment.getTerminal() instanceof RuleCall) {
		completeRuleCall(((RuleCall)assignment.getTerminal()), context, acceptor);
	}
	if (assignment.getTerminal() instanceof Keyword) {
		// subclasses may override
	}
}
 
Example 9
Source File: XbaseProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected String getConcreteSyntaxRuleName(Assignment assignment) {
	AbstractElement terminal = assignment.getTerminal();
	if (terminal instanceof CrossReference) {
		return getConcreteSyntaxRuleName((CrossReference) terminal);
	}
	String ruleName = null;
	if (terminal instanceof RuleCall) {
		ruleName = getConcreteSyntaxRuleName((RuleCall) terminal);
	}
	return ruleName;
}
 
Example 10
Source File: XtextProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Do not propose enum and parser rules inside of terminal rules.
 */
@Override
public void completeTerminalRuleCall_Rule(EObject model, Assignment assignment, ContentAssistContext context,
		ICompletionProposalAcceptor acceptor) {
	CrossReference crossReference = (CrossReference) assignment.getTerminal();
	lookupCrossReference(crossReference, context, acceptor, new Predicate<IEObjectDescription>() {
		@Override
		public boolean apply(IEObjectDescription input) {
			return input.getEClass() == XtextPackage.Literals.TERMINAL_RULE;
		}
	});
}
 
Example 11
Source File: XtextParserTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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);
	}
 
Example 12
Source File: XtextLinkerTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testImplicitNamedParameterLinking_02() 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<MyParam>: rule=Rule<true>;");
  _builder.newLine();
  _builder.append("Rule<MyParam>: name=ID child=Root<false>?;");
  _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.assertFalse(((LiteralCondition) _value).isTrue());
}
 
Example 13
Source File: CrossRefTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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());
}
 
Example 14
Source File: ParseTreeConstructorUtil.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public static List<AbstractElement> getNestedElementsFromAssignment(Assignment assignment) {
	AbstractElement t = assignment.getTerminal();
	ArrayList<AbstractElement> r = new ArrayList<AbstractElement>();
	if (t instanceof Keyword || t instanceof RuleCall || t instanceof CrossReference)
		r.add(t);
	else {
		r.addAll(GrammarUtil.containedKeywords(t));
		r.addAll(GrammarUtil.containedRuleCalls(t));
		r.addAll(GrammarUtil.containedCrossReferences(t));
	}
	return r;
}
 
Example 15
Source File: XbaseIdeContentProposalProvider.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void completeXMemberFeatureCall(final EObject model, final Assignment assignment, final ContentAssistContext context, final IIdeContentProposalAcceptor acceptor) {
  if ((model instanceof XMemberFeatureCall)) {
    AbstractElement _terminal = assignment.getTerminal();
    this.createReceiverProposals(((XMemberFeatureCall)model).getMemberCallTarget(), 
      ((CrossReference) _terminal), context, acceptor);
  } else {
    if ((model instanceof XAssignment)) {
      AbstractElement _terminal_1 = assignment.getTerminal();
      this.createReceiverProposals(((XAssignment)model).getAssignable(), 
        ((CrossReference) _terminal_1), context, acceptor);
    }
  }
}
 
Example 16
Source File: XbaseIdeContentProposalProvider.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void completeBinaryOperation(final EObject model, final Assignment assignment, final ContentAssistContext context, final IIdeContentProposalAcceptor acceptor) {
  if ((model instanceof XBinaryOperation)) {
    int _length = context.getPrefix().length();
    boolean _tripleEquals = (_length == 0);
    if (_tripleEquals) {
      final INode currentNode = context.getCurrentNode();
      final int offset = currentNode.getOffset();
      final int endOffset = currentNode.getEndOffset();
      if ((((offset < context.getOffset()) && (endOffset >= context.getOffset())) && (currentNode.getGrammarElement() instanceof CrossReference))) {
        return;
      }
    }
    int _endOffset = NodeModelUtils.findActualNodeFor(model).getEndOffset();
    int _offset = context.getOffset();
    boolean _lessEqualsThan = (_endOffset <= _offset);
    if (_lessEqualsThan) {
      AbstractElement _terminal = assignment.getTerminal();
      this.createReceiverProposals(((XExpression) model), ((CrossReference) _terminal), context, acceptor);
    } else {
      AbstractElement _terminal_1 = assignment.getTerminal();
      this.createReceiverProposals(((XBinaryOperation)model).getLeftOperand(), 
        ((CrossReference) _terminal_1), context, acceptor);
    }
  } else {
    final EObject previousModel = context.getPreviousModel();
    if ((previousModel instanceof XExpression)) {
      if (((context.getPrefix().length() == 0) && (NodeModelUtils.getNode(previousModel).getEndOffset() > context.getOffset()))) {
        return;
      }
      AbstractElement _terminal_2 = assignment.getTerminal();
      this.createReceiverProposals(((XExpression)previousModel), 
        ((CrossReference) _terminal_2), context, acceptor);
    }
  }
}
 
Example 17
Source File: AbstractSARLProposalProvider.java    From sarl with Apache License 2.0 5 votes vote down vote up
public void completeEventMember_Modifiers(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
	if (assignment.getTerminal() instanceof RuleCall) {
		completeRuleCall(((RuleCall)assignment.getTerminal()), context, acceptor);
	}
	if (assignment.getTerminal() instanceof Keyword) {
		// subclasses may override
	}
}
 
Example 18
Source File: AbstractGamlProposalProvider.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
public void completeClassicFacet_Key(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
	if (assignment.getTerminal() instanceof RuleCall) {
		completeRuleCall(((RuleCall)assignment.getTerminal()), context, acceptor);
	}
	if (assignment.getTerminal() instanceof Keyword) {
		// subclasses may override
	}
}
 
Example 19
Source File: AbstractGamlProposalProvider.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
public void completeArgumentPair_Op(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
	if (assignment.getTerminal() instanceof RuleCall) {
		completeRuleCall(((RuleCall)assignment.getTerminal()), context, acceptor);
	}
	if (assignment.getTerminal() instanceof Alternatives) {
		completeRuleCall(((RuleCall)((Alternatives)assignment.getTerminal()).getElements().get(0)), context, acceptor);
		completeRuleCall(((RuleCall)((Alternatives)assignment.getTerminal()).getElements().get(1)), context, acceptor);
		completeRuleCall(((RuleCall)((Alternatives)assignment.getTerminal()).getElements().get(2)), context, acceptor);
		completeRuleCall(((RuleCall)((Alternatives)assignment.getTerminal()).getElements().get(3)), context, acceptor);
		completeRuleCall(((RuleCall)((Alternatives)assignment.getTerminal()).getElements().get(4)), context, acceptor);
	}
}
 
Example 20
Source File: XtextLinkerTest.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testNamedParameterAdjustment() 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<MyParam>: rule=Rule<true>;");
  _builder.newLine();
  _builder.append("Rule<MyParam>: name=ID child=Root<false>?;");
  _builder.newLine();
  final String grammarAsString = _builder.toString();
  EObject _model = this.getModel(grammarAsString);
  final Grammar grammar = ((Grammar) _model);
  final ResourceSet resourceSet = grammar.eResource().getResourceSet();
  final Resource otherResource = resourceSet.createResource(URI.createURI("other.xtext"));
  StringConcatenation _builder_1 = new StringConcatenation();
  _builder_1.append("grammar test.SubLang with test.Lang");
  _builder_1.newLine();
  _builder_1.append("import \'http://test\'");
  _builder_1.newLine();
  _builder_1.append("Root<MyParam>: rule=super::Rule<true>;");
  _builder_1.newLine();
  LazyStringInputStream _lazyStringInputStream = new LazyStringInputStream(_builder_1.toString());
  otherResource.load(_lazyStringInputStream, null);
  EObject _head = IterableExtensions.<EObject>head(otherResource.getContents());
  final Grammar subGrammar = ((Grammar) _head);
  AbstractRule _head_1 = IterableExtensions.<AbstractRule>head(subGrammar.getRules());
  final ParserRule rootRule = ((ParserRule) _head_1);
  AbstractRule _last = IterableExtensions.<AbstractRule>last(grammar.getRules());
  final ParserRule parentRule = ((ParserRule) _last);
  AbstractElement _alternatives = parentRule.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.assertFalse(((LiteralCondition) _value).isTrue());
}