Java Code Examples for org.eclipse.xtext.Grammar#getMetamodelDeclarations()

The following examples show how to use org.eclipse.xtext.Grammar#getMetamodelDeclarations() . 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: GrammarAccessFragment.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
public void replaceResourceURIsWithNsURIs(Grammar grammar, ResourceSet set) {
	for (AbstractMetamodelDeclaration metamodelDecl : grammar.getMetamodelDeclarations()) {
		EPackage pack = metamodelDecl.getEPackage();
		Resource packResource = pack.eResource();
		if (!packResource.getURI().toString().equals(pack.getNsURI())) {
			ResourceSet packResourceSet = packResource.getResourceSet();
			if (packResourceSet != null) {
				EPackage topMost = pack;
				// we need to be aware of empty subpackages
				while (topMost.getESuperPackage() != null
						&& topMost.getESuperPackage().eResource() == topMost.eResource())
					topMost = topMost.getESuperPackage();
				if (packResource.getContents().contains(topMost) && packResource.getContents().size() == 1) {
					if (!topMost.getEClassifiers().isEmpty())
						packResource.setURI(URI.createURI(topMost.getNsURI()));
					else
						moveSubpackagesToNewResource(topMost, set);
				}
				if (!topMost.eResource().getURI().toString().equals(topMost.getNsURI())) 
					movePackageToNewResource(topMost, set);
			}
		}
	}
}
 
Example 2
Source File: Xtext2EcoreTransformer.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
private EClassifierInfos createClassifierInfosFor(Grammar grammar, Set<Grammar> visitedGrammars) {
	if (!visitedGrammars.add(grammar))
		return null;
	final EClassifierInfos result = new EClassifierInfos(grammar);
	for(AbstractMetamodelDeclaration declaration: grammar.getMetamodelDeclarations()) {
		final EPackage referencedEPackage = declaration.getEPackage();
		if (referencedEPackage != null) {
			collectClassInfosOf(result, referencedEPackage, declaration, false);
		}
	}
	for(Grammar usedGrammar: grammar.getUsedGrammars()) {
		EClassifierInfos parent = createClassifierInfosFor(usedGrammar, visitedGrammars);
		if (parent != null)
			result.addParent(parent);
	}
	return result;
}
 
Example 3
Source File: XtextProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void completeTypeRef_Classifier(EObject model, Assignment assignment, ContentAssistContext context,
		ICompletionProposalAcceptor acceptor) {
	Grammar grammar = GrammarUtil.getGrammar(model);
	ContentAssistContext.Builder myContextBuilder = context.copy();
	myContextBuilder.setMatcher(new ClassifierPrefixMatcher(context.getMatcher(), classifierQualifiedNameConverter));
	if (model instanceof TypeRef) {
		ICompositeNode node = NodeModelUtils.getNode(model);
		if (node != null) {
			int offset = node.getOffset();
			Region replaceRegion = new Region(offset, context.getReplaceRegion().getLength()
					+ context.getReplaceRegion().getOffset() - offset);
			myContextBuilder.setReplaceRegion(replaceRegion);
			myContextBuilder.setLastCompleteNode(node);
			StringBuilder availablePrefix = new StringBuilder(4);
			for (ILeafNode leaf : node.getLeafNodes()) {
				if (leaf.getGrammarElement() != null && !leaf.isHidden()) {
					if ((leaf.getTotalLength() + leaf.getTotalOffset()) < context.getOffset())
						availablePrefix.append(leaf.getText());
					else
						availablePrefix.append(leaf.getText().substring(0,
								context.getOffset() - leaf.getTotalOffset()));
				}
				if (leaf.getTotalOffset() >= context.getOffset())
					break;
			}
			myContextBuilder.setPrefix(availablePrefix.toString());
		}
	}
	ContentAssistContext myContext = myContextBuilder.toContext();
	for (AbstractMetamodelDeclaration declaration : grammar.getMetamodelDeclarations()) {
		if (declaration.getEPackage() != null) {
			createClassifierProposals(declaration, model, myContext, acceptor);
		}
	}
}
 
Example 4
Source File: XtextProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private void completeParserRule(EObject model, final ContentAssistContext context,
		ICompletionProposalAcceptor acceptor) {
	final Grammar grammar = GrammarUtil.getGrammar(model);
	for (AbstractMetamodelDeclaration metamodelDeclaration : grammar.getMetamodelDeclarations()) {
		if (metamodelDeclaration instanceof ReferencedMetamodel) {
			ReferencedMetamodel referencedMetamodel = (ReferencedMetamodel) metamodelDeclaration;
			EPackage ePackage = referencedMetamodel.getEPackage();
			if (ePackage != null) {
				for (EClassifier eClassifier : ePackage.getEClassifiers()) {
					if (isProposeParserRule(eClassifier, grammar)) {
						String proposal = eClassifier.getName();
						String metamodelAlias = referencedMetamodel.getAlias();
						if (metamodelAlias != null) {
							proposal = proposal + " returns " + metamodelAlias + "::" + eClassifier.getName();
						}
						proposal = proposal + ": \n;\n";
						ConfigurableCompletionProposal completionProposal = (ConfigurableCompletionProposal) createCompletionProposal(
								proposal, eClassifier.getName() + " - parser rule",
								getImage(XtextFactory.eINSTANCE.createParserRule()), context);
						if (completionProposal != null) {
							completionProposal.setCursorPosition(proposal.length() - 3);
							acceptor.accept(completionProposal);
						}
					}
				}
			}
		}
	}
}
 
Example 5
Source File: GrammarTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testExampleGrammar() throws Exception {
	with(Ecore2XtextTestStandaloneSetup.class);
	Grammar grammar = getGrammarAccess().getGrammar();
	EList<AbstractMetamodelDeclaration> metamodelDeclarations = grammar.getMetamodelDeclarations();
	assertEquals(2, metamodelDeclarations.size());
	
	AbstractRule intDatatypeRule = GrammarUtil.findRuleForName(grammar, "INT0");
	assertNotNull(intDatatypeRule);

	AbstractRule concrete0Rule = GrammarUtil.findRuleForName(grammar, "Concrete0");
	assertNotNull(concrete0Rule);
	
	AbstractRule abstractRule = GrammarUtil.findRuleForName(grammar, "Abstract");
	AbstractElement alternatives = abstractRule.getAlternatives();
	assertTrue(alternatives instanceof Alternatives);
	assertEquals(3, ((Alternatives) alternatives).getElements().size());
	assertTrue(GrammarUtil.containedAssignments(abstractRule).isEmpty());
	
	checkConcreteImplRule(grammar, "Concrete0_Impl");
	checkConcreteImplRule(grammar, "Concrete1_Impl");
	
	ParserRule rootRule = (ParserRule) grammar.getRules().get(0);
	assertEquals("Root", rootRule.getName());
	List<Assignment> assignments = GrammarUtil.containedAssignments(rootRule);
	assertEquals(4, assignments.size());
	assertEquals("name", assignments.get(0).getFeature());
	assertEquals("classes", assignments.get(1).getFeature());
	assertEquals("+=", assignments.get(1).getOperator());
	assertEquals("classes", assignments.get(2).getFeature());
	assertEquals("+=", assignments.get(2).getOperator());
	assertEquals("concrete0", assignments.get(3).getFeature());
	assertEquals("=", assignments.get(3).getOperator());
}
 
Example 6
Source File: Xtext2EcoreTransformer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public AbstractMetamodelDeclaration findMetamodel(Grammar grammar, String alias, String containedClassifier) {
	final List<AbstractMetamodelDeclaration> declarations = grammar.getMetamodelDeclarations();
	AbstractMetamodelDeclaration result = null;
	for (AbstractMetamodelDeclaration decl : declarations) {
		if (isSameAlias(decl.getAlias(), alias)) {
			EPackage pack = decl.getEPackage();
			if (pack != null && pack.getEClassifier(containedClassifier) != null) {
				if (result != null)
					return null;
				result = decl;
			}
		}
	}
	return result;
}
 
Example 7
Source File: XtextLinkingService.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private List<EObject> getPackage(String nsUri, Grammar grammar, Set<Grammar> visitedGrammars) {
	if (!visitedGrammars.add(grammar))
		return null;
	for(AbstractMetamodelDeclaration declaration: grammar.getMetamodelDeclarations()) {
		EPackage pack = declaration.getEPackage();
		if (pack != null && nsUri.equals(pack.getNsURI()))
			return Collections.<EObject>singletonList(pack);
	}
	for (Grammar usedGrammar: grammar.getUsedGrammars()) {
		List<EObject> result = getPackage(nsUri, usedGrammar, visitedGrammars);
		if (result != null)
			return result;
	}
	return null;
}
 
Example 8
Source File: XtextLinkingService.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private boolean isReferencedByUsedGrammar(Grammar grammar, String nsURI, Set<Grammar> visitedGrammars) {
	if (!visitedGrammars.add(grammar)) 
		return false;
	for(AbstractMetamodelDeclaration decl: grammar.getMetamodelDeclarations()) {
		EPackage pack = decl.getEPackage();
		if (pack != null && nsURI.equals(pack.getNsURI())) {
			return true;
		}
	}
	for (Grammar usedGrammar: grammar.getUsedGrammars()) {
		if (isReferencedByUsedGrammar(usedGrammar, nsURI, visitedGrammars))
			return true;
	}
	return false;
}
 
Example 9
Source File: XtextScopeProvider.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected IScope createReferencedPackagesScope(Grammar g) {
	final Collection<EClassifier> allClassifiers = new ArrayList<EClassifier>();
	for(AbstractMetamodelDeclaration decl: g.getMetamodelDeclarations()) {
		if (decl.getEPackage() != null)
			allClassifiers.addAll(decl.getEPackage().getEClassifiers());
	}
	return createClassifierScope(allClassifiers);
}
 
Example 10
Source File: EPackageScopeProvider.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Collects all EPackages referenced by a given grammar and its mixins.
 *
 * @param grammar
 *          grammar to collect packages for
 * @return all referenced EPackages
 */
public Map<String, EPackage> collectPackages(final Grammar grammar) {
  final Map<String, EPackage> result = new HashMap<String, EPackage>();
  for (final Grammar usedGrammar : grammar.getUsedGrammars()) {
    result.putAll(collectPackages(usedGrammar));
  }
  for (final AbstractMetamodelDeclaration decl : grammar.getMetamodelDeclarations()) {
    final EPackage ePackage = decl.getEPackage();
    if (ePackage != null) { // NOPMD
      result.put(ePackage.getNsURI(), ePackage);
    }
  }
  return result;
}
 
Example 11
Source File: GrammarAccessFragment2.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected void addAllGrammarsToResource(final Resource resource, final Grammar grammar, final Set<Grammar> visitedGrammars) {
  boolean _add = visitedGrammars.add(grammar);
  boolean _not = (!_add);
  if (_not) {
    return;
  }
  resource.getContents().add(grammar);
  EList<AbstractMetamodelDeclaration> _metamodelDeclarations = grammar.getMetamodelDeclarations();
  for (final AbstractMetamodelDeclaration metamodelDecl : _metamodelDeclarations) {
    {
      final EPackage pack = metamodelDecl.getEPackage();
      final Resource packResource = pack.eResource();
      String _string = packResource.getURI().toString();
      String _nsURI = pack.getNsURI();
      boolean _notEquals = (!Objects.equal(_string, _nsURI));
      if (_notEquals) {
        final ResourceSet packResourceSet = packResource.getResourceSet();
        if ((packResourceSet != null)) {
          EPackage topMost = pack;
          while (((topMost.getESuperPackage() != null) && (topMost.getESuperPackage().eResource() == topMost.eResource()))) {
            topMost = topMost.getESuperPackage();
          }
          if ((packResource.getContents().contains(topMost) && (packResource.getContents().size() == 1))) {
            boolean _isEmpty = topMost.getEClassifiers().isEmpty();
            boolean _not_1 = (!_isEmpty);
            if (_not_1) {
              packResource.setURI(URI.createURI(topMost.getNsURI()));
            } else {
              this.moveSubpackagesToNewResource(topMost, resource.getResourceSet());
            }
          }
          boolean _equals = topMost.eResource().getURI().toString().equals(topMost.getNsURI());
          boolean _not_2 = (!_equals);
          if (_not_2) {
            this.movePackageToNewResource(topMost, resource.getResourceSet());
          }
        }
      }
    }
  }
  EList<Grammar> _usedGrammars = grammar.getUsedGrammars();
  for (final Grammar usedGrammar : _usedGrammars) {
    this.addAllGrammarsToResource(resource, usedGrammar, visitedGrammars);
  }
}