use of org.eclipse.xtext.RuleCall in project xtext-core by eclipse.
the class XtextValidator method checkCurrentMustBeUnassigned.
private void checkCurrentMustBeUnassigned(final AbstractElement element) {
final ParserRule rule = GrammarUtil.containingParserRule(element);
if (GrammarUtil.isDatatypeRule(rule))
return;
XtextSwitch<Boolean> visitor = new XtextSwitch<Boolean>() {
private boolean isNull = !rule.isFragment();
@Override
public Boolean caseAbstractElement(AbstractElement object) {
return isNull;
}
@Override
public Boolean caseAlternatives(Alternatives object) {
final boolean wasIsNull = isNull;
boolean localIsNull = wasIsNull;
for (AbstractElement element : object.getElements()) {
isNull = wasIsNull;
localIsNull &= doSwitch(element);
}
isNull = localIsNull;
return isNull;
}
@Override
public Boolean caseUnorderedGroup(UnorderedGroup object) {
final boolean wasIsNull = isNull;
boolean localIsNull = wasIsNull;
for (AbstractElement element : object.getElements()) {
isNull = wasIsNull;
localIsNull |= doSwitch(element);
}
isNull = localIsNull;
return isNull;
}
@Override
public Boolean caseAssignment(Assignment object) {
isNull = false;
return isNull;
}
@Override
public Boolean caseGroup(Group object) {
for (AbstractElement element : object.getElements()) doSwitch(element);
return isNull;
}
@Override
public Boolean caseAction(Action object) {
if (object == element) {
if (!(isNull && !isMany(object))) {
error("An unassigned action is not allowed, when the 'current' was already created.", object, null);
checkDone();
}
}
isNull = false;
return isNull;
}
@Override
public Boolean caseRuleCall(RuleCall object) {
if (object == element) {
AbstractRule calledRule = object.getRule();
if (calledRule instanceof ParserRule && ((ParserRule) calledRule).isFragment()) {
isNull = false;
return isNull;
}
if (!(isNull && !isMany(object))) {
error("An unassigned rule call is not allowed, when the 'current' was already created.", object, null);
checkDone();
}
}
return doSwitch(object.getRule());
}
@Override
public Boolean caseParserRule(ParserRule object) {
isNull = GrammarUtil.isDatatypeRule(object);
return isNull;
}
@Override
public Boolean caseTerminalRule(TerminalRule object) {
isNull = true;
return isNull;
}
public boolean isMany(AbstractElement element) {
return GrammarUtil.isMultipleCardinality(element) || ((element.eContainer() instanceof AbstractElement) && isMany((AbstractElement) element.eContainer()));
}
};
visitor.doSwitch(rule.getAlternatives());
}
use of org.eclipse.xtext.RuleCall in project xtext-core by eclipse.
the class XtextValidator method checkAssignedActionAfterAssignment.
@Check
public void checkAssignedActionAfterAssignment(final Action action) {
if (action.getFeature() != null) {
ParserRule rule = GrammarUtil.containingParserRule(action);
if (rule.isFragment() && !rule.isWildcard()) {
error("An action is not allowed in fragments.", action, null);
return;
}
XtextSwitch<Boolean> visitor = new XtextSwitch<Boolean>() {
private boolean assignedActionAllowed = false;
@Override
public Boolean caseAbstractElement(AbstractElement object) {
return assignedActionAllowed;
}
@Override
public Boolean caseAlternatives(Alternatives object) {
boolean wasActionAllowed = assignedActionAllowed;
boolean localActionAllowed = true;
for (AbstractElement element : object.getElements()) {
assignedActionAllowed = wasActionAllowed;
localActionAllowed &= doSwitch(element);
}
assignedActionAllowed = wasActionAllowed || (localActionAllowed && !GrammarUtil.isOptionalCardinality(object));
return assignedActionAllowed;
}
@Override
public Boolean caseUnorderedGroup(UnorderedGroup object) {
boolean wasActionAllowed = assignedActionAllowed;
boolean localActionAllowed = false;
for (AbstractElement element : object.getElements()) {
assignedActionAllowed = wasActionAllowed;
localActionAllowed |= doSwitch(element);
}
assignedActionAllowed = wasActionAllowed || (localActionAllowed && !GrammarUtil.isOptionalCardinality(object));
return assignedActionAllowed;
}
@Override
public Boolean caseAssignment(Assignment object) {
assignedActionAllowed = assignedActionAllowed || !GrammarUtil.isOptionalCardinality(object);
return assignedActionAllowed;
}
@Override
public Boolean caseGroup(Group object) {
boolean wasAssignedActionAllowed = assignedActionAllowed;
for (AbstractElement element : object.getElements()) doSwitch(element);
assignedActionAllowed = wasAssignedActionAllowed || (assignedActionAllowed && !GrammarUtil.isOptionalCardinality(object));
return assignedActionAllowed;
}
@Override
public Boolean caseAction(Action object) {
if (object == action) {
if (!assignedActionAllowed) {
error("An action is not allowed in fragments and when the current may still be unassigned.", null);
checkDone();
}
}
assignedActionAllowed = true;
return assignedActionAllowed;
}
@Override
public Boolean caseRuleCall(RuleCall object) {
if (object.getRule() == null)
return assignedActionAllowed;
assignedActionAllowed = assignedActionAllowed || doSwitch(object.getRule()) && !GrammarUtil.isOptionalCardinality(object);
return assignedActionAllowed;
}
@Override
public Boolean caseParserRule(ParserRule object) {
assignedActionAllowed = !GrammarUtil.isDatatypeRule(object) && !object.isFragment();
return assignedActionAllowed;
}
@Override
public Boolean caseTerminalRule(TerminalRule object) {
return assignedActionAllowed;
}
};
visitor.doSwitch(rule.getAlternatives());
}
}
use of org.eclipse.xtext.RuleCall in project xtext-core by eclipse.
the class XtextLinkerTest method testQualifiedRuleCall_03.
@Test
public void testQualifiedRuleCall_03() throws Exception {
StringConcatenation _builder = new StringConcatenation();
_builder.append("grammar test with org.eclipse.xtext.common.Terminals");
_builder.newLine();
_builder.append("generate test \'http://test\'");
_builder.newLine();
_builder.append("Rule: name=ID;");
_builder.newLine();
_builder.append("terminal STRING: super;");
_builder.newLine();
_builder.append("terminal super: \'super\';");
_builder.newLine();
final String grammarAsString = _builder.toString();
final XtextResource resource = this.getResourceFromString(grammarAsString);
EObject _get = resource.getContents().get(0);
Grammar grammar = ((Grammar) _get);
AbstractRule _get_1 = grammar.getRules().get(1);
final TerminalRule string = ((TerminalRule) _get_1);
AbstractElement _alternatives = string.getAlternatives();
final RuleCall callToSuper = ((RuleCall) _alternatives);
Assert.assertEquals(IterableExtensions.<AbstractRule>last(grammar.getRules()), callToSuper.getRule());
}
use of org.eclipse.xtext.RuleCall in project xtext-core by eclipse.
the class XtextLinkerTest method testImplicitNamedParameterLinking_02.
@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());
}
use of org.eclipse.xtext.RuleCall in project xtext-core by eclipse.
the class XtextLinkerTest method testNamedParameterAdjustment.
@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());
}
Aggregations