001/*-------------------------------------------------------------------------+
002|                                                                          |
003| Copyright (c) 2009-2017 CQSE GmbH                                        |
004|                                                                          |
005+-------------------------------------------------------------------------*/
006package eu.cqse.check.framework.util;
007
008import java.util.List;
009import java.util.Set;
010
011import org.conqat.lib.commons.collections.CollectionUtils;
012
013import eu.cqse.check.framework.scanner.ELanguage;
014import eu.cqse.check.framework.scanner.IToken;
015import eu.cqse.check.framework.shallowparser.framework.EShallowEntityType;
016import eu.cqse.check.framework.shallowparser.framework.ShallowEntity;
017import eu.cqse.check.framework.util.python.PythonVariableNameExtractor;
018
019/** Language feature parser for Python. */
020public class PythonLanguageFeatureParser implements ILanguageFeatureParser {
021
022        /** All built-in names for Python 3. */
023        private static final Set<String> BUILT_IN_NAMES = CollectionUtils.asHashSet("abs", "dict", "help", "min", "setattr",
024                        "all", "dir", "hex", "next", "slice", "any", "divmod", "id", "object", "sorted", "ascii", "enumerate",
025                        "input", "oct", "staticmethod", "bin", "eval", "int", "open", "str", "bool", "exec", "isinstance", "ord",
026                        "sum", "bytearray", "filter", "issubclass", "pow", "super", "bytes", "float", "iter", "print", "tuple",
027                        "callable", "format", "len", "property", "type", "chr", "frozenset", "list", "range", "vars", "classmethod",
028                        "getattr", "locals", "repr", "zip", "compile", "globals", "map", "reversed", "__import__", "complex",
029                        "hasattr", "max", "round", "delattr", "hash", "memoryview", "set");
030
031        /**
032         * Variable name extractor for retrieving variable names that are defined in
033         * shallow entities.
034         */
035        private static final PythonVariableNameExtractor VARIABLE_NAME_EXTRACTOR = new PythonVariableNameExtractor();
036
037        /** Returns whether the given name is a built-in name. */
038        public boolean isBuiltInName(String name) {
039                return BUILT_IN_NAMES.contains(name);
040        }
041
042        /**
043         * Returns all variable names that are declared in the given entity. For details
044         * see {@link PythonVariableNameExtractor}.
045         */
046        public List<IToken> getDeclaredVariableNames(ShallowEntity entity) {
047                return VARIABLE_NAME_EXTRACTOR.extractVariableNames(entity);
048        }
049
050        /**
051         * Returns true if the given {@link ShallowEntity} is a method defined on a
052         * class. Otherwise, returns false.
053         */
054        public boolean isDefinedOnClass(ShallowEntity entity) {
055                ShallowEntity parentEntity = entity.getParent();
056                if (parentEntity == null) {
057                        return false;
058                }
059                return parentEntity.getType() == EShallowEntityType.TYPE;
060        }
061
062        /** Returns true if the given {@link ShallowEntity} is a method. */
063        public boolean isMethod(ShallowEntity entity) {
064                return entity.getType() == EShallowEntityType.METHOD;
065        }
066
067        /** {@inheritDoc} */
068        @Override
069        public ELanguage getLanguage() {
070                return ELanguage.PYTHON;
071        }
072}