Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
691 views
in Technique[技术] by (71.8m points)

monaco editor - How to create rule for "todo" in a comment

I tried these rules for a custom language, in which comments are C# like.

I want to match word "todo" when it is in a comment (especially single line comment). My attempt here fails.

Can anyone guide me?

const options = {
    defaultToken: "",
    tokenPostfix: ".dbl",

    brackets: [
        { open: "{", close: "}", token: "delimiter.curly" },
        { open: "[", close: "]", token: "delimiter.square" },
        { open: "(", close: ")", token: "delimiter.parenthesis" },
        { open: "<", close: ">", token: "delimiter.angle" },
    ],

    keywords: [
        "Indexes",
        // ...
    ],

    symbols: /[=><!~?:&|+-*/^%]+/,

    // escape sequences
    escapes: /\(?:[\"'`])/,

    // The main tokenizer for our languages
    tokenizer: {
        root: [
            { include: '@comments' },
            { include: '@whitespace' },
            { include: '@strings_single' },
            { include: '@strings_double' },

            // identifiers and keywords
            [
                /[a-zA-Z_]w*/i,
                {
                    cases: {
                        "@keywords": { token: "keyword.$0", next: "@qualified" },
                        "@default": { token: "identifier.$0", next: "@qualified" },
                    },
                },
            ],

            // delimiters and operators
            [
                /}/,
                {
                    cases: {
                        "@default": "@brackets",
                    },
                },
            ],

            [/[{}()[]]/, "@brackets"],

            [
                /@symbols/,
                {
                    cases: {
                        "@operators": "delimiter",
                        "@default": "",
                    },
                },
            ],

            // numbers
            [/[0-9_]*.[0-9_]+([eE][-+]?d+)?[fFdD]?/, "number.float"],
            [/0[xX][0-9a-fA-F_]+/, "number.hex"],
            [/0[bB][01_]+/, "number.hex"], // binary: use same theme style as hex
            [/[0-9_]+/, "number"],

            // delimiter: after number because of .d floats
            [/[;,.]/, "delimiter"],
        ],

        whitespace: [
            [/s+/, "white-spaces"],
        ],

        qualified: [
            [
                /[a-zA-Z_][w]*/,
                {
                    cases: {
                        "@default": { token: "identifier.$0", next: "@pop" },
                    },
                },
            ],
            [/./, "delimiter"],
            ["", "", "@pop"],
        ],

        comments: [
            [////, "comment", "@comment_single"],
            [//*/, 'comment.quote', '@comment_quote']
        ],

        comment_single: [
            [/.*/, 'comment', '@pop'],
            [/todo/i, 'keyword.todo'],
        ],
   //....

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I devised a solution.

        comments: [
            [////, "comment", "@comment_single"],
            [//*/, 'comment.quote', '@comment_quote']
        ],

        comment_single: [
            [/todo/, 'keyword.todo'],
            [/.$/, 'comment', "@pop"],
            [/./, 'comment'],
        ],

        comment_quote: [
            [/*//, 'comment.quote', '@pop'],
            [/todo/, 'keyword.todo'],
            [/((?!todo).)*/, 'comment.quote'],
            [/[^*/]+/, 'comment.quote'],
            [/./, 'comment.quote'],
        ],

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...