Search in sources :

Example 1 with XtextGrammarAccess

use of org.eclipse.xtext.services.XtextGrammarAccess in project xtext-core by eclipse.

the class XtextFormatter method configureFormatting.

@Override
protected void configureFormatting(FormattingConfig cfg) {
    XtextGrammarAccess g = (XtextGrammarAccess) getGrammarAccess();
    cfg.setAutoLinewrap(120);
    // Comments
    cfg.setLinewrap(0, 1, 2).before(g.getSL_COMMENTRule());
    cfg.setLinewrap(0, 1, 2).before(g.getML_COMMENTRule());
    cfg.setLinewrap(0, 1, 1).after(g.getML_COMMENTRule());
    // general keywords
    for (Pair<Keyword, Keyword> pair : g.findKeywordPairs("(", ")")) {
        cfg.setNoSpace().after(pair.getFirst());
        cfg.setNoSpace().before(pair.getSecond());
    }
    for (Pair<Keyword, Keyword> pair : g.findKeywordPairs("[", "]")) {
        cfg.setNoSpace().after(pair.getFirst());
        cfg.setNoSpace().before(pair.getSecond());
    }
    for (Pair<Keyword, Keyword> pair : g.findKeywordPairs("<", ">")) {
        cfg.setNoSpace().after(pair.getFirst());
        cfg.setNoSpace().before(pair.getSecond());
    }
    for (Keyword comma : g.findKeywords(",")) {
        cfg.setNoSpace().before(comma);
    }
    for (Pair<Keyword, Keyword> pair : g.findKeywordPairs(":", ";")) {
        cfg.setNoSpace().before(pair.getFirst());
        cfg.setNoSpace().before(pair.getSecond());
        cfg.setLinewrap().after(pair.getFirst());
        cfg.setLinewrap().after(pair.getSecond());
        cfg.setIndentation(pair.getFirst(), pair.getSecond());
        cfg.setLinewrap(0, 0, 1).range(pair.getFirst(), pair.getSecond());
    }
    // Grammar
    GrammarElements gr = g.getGrammarAccess();
    cfg.setNoLinewrap().between(gr.getWithKeyword_2_0(), gr.getUsedGrammarsAssignment_2_1());
    cfg.setNoSpace().between(gr.getDefinesHiddenTokensAssignment_3_0(), gr.getLeftParenthesisKeyword_3_1());
    cfg.setLinewrap(2).between(gr.getNameAssignment_1(), gr.getMetamodelDeclarationsAssignment_4());
    cfg.setLinewrap(2).between(gr.getGroup_2(), gr.getMetamodelDeclarationsAssignment_4());
    cfg.setLinewrap(2).between(gr.getGroup_3(), gr.getMetamodelDeclarationsAssignment_4());
    cfg.setLinewrap().after(gr.getMetamodelDeclarationsAssignment_4());
    cfg.setLinewrap(2).between(gr.getMetamodelDeclarationsAssignment_4(), gr.getRulesAssignment_5());
    cfg.setLinewrap(2).before(gr.getRulesAssignment_5());
    // ParserRule
    ParserRuleElements pr = g.getParserRuleAccess();
    cfg.setNoSpace().before(pr.getLeftParenthesisKeyword_2_1());
    // TypeRef
    TypeRefElements typeRef = g.getTypeRefAccess();
    cfg.setNoSpace().around(typeRef.getColonColonKeyword_0_1());
    // UnorderedGroup
    UnorderedGroupElements uge = g.getUnorderedGroupAccess();
    cfg.setNoSpace().around(uge.getUnorderedGroupElementsAction_1_0());
    // AbstractToken
    AbstractTokenWithCardinalityElements at = g.getAbstractTokenWithCardinalityAccess();
    cfg.setNoSpace().before(at.getCardinalityAsteriskKeyword_1_0_1());
    cfg.setNoSpace().before(at.getCardinalityPlusSignKeyword_1_0_2());
    cfg.setNoSpace().before(at.getCardinalityQuestionMarkKeyword_1_0_0());
    // Action
    ActionElements ac = g.getActionAccess();
    cfg.setNoSpace().around(ac.getOperatorAssignment_2_2());
    cfg.setNoSpace().around(ac.getFullStopKeyword_2_0());
    cfg.setNoSpace().after(ac.getLeftCurlyBracketKeyword_0());
    cfg.setNoSpace().before(ac.getRightCurlyBracketKeyword_3());
    // Assignment
    AssignmentElements as = g.getAssignmentAccess();
    cfg.setNoSpace().around(as.getOperatorAssignment_2());
    // CrossReference
    CrossReferenceElements cr = g.getCrossReferenceAccess();
    cfg.setNoSpace().around(cr.getVerticalLineKeyword_2_0());
    // TerminalToken
    TerminalTokenElements tt = g.getTerminalTokenAccess();
    cfg.setNoSpace().before(tt.getCardinalityAssignment_1());
    // NegatedToken
    NegatedTokenElements ne = g.getNegatedTokenAccess();
    cfg.setNoSpace().after(ne.getExclamationMarkKeyword_0());
    // UntilToken
    UntilTokenElements ut = g.getUntilTokenAccess();
    cfg.setNoSpace().around(ut.getHyphenMinusGreaterThanSignKeyword_0());
    // CharacterRange
    CharacterRangeElements cre = g.getCharacterRangeAccess();
    cfg.setNoSpace().around(cre.getFullStopFullStopKeyword_1_1());
    // EnumLiteralDeclaration
    EnumLiteralDeclarationElements eld = g.getEnumLiteralDeclarationAccess();
    cfg.setNoSpace().around(eld.getEqualsSignKeyword_1_0());
    // GuardCondition
    NegationElements na = g.getNegationAccess();
    cfg.setNoSpace().after(na.getExclamationMarkKeyword_1_1());
    // RuleCall
    RuleCallElements rca = g.getRuleCallAccess();
    cfg.setNoSpace().before(rca.getLessThanSignKeyword_1_0());
    // NamedArgument
    NamedArgumentElements naa = g.getNamedArgumentAccess();
    cfg.setNoSpace().around(naa.getCalledByNameAssignment_0_1());
    cfg.setNoSpace().around(naa.getValueAssignment_1());
    cfg.setNoSpace().around(naa.getParameterAssignment_0_0());
    // @Override
    AnnotationElements a = g.getAnnotationAccess();
    cfg.setNoSpace().between(a.getCommercialAtKeyword_0(), a.getNameAssignment_1());
    cfg.setLinewrap().after(g.getAnnotationRule());
    cfg.setLinewrap(2).before(g.getAnnotationRule());
// saveDebugGraphvizDiagram("XtextFormatting.dot");
}
Also used : AnnotationElements(org.eclipse.xtext.services.XtextGrammarAccess.AnnotationElements) Keyword(org.eclipse.xtext.Keyword) ParserRuleElements(org.eclipse.xtext.services.XtextGrammarAccess.ParserRuleElements) CharacterRangeElements(org.eclipse.xtext.services.XtextGrammarAccess.CharacterRangeElements) NamedArgumentElements(org.eclipse.xtext.services.XtextGrammarAccess.NamedArgumentElements) TypeRefElements(org.eclipse.xtext.services.XtextGrammarAccess.TypeRefElements) AbstractTokenWithCardinalityElements(org.eclipse.xtext.services.XtextGrammarAccess.AbstractTokenWithCardinalityElements) XtextGrammarAccess(org.eclipse.xtext.services.XtextGrammarAccess) UnorderedGroupElements(org.eclipse.xtext.services.XtextGrammarAccess.UnorderedGroupElements) ActionElements(org.eclipse.xtext.services.XtextGrammarAccess.ActionElements) EnumLiteralDeclarationElements(org.eclipse.xtext.services.XtextGrammarAccess.EnumLiteralDeclarationElements) RuleCallElements(org.eclipse.xtext.services.XtextGrammarAccess.RuleCallElements) GrammarElements(org.eclipse.xtext.services.XtextGrammarAccess.GrammarElements) CrossReferenceElements(org.eclipse.xtext.services.XtextGrammarAccess.CrossReferenceElements) AssignmentElements(org.eclipse.xtext.services.XtextGrammarAccess.AssignmentElements) NegatedTokenElements(org.eclipse.xtext.services.XtextGrammarAccess.NegatedTokenElements) NegationElements(org.eclipse.xtext.services.XtextGrammarAccess.NegationElements) TerminalTokenElements(org.eclipse.xtext.services.XtextGrammarAccess.TerminalTokenElements) UntilTokenElements(org.eclipse.xtext.services.XtextGrammarAccess.UntilTokenElements)

Example 2 with XtextGrammarAccess

use of org.eclipse.xtext.services.XtextGrammarAccess in project xtext-core by eclipse.

the class XtextGrammarSerializationTest method _testXtestSerializationSelfTest.

public void _testXtestSerializationSelfTest() throws Exception {
    Resource res = this.<XtextResourceSet>get(XtextResourceSet.class).createResource(URI.createURI("myfile.xtext"), ContentHandler.UNSPECIFIED_CONTENT_TYPE);
    res.getContents().add(this.<XtextGrammarAccess>get(XtextGrammarAccess.class).getGrammar());
    OutputStream outputStream = new ByteArrayOutputStream();
    res.save(outputStream, Collections.<Object, Object>emptyMap());
}
Also used : XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) XtextGrammarAccess(org.eclipse.xtext.services.XtextGrammarAccess)

Example 3 with XtextGrammarAccess

use of org.eclipse.xtext.services.XtextGrammarAccess in project xtext-core by eclipse.

the class XtextScopingTest method testScope_01.

@Test
public void testScope_01() {
    ParserRule rule = (ParserRule) GrammarUtil.findRuleForName(grammar, "AbstractCallOverridenParserRule");
    assertNotNull(rule);
    Group group = (Group) rule.getAlternatives();
    Assignment ass = (Assignment) group.getElements().get(1);
    assertNotNull(ass);
    RuleCall call = (RuleCall) ass.getTerminal();
    AbstractRule calledRule = call.getRule();
    assertNotNull(calledRule);
    XtextGrammarAccess ga = (XtextGrammarAccess) get(IGrammarAccess.class);
    String text = get(ICrossReferenceSerializer.class).serializeCrossRef(call, ga.getRuleCallAccess().getRuleAbstractRuleCrossReference_0_0(), calledRule, null);
    assertNotNull(text);
    assertEquals(calledRule.getName(), text);
}
Also used : Assignment(org.eclipse.xtext.Assignment) ParserRule(org.eclipse.xtext.ParserRule) Group(org.eclipse.xtext.Group) ICrossReferenceSerializer(org.eclipse.xtext.parsetree.reconstr.ITokenSerializer.ICrossReferenceSerializer) IGrammarAccess(org.eclipse.xtext.IGrammarAccess) AbstractRule(org.eclipse.xtext.AbstractRule) XtextGrammarAccess(org.eclipse.xtext.services.XtextGrammarAccess) RuleCall(org.eclipse.xtext.RuleCall) Test(org.junit.Test)

Example 4 with XtextGrammarAccess

use of org.eclipse.xtext.services.XtextGrammarAccess in project xtext-core by eclipse.

the class XtextSerializerTest method testGroup.

@Test
public void testGroup() {
    XtextGrammarAccess grammarAccess = get(XtextGrammarAccess.class);
    String string = get(ISerializer.class).serialize(grammarAccess.getGrammarAccess().getGroup_2());
    Assert.assertEquals("(\"with\" usedGrammars+=[Grammar|GrammarID] (\",\" usedGrammars+=[Grammar|GrammarID])*)?", string);
}
Also used : XtextGrammarAccess(org.eclipse.xtext.services.XtextGrammarAccess) Test(org.junit.Test)

Aggregations

XtextGrammarAccess (org.eclipse.xtext.services.XtextGrammarAccess)4 Test (org.junit.Test)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 AbstractRule (org.eclipse.xtext.AbstractRule)1 Assignment (org.eclipse.xtext.Assignment)1 Group (org.eclipse.xtext.Group)1 IGrammarAccess (org.eclipse.xtext.IGrammarAccess)1 Keyword (org.eclipse.xtext.Keyword)1 ParserRule (org.eclipse.xtext.ParserRule)1 RuleCall (org.eclipse.xtext.RuleCall)1 ICrossReferenceSerializer (org.eclipse.xtext.parsetree.reconstr.ITokenSerializer.ICrossReferenceSerializer)1 XtextResource (org.eclipse.xtext.resource.XtextResource)1 XtextResourceSet (org.eclipse.xtext.resource.XtextResourceSet)1 AbstractTokenWithCardinalityElements (org.eclipse.xtext.services.XtextGrammarAccess.AbstractTokenWithCardinalityElements)1 ActionElements (org.eclipse.xtext.services.XtextGrammarAccess.ActionElements)1 AnnotationElements (org.eclipse.xtext.services.XtextGrammarAccess.AnnotationElements)1 AssignmentElements (org.eclipse.xtext.services.XtextGrammarAccess.AssignmentElements)1 CharacterRangeElements (org.eclipse.xtext.services.XtextGrammarAccess.CharacterRangeElements)1