如何在评论中为 "todo" 创建规则
How to create rule for "todo" in a comment
我针对自定义语言尝试了这些规则,其中的注释类似于 C#。
我想在评论(尤其是单行评论)中匹配单词“todo”。我在这里的尝试失败了。
谁能指导我?
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.[=11=]", next: "@qualified" },
"@default": { token: "identifier.[=11=]", 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.[=11=]", next: "@pop" },
},
},
],
[/\./, "delimiter"],
["", "", "@pop"],
],
comments: [
[/\/\//, "comment", "@comment_single"],
[/\/\*/, 'comment.quote', '@comment_quote']
],
comment_single: [
[/.*/, 'comment', '@pop'],
[/todo/i, 'keyword.todo'],
],
//....
我设计了一个解决方案。
comments: [
[/\/\//, "comment", "@comment_single"],
[/\/\*/, 'comment.quote', '@comment_quote']
],
comment_single: [
[/\btodo\b/, 'keyword.todo'],
[/.$/, 'comment', "@pop"],
[/./, 'comment'],
],
comment_quote: [
[/\*\//, 'comment.quote', '@pop'],
[/\btodo\b/, 'keyword.todo'],
[/((?!\btodo\b).)*/, 'comment.quote'],
[/[^*/]+/, 'comment.quote'],
[/./, 'comment.quote'],
],
我针对自定义语言尝试了这些规则,其中的注释类似于 C#。
我想在评论(尤其是单行评论)中匹配单词“todo”。我在这里的尝试失败了。
谁能指导我?
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.[=11=]", next: "@qualified" },
"@default": { token: "identifier.[=11=]", 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.[=11=]", next: "@pop" },
},
},
],
[/\./, "delimiter"],
["", "", "@pop"],
],
comments: [
[/\/\//, "comment", "@comment_single"],
[/\/\*/, 'comment.quote', '@comment_quote']
],
comment_single: [
[/.*/, 'comment', '@pop'],
[/todo/i, 'keyword.todo'],
],
//....
我设计了一个解决方案。
comments: [
[/\/\//, "comment", "@comment_single"],
[/\/\*/, 'comment.quote', '@comment_quote']
],
comment_single: [
[/\btodo\b/, 'keyword.todo'],
[/.$/, 'comment', "@pop"],
[/./, 'comment'],
],
comment_quote: [
[/\*\//, 'comment.quote', '@pop'],
[/\btodo\b/, 'keyword.todo'],
[/((?!\btodo\b).)*/, 'comment.quote'],
[/[^*/]+/, 'comment.quote'],
[/./, 'comment.quote'],
],