From e6d706f6b81cb06f63a36f27ab304137afe4d5df Mon Sep 17 00:00:00 2001 From: Guldoman Date: Mon, 25 Mar 2024 00:20:32 +0100 Subject: [PATCH] Limit `language_js` regex avoidance to numbers, and fix starting `/*` comments (#1744) * Avoid starting regexes only after numbers in `language_js` * Allow starting `/*` comments after numbers in `language_js` --- data/plugins/language_js.lua | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/data/plugins/language_js.lua b/data/plugins/language_js.lua index c81f233e..1921f8d2 100644 --- a/data/plugins/language_js.lua +++ b/data/plugins/language_js.lua @@ -20,10 +20,10 @@ local syntax = require "core.syntax" -- followed by pattern options, and anything that can -- be after a pattern. -- --- Demo with some unit tests (click on the Unit Tests entry): https://regex101.com/r/R0w8Qw/1 +-- Demo with some unit tests (click on the Unit Tests entry): https://regex101.com/r/Vx5L5V/1 -- Note that it has a couple of changes to make it work on that platform. local regex_pattern = { - [=[/(?=(?!/)(?:(?>[^\\[\/]++|\\.|\[(?:[^\\\]]++|\\.)*+\])*+)++/[gmiyuvsd]*\s*[\n,;\)\]\}\.])()]=], + [=[\/(?=(?!\/)(?:(?>[^\\[\/]++|\\.|\[(?:[^\\\]]++|\\.)*+\])*+)++\/[gmiyuvsd]*\s*(?:[\n,;\)\]\}\.]|\/[\/*]))()]=], "/()[gmiyuvsd]*", "\\" } @@ -57,19 +57,19 @@ syntax.add { comment = "//", block_comment = { "/*", "*/" }, patterns = { - { pattern = "//.*", type = "comment" }, - { pattern = { "/%*", "%*/" }, type = "comment" }, - { regex = regex_pattern, syntax = inner_regex_syntax, type = {"string", "string"} }, - { pattern = { '"', '"', '\\' }, type = "string" }, - { pattern = { "'", "'", '\\' }, type = "string" }, - { pattern = { "`", "`", '\\' }, type = "string" }, - -- Use (?:\/(?!\/))? to avoid that a regex can start after a number, while also allowing // comments - { regex = [[-?0[xXbBoO][\da-fA-F_]+n?()\s*()(?:\/(?!\/))?]], type = {"number", "normal", "operator"} }, - { regex = [[-?\d+[0-9.eE_n]*()\s*()(?:\/(?!\/))?]], type = {"number", "normal", "operator"} }, - { regex = [[-?\.?\d+()\s*()(?:\/(?!\/))?]], type = {"number", "normal", "operator"} }, - { pattern = "[%+%-=/%*%^%%<>!~|&]", type = "operator" }, - { pattern = "[%a_][%w_]*%f[(]", type = "function" }, - { regex = [=[[a-zA-Z_]\w*()\s*()(?:\/(?!\/))?]=], type = {"symbol", "normal", "operator"} }, + { pattern = "//.*", type = "comment" }, + { pattern = { "/%*", "%*/" }, type = "comment" }, + { regex = regex_pattern, syntax = inner_regex_syntax, type = {"string", "string"} }, + { pattern = { '"', '"', '\\' }, type = "string" }, + { pattern = { "'", "'", '\\' }, type = "string" }, + { pattern = { "`", "`", '\\' }, type = "string" }, + -- Use (?:\/(?!\/|\*))? to avoid that a regex can start after a number, while also allowing // and /* comments + { regex = [[-?0[xXbBoO][\da-fA-F_]+n?()\s*()(?:\/(?!\/|\*))?]], type = {"number", "normal", "operator"} }, + { regex = [[-?\d+[0-9.eE_n]*()\s*()(?:\/(?!\/|\*))?]], type = {"number", "normal", "operator"} }, + { regex = [[-?\.?\d+()\s*()(?:\/(?!\/|\*))?]], type = {"number", "normal", "operator"} }, + { pattern = "[%+%-=/%*%^%%<>!~|&]", type = "operator" }, + { pattern = "[%a_][%w_]*%f[(]", type = "function" }, + { pattern = "[%a_][%w_]*", type = "symbol" }, }, symbols = { ["async"] = "keyword",