<commit_before>/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2016 <NAME>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package lt.compiler;

import lt.compiler.lexical.*;
import lt.compiler.syntactic.pre.Modifier;

import java.util.*;

/**
 * compile util<br>
 * checks the element type<br>
 * and defines keywords and operator priority
 */
public class CompileUtil {
        /**
         * check the given string is a number
         *
         * @param str string
         * @return true if the string represents a number
         */
        public static boolean isNumber(String str) {
                try {
                        //noinspection ResultOfMethodCallIgnored
                        Double.parseDouble(str);
                        return true;
                } catch (NumberFormatException ignore) {
                        return false;
                }
        }

        /**
         * check the given string is a boolean literal
         *
         * @param str string
         * @return true if it's a boolean.(true,false,yes,no)
         */
        public static boolean isBoolean(String str) {
                return str.equals("true") || str.equals("false") || str.equals("yes") || str.equals("no");
        }

        /**
         * check the given string is a string literal
         *
         * @param str string
         * @return true/false
         */
        public static boolean isString(String str) {
                return (
                        (str.startsWith("\"") && str.endsWith("\"")) || (str.startsWith("'") && str.endsWith("'"))
                ) && str.length() > 1;
        }

        private static Set<String> keys = new HashSet<String>(Arrays.asList(
                "is", "not", "bool", "yes", "no", "type", "as",
                "in", "elseif", "package", "import",
                "break", "continue", "return", "fun", "require",
                "new", "object", "implicit", "match", "case",
                "annotation"
        ));

        private static Set<String> javaKeys = new HashSet<String>(Arrays.asList(
                "abstract", "assert", "boolean", "break", "byte", "case",
                "catch", "char", "class", "const", "continue", "default",
                "do", "double", "else", "enum", "extends", "final", "finally",
                "float", "for", "if", "implements", "import", "instanceof",
                "int", "interface", "long", "native", "new", "null", "package",
                "private", "protected", "public", "return", "short", "static",
                "strictfp", "throw", "try", "while", "void"
        ));

        public static boolean isKey(String str) {
                return keys.contains(str);
        }

        public static String SingletonFieldName = "singletonInstance";

        /**
         * check whether the given string can be a valid java name
         *
         * @param str string
         * @return true/false
         */
        public static boolean isJavaValidName(String str) {
                if (str.isEmpty()) return false;
                if (javaKeys.contains(str)) return false;
                char first = str.charAt(0);
                if (isValidNameStartChar(first)) {
                        for (int i = 1; i < str.length(); ++i) {
                                char c = str.charAt(i);
                                if (!isValidNameChar(c)) return false;
                        }
                        return true;
                } else {
                        return false;
                }
        }

        /**
         * check whether the given string can be a name
         *
         * @param str string
         * @return true/false
         */
        public static boolean isValidName(String str) {
                if (str.startsWith("`") && str.endsWith("`")) {
                        return isJavaValidName(str.substring(1, str.length() - 1));
                }
                return isJavaValidName(str) && !keys.contains(str);
        }

        /**
         * check whether is defined as a package access
         *
         * @param element element
         * @return true/false (packageName::name)
         */
        public static boolean isPackage(Element element) {
                if (element.getTokenType() == TokenType.VALID_NAME && element.hasNext()) {
                        Node next = element.next();
                        if (next instanceof Element) {
                                String nextContent = ((Element) next).getContent();
                                if (nextContent.equals("::") && next.hasNext()) {
                                        Node nextNext = next.next();
                                        if (nextNext instanceof Element) {
                                                return nextNext.getTokenType() == TokenType.VALID_NAME;
                                        }
                                }
                        }
                }
                return false;
        }

        /**
         * check whether the given char can be one of a name
         *
         * @param c char
         * @return true/false (a-z|A-Z|$|_|0-9|>=128)
         */
        public static boolean isValidNameChar(char c) {
                return isValidNameStartChar(c) || (c >= '0' && c <= '9') || c >= 128;
        }

        /**
         * check whether the given char can be start of a name
         *
         * @param c char
         * @return true/false (a-z|A-Z|$|_|>=128)<commit_msg>comment fix: pass java doc gen<commit_after>/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2016 <NAME>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package lt.compiler;

import lt.compiler.lexical.*;
import lt.compiler.syntactic.pre.Modifier;

import java.util.*;

/**
 * compile util<br>
 * checks the element type<br>
 * and defines keywords and operator priority
 */
public class CompileUtil {
        /**
         * check the given string is a number
         *
         * @param str string
         * @return true if the string represents a number
         */
        public static boolean isNumber(String str) {
                try {
                        //noinspection ResultOfMethodCallIgnored
                        Double.parseDouble(str);
                        return true;
                } catch (NumberFormatException ignore) {
                        return false;
                }
        }

        /**
         * check the given string is a boolean literal
         *
         * @param str string
         * @return true if it's a boolean.(true,false,yes,no)
         */
        public static boolean isBoolean(String str) {
                return str.equals("true") || str.equals("false") || str.equals("yes") || str.equals("no");
        }

        /**
         * check the given string is a string literal
         *
         * @param str string
         * @return true/false
         */
        public static boolean isString(String str) {
                return (
                        (str.startsWith("\"") && str.endsWith("\"")) || (str.startsWith("'") && str.endsWith("'"))
                ) && str.length() > 1;
        }

        private static Set<String> keys = new HashSet<String>(Arrays.asList(
                "is", "not", "bool", "yes", "no", "type", "as",
                "in", "elseif", "package", "import",
                "break", "continue", "return", "fun", "require",
                "new", "object", "implicit", "match", "case",
                "annotation"
        ));

        private static Set<String> javaKeys = new HashSet<String>(Arrays.asList(
                "abstract", "assert", "boolean", "break", "byte", "case",
                "catch", "char", "class", "const", "continue", "default",
                "do", "double", "else", "enum", "extends", "final", "finally",
                "float", "for", "if", "implements", "import", "instanceof",
                "int", "interface", "long", "native", "new", "null", "package",
                "private", "protected", "public", "return", "short", "static",
                "strictfp", "throw", "try", "while", "void"
        ));

        public static boolean isKey(String str) {
                return keys.contains(str);
        }

        public static String SingletonFieldName = "singletonInstance";

        /**
         * check whether the given string can be a valid java name
         *
         * @param str string
         * @return true/false
         */
        public static boolean isJavaValidName(String str) {
                if (str.isEmpty()) return false;
                if (javaKeys.contains(str)) return false;
                char first = str.charAt(0);
                if (isValidNameStartChar(first)) {
                        for (int i = 1; i < str.length(); ++i) {
                                char c = str.charAt(i);
                                if (!isValidNameChar(c)) return false;
                        }
                        return true;
                } else {
                        return false;
                }
        }

        /**
         * check whether the given string can be a name
         *
         * @param str string
         * @return true/false
         */
        public static boolean isValidName(String str) {
                if (str.startsWith("`") && str.endsWith("`")) {
                        return isJavaValidName(str.substring(1, str.length() - 1));
                }
                return isJavaValidName(str) && !keys.contains(str);
        }

        /**
         * check whether is defined as a package access
         *
         * @param element element
         * @return true/false (packageName::name)
         */
        public static boolean isPackage(Element element) {
                if (element.getTokenType() == TokenType.VALID_NAME && element.hasNext()) {
                        Node next = element.next();
                        if (next instanceof Element) {
                                String nextContent = ((Element) next).getContent();
                                if (nextContent.equals("::") && next.hasNext()) {
                                        Node nextNext = next.next();
                                        if (nextNext instanceof Element) {
                                                return nextNext.getTokenType() == TokenType.VALID_NAME;
                                        }
                                }
                        }
                }
                return false;
        }

        /**
         * check whether the given char can be one of a name
         *
         * @param c char
         * @return true/false (a-z|A-Z|$|_|0-9|&gt;=128)
         */
        public static boolean isValidNameChar(char c) {
                return isValidNameStartChar(c) || (c >= '0' && c <= '9') || c >= 128;
        }

        /**
         * check whether the given char can be start of a name
         *
         * @param c char
         * @return true/false (a-z|A-Z|$|_|&gt;=128)
{'fraction_non_alphanumeric': 0.1130249218809542, 'fraction_numerical': 0.0033534029418489444, 'mean_word_length': 1.5689115113547376, 'pattern_counts': {'":': 2, '<': 21, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7056541', 'n_tokens_mistral': 3349, 'n_tokens_neox': 3167, 'n_words': 1098}