Java Code Examples for org.eclipse.xtext.GrammarUtil#isOptionalCardinality()

The following examples show how to use org.eclipse.xtext.GrammarUtil#isOptionalCardinality() . 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: MatcherState.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
protected boolean isOptional(AbstractElement ele) {
	if (GrammarUtil.isOptionalCardinality(ele) || ele instanceof Action)
		return true;
	if (ele instanceof CompoundElement) {
		List<EObject> children = ele.eContents();
		if (children.isEmpty() && getBuilder().filter(ele))
			return true;
		if (ele instanceof Alternatives) {
			for (AbstractElement a : ((Alternatives) ele).getElements())
				if (isOptional(a))
					return true;
			return false;
		}
		for (EObject e : children)
			if (e instanceof AbstractElement && !isOptional((AbstractElement) e))
				return false;
		return true;
	} else
		return false;
}
 
Example 2
Source File: TerminalRuleInterpreter.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public Boolean caseNegatedToken(NegatedToken object) {
	boolean result = false;
	OUTER: do {
		if (eof())
			break OUTER;
		IMarker marker = markerFactory.mark();
		if (!doSwitch(object.getTerminal())) {
			result = true;
			input.incOffset();
			continue OUTER;
		}
		marker.rollback();
		break OUTER;
	} while(GrammarUtil.isMultipleCardinality(object));
	return result || GrammarUtil.isOptionalCardinality(object);
}
 
Example 3
Source File: RequiredRuleNameComputer.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @since 2.14
 */
protected String[][] getAdjustedRequiredRuleNames(Param param, AbstractElement elementToParse,
		String originalRuleName) {
	String adjustedRuleName = adjustRuleName(originalRuleName, param);
	if (!(GrammarUtil.isOptionalCardinality(elementToParse)
			|| GrammarUtil.isOneOrMoreCardinality(elementToParse))) {
		return new String[][] { { adjustedRuleName } };
	}
	EObject container = elementToParse.eContainer();
	if (container instanceof Group) {
		Group group = (Group) container;
		List<AbstractElement> filteredElements = getFilteredElements(group.getElements(), param);
		int idx = filteredElements.indexOf(elementToParse) + 1;
		if (idx != filteredElements.size()) {
			String adjustedSecondRule = getAdjustedSecondRule(param, group, idx);
			return getRuleNamesInGroup(param, elementToParse, adjustedRuleName, adjustedSecondRule);
		}
	}
	return new String[][] { { adjustedRuleName } };
}
 
Example 4
Source File: FollowElementComputer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private boolean isAlternativeWithEmptyPath(AbstractElement abstractElement) {
	if (abstractElement instanceof Alternatives) {
		Alternatives alternatives = (Alternatives) abstractElement;
		for(AbstractElement path: alternatives.getElements()) {
			if (GrammarUtil.isOptionalCardinality(path))
				return true;
		}
	}
	return false;
}
 
Example 5
Source File: RuleWithoutInstantiationInspector.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Boolean caseAlternatives(Alternatives object) {
	if (GrammarUtil.isOptionalCardinality(object))
		return Boolean.FALSE;
	for(AbstractElement element: object.getElements()) {
		if (!doSwitch(element))
			return Boolean.FALSE;
	}
	return Boolean.TRUE;
}
 
Example 6
Source File: TerminalRuleInterpreter.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Boolean caseUntilToken(UntilToken object) {
	if (GrammarUtil.isOptionalCardinality(object) || GrammarUtil.isMultipleCardinality(object))
		throw new IllegalStateException("cardinality has to be default for until tokens");
	IMarker marker = markerFactory.mark();
	while (!eof()) {
		if (doSwitch(object.getTerminal())) {
			return true;
		}
		input.incOffset();
	}
	marker.rollback();
	return false;
}
 
Example 7
Source File: ValidEntryRuleInspector.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Pair<Boolean, Boolean> caseAlternatives(Alternatives object) {
	boolean valid = true;
	boolean instantiated = !GrammarUtil.isOptionalCardinality(object);
	for(AbstractElement element: object.getElements()) {
		Pair<Boolean, Boolean> elementResult = doSwitch(element);
		valid &= elementResult.getFirst().booleanValue();
		instantiated &= elementResult.getSecond().booleanValue();
	}
	return Tuples.create(valid, instantiated);
}
 
Example 8
Source File: ParseTreeConstructorUtil.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
private static boolean isOptionalGroup(Group group) {
	if (GrammarUtil.containedAssignments(group).size() != 1)
		return false;
	if (GrammarUtil.isOptionalCardinality(group))
		return true;
	if (group.eContainer() instanceof Group)
		return isOptionalGroup((Group) group.eContainer());
	if (group.eContainer() instanceof UnorderedGroup)
		return isOptionalUnorderedGroup((UnorderedGroup) group.eContainer());
	return false;
}
 
Example 9
Source File: TerminalRuleInterpreter.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Boolean caseAlternatives(Alternatives object) {
	boolean result = false;
	OUTER: do {
		for (AbstractElement element: object.getElements()) {
			if (doSwitch(element)) {
				result = true;
				continue OUTER;
			}
		}
		break OUTER;
	} while(GrammarUtil.isMultipleCardinality(object));
	return result || GrammarUtil.isOptionalCardinality(object);
}
 
Example 10
Source File: Xtext2RailroadFactory.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected ISegmentFigure wrapCardinalitySegments(AbstractElement element, ISegmentFigure segment) {
	ISegmentFigure result = segment;
	if (GrammarUtil.isMultipleCardinality(element)) {
		result = new LoopSegment(element, result, primitiveFactory);
	}
	if (GrammarUtil.isOptionalCardinality(element)) {
		result = new BypassSegment(element, result, primitiveFactory);
	}
	return result;
}
 
Example 11
Source File: ParserBasedContentAssistContextFactory.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private boolean isAlternativeWithEmptyPath(AbstractElement abstractElement) {
	if (abstractElement instanceof Alternatives) {
		Alternatives alternatives = (Alternatives) abstractElement;
		for(AbstractElement path: alternatives.getElements()) {
			if (GrammarUtil.isOptionalCardinality(path))
				return true;
		}
	}
	return false;
}
 
Example 12
Source File: FirstSetComputer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Boolean caseGroup(Group object) {
	for(AbstractElement element: object.getElements()) {
		if (!doSwitch(element)) {
			return GrammarUtil.isOptionalCardinality(object);
		}
	}
	return GrammarUtil.isOptionalCardinality(object);
}
 
Example 13
Source File: AntlrContentAssistGrammarGenerator.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected CharSequence _compileRule(final UnorderedGroup it, final Grammar grammar, final AntlrOptions options) {
  CharSequence _xblockexpression = null;
  {
    final Function1<AbstractElement, Boolean> _function = (AbstractElement it_1) -> {
      boolean _isOptionalCardinality = GrammarUtil.isOptionalCardinality(it_1);
      return Boolean.valueOf((!_isOptionalCardinality));
    };
    final boolean hasMandatoryContent = IterableExtensions.<AbstractElement>exists(it.getElements(), _function);
    StringConcatenation _builder = new StringConcatenation();
    String _contentAssistRuleName = AntlrGrammarGenUtil.getContentAssistRuleName(GrammarUtil.containingRule(it));
    _builder.append(_contentAssistRuleName);
    _builder.append("__");
    String _gaElementIdentifier = this._grammarAccessExtensions.gaElementIdentifier(AntlrGrammarGenUtil.<UnorderedGroup>getOriginalElement(it));
    _builder.append(_gaElementIdentifier);
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.append("@init {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("int stackSize = keepStackSize();");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("getUnorderedGroupHelper().enter(grammarAccess.");
    String _gaRuleElementAccessor = this._grammarAccessExtensions.gaRuleElementAccessor(AntlrGrammarGenUtil.<UnorderedGroup>getOriginalElement(it));
    _builder.append(_gaRuleElementAccessor, "\t\t");
    _builder.append(");");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append(":");
    _builder.newLine();
    _builder.append("\t");
    String _contentAssistRuleName_1 = AntlrGrammarGenUtil.getContentAssistRuleName(GrammarUtil.containingRule(it));
    _builder.append(_contentAssistRuleName_1, "\t");
    _builder.append("__");
    String _gaElementIdentifier_1 = this._grammarAccessExtensions.gaElementIdentifier(AntlrGrammarGenUtil.<UnorderedGroup>getOriginalElement(it));
    _builder.append(_gaElementIdentifier_1, "\t");
    _builder.append("__0");
    _builder.newLineIfNotEmpty();
    {
      if (hasMandatoryContent) {
        _builder.append("\t");
        _builder.append("{getUnorderedGroupHelper().canLeave(grammarAccess.");
        String _gaRuleElementAccessor_1 = this._grammarAccessExtensions.gaRuleElementAccessor(AntlrGrammarGenUtil.<UnorderedGroup>getOriginalElement(it));
        _builder.append(_gaRuleElementAccessor_1, "\t");
        _builder.append(")}?");
        _builder.newLineIfNotEmpty();
      } else {
        _builder.append("\t");
        _builder.append("?");
        _builder.newLine();
      }
    }
    _builder.append(";");
    _builder.newLine();
    _builder.append("finally {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("getUnorderedGroupHelper().leave(grammarAccess.");
    String _gaRuleElementAccessor_2 = this._grammarAccessExtensions.gaRuleElementAccessor(AntlrGrammarGenUtil.<UnorderedGroup>getOriginalElement(it));
    _builder.append(_gaRuleElementAccessor_2, "\t");
    _builder.append(");");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.append("restoreStackSize(stackSize);");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    CharSequence _ruleImpl = this.ruleImpl(it, grammar, options);
    _builder.append(_ruleImpl);
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    CharSequence _ruleImpl_1 = this.ruleImpl(it, grammar, options, 0);
    _builder.append(_ruleImpl_1);
    _builder.newLineIfNotEmpty();
    _xblockexpression = _builder;
  }
  return _xblockexpression;
}
 
Example 14
Source File: GrammarWithoutLeftRecursionInspector.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Boolean caseAssignment(Assignment object) {
	Boolean result = doSwitch(object.getTerminal());
	return result || GrammarUtil.isOptionalCardinality(object);
}
 
Example 15
Source File: AbstractNFAState.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected void collectOutgoing(AbstractElement element, Set<AbstractElement> visited, boolean isRuleCall,
		AbstractElement loopCenter) {
	if (element instanceof Group || element instanceof UnorderedGroup) {
		List<AbstractElement> elements = ((CompoundElement) element).getElements();
		switch (builder.getDirection()) {
			case FORWARD:
				int j = 0;
				while (j < elements.size()) {
					addOutgoing(elements.get(j), visited, isRuleCall, loopCenter);
					if (!GrammarUtil.isOptionalCardinality(elements.get(j)))
						break;
					j++;
				}
				// add transitions to the next sibling of this element if all children are optional
				if (j >= elements.size())
					collectOutgoingByContainer(element, visited, isRuleCall, loopCenter);
				break;
			case BACKWARD:
				int i = elements.size() - 1;
				while (i >= 0) {
					addOutgoing(elements.get(i), visited, isRuleCall, loopCenter);
					if (!GrammarUtil.isOptionalCardinality(elements.get(i)))
						break;
					i--;
				}
				// add transitions to the next sibling of this element if all children are optional
				if (i < 0)
					collectOutgoingByContainer(element, visited, isRuleCall, loopCenter);
				break;
		}
	} else if (element instanceof Alternatives) {
		boolean hasOptional = false;
		for (AbstractElement e : ((Alternatives) element).getElements()) {
			hasOptional |= GrammarUtil.isOptionalCardinality(e);
			addOutgoing(e, visited, isRuleCall, loopCenter);
		}
		if (hasOptional)
			collectOutgoingByContainer(element, visited, isRuleCall, loopCenter);
	} else if (element instanceof Assignment)
		addOutgoing(((Assignment) element).getTerminal(), visited, isRuleCall, loopCenter);
	else if (element instanceof CrossReference)
		addOutgoing(((CrossReference) element).getTerminal(), visited, isRuleCall, loopCenter);
	else if (GrammarUtil.isEObjectRuleCall(element)) {
		addOutgoing(((RuleCall) element).getRule().getAlternatives(), visited, true, loopCenter);
		collectOutgoingByContainer(element, visited, isRuleCall, loopCenter);
	} else {
		if (GrammarUtil.isMultipleCardinality(element) && !filter(element))
			addOutgoing(element, visited, isRuleCall, element);
		collectOutgoingByContainer(element, visited, isRuleCall, loopCenter);
	}
}
 
Example 16
Source File: GrammarWithoutLeftRecursionInspector.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Boolean caseAbstractElement(AbstractElement object) {
	return GrammarUtil.isOptionalCardinality(object);
}
 
Example 17
Source File: FollowElementCalculator.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public boolean isOptional(AbstractElement element) {
	return GrammarUtil.isOptionalCardinality(element);
}
 
Example 18
Source File: RuleWithoutInstantiationInspector.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Boolean caseRuleCall(RuleCall object) {
	if (GrammarUtil.isOptionalCardinality(object))
		return Boolean.FALSE;
	return object.getRule() == null || (object.getRule() instanceof ParserRule && !GrammarUtil.isDatatypeRule((ParserRule) object.getRule()));
}
 
Example 19
Source File: ValidEntryRuleInspector.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Pair<Boolean, Boolean> caseRuleCall(RuleCall object) {
	boolean instantiated = !GrammarUtil.isOptionalCardinality(object) && ((object.getRule() == null) || 
		(object.getRule() instanceof ParserRule && !GrammarUtil.isDatatypeRule((ParserRule) object.getRule())));
	return Tuples.create(instantiated, instantiated);
}
 
Example 20
Source File: GrammarWithoutLeftRecursionInspector.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Boolean caseCrossReference(CrossReference object) {
	Boolean result = doSwitch(object.getTerminal());
	return result || GrammarUtil.isOptionalCardinality(object);
}