<commit_before>/*******************************************************************************
 * Copyright (c) 2008, 2009 itemis AG (http://www.itemis.eu) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package org.eclipse.xtext;

import static com.google.common.collect.Iterables.*;
import static com.google.common.collect.Sets.*;
import static org.eclipse.emf.ecore.util.EcoreUtil.*;
import static org.eclipse.xtext.EcoreUtil2.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.xtext.nodemodel.BidiIterator;
import org.eclipse.xtext.nodemodel.ICompositeNode;
import org.eclipse.xtext.nodemodel.ILeafNode;
import org.eclipse.xtext.nodemodel.INode;
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
import org.eclipse.xtext.util.Pair;
import org.eclipse.xtext.util.Strings;
import org.eclipse.xtext.util.Tuples;
import org.eclipse.xtext.xtext.CurrentTypeFinder;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

/**
 * @author <NAME>
 * @author <NAME>
 * @author <NAME>
 * @author <NAME>
 */
public class GrammarUtil {

	public static String getClasspathRelativePathToXmi(Grammar grammar) {
		return getLanguageId(grammar).replace('.', '/') + ".xmi";
	}
	
	/**
	 * @since 2.4
	 */
	public static String getClasspathRelativePathToBinGrammar(Grammar grammar) {
		return getLanguageId(grammar).replace('.', '/') + ".xtextbin";
	}

	public static String getLanguageId(Grammar g) {
		return g.getName();
	}
	
	/**
	 * @since 2.9
	 */
	public static String getSimpleName(Grammar g) {
		String fullName = g.getName();
		if (Strings.isEmpty(fullName))
			return null;
		int nameIndex = fullName.lastIndexOf('.');
		return fullName.substring(nameIndex + 1);
		
	}

	/**
	 * @deprecated replaced by {@link #getSimpleName(Grammar)} for more clarity
	 */
	@Deprecated
	public static String getName(Grammar g) {
		return getSimpleName(g);
	}

	public static String getNamespace(Grammar g) {
		String fullName = g.getName();
		if (Strings.isEmpty(fullName))
			return null;
		int nameIndex = fullName.lastIndexOf('.');
		if (nameIndex < 0)
			return "";
		else
			return fullName.substring(0, nameIndex);
	}

	public static Grammar getGrammar(EObject grammarElement) {
		EObject root = getRootContainer(grammarElement);
		if (root instanceof Grammar) {
			return (Grammar) root;
		}
		return null;
	}

	public static AbstractRule containingRule(EObject e) {
		return getContainerOfType(e, AbstractRule.class);
	}

	public static ParserRule containingParserRule(EObject e) {
		return getContainerOfType(e, ParserRule.class);
	}

	public static EnumRule containingEnumRule(EObject e) {
		return getContainerOfType(e, EnumRule.class);
	}

	public static Assignment containingAssignment(EObject e) {
		return getContainerOfType(e, Assignment.class);
	}

	public static Group containingGroup(EObject e) {
		return getContainerOfType(e, Group.class);
	}

	public static UnorderedGroup containingUnorderedGroup(EObject e) {
		return getContainerOfType(e, UnorderedGroup.class);
	}

	public static CrossReference containingCrossReference(EObject e) {
		return getContainerOfType(e, CrossReference.class);
	}<commit_msg>[GrammarUtil/perf] make containing*() fast for misses<commit_after>/*******************************************************************************
 * Copyright (c) 2008, 2009 itemis AG (http://www.itemis.eu) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package org.eclipse.xtext;

import static com.google.common.collect.Iterables.*;
import static com.google.common.collect.Sets.*;
import static org.eclipse.emf.ecore.util.EcoreUtil.*;
import static org.eclipse.xtext.EcoreUtil2.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.xtext.nodemodel.BidiIterator;
import org.eclipse.xtext.nodemodel.ICompositeNode;
import org.eclipse.xtext.nodemodel.ILeafNode;
import org.eclipse.xtext.nodemodel.INode;
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
import org.eclipse.xtext.util.Pair;
import org.eclipse.xtext.util.Strings;
import org.eclipse.xtext.util.Tuples;
import org.eclipse.xtext.xtext.CurrentTypeFinder;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

/**
 * @author <NAME>
 * @author <NAME>
 * @author <NAME>
 * @author <NAME>
 */
public class GrammarUtil {

	public static String getClasspathRelativePathToXmi(Grammar grammar) {
		return getLanguageId(grammar).replace('.', '/') + ".xmi";
	}
	
	/**
	 * @since 2.4
	 */
	public static String getClasspathRelativePathToBinGrammar(Grammar grammar) {
		return getLanguageId(grammar).replace('.', '/') + ".xtextbin";
	}

	public static String getLanguageId(Grammar g) {
		return g.getName();
	}
	
	/**
	 * @since 2.9
	 */
	public static String getSimpleName(Grammar g) {
		String fullName = g.getName();
		if (Strings.isEmpty(fullName))
			return null;
		int nameIndex = fullName.lastIndexOf('.');
		return fullName.substring(nameIndex + 1);
		
	}

	/**
	 * @deprecated replaced by {@link #getSimpleName(Grammar)} for more clarity
	 */
	@Deprecated
	public static String getName(Grammar g) {
		return getSimpleName(g);
	}

	public static String getNamespace(Grammar g) {
		String fullName = g.getName();
		if (Strings.isEmpty(fullName))
			return null;
		int nameIndex = fullName.lastIndexOf('.');
		if (nameIndex < 0)
			return "";
		else
			return fullName.substring(0, nameIndex);
	}

	public static Grammar getGrammar(EObject grammarElement) {
		EObject root = getRootContainer(grammarElement);
		if (root instanceof Grammar) {
			return (Grammar) root;
		}
		return null;
	}

	public static AbstractRule containingRule(EObject e) {
		return getContainerOfType(e, AbstractRule.class);
	}

	public static ParserRule containingParserRule(EObject e) {
		return getContainerOfType(e, ParserRule.class);
	}

	public static EnumRule containingEnumRule(EObject e) {
		return getContainerOfType(e, EnumRule.class);
	}

	public static Assignment containingAssignment(EObject e) {
		EObject current = e;
		while (current != null) {
			switch (current.eClass().getClassifierID()) {
			case XtextPackage.ASSIGNMENT:
				return (Assignment) current;
			case XtextPackage.GROUP:
			case XtextPackage.UNORDERED_GROUP:
			case XtextPackage.PARSER_RULE:
			case XtextPackage.TERMINAL_RULE:
			case XtextPackage.ENUM_RULE:
				return null;
			case XtextPackage.ALTERNATIVES:
				if (((Alternatives) current).getCardinality() != null) {
					return null;
				}
			default:
				current = current.eContainer();
			}
		}
		return null;
	}

	public static Group containingGroup(EObject e) {
		return getContainerOfType(e, Group.class);
	}

	public static UnorderedGroup containingUnorderedGroup(EObject e) {
		return getContainerOfType(e, UnorderedGroup.class);
	}

	public static CrossReference containingCrossReference(EObject e) {
		EObject current = e;
		while (current != null) {
			switch (current.eClass().getClassifierID()) {
			case XtextPackage.CROSS_REFERENCE:
				return (CrossReference) current;
			case XtextPackage.GROUP:
			case XtextPackage.UNORDERED_GROUP:
			case XtextPackage.ALTERNATIVES:
			case XtextPackage.PARSER_RULE:
			case XtextPackage.TERMINAL_RULE:
			case XtextPackage.ENUM_RULE:
				return null;
			default:
				current = current.eContainer();
			}
		}
		return null;
	}
{'fraction_non_alphanumeric': 0.14407707466608277, 'fraction_numerical': 0.004598204510619663, 'mean_word_length': 4.870822622107969, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1886188', 'n_tokens_mistral': 3108, 'n_tokens_neox': 2863, 'n_words': 633}