Subject: bug#61849: 29.0.60; Unable to use treesit-install-language-grammar because repo doesn't have parser.c

From: Przemysław Kryger <[email-masked]>
Date: Mon, 31 Mar 2025 17:35:40 +0100

Just to add another example.  It seems that LaTeX grammar [1] maintainers made
a decision to remove parser.c in
https://github.com/latex-lsp/tree-sitter-latex/pull/114.

And for posterity, here's a workaround I added to my configuration (sightly
more complicated than it could be for Emacs 30, but it has been written with
Emacs 29 in mind (the latter I still use)):

(defun pk/treesit-generate-parser (&rest args)
  "Try to run \\='tree-sitter generate\\=' if there's no parser.c."
  (when-let* (((equal "parser.c" (car (last args))))
              ((not (file-exists-p (expand-file-name "parser.c"))))
              (tree-sitter (executable-find "tree-sitter"))
              (default-directory (file-name-parent-directory default-directory)))
      (message "Generating parser.c")
      (treesit--call-process-signal
       tree-sitter nil t nil "generate")))

(advice-add 'treesit--call-process-signal
            :before #'pk/treesit-generate-parser)

Perhaps similar concept could be added to the treesit-install-language-grammar:
when user provides tree-sitter executable in LANG recipe and parser.c is not
found then generate it?  Alternatively, always generate parser.c if the
tree-sitter executable is passed in the LANG recipe.  Or make it the decision
whether to generate the parser yet another parameter in the LANG recipe.

Cheers,
PK

[1] https://github.com/latex-lsp/tree-sitter-latex