Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: version regex #283

Open
Erfram opened this issue Oct 1, 2024 · 0 comments
Open

Bug: version regex #283

Erfram opened this issue Oct 1, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@Erfram
Copy link

Erfram commented Oct 1, 2024

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 Erfram added the bug Something isn't working label Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant