all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Yuan Fu <casouri@gmail.com>
To: "\"Augustin Chéneau (BTuin)\"" <btuin@mailo.com>
Cc: emacs-devel <emacs-devel@gnu.org>
Subject: Re: Questions about tree-sitter
Date: Mon, 18 Sep 2023 21:00:06 -0700	[thread overview]
Message-ID: <9AF2C9AC-40A8-4DCA-8680-F5433FA9F880@gmail.com> (raw)
In-Reply-To: <23661f5b-51bf-4e23-a134-51a52764fe26@mailo.com>

[-- Attachment #1: Type: text/plain, Size: 1351 bytes --]

> 
> It indeed works much better, thanks!
> 
> I found a bug and a way to replicate it (you'll need to update your Bison grammar):
> - Open the file "treesit-bug-highlighting-demo";
> - Enable bison-ts-mode;
> - At the beginning of the second line (the part managed by the embedded C parser, with "static void ..."), add a space;
> 
> => The whole line loses its highlighting.
> 
> If you add a space again, the highlighting works correctly again.
> Not a big issue, but pretty weird.

Thanks. Weird indeed. I found the bug and fixed it. Latest master should work fine now.

> 
> Also, I have one (last?) question:
> 
> Since the C code uses its own indentation, it's entirely independent of Bison's nodes positions.
> Is it possible to add an offset to the indentation of the embedded parts, relative to its container node?
> 
> For instance, rather than:
> 
> %%
> grammar_declaration:
>      grammar_rule
>    {
> int myvar;
>    }
>    ;
> %%
> 
> 
> 
> I would like to get
> 
> 
> 
> %%
> grammar_declaration:
>      grammar_rule
>    {
>      int myvar;
>    }
>    ;
> %%
> 
> 
> ("int myvar;" is managed by a C parser).

Makes sense. You can use a custom matcher to indent the top level nodes in the C code. I modified your mode for a POC. The modified parts are marked with "!!!".

Yuan


[-- Attachment #2: bison-ts-mode.el --]
[-- Type: application/octet-stream, Size: 6305 bytes --]

;;; bison-ts-mode --- Tree-sitter mode for Bison -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:

(require 'treesit)
(require 'c-ts-mode)

(declare-function treesit-parser-create "treesit.c")
(declare-function treesit-induce-sparse-tree "treesit.c")
(declare-function treesit-node-child-by-field-name "treesit.c")
(declare-function treesit-search-subtree "treesit.c")
(declare-function treesit-node-parent "treesit.c")
(declare-function treesit-node-next-sibling "treesit.c")
(declare-function treesit-node-type "treesit.c")
(declare-function treesit-node-child "treesit.c")
(declare-function treesit-node-end "treesit.c")
(declare-function treesit-node-start "treesit.c")
(declare-function treesit-node-string "treesit.c")
(declare-function treesit-query-compile "treesit.c")
(declare-function treesit-query-capture "treesit.c")
(declare-function treesit-parser-add-notifier "treesit.c")
(declare-function treesit-parser-buffer "treesit.c")
(declare-function treesit-parser-list "treesit.c")


(defgroup bison nil
  "Support for the Bison and Flex."
  :group 'languages)

(defcustom bison-ts-mode-indent-offset 4
  "Number of spaces for each indentation step in `bison-ts-mode'."
  :version "30.1"
  :type 'integer
  :safe 'integerp
  :group 'bison)


(defun treesit--merge-feature-lists (l1 l2)
  "Merge the lists of lists L1 and L2.
The first sublist of L1 is merged with the first sublist of L2 and so on.
L1 and L2 don't need to have the same size."
  (let ((res ()))
    (while (or l1 l2)
      (setq res (push (append (car l1) (car l2)) res))
      (setq l1 (cdr l1) l2 (cdr l2)))
    (nreverse res)))


(defun bison-ts--language-at-point-function (position)
  "Return the language at POSITION."
  (let* ((node (treesit-node-at position 'bison)))
    (if (equal (treesit-node-type node)
	           "embedded_code")
	    'c
      'bison)))

(defun bison-ts--font-lock-settings (language)
  (treesit-font-lock-rules
   :language language
   :feature 'bison-comment
   '((comment) @font-lock-comment-face)

   :language language
   :feature 'bison-declaration
   '((declaration (declaration_name) @font-lock-keyword-face))

   :language language
   :feature 'bison-type
   '((type) @font-lock-type-face)

   :language language
   :feature 'bison-grammar-rule-usage
   '((grammar_rule_identifier) @font-lock-variable-use-face)

   :language language
   :feature 'bison-grammar-rule-declaration
   '((grammar_rule (grammar_rule_declaration)
		           @font-lock-variable-use-face))

   :language language
   :feature 'bison-string
   :override t
   '((string) @font-lock-string-face)

   :language language
   :feature 'bison-literal
   :override t
   '((char_literal) @font-lock-keyword-face
     (number_literal) @font-lock-number-face)

   :language language
   :feature 'bison-directive-grammar-rule
   :override t
   '((grammar_rule (directive) @font-lock-keyword-face))

   :language language
   :feature 'bison-operator
   :override t
   '(["|"] @font-lock-operator-face)

   :language language
   :feature 'bison-delimiter
   :override t
   '([";"] @font-lock-delimiter-face)))


(treesit-query-validate 'bison '((grammar_rule (grammar_rule_declaration)  @font-lock-variable-name-face)))

(defvar bison-ts-mode--font-lock-feature-list
  '(( bison-comment bison-declaration bison-type
      bison-grammar-rule-usage bison-grammar-rule-declaration
      bison-string bison-literal bison-directive-grammar-rule
      bison-operator bison-delimiter)))

;; !!!New matcher
(defun bison-ts-mode--bison-parent (_node _parent bol &rest _)
  "Get the parent of the bison node at BOL."
  (treesit-node-start (treesit-node-parent (treesit-node-at bol 'bison))))

(defun bison-ts--indent-rules ()
  "Indent rules supported by `bison-ts-mode'."
  (let*
      ((common
	    `(((node-is "^declaration$")
	       column-0 0)

	      ((and (parent-is "^declaration$")
		        (not (node-is "^code_block$")))
	       column-0 2)

	      ((and (parent-is "^declaration$")
		        (node-is "^code_block$"))
	       column-0 0)

	      ((parent-is "^declaration$")
	       parent 2)

	      ((node-is "^grammar_rule$")
	       column-0 0)

	      ((and
	        (parent-is "^grammar_rule$")
	        (node-is ";"))
	       column-0 bison-ts-mode-indent-offset)

	      ((and (parent-is "^grammar_rule$")
		        (node-is "|"))
	       column-0 bison-ts-mode-indent-offset)

	      ((and (parent-is "^grammar_rule$")
		        (not (node-is "^grammar_rule_declaration$"))
		        (not (node-is "^action$")))
	       column-0 ,(+ bison-ts-mode-indent-offset 2))

	      ((or
	        (node-is "^action$")
	        (node-is "}"))
	       column-0 12)

	      ;; Set '%%' at the beginning of the line
	      ((or
	        (and (parent-is "^grammar_rules_section$")
		         (node-is "%%"))
	        (node-is "^grammar_rules_section$"))
	       column-0 0)

	      (no-node parent 0)
	      )
	    ))
    `((bison . ,common)
      ;; !!! Import and override C rules.
      (c
       ((parent-is "translation_unit")
        bison-ts-mode--bison-parent ,bison-ts-mode-indent-offset)
       ,@(alist-get 'c (c-ts-mode--get-indent-style 'c))))))


(define-derived-mode bison-ts-mode prog-mode "Bison"
  "A mode for Bison."
  (when (treesit-ready-p 'bison)
    (setq-local treesit-font-lock-settings
                (append (bison-ts--font-lock-settings 'bison)
                        (c-ts-mode--font-lock-settings 'c)))

    (setq-local treesit-font-lock-feature-list
                (treesit--merge-feature-lists
		         bison-ts-mode--font-lock-feature-list
		         c-ts-mode--feature-list))

    (setq-local treesit-simple-imenu-settings
		        `(("Grammar"
		           "\\`grammar_rule_declaration\\'"
		           nil
		           (lambda (node) (treesit-node-text node) ))))

    (setq-local treesit-simple-indent-rules
                ;; !!! C rules are imported already.
                (bison-ts--indent-rules))

    (setq-local treesit-language-at-point-function 'bison-ts--language-at-point-function)

    (setq-local treesit-range-settings
		        (treesit-range-rules
		         :embed 'c
		         :host 'bison
		         :local t
		         '((embedded_code) @capture)
		         ))

    (treesit-major-mode-setup)))

(provide 'bison-ts-mode)
;;; bison-ts-mode.el ends here

[-- Attachment #3: Type: text/plain, Size: 2 bytes --]




  reply	other threads:[~2023-09-19  4:00 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-29 21:26 Questions about tree-sitter Augustin Chéneau (BTuin)
2023-08-30  7:03 ` Yuan Fu
2023-08-30 11:28   ` Augustin Chéneau (BTuin)
2023-09-06  4:07     ` Yuan Fu
2023-09-08 11:53       ` Augustin Chéneau (BTuin)
2023-09-08 16:43         ` Yuan Fu
2023-09-09 16:39           ` Augustin Chéneau (BTuin)
2023-09-12  0:22             ` Yuan Fu
2023-09-13 12:43               ` Augustin Chéneau (BTuin)
2023-09-14  4:11                 ` Yuan Fu
2023-09-18 17:04                   ` Augustin Chéneau (BTuin)
2023-09-19  4:00                     ` Yuan Fu [this message]
2023-09-01  2:39   ` Madhu
2023-09-01  6:53     ` Eli Zaretskii
2023-09-01  9:15       ` Madhu
2023-09-01 10:45         ` Dmitry Gutov
2023-09-01 10:58         ` Eli Zaretskii
2023-11-27  7:16           ` Madhu
2023-09-06 16:11   ` Lynn Winebarger
2023-09-07 23:42     ` Yuan Fu
2023-09-08  0:11       ` Lynn Winebarger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9AF2C9AC-40A8-4DCA-8680-F5433FA9F880@gmail.com \
    --to=casouri@gmail.com \
    --cc=btuin@mailo.com \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.