Java Code Examples for org.eclipse.xtext.Alternatives#getElements()

The following examples show how to use org.eclipse.xtext.Alternatives#getElements() . 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: AbstractAntlrGrammarGenerator.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
protected String _dataTypeEbnf2(final Alternatives it, final boolean supportActions) {
  StringConcatenation _builder = new StringConcatenation();
  {
    EList<AbstractElement> _elements = it.getElements();
    boolean _hasElements = false;
    for(final AbstractElement e : _elements) {
      if (!_hasElements) {
        _hasElements = true;
      } else {
        _builder.appendImmediate("\n    |", "");
      }
      String _dataTypeEbnf = this.dataTypeEbnf(e, supportActions);
      _builder.append(_dataTypeEbnf);
    }
  }
  _builder.newLineIfNotEmpty();
  return _builder.toString();
}
 
Example 2
Source File: KeywordBasedValueConverter.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @throws IllegalArgumentException if the rule is not a datatype rule or does not fulfill
 *   the pattern <pre>RuleName: 'keyword' | 'other';</pre>
 */
@Override
public void setRule(AbstractRule rule) {
	this.rule = rule;
	if (!GrammarUtil.isDatatypeRule(rule))
		throw new IllegalArgumentException(rule.getName() + " is not a data type rule");
	if (!(rule.getAlternatives() instanceof Alternatives) && !(rule.getAlternatives() instanceof Keyword)) {
		throw new IllegalArgumentException(rule.getName() + " is not a simple keyword nor an alternative");
	}
	if (rule.getAlternatives() instanceof Keyword) {
		keywords = ImmutableSet.of(keywordToString((Keyword) rule.getAlternatives()));
	} else {
		Alternatives alternatives = (Alternatives) rule.getAlternatives();
		ImmutableSet.Builder<String> builder = ImmutableSet.builder();
		for(AbstractElement element: alternatives.getElements()) {
			if (!(element instanceof Keyword)) {
				throw new IllegalArgumentException(rule.getName() + "'s body does not contain an alternative of keywords");
			}
			builder.add(keywordToString((Keyword) element));
		}
		keywords = builder.build();
	}
}
 
Example 3
Source File: KeywordAlternativeConverter.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @throws IllegalArgumentException if the rule is not a datatype rule or does not match
 *   the pattern <pre>RuleName: 'keyword' | 'other';</pre>
 */
@Override
public void setRule(AbstractRule rule) {
	if (!GrammarUtil.isDatatypeRule(rule))
		throw new IllegalArgumentException(rule.getName() + " is not a data type rule");
	if (!(rule.getAlternatives() instanceof Alternatives)) {
		if (rule.getAlternatives() instanceof RuleCall) {
			delegateRule = ((RuleCall) rule.getAlternatives()).getRule();
			keywords = Collections.emptySet();
			return;
		}
		throw mismatchedRuleBody(rule);
	}
	Alternatives alternatives = (Alternatives) rule.getAlternatives();
	ImmutableSet.Builder<String> builder = ImmutableSet.builder();
	for (AbstractElement element : alternatives.getElements()) {
		processElement(element, rule, builder);
	}
	keywords = builder.build();
}
 
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: 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 6
Source File: FollowElementCalculator.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Boolean caseAlternatives(Alternatives object) {
	boolean more = false;
	for(AbstractElement element: object.getElements()) {
		more = doSwitch(element) || more;
	}
	return more || isOptional(object);
}
 
Example 7
Source File: PredicatedElementTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected String _toXtext(final Alternatives alt) {
  EList<AbstractElement> _elements = alt.getElements();
  String _elvis = null;
  String _cardinality = alt.getCardinality();
  if (_cardinality != null) {
    _elvis = _cardinality;
  } else {
    _elvis = "";
  }
  String _plus = (")" + _elvis);
  final Function1<AbstractElement, CharSequence> _function = (AbstractElement it) -> {
    return this.toXtext(it);
  };
  return IterableExtensions.<AbstractElement>join(_elements, "(", " | ", _plus, _function);
}
 
Example 8
Source File: KeywordCollector.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public Boolean caseAlternatives(final Alternatives object) {
  for (final AbstractElement element : object.getElements()) {
    if (!doSwitch(element)) {
      return false;
    }
  }
  return true;
}
 
Example 9
Source File: TerminalRuleToLexerBody.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public String caseAlternatives(Alternatives object) {
	result.append('(');
	boolean first = true;
	for(AbstractElement elem: object.getElements()) {
		if (!first) result.append('|');
		first = false;
		doSwitch(elem);
	}
	result.append(')').append(Strings.emptyIfNull(object.getCardinality()));
	return "";
}
 
Example 10
Source File: FirstSetComputer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Boolean caseAlternatives(Alternatives object) {
	boolean result = false;
	for(AbstractElement element: object.getElements()) {
		if (doSwitch(element)) {
			result = true;
		}
	}
	return result || GrammarUtil.isOptionalCardinality(object);
}
 
Example 11
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 12
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 13
Source File: CurrentTypeFinder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Boolean caseAlternatives(Alternatives object) {
	EClassifier wasType = currentType;
	List<EClassifier> alternativeTypes = null;
	for(AbstractElement element: object.getElements()) {
		currentType = wasType;
		if (doSwitch(element))
			return true;
		if (currentType != wasType) {
			if (alternativeTypes != null)
				alternativeTypes.add(currentType);
			else
				alternativeTypes = Lists.newArrayList(currentType);
		}
	}
	if (alternativeTypes != null) {
		if (alternativeTypes.size() != object.getElements().size()) {
			alternativeTypes.add(wasType);
		}
		currentType = null;
		for(EClassifier classifier: alternativeTypes) {
			currentType = getCompatibleType(currentType, classifier, object);
		}
	}
	if (object == stopElement)
		return true;
	if (GrammarUtil.isOptionalCardinality(object))
		currentType = getCompatibleType(currentType, wasType, object);
	return false;
}
 
Example 14
Source File: GrammarWithoutLeftRecursionInspector.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Boolean caseAlternatives(Alternatives object) {
	Boolean result = Boolean.FALSE;
	for(AbstractElement element: object.getElements()) {
		if (doSwitch(element))
			result = Boolean.TRUE;
	}
	return result || GrammarUtil.isOptionalCardinality(object);
}
 
Example 15
Source File: ElementTypeCalculator.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public EClassifier caseAlternatives(Alternatives object) {
	final Set<EClassifier> types = Sets.newLinkedHashSet();
	for (AbstractElement group : object.getElements())
		types.add(doSwitch(group));
	try {
		return classifierInfos.getCompatibleTypeNameOf(types, true);
	} catch (IllegalArgumentException e) {
		return null;
	}
}
 
Example 16
Source File: SLCommentPrefixCalculator.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Boolean caseAlternatives(Alternatives object) {
	boolean result = true;
	for(AbstractElement elem: object.getElements()) {
		result &= doSwitch(elem);
	}
	return result;
}
 
Example 17
Source File: ParserBasedContentAssistContextFactory.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Boolean caseAlternatives(Alternatives object) {
	boolean more = false;
	for(AbstractElement element: object.getElements()) {
		more = doSwitch(element) || more;
	}
	return more || isOptional(object);
}
 
Example 18
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 19
Source File: TerminalRuleToLexerBody.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public String caseAlternatives(Alternatives object) {
	result.append('(');
	boolean first = true;
	for(AbstractElement elem: object.getElements()) {
		if (!first) result.append('|');
		first = false;
		doSwitch(elem);
	}
	result.append(')').append(Strings.emptyIfNull(object.getCardinality()));
	return "";
}
 
Example 20
Source File: AntlrContentAssistGrammarGenerator.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected CharSequence _compileRule(final Alternatives it, final Grammar grammar, final AntlrOptions options) {
  StringConcatenation _builder = new StringConcatenation();
  String _contentAssistRuleName = AntlrGrammarGenUtil.getContentAssistRuleName(GrammarUtil.containingRule(it));
  _builder.append(_contentAssistRuleName);
  _builder.append("__");
  String _gaElementIdentifier = this._grammarAccessExtensions.gaElementIdentifier(AntlrGrammarGenUtil.<Alternatives>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");
  _builder.append("}");
  _builder.newLine();
  _builder.append(":");
  _builder.newLine();
  _builder.append("\t");
  {
    EList<AbstractElement> _elements = it.getElements();
    boolean _hasElements = false;
    for(final AbstractElement element : _elements) {
      if (!_hasElements) {
        _hasElements = true;
      } else {
        _builder.appendImmediate("\n|", "\t");
      }
      String _ebnf = this.ebnf(element, options, false);
      _builder.append(_ebnf, "\t");
    }
  }
  _builder.newLineIfNotEmpty();
  _builder.append(";");
  _builder.newLine();
  _builder.append("finally {");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("restoreStackSize(stackSize);");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  return _builder;
}