From df7e575393c44976a4389641975f9e4aab7efb24 Mon Sep 17 00:00:00 2001 From: "Basil L. Contovounesios" Date: Wed, 7 Jun 2023 12:26:25 +0100 Subject: [PATCH] Fix some tree-sitter :match regexps Some of these issues were caught by modified versions of the GNU ELPA packages xr and relint: - https://github.com/mattiase/xr/pull/6 - https://github.com/mattiase/relint/pull/14 * lisp/progmodes/c-ts-mode.el (c-ts-mode--font-lock-settings) (c-ts-mode--c-or-c++-regexp): * lisp/progmodes/cmake-ts-mode.el (cmake-ts-mode--font-lock-settings): * lisp/progmodes/java-ts-mode.el (java-ts-mode--font-lock-settings): * lisp/progmodes/js.el (js--plain-method-re): (js--treesit-font-lock-settings): * lisp/progmodes/python.el (python--treesit-settings): * lisp/progmodes/rust-ts-mode.el (rust-ts-mode--font-lock-settings): * lisp/progmodes/sh-script.el (sh-mode--treesit-settings): * lisp/progmodes/typescript-ts-mode.el (typescript-ts-mode--font-lock-settings): * test/src/treesit-tests.el (treesit-query-api): Replace occurrences of [\\d], which matches '\' or 'd', with the most likely intention [0-9]. Anchor :match regexps at beginning/end of string, not line. Fix shy groups mistyped as optional colon. --- lisp/progmodes/c-ts-mode.el | 4 ++-- lisp/progmodes/cmake-ts-mode.el | 3 ++- lisp/progmodes/java-ts-mode.el | 4 ++-- lisp/progmodes/js.el | 6 +++--- lisp/progmodes/python.el | 2 +- lisp/progmodes/rust-ts-mode.el | 19 +++++++++++-------- lisp/progmodes/sh-script.el | 2 +- lisp/progmodes/typescript-ts-mode.el | 4 ++-- test/src/treesit-tests.el | 4 ++-- 9 files changed, 26 insertions(+), 22 deletions(-) diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el index c6cb9520e58..4775cbd724d 100644 --- a/lisp/progmodes/c-ts-mode.el +++ b/lisp/progmodes/c-ts-mode.el @@ -701,7 +701,7 @@ c-ts-mode--font-lock-settings `(((call_expression (call_expression function: (identifier) @fn) @c-ts-mode--fontify-DEFUN) - (:match "^DEFUN$" @fn)) + (:match "\\`DEFUN\\'" @fn)) ((function_definition type: (_) @for-each-tail) @c-ts-mode--fontify-for-each-tail @@ -1319,7 +1319,7 @@ c-ts-mode--c-or-c++-regexp "\\|" id "::" "\\|" id ws-maybe "=\\)" "\\|" "\\(?:inline" ws "\\)?namespace" - "\\(:?" ws "\\(?:" id "::\\)*" id "\\)?" ws-maybe "{" + "\\(?:" ws "\\(?:" id "::\\)*" id "\\)?" ws-maybe "{" "\\|" "class" ws id "\\(?:" ws "final" "\\)?" ws-maybe "[:{;\n]" "\\|" "struct" ws id "\\(?:" ws "final" ws-maybe "[:{\n]" diff --git a/lisp/progmodes/cmake-ts-mode.el b/lisp/progmodes/cmake-ts-mode.el index d83a956af21..9d35d8077bd 100644 --- a/lisp/progmodes/cmake-ts-mode.el +++ b/lisp/progmodes/cmake-ts-mode.el @@ -134,7 +134,8 @@ cmake-ts-mode--font-lock-settings :language 'cmake :feature 'number '(((unquoted_argument) @font-lock-number-face - (:match "^[[:digit:]]*\\.?[[:digit:]]*\\.?[[:digit:]]+$" @font-lock-number-face))) + (:match "\\`[[:digit:]]*\\.?[[:digit:]]*\\.?[[:digit:]]+\\'" + @font-lock-number-face))) :language 'cmake :feature 'string diff --git a/lisp/progmodes/java-ts-mode.el b/lisp/progmodes/java-ts-mode.el index 44dfd74cafd..7f2fc4188a3 100644 --- a/lisp/progmodes/java-ts-mode.el +++ b/lisp/progmodes/java-ts-mode.el @@ -168,7 +168,7 @@ java-ts-mode--font-lock-settings :override t :feature 'constant `(((identifier) @font-lock-constant-face - (:match "^[A-Z_][A-Z_\\d]*$" @font-lock-constant-face)) + (:match "\\`[A-Z_][0-9A-Z_]*\\'" @font-lock-constant-face)) [(true) (false)] @font-lock-constant-face) :language 'java :override t @@ -237,7 +237,7 @@ java-ts-mode--font-lock-settings (scoped_identifier (identifier) @font-lock-constant-face) ((scoped_identifier name: (identifier) @font-lock-type-face) - (:match "^[A-Z]" @font-lock-type-face)) + (:match "\\`[A-Z]" @font-lock-type-face)) (type_identifier) @font-lock-type-face diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el index 52ed19cc682..48fecf69537 100644 --- a/lisp/progmodes/js.el +++ b/lisp/progmodes/js.el @@ -106,7 +106,7 @@ js--opt-cpp-start (defconst js--plain-method-re (concat "^\\s-*?\\(" js--dotted-name-re "\\)\\.prototype" - "\\.\\(" js--name-re "\\)\\s-*?=\\s-*?\\(\\(:?async[ \t\n]+\\)function\\)\\_>") + "\\.\\(" js--name-re "\\)\\s-*?=\\s-*?\\(\\(?:async[ \t\n]+\\)function\\)\\_>") "Regexp matching an explicit JavaScript prototype \"method\" declaration. Group 1 is a (possibly-dotted) class name, group 2 is a method name, and group 3 is the `function' keyword.") @@ -3493,7 +3493,7 @@ js--treesit-font-lock-settings :language 'javascript :feature 'constant '(((identifier) @font-lock-constant-face - (:match "^[A-Z_][A-Z_\\d]*$" @font-lock-constant-face)) + (:match "\\`[A-Z_][0-9A-Z_]*\\'" @font-lock-constant-face)) [(true) (false) (null)] @font-lock-constant-face) @@ -3612,7 +3612,7 @@ js--treesit-font-lock-settings :feature 'number '((number) @font-lock-number-face ((identifier) @font-lock-number-face - (:match "^\\(:?NaN\\|Infinity\\)$" @font-lock-number-face))) + (:match "\\`\\(?:NaN\\|Infinity\\)\\'" @font-lock-number-face))) :language 'javascript :feature 'operator diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index fd196df7550..d9ca37145e1 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -1106,7 +1106,7 @@ python--treesit-settings :language 'python `([,@python--treesit-keywords] @font-lock-keyword-face ((identifier) @font-lock-keyword-face - (:match "^self$" @font-lock-keyword-face))) + (:match "\\`self\\'" @font-lock-keyword-face))) :feature 'definition :language 'python diff --git a/lisp/progmodes/rust-ts-mode.el b/lisp/progmodes/rust-ts-mode.el index c3cf8d0cf44..999c1d7ae96 100644 --- a/lisp/progmodes/rust-ts-mode.el +++ b/lisp/progmodes/rust-ts-mode.el @@ -143,7 +143,7 @@ rust-ts-mode--font-lock-settings eol)) @font-lock-builtin-face))) ((identifier) @font-lock-type-face - (:match "^\\(:?Err\\|Ok\\|None\\|Some\\)$" @font-lock-type-face))) + (:match "\\`\\(?:Err\\|Ok\\|None\\|Some\\)\\'" @font-lock-type-face))) :language 'rust :feature 'comment @@ -212,11 +212,11 @@ rust-ts-mode--font-lock-settings (scoped_use_list path: (scoped_identifier name: (identifier) @font-lock-constant-face)) ((use_as_clause alias: (identifier) @font-lock-type-face) - (:match "^[A-Z]" @font-lock-type-face)) + (:match "\\`[A-Z]" @font-lock-type-face)) ((use_as_clause path: (identifier) @font-lock-type-face) - (:match "^[A-Z]" @font-lock-type-face)) + (:match "\\`[A-Z]" @font-lock-type-face)) ((use_list (identifier) @font-lock-type-face) - (:match "^[A-Z]" @font-lock-type-face)) + (:match "\\`[A-Z]" @font-lock-type-face)) (use_wildcard [(identifier) @rust-ts-mode--fontify-scope (scoped_identifier name: (identifier) @rust-ts-mode--fontify-scope)]) @@ -232,9 +232,12 @@ rust-ts-mode--font-lock-settings (type_identifier) @font-lock-type-face ((scoped_identifier name: (identifier) @rust-ts-mode--fontify-tail)) ((scoped_identifier path: (identifier) @font-lock-type-face) - (:match - "^\\(u8\\|u16\\|u32\\|u64\\|u128\\|usize\\|i8\\|i16\\|i32\\|i64\\|i128\\|isize\\|char\\|str\\)$" - @font-lock-type-face)) + (:match ,(rx bos + (or "u8" "u16" "u32" "u64" "u128" "usize" + "i8" "i16" "i32" "i64" "i128" "isize" + "char" "str") + eos) + @font-lock-type-face)) ((scoped_identifier path: (identifier) @rust-ts-mode--fontify-scope)) ((scoped_type_identifier path: (identifier) @rust-ts-mode--fontify-scope)) (type_identifier) @font-lock-type-face) @@ -249,7 +252,7 @@ rust-ts-mode--font-lock-settings :feature 'constant `((boolean_literal) @font-lock-constant-face ((identifier) @font-lock-constant-face - (:match "^[A-Z][A-Z\\d_]*$" @font-lock-constant-face))) + (:match "\\`[A-Z][0-9A-Z_]*\\'" @font-lock-constant-face))) :language 'rust :feature 'variable diff --git a/lisp/progmodes/sh-script.el b/lisp/progmodes/sh-script.el index 54da1e0468e..9bc1f4dcfdc 100644 --- a/lisp/progmodes/sh-script.el +++ b/lisp/progmodes/sh-script.el @@ -3363,7 +3363,7 @@ sh-mode--treesit-settings :feature 'number :language 'bash `(((word) @font-lock-number-face - (:match "^[0-9]+$" @font-lock-number-face))) + (:match "\\`[0-9]+\\'" @font-lock-number-face))) :feature 'bracket :language 'bash diff --git a/lisp/progmodes/typescript-ts-mode.el b/lisp/progmodes/typescript-ts-mode.el index 1c19a031878..68aefd90f92 100644 --- a/lisp/progmodes/typescript-ts-mode.el +++ b/lisp/progmodes/typescript-ts-mode.el @@ -153,7 +153,7 @@ typescript-ts-mode--font-lock-settings :language language :feature 'constant `(((identifier) @font-lock-constant-face - (:match "^[A-Z_][A-Z_\\d]*$" @font-lock-constant-face)) + (:match "\\`[A-Z_][0-9A-Z_]*\\'" @font-lock-constant-face)) [(true) (false) (null)] @font-lock-constant-face) :language language @@ -311,7 +311,7 @@ typescript-ts-mode--font-lock-settings :feature 'number `((number) @font-lock-number-face ((identifier) @font-lock-number-face - (:match "^\\(:?NaN\\|Infinity\\)$" @font-lock-number-face))) + (:match "\\`\\(?:NaN\\|Infinity\\)\\'" @font-lock-number-face))) :language language :feature 'operator diff --git a/test/src/treesit-tests.el b/test/src/treesit-tests.el index fef603840f9..69db37fc0b4 100644 --- a/test/src/treesit-tests.el +++ b/test/src/treesit-tests.el @@ -368,14 +368,14 @@ treesit-query-api ;; String query. '("(string) @string (pair key: (_) @keyword) -((_) @bob (#match \"^B.b$\" @bob)) +((_) @bob (#match \"\\\\`B.b\\\\'\" @bob)) (number) @number ((number) @n3 (#equal \"3\" @n3)) ((number) @n3p (#pred treesit--ert-pred-last-sibling @n3p))" ;; Sexp query. ((string) @string (pair key: (_) @keyword) - ((_) @bob (:match "^B.b$" @bob)) + ((_) @bob (:match "\\`B.b\\'" @bob)) (number) @number ((number) @n3 (:equal "3" @n3)) ((number) @n3p (:pred treesit--ert-pred-last-sibling -- 2.34.1