Skip to content
This repository has been archived by the owner on Apr 21, 2023. It is now read-only.

Commit

Permalink
[#1489] Java based Xtext formatter (formatter2 API).
Browse files Browse the repository at this point in the history
Signed-off-by: Arne Deutsch <[email protected]>
  • Loading branch information
ArneDeutsch committed Nov 6, 2020
1 parent 4839b3c commit 3a7bd31
Show file tree
Hide file tree
Showing 11 changed files with 454 additions and 181 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ terminal ML_COMMENT2:

@Override
terminal ID:
super;
super;
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ public void setUp() throws Exception {

// check
assertFalse(model.equals(result));
String expectedModel = LineDelimiters.toPlatform("grammar foo with org.eclipse.xtext.common.Terminals\n\nHONOLULU:\n name=ID;");
String expectedModel = LineDelimiters.toPlatform("grammar foo with org.eclipse.xtext.common.Terminals\n\nHONOLULU:\n name=ID;\n");
assertEquals(expectedModel, result);
}

@Test public void testSelf() {
Grammar g = getGrammarAccess().getGrammar();
Grammar g = load(URI.createURI("classpath:/org/eclipse/xtext/Xtext.xtext"));
for (AbstractRule r : g.getRules()) {
// AbstractRule r = GrammarUtil.findRuleForName(g, "GrammarID");
// System.out.println("serializing :" + r.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,30 @@
*******************************************************************************/
package org.eclipse.xtext.serializer;

import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtext.AbstractRule;
import org.eclipse.xtext.Grammar;
import org.eclipse.xtext.GrammarUtil;
import org.eclipse.xtext.Group;
import org.eclipse.xtext.RuleCall;
import org.eclipse.xtext.TerminalRule;
import org.eclipse.xtext.XtextStandaloneSetup;
import org.eclipse.xtext.common.services.TerminalsGrammarAccess;
import org.eclipse.xtext.grammarinheritance.services.BaseInheritanceTestLanguageGrammarAccess;
import org.eclipse.xtext.grammarinheritance.services.InheritanceTestLanguageGrammarAccess;
import org.eclipse.xtext.nodemodel.ICompositeNode;
import org.eclipse.xtext.resource.XtextResourceSet;
import org.eclipse.xtext.service.GrammarProvider;
import org.eclipse.xtext.services.XtextGrammarAccess;
import org.eclipse.xtext.testing.serializer.SerializerTestHelper;
import org.eclipse.xtext.tests.AbstractXtextTests;
import org.eclipse.xtext.util.Pair;
import org.eclipse.xtext.util.Tuples;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

import com.google.common.collect.Lists;
import com.google.inject.Inject;
import com.google.inject.Provider;

/**
* @author Moritz Eysholdt - Initial contribution and API
Expand All @@ -38,11 +40,6 @@ public class XtextSerializerTest extends AbstractXtextTests {
@Inject
private SerializerTestHelper tester;

private Grammar load(URI uri) {
XtextResourceSet rs = new XtextResourceSet();
return (Grammar) rs.getResource(uri, true).getContents().get(0);
}

@Override
public void setUp() throws Exception {
super.setUp();
Expand All @@ -58,48 +55,14 @@ public void testXtextXtextWithNM() throws Exception {
}

@Test
@Ignore("To be done")
public void testXtextXtextWithoutNM() throws Exception {
public void testGroup() {
Grammar grammar = load(URI.createURI("classpath:/org/eclipse/xtext/Xtext.xtext"));
tester.assertSerializeWithoutNodeModel(grammar);
AbstractRule rule = GrammarUtil.findRuleForName(grammar, "org.eclipse.xtext.Xtext.Grammar");
Group cGroup_2 = (Group) rule.eContents().get(1).eContents().get(2);
detachNodeModel(grammar);
String string = get(ISerializer.class).serialize(cGroup_2);
Assert.assertEquals("(\"with\" usedGrammars+=[Grammar|GrammarID] (\",\" usedGrammars+=[Grammar|GrammarID])*)?",
string);
}

@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);
}

@Test
public void testFQNInSuper_01() {
GrammarProvider grammarProvider = new GrammarProvider("org.eclipse.xtext.grammarinheritance.InheritanceTestLanguage", new Provider<XtextResourceSet>() {
@Override
public XtextResourceSet get() {
return XtextSerializerTest.this.get(XtextResourceSet.class);
}
});
grammarProvider.setClassLoader(getClass().getClassLoader());
TerminalsGrammarAccess gaTerminals = new TerminalsGrammarAccess(grammarProvider);
BaseInheritanceTestLanguageGrammarAccess gaBaseInheritanceTestLanguage = new BaseInheritanceTestLanguageGrammarAccess(grammarProvider, gaTerminals);
InheritanceTestLanguageGrammarAccess grammarAccess = new InheritanceTestLanguageGrammarAccess(grammarProvider, gaBaseInheritanceTestLanguage, gaTerminals);
String string = get(ISerializer.class).serialize(grammarAccess.getFQNRule().getAlternatives());
Assert.assertEquals("ID (\".\" ID)*", string);
}

@Ignore("Serialization does not have the correct context information")
@Test
public void testFQNInSuper_02() {
Grammar grammar = load(URI.createURI("classpath:/org/eclipse/xtext/grammarinheritance/InheritanceTestLanguage.xtext"));
AbstractRule rule = GrammarUtil.findRuleForName(grammar, "FQN");
Assert.assertNotNull(rule);
Group group = (Group) rule.getAlternatives();
RuleCall ruleCall = (RuleCall) group.getElements().get(0);
TerminalRule id = (TerminalRule) ruleCall.getRule();
Assert.assertSame(grammar, GrammarUtil.getGrammar(id));
String string = get(ISerializer.class).serialize(rule.getAlternatives());
Assert.assertEquals("ID (\".\" ID)*", string);
// currently wrong result is
Assert.assertEquals("super::ID (\".\" super::ID)*", string);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource.Diagnostic;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtext.Constants;
import org.eclipse.xtext.Grammar;
import org.eclipse.xtext.IGrammarAccess;
import org.eclipse.xtext.ISetup;
import org.eclipse.xtext.conversion.IValueConverterService;
Expand All @@ -42,11 +46,14 @@
import org.eclipse.xtext.testing.serializer.SerializerTestHelper;
import org.eclipse.xtext.util.CancelIndicator;
import org.eclipse.xtext.util.LazyStringInputStream;
import org.eclipse.xtext.util.Pair;
import org.eclipse.xtext.util.Tuples;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;

import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
Expand Down Expand Up @@ -390,6 +397,30 @@ protected String readFileIntoString(String filePath) throws IOException {
throw new IllegalStateException("May not happen, but helps to suppress false positives in eclipse' control flow analysis.");
}

protected Grammar load(URI uri) {
XtextResourceSet rs = new XtextResourceSet();
return (Grammar) rs.getResource(uri, true).getContents().get(0);
}

protected List<Pair<EObject, ICompositeNode>> detachNodeModel(EObject eObject) {
EcoreUtil.resolveAll(eObject);
List<Pair<EObject, ICompositeNode>> result = Lists.newArrayList();
Iterator<Object> iterator = EcoreUtil.getAllContents(eObject.eResource(), false);
while (iterator.hasNext()) {
EObject object = (EObject) iterator.next();
Iterator<Adapter> adapters = object.eAdapters().iterator();
while (adapters.hasNext()) {
Adapter adapter = adapters.next();
if (adapter instanceof ICompositeNode) {
adapters.remove();
result.add(Tuples.create(object, (ICompositeNode) adapter));
break;
}
}
}
return result;
}

public static final class Keys {
private static final TypeLiteral<Provider<XtextResourceSet>> resourceSetLiteral = new TypeLiteral<Provider<XtextResourceSet>>(){
};
Expand Down
Loading

0 comments on commit 3a7bd31

Please sign in to comment.