Java Code Examples for org.eclipse.xtext.grammaranalysis.impl.GrammarElementTitleSwitch#apply()

The following examples show how to use org.eclipse.xtext.grammaranalysis.impl.GrammarElementTitleSwitch#apply() . 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: AssertStructureAcceptor.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected void assertElement(AbstractElement element) {
	AbstractRule expectedRule = null;
	if (stack.isEmpty()) {
		// FIXME: this doesn't work if the serialized EObject is not the model's root. 
		// expectedRule = EcoreUtil2.getContainerOfType(element, Grammar.class).getRules().get(0);
		return;
	} else
		expectedRule = stack.peek().getRule();
	AbstractRule actualRule = EcoreUtil2.getContainerOfType(element, AbstractRule.class);
	if (expectedRule != actualRule) {
		GrammarElementTitleSwitch formatter = new GrammarElementTitleSwitch().showQualified().showRule();
		String elementName = formatter.apply(element);
		String expName = expectedRule.getName();
		String actualName = actualRule.getName();
		String msg = "Element " + elementName + " should be in rule " + expName + " but it is in " + actualName;
		throw new IllegalStateException(msg);
	}
}
 
Example 2
Source File: SerializerPDA.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public String toString() {
	GrammarElementTitleSwitch fmt = new GrammarElementTitleSwitch().hideCardinality().showQualified()
			.showAssignments();
	switch (type) {
		case ELEMENT:
			return fmt.apply(grammarElement);
		case POP:
			return "<<" + fmt.apply(grammarElement);
		case PUSH:
			return ">>" + fmt.apply(grammarElement);
		case START:
			return "start";
		case STOP:
			return "stop";
	}
	return "";
}
 
Example 3
Source File: AssertStructureAcceptor.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected void assertPop(RuleCall call) {
	RuleCall expected = stack.pop();
	if (call != expected) {
		GrammarElementTitleSwitch formatter = new GrammarElementTitleSwitch().showQualified().showRule();
		String expectedName = formatter.apply(expected);
		String actualName = formatter.apply(call);
		throw new IllegalStateException("Expected rule call " + expectedName + " but got " + actualName);
	}
}
 
Example 4
Source File: ContentAssistContextTestHelper.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public String firstSetGrammarElementsToString(final ContentAssistContextFactory factory) {
  final int offset = this.document.indexOf(this.cursor);
  Preconditions.checkArgument((offset >= 0), "you forgot to provide a cursor");
  final String doc = this.document.replace(this.cursor, "");
  final XtextResource res = this.parse(doc);
  factory.setPool(Executors.newCachedThreadPool());
  TextRegion _textRegion = new TextRegion(0, 0);
  final ContentAssistContext[] ctxs = factory.create(doc, _textRegion, offset, res);
  final GrammarElementTitleSwitch f = new GrammarElementTitleSwitch().showAssignments().showQualified().showRule();
  StringConcatenation _builder = new StringConcatenation();
  {
    Iterable<Pair<Integer, ContentAssistContext>> _indexed = IterableExtensions.<ContentAssistContext>indexed(((Iterable<? extends ContentAssistContext>)Conversions.doWrapArray(ctxs)));
    for(final Pair<Integer, ContentAssistContext> ctx : _indexed) {
      _builder.append("context");
      Integer _key = ctx.getKey();
      _builder.append(_key);
      _builder.append(" {");
      _builder.newLineIfNotEmpty();
      {
        ImmutableList<AbstractElement> _firstSetGrammarElements = ctx.getValue().getFirstSetGrammarElements();
        for(final AbstractElement ele : _firstSetGrammarElements) {
          _builder.append("\t");
          String _name = ele.eClass().getName();
          _builder.append(_name, "\t");
          _builder.append(": ");
          String _apply = f.apply(ele);
          _builder.append(_apply, "\t");
          _builder.newLineIfNotEmpty();
        }
      }
      _builder.append("}");
      _builder.newLine();
    }
  }
  return _builder.toString();
}
 
Example 5
Source File: GrammarPDAProviderTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private String toListString(final Pda<ISerState, RuleCall> pda) {
  final GrammarElementTitleSwitch ts = new GrammarElementTitleSwitch().showAssignments().hideCardinality().showQualified();
  final PdaListFormatter<ISerState, RuleCall> formatter = new PdaListFormatter<ISerState, RuleCall>();
  final Function<ISerState, String> _function = (ISerState it) -> {
    String _switchResult = null;
    ISerState.SerStateType _type = it.getType();
    if (_type != null) {
      switch (_type) {
        case START:
          _switchResult = "start";
          break;
        case STOP:
          _switchResult = "stop";
          break;
        default:
          _switchResult = ts.apply(it.getGrammarElement());
          break;
      }
    } else {
      _switchResult = ts.apply(it.getGrammarElement());
    }
    return _switchResult;
  };
  formatter.setStateFormatter(_function);
  formatter.setStackitemFormatter(new GrammarElementTitleSwitch().showAssignments().hideCardinality());
  formatter.sortFollowers();
  String _format = formatter.format(pda);
  return (_format + "\n");
}