Search in sources :

Example 6 with GrammarElementTitleSwitch

use of org.eclipse.xtext.grammaranalysis.impl.GrammarElementTitleSwitch in project xtext-core by eclipse.

the class ContentAssistContextTestHelper method firstSetGrammarElementsToString.

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();
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) AbstractElement(org.eclipse.xtext.AbstractElement) XtextResource(org.eclipse.xtext.resource.XtextResource) GrammarElementTitleSwitch(org.eclipse.xtext.grammaranalysis.impl.GrammarElementTitleSwitch) ContentAssistContext(org.eclipse.xtext.ide.editor.contentassist.ContentAssistContext) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Pair(org.eclipse.xtext.xbase.lib.Pair)

Example 7 with GrammarElementTitleSwitch

use of org.eclipse.xtext.grammaranalysis.impl.GrammarElementTitleSwitch in project xtext-core by eclipse.

the class FirstSetComputationTest method assertFirstSet.

protected void assertFirstSet(String expectation, AbstractRule rule) {
    RuleCall ruleCall = XtextFactory.eINSTANCE.createRuleCall();
    ruleCall.setRule(rule);
    List<AbstractElement> firstSet = AntlrGrammarGenUtil.getFirstSet(ruleCall);
    StringBuilder actual = new StringBuilder();
    GrammarElementTitleSwitch stringifier = new GrammarElementTitleSwitch();
    for (int i = 0; i < firstSet.size(); i++) {
        if (i != 0)
            actual.append(", ");
        actual.append(stringifier.apply(firstSet.get(i)));
    }
    assertEquals(expectation, actual.toString());
}
Also used : GrammarElementTitleSwitch(org.eclipse.xtext.grammaranalysis.impl.GrammarElementTitleSwitch) AbstractElement(org.eclipse.xtext.AbstractElement) RuleCall(org.eclipse.xtext.RuleCall)

Example 8 with GrammarElementTitleSwitch

use of org.eclipse.xtext.grammaranalysis.impl.GrammarElementTitleSwitch in project xtext-core by eclipse.

the class SyntacticSequencerDiagnosticProvider method createUnexpectedStackStateDiagnostic.

@Override
public ISerializationDiagnostic createUnexpectedStackStateDiagnostic(EObject semanticObject, RuleCallStack stack, RuleCall popped, ISynState toConsume) {
    String poppedStr = popped == null ? "null" : new GrammarElementTitleSwitch().showAssignments().doSwitch(popped);
    StringBuilder buf = new StringBuilder();
    buf.append("Unexpected stack state.\n");
    buf.append("Found on top of the stack: " + poppedStr + "\n");
    buf.append("Expected: " + toConsume + "\n");
    buf.append("Rest of the stack: " + stack + "\n");
    return new SerializationDiagnostic(UNEXPECTED_STACK_TRACE, semanticObject, (ISerializationContext) null, grammarAccess.getGrammar(), buf.toString());
}
Also used : GrammarElementTitleSwitch(org.eclipse.xtext.grammaranalysis.impl.GrammarElementTitleSwitch)

Example 9 with GrammarElementTitleSwitch

use of org.eclipse.xtext.grammaranalysis.impl.GrammarElementTitleSwitch in project xtext-core by eclipse.

the class NodeModelUtils method compactDump.

private static void compactDump(INode node, boolean showHidden, String prefix, Appendable result) throws IOException {
    if (!showHidden && node instanceof ILeafNode && ((ILeafNode) node).isHidden())
        return;
    if (prefix.length() != 0) {
        result.append("\n");
        result.append(prefix);
    }
    if (node instanceof ICompositeNode) {
        if (node.getGrammarElement() != null)
            result.append(new GrammarElementTitleSwitch().showAssignments().doSwitch(node.getGrammarElement()));
        else
            result.append("(unknown)");
        String newPrefix = prefix + "  ";
        result.append(" {");
        BidiIterator<INode> children = ((ICompositeNode) node).getChildren().iterator();
        while (children.hasNext()) {
            INode child = children.next();
            compactDump(child, showHidden, newPrefix, result);
        }
        result.append("\n");
        result.append(prefix);
        result.append("}");
        SyntaxErrorMessage error = node.getSyntaxErrorMessage();
        if (error != null)
            result.append(" SyntaxError: [" + error.getIssueCode() + "] " + error.getMessage());
    } else if (node instanceof ILeafNode) {
        if (((ILeafNode) node).isHidden())
            result.append("hidden ");
        if (node.getGrammarElement() != null)
            result.append(new GrammarElementTitleSwitch().showAssignments().doSwitch(node.getGrammarElement()));
        else
            result.append("(unknown)");
        result.append(" => '");
        result.append(node.getText());
        result.append("'");
        SyntaxErrorMessage error = node.getSyntaxErrorMessage();
        if (error != null)
            result.append(" SyntaxError: [" + error.getIssueCode() + "] " + error.getMessage());
    } else if (node == null) {
        result.append("(null)");
    } else {
        result.append("unknown type ");
        result.append(node.getClass().getName());
    }
}
Also used : GrammarElementTitleSwitch(org.eclipse.xtext.grammaranalysis.impl.GrammarElementTitleSwitch) INode(org.eclipse.xtext.nodemodel.INode) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) SyntaxErrorMessage(org.eclipse.xtext.nodemodel.SyntaxErrorMessage) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode)

Example 10 with GrammarElementTitleSwitch

use of org.eclipse.xtext.grammaranalysis.impl.GrammarElementTitleSwitch in project xtext-core by eclipse.

the class GrammarPDAProviderTest method toListString.

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");
}
Also used : ISerState(org.eclipse.xtext.serializer.analysis.ISerState) GrammarElementTitleSwitch(org.eclipse.xtext.grammaranalysis.impl.GrammarElementTitleSwitch) PdaListFormatter(org.eclipse.xtext.util.formallang.PdaListFormatter) RuleCall(org.eclipse.xtext.RuleCall)

Aggregations

GrammarElementTitleSwitch (org.eclipse.xtext.grammaranalysis.impl.GrammarElementTitleSwitch)13 AbstractElement (org.eclipse.xtext.AbstractElement)4 RuleCall (org.eclipse.xtext.RuleCall)4 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)3 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)3 INode (org.eclipse.xtext.nodemodel.INode)3 ISerState (org.eclipse.xtext.serializer.analysis.ISerState)3 PdaListFormatter (org.eclipse.xtext.util.formallang.PdaListFormatter)3 EObject (org.eclipse.emf.ecore.EObject)2 Grammar (org.eclipse.xtext.Grammar)2 Pda (org.eclipse.xtext.util.formallang.Pda)2 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)1 AbstractRule (org.eclipse.xtext.AbstractRule)1 CrossReference (org.eclipse.xtext.CrossReference)1 MultilineCommentReplacer (org.eclipse.xtext.formatting2.internal.MultilineCommentReplacer)1 SinglelineCodeCommentReplacer (org.eclipse.xtext.formatting2.internal.SinglelineCodeCommentReplacer)1 SinglelineDocCommentReplacer (org.eclipse.xtext.formatting2.internal.SinglelineDocCommentReplacer)1 ContentAssistContext (org.eclipse.xtext.ide.editor.contentassist.ContentAssistContext)1 SyntaxErrorMessage (org.eclipse.xtext.nodemodel.SyntaxErrorMessage)1 XtextResource (org.eclipse.xtext.resource.XtextResource)1