const END_CHARS = [
".",
",",
":",
";",
"!",
"?",
"\\",
"'",
'"',
"}",
"]",
")",
">",
];
const STOP_CHARS = [
"/",
"'",
'"',
"<",
"(",
"[",
"{",
".",
",",
":",
";",
"!",
"?",
"\\",
"}",
"]",
")",
">",
"-",
];
module.exports = grammar({
name: "comment",
externals: ($) => [
$.name,
$.invalid_token
],
rules: {
source: ($) => repeat(
choice(
$.tag,
$._full_uri,
alias($._text, "text"),
),
),
tag: ($) => seq(
$.name,
optional($._user),
":",
),
_user: ($) => seq(
"(",
alias(/[^()]+/, $.user),
")",
),
_full_uri: ($) => seq($.uri, choice(alias($._end_char, "text"), /\s/)),
uri: ($) => get_uri_regex(),
_text: ($) => choice($._stop_char, notmatching(STOP_CHARS)),
_stop_char: ($) => choice(...STOP_CHARS),
_end_char: ($) => choice(...END_CHARS),
},
});
function get_uri_regex() {
let end_chars = escapeRegExp(END_CHARS.join(""));
return new RegExp(
`https?://([^\\s${end_chars}]|[${end_chars}][a-zA-Z0-9])+`
);
}
function notmatching(chars) {
chars = escapeRegExp(chars.join(""));
return new RegExp(`[^\\s${chars}]+`);
}
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}