From: Eli Zaretskii <eliz@gnu.org>
To: Po Lu <luangruo@yahoo.com>
Cc: holgerschurig@gmail.com, Emacs-devel@gnu.org
Subject: Re: tree-sitter: conceptional problem solvable at Emacs' level?
Date: Thu, 09 Feb 2023 10:50:59 +0200 [thread overview]
Message-ID: <83h6vvmdcc.fsf@gnu.org> (raw)
In-Reply-To: <87zg9n45ig.fsf@yahoo.com> (message from Po Lu on Thu, 09 Feb 2023 16:17:27 +0800)
> From: Po Lu <luangruo@yahoo.com>
> Cc: Emacs-devel@gnu.org
> Date: Thu, 09 Feb 2023 16:17:27 +0800
>
> Holger Schurig <holgerschurig@gmail.com> writes:
>
> > Hi, I run branch emacs-29 since some time with great success. And now I
> > wanted to test out tree-sitter and c++-test-mode. Unfortunately, I
> > stumbled into some conceptional problems and wonder if this is actually
> > solvable by Emacs, or if some would need a completely new grammar.
> >
> > The issue is: tree-sitter doesn't work well with C macros.
> >
> > I program a lot in C++/Qt. So let's look at this (valid) C++ program:
> >
> > -----------------------------------------------------------------------------
> > #include <QObject>
> >
> > class Test : public QObject
> > {
> > Q_OBJECT
> > public:
> > Test() : QObject() {};
> > public slots:
> > void someSlot() {};
> > };
> > -----------------------------------------------------------------------------
> >
> > If have the libraries installed (e.g. qtbase5-dev on Debian), you can
> > compile this perfectly.
> >
> > However, tree-sitter produces a garbage syntax tree:
> >
> > - contain some bitfield node (which isn't really there)
> > - contains an error node (despite the code being compilable)
> >
> > And as a result, BOTH the indentation and the font-locking is wrong.
> >
> >
> > Would I need to create a tree-sitter grammar in JavaScript that
> > understands this macro-enhanced C++? That would be quite difficult.
> > Or will there be a method to add some kind of tiny-preprocessor to
> > c++-ts-mode, so that it can substitute "Q_OBJECT", "signals" and "slots"
> > with nothing before handing things over to tree-sitter?
> >
> >
> > In comparison, I could teach the old cc-mode about this macro-enriched
> > C++ just with
> >
> > (c-add-style "qt-gnu"
> > '("gnu" (c-access-key .
> > "\\<\\(signals\\|public\\|protected\\|private\\|public
> > slots\\|protected slots\\|private slots\\):")))
> >
> >
> > I guess that a lot of C and C++ programs use macros. And if there is no
> > simple way to aid tree-sitter in understanding this, then I fear
> > tree-sitter enhanced modes will often be unusable on them.
>
> My suggestion is simply to stay with CC Mode.
Suggestions for what to do for now aside, I would still want us to try
to figure out the possibilities for better handling of C/C++ macros in
tree-sitter supported modes. I don't want to give up yet, because the
kludges similar to c-add-style used by CC mode might be possible with
tree-sitter modes as well. Or maybe some other solution could work,
including the idea of letting tree-sitter see preprocessed source code
(although this is probably harder to implement, and must be done on
the C level).
We just started using these modes in Emacs, so it is small wonder that
issues like this are popping up, and will probably keep popping up for
some time to come. I see no reason whatsoever to give up on
tree-sitter just because these minor problems in marginal cases are
brought up; we should instead solve them one by one. Being minor
problems, they in no way invalidate the basic decision to try using
tree-sitter in Emacs, not from where I stand.
next prev parent reply other threads:[~2023-02-09 8:50 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-09 8:09 tree-sitter: conceptional problem solvable at Emacs' level? Holger Schurig
2023-02-09 8:17 ` Po Lu
2023-02-09 8:50 ` Eli Zaretskii [this message]
2023-02-09 10:13 ` Po Lu
2023-02-09 10:55 ` Eli Zaretskii
2023-02-10 7:33 ` Yuan Fu
2023-02-10 8:42 ` Eli Zaretskii
[not found] ` <CAOpc7mHX6s0B8vdDee+9FMvQejGTSL3jzgwVekS7Esg-AOf=jw@mail.gmail.com>
2023-02-10 11:48 ` Eli Zaretskii
2023-02-11 2:17 ` Po Lu
2023-02-11 6:25 ` Konstantin Kharlamov
2023-02-11 6:36 ` Konstantin Kharlamov
2023-02-11 6:51 ` Theodor Thornhill
2023-02-11 7:11 ` Konstantin Kharlamov
2023-02-11 7:53 ` Konstantin Kharlamov
2023-02-11 8:22 ` Konstantin Kharlamov
2023-02-11 8:41 ` Theodor Thornhill
2023-02-11 9:37 ` Konstantin Kharlamov
2023-02-11 10:25 ` Konstantin Kharlamov
2023-02-11 8:43 ` Eli Zaretskii
2023-04-16 19:21 ` Konstantin Kharlamov
2023-02-11 9:34 ` Ihor Radchenko
2023-02-11 10:42 ` Eli Zaretskii
2023-02-11 13:58 ` Lynn Winebarger
2023-02-09 16:25 ` Ergus
2023-02-09 20:09 ` Dmitry Gutov
2023-02-10 7:41 ` Holger Schurig
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=83h6vvmdcc.fsf@gnu.org \
--to=eliz@gnu.org \
--cc=Emacs-devel@gnu.org \
--cc=holgerschurig@gmail.com \
--cc=luangruo@yahoo.com \
/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.