imgui-java icon indicating copy to clipboard operation
imgui-java copied to clipboard

Bug: version regex

Open Erfram opened this issue 1 year ago • 0 comments

Version

1.86.11

What happened?

Hello, I'm developing a Code Editor for JavaScript and there's no way I can figure out which version of regex imgui uses. I would like a normal text highlighting.... If you use a modern regex, it doesn't work

Here is the code:

public static TextEditorLanguageDefinition javaScript() {
        TextEditorLanguageDefinition langDef = new TextEditorLanguageDefinition();
        String[] keywords = {
                "break",
                "continue",
                "switch",
                "case",
                "default",
                "try",
                "catch",
                "delete",
                "do",
                "while",
                "else",
                "finally",
                "if",
                "for",
                "each",
                "in",
                "instanceof",
                "new",
                "throw",
                "typeof",
                "with",
                "yield",
                "return"
        };

        langDef.setKeywords(keywords);

        String[] identifiers = {
                "Array", "ArrayBuffer", "AsyncFunction", "Atomics", "BigInt", "BigInt64Array", "BigUint64Array",
                "Boolean", "DataView", "Date", "Error", "EvalError", "Float32Array", "Float64Array", "Function",
                "Generator", "GeneratorFunction", "Int8Array", "Int16Array", "Int32Array", "Intl", "JSON", "Map",
                "Math", "Number", "Object", "Promise", "Proxy", "RangeError", "ReferenceError", "Reflect", "RegExp",
                "Set", "SharedArrayBuffer", "String", "Symbol", "SyntaxError", "TypeError", "Uint8Array",
                "Uint8ClampedArray", "Uint16Array", "Uint32Array", "URIError", "WeakMap", "WeakSet",

                // Глобальные функции
                "atob", "btoa", "clearInterval", "clearTimeout", "decodeURI", "decodeURIComponent", "encodeURI",
                "encodeURIComponent", "escape", "eval", "isFinite", "isNaN", "parseFloat", "parseInt", "setInterval",
                "setTimeout", "unescape"
        };

        String[] syntaxKeywords = new String[]{
                "var",
                "let",
                "const",
                "function",
                "prototype",
                "Math",
                "JSON",
                "CubeCode"
        };

        Map<String, String> mapIdentifiers = new HashMap<>();

        for (String identifier : identifiers) {
            mapIdentifiers.put(identifier, "");
        }

        langDef.setIdentifiers(mapIdentifiers);

        Map<String, Integer> tokenRegexStrings = new HashMap<>();

        tokenRegexStrings.put("L?\\\"(\\\\.|[^\\\"])*\\\"", TextEditorPaletteIndex.String);
        tokenRegexStrings.put("\\'\\\\?[^\\']\\'", TextEditorPaletteIndex.String);
        tokenRegexStrings.put("[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)([eE][+-]?[0-9]+)?[fF]?", TextEditorPaletteIndex.Number);
        tokenRegexStrings.put("[+-]?[0-9]+[Uu]?[lL]?[lL]?", TextEditorPaletteIndex.Number);
        tokenRegexStrings.put("0[0-7]+[Uu]?[lL]?[lL]?", TextEditorPaletteIndex.Number);
        tokenRegexStrings.put("0[xX][0-9a-fA-F]+[uU]?[lL]?[lL]?", TextEditorPaletteIndex.Number);
        tokenRegexStrings.put("[a-zA-Z_][a-zA-Z0-9_]*", TextEditorPaletteIndex.Identifier);
        tokenRegexStrings.put("[\\[\\]\\{\\}\\!\\%\\^\\&\\*\\(\\)\\-\\+\\=\\~\\|\\<\\>\\?\\/\\;\\,\\.]", TextEditorPaletteIndex.Punctuation);
        tokenRegexStrings.put(collectRegex(syntaxKeywords), TextEditorPaletteIndex.Preprocessor);

        langDef.setTokenRegexStrings(tokenRegexStrings);

        langDef.setCommentStart("/*");
        langDef.setCommentEnd("*/");
        langDef.setSingleLineComment("//");

        langDef.setAutoIdentation(false);

        langDef.setName("JavaScript");;

        return langDef;
    }
    
    public static String collectRegex(String[] words) {
        StringBuilder regex = new StringBuilder("(^|\\W)+("); //
        String end = ")(\\W+|$)"; //

        for (int i = 0; i < words.length; i++) {
            String word = words[i];
            regex.append(word);

            if (i < words.length - 1) {
                regex.append("|");
            }
        }

        return regex.append(end).toString();
    }

Reproduction

using TextEditorLanguageDefinition

Relevant log output

No response

Erfram avatar Oct 01 '24 17:10 Erfram