emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: tree-sitter: conceptional problem solvable at Emacs' level?


From: Yuan Fu
Subject: Re: tree-sitter: conceptional problem solvable at Emacs' level?
Date: Thu, 9 Feb 2023 23:33:10 -0800


> On Feb 9, 2023, at 12:17 AM, Po Lu <luangruo@yahoo.com> wrote:
> 
> 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.
> 
> Parsers (without a full C preprocessor inside) can only work for
> languages like Python, which cannot be enhanced with syntax-modifying
> macros.
> 

Right. Our best hope is for someone to try extend the current tree-sitter-c 
grammar, but I don’t know how feasible it is. Emacs can also do some limited 
workaround, but the potential in that department is slim.

Yuan


reply via email to

[Prev in Thread] Current Thread [Next in Thread]