mirror of
https://github.com/sigmasternchen/xtext-core
synced 2025-03-16 00:38:56 +00:00
grammar elements are now resolved through their URI (incl. language inheritance)
This commit is contained in:
parent
48c95a38e4
commit
69f9276002
6 changed files with 45 additions and 30 deletions
|
@ -27,6 +27,9 @@ String getName(Grammar g) :
|
|||
String getNamespace(Grammar g) :
|
||||
JAVA org.eclipse.xtext.GrammarUtil.getNamespace(org.eclipse.xtext.Grammar);
|
||||
|
||||
String getClasspathRelativePathToXmi(Grammar g) :
|
||||
JAVA org.eclipse.xtext.GrammarUtil.getClasspathRelativePathToXmi(org.eclipse.xtext.Grammar);
|
||||
|
||||
// ***********************************************************************************
|
||||
// Containers
|
||||
|
||||
|
|
|
@ -43,9 +43,9 @@ public class GrammarUtil {
|
|||
}
|
||||
|
||||
public static String getNamespace(Grammar g) {
|
||||
return Strings.concat(".", g.getIdElements(), 1);
|
||||
return Strings.concat(".", g.getIdElements(), 1);
|
||||
}
|
||||
|
||||
|
||||
public static Grammar getGrammar(EObject grammarElement) {
|
||||
EObject root = getRootContainer(grammarElement);
|
||||
if (root instanceof Grammar) {
|
||||
|
@ -85,8 +85,7 @@ public class GrammarUtil {
|
|||
for (AbstractElement ae : g.getAbstractTokens()) {
|
||||
if (ae == _this || eAllContentsAsList(ae).contains(_this)) {
|
||||
return result;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
result.add(ae);
|
||||
}
|
||||
}
|
||||
|
@ -121,30 +120,30 @@ public class GrammarUtil {
|
|||
}
|
||||
|
||||
public static Grammar getSuperGrammar(Grammar _this) {
|
||||
if (IXtextBuiltin.ID.equals(getLanguageId(_this))) {
|
||||
return null;
|
||||
}
|
||||
if(IXtextBuiltin.ID.equals(getLanguageId(_this))) {
|
||||
return null;
|
||||
}
|
||||
String id = getSuperGrammarId(_this);
|
||||
if (id == null) {
|
||||
id = IXtextBuiltin.ID;
|
||||
}
|
||||
ILanguageDescriptor descriptor = LanguageDescriptorFactory.get(id);
|
||||
if (descriptor == null)
|
||||
throw new IllegalStateException("Language '" + id + "' has not been set up properly");
|
||||
throw new IllegalStateException("Language '"+id+"' has not been set up properly");
|
||||
IGrammarAccess service = ServiceRegistry.getService(descriptor, IGrammarAccess.class);
|
||||
if (service == null)
|
||||
throw new IllegalStateException("Language '" + id + "' has not been set up properly");
|
||||
throw new IllegalStateException("Language '"+id+"' has not been set up properly");
|
||||
Grammar superGrammar = service.getGrammar();
|
||||
return superGrammar == _this ? null : superGrammar;
|
||||
}
|
||||
|
||||
|
||||
public static String getSuperGrammarId(Grammar _this) {
|
||||
if (_this.getSuperGrammarIdElements().isEmpty())
|
||||
return null;
|
||||
StringBuffer buff = new StringBuffer();
|
||||
for (int i = 0, x = _this.getSuperGrammarIdElements().size(); i < x; i++) {
|
||||
for (int i = 0, x = _this.getSuperGrammarIdElements().size(); i<x; i++) {
|
||||
buff.append(_this.getSuperGrammarIdElements().get(i));
|
||||
if ((i + 1) < x)
|
||||
if ((i+1)<x)
|
||||
buff.append(".");
|
||||
}
|
||||
return buff.toString();
|
||||
|
@ -254,26 +253,30 @@ public class GrammarUtil {
|
|||
Set<String> kws = new HashSet<String>();
|
||||
List<ParserRule> rules = allParserRules(g);
|
||||
for (ParserRule parserRule : rules) {
|
||||
List<Keyword> list = typeSelect(eAllContentsAsList(parserRule), Keyword.class);
|
||||
List<Keyword> list = typeSelect(eAllContentsAsList(parserRule),Keyword.class);
|
||||
for (Keyword keyword : list) {
|
||||
kws.add(keyword.getValue());
|
||||
}
|
||||
}
|
||||
return kws;
|
||||
}
|
||||
|
||||
|
||||
public static boolean isOptionalCardinality(AbstractElement e) {
|
||||
return e.getCardinality() != null && (e.getCardinality().equals("?") || e.getCardinality().equals("*"));
|
||||
return e.getCardinality()!=null && (e.getCardinality().equals("?") || e.getCardinality().equals("*"));
|
||||
}
|
||||
|
||||
|
||||
public static boolean isMultipleCardinality(AbstractElement e) {
|
||||
return isOneOrMoreCardinality(e) || isAnyCardinality(e);
|
||||
}
|
||||
|
||||
public static String getClasspathRelativePathToXmi(Grammar grammar) {
|
||||
return getLanguageId(grammar).replace('.', '/')+".xmi";
|
||||
}
|
||||
public static boolean isOneOrMoreCardinality(AbstractElement e) {
|
||||
return e.getCardinality() != null && (e.getCardinality().equals("+"));
|
||||
}
|
||||
|
||||
|
||||
public static boolean isAnyCardinality(AbstractElement e) {
|
||||
return e.getCardinality() != null && (e.getCardinality().equals("*"));
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@ import org.antlr.runtime.TokenStream;
|
|||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.eclipse.emf.common.util.EList;
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
import org.eclipse.emf.common.util.WrappedException;
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.xtext.AbstractRule;
|
||||
|
@ -92,7 +93,7 @@ public abstract class AbstractAntlrParser extends Parser {
|
|||
}
|
||||
LeafNode leafNode = createLeafNode(isSemanticChannel(token));
|
||||
leafNode.setText(token.getText());
|
||||
leafNode.setGrammarElement(grammar.eResource().getEObject(grammarElementID));
|
||||
leafNode.setGrammarElement(getGrammarElement(grammarElementID));
|
||||
leafNode.setFeature(feature);
|
||||
parentNode.getChildren().add(leafNode);
|
||||
lastConsumedIndex = token.getTokenIndex();
|
||||
|
@ -103,6 +104,10 @@ public abstract class AbstractAntlrParser extends Parser {
|
|||
return null;
|
||||
}
|
||||
|
||||
private EObject getGrammarElement(String grammarElementID) {
|
||||
return grammar.eResource().getResourceSet().getEObject(URI.createURI(grammarElementID),true);
|
||||
}
|
||||
|
||||
private Map<Integer, String> antlrTypeToLexerName = null;
|
||||
|
||||
public Map<Integer, String> getTokenTypeMap() {
|
||||
|
@ -145,7 +150,8 @@ public abstract class AbstractAntlrParser extends Parser {
|
|||
CompositeNode compositeNode = ParsetreeFactory.eINSTANCE.createCompositeNode();
|
||||
if (parentNode != null)
|
||||
parentNode.getChildren().add(compositeNode);
|
||||
compositeNode.setGrammarElement(grammar.eResource().getEObject(grammarElementID));
|
||||
EObject grammarEle = getGrammarElement(grammarElementID);
|
||||
compositeNode.setGrammarElement(grammarEle);
|
||||
return compositeNode;
|
||||
}
|
||||
|
||||
|
|
|
@ -13,6 +13,7 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
|
||||
import org.eclipse.emf.common.util.EList;
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
import org.eclipse.emf.ecore.EClass;
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.emf.ecore.EStructuralFeature;
|
||||
|
@ -282,4 +283,8 @@ public abstract class AbstractInternalParseTreeConstructor {
|
|||
newOne.setParserNode(current);
|
||||
}
|
||||
|
||||
protected EObject getGrammarElement(String string) {
|
||||
return getGrammar().eResource().getResourceSet().getEObject(URI.createURI(string), true);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -61,18 +61,6 @@ public class NodeModelTest extends AbstractGeneratorTest {
|
|||
LeafNode leafNode = (LeafNode) next;
|
||||
EObject grammarElement = leafNode.getGrammarElement();
|
||||
assertNotNull(grammarElement);
|
||||
//TODO is this test obsolete since we've removed the tokenType feature?
|
||||
// if (grammarElement instanceof Keyword) {
|
||||
// assertEquals(ITokenTypes.KEYWORD, tokenType);
|
||||
// } else if (GrammarUtil.isWhitespaceLexerRule(grammarElement)) {
|
||||
// assertEquals(ITokenTypes.WHITESPACE, tokenType);
|
||||
// } else if (GrammarUtil.isLexerRuleCall(grammarElement)) {
|
||||
// assertEquals(((LexerRule) GrammarUtil
|
||||
// .calledRule((RuleCall) grammarElement))
|
||||
// .getTokenType(), tokenType);
|
||||
// } else {
|
||||
// fail("LeafNodes must correspond to keywords, whitespaces or lexerRules "+grammarElement);
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,8 @@
|
|||
package org.eclipse.xtext.parsetree.reconstr;
|
||||
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.xtext.RuleCall;
|
||||
import org.eclipse.xtext.parsetree.CompositeNode;
|
||||
import org.eclipse.xtext.parsetree.IParseTreeConstructor;
|
||||
import org.eclipse.xtext.parsetree.NodeUtil;
|
||||
import org.eclipse.xtext.tests.AbstractGeneratorTest;
|
||||
|
@ -38,4 +40,12 @@ public class ComplexReconstrTest extends AbstractGeneratorTest {
|
|||
String resultString = NodeUtil.getRootNode(result).serialize();
|
||||
return resultString;
|
||||
}
|
||||
|
||||
// public void testNormalizableCompositeNodesIncluded() throws Exception {
|
||||
// EObject model = getModel("a");
|
||||
// IParseTreeConstructor con = getParseTreeConstructor();
|
||||
// con.update(model);
|
||||
// CompositeNode node = NodeUtil.getRootNode(model);
|
||||
// assertEquals("Op",((RuleCall)node.getGrammarElement()).getName());
|
||||
// }
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue