From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Yuan Fu Newsgroups: gmane.emacs.devel Subject: Re: How does c-ts-mode, tree-sitter indentation, and preprocessor directives work? Date: Sat, 30 Nov 2024 22:18:36 -0800 Message-ID: <52D99EBA-1DCB-4559-A645-A53E7CF82FED@gmail.com> References: <86plmferwu.fsf@gnu.org> Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3776.700.51\)) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="26506"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Eli Zaretskii , =?utf-8?Q?Bj=C3=B6rn_Lindqvist?= , emacs-devel@gnu.org To: Filippo Argiolas Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sun Dec 01 07:19:20 2024 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1tHdId-0006no-Mi for ged-emacs-devel@m.gmane-mx.org; Sun, 01 Dec 2024 07:19:19 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tHdIG-0005TQ-TH; Sun, 01 Dec 2024 01:18:56 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tHdIE-0005TH-S2 for emacs-devel@gnu.org; Sun, 01 Dec 2024 01:18:54 -0500 Original-Received: from mail-pg1-x52a.google.com ([2607:f8b0:4864:20::52a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tHdIA-0002c4-UV; Sun, 01 Dec 2024 01:18:53 -0500 Original-Received: by mail-pg1-x52a.google.com with SMTP id 41be03b00d2f7-7fbc29b3145so2964611a12.0; Sat, 30 Nov 2024 22:18:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733033929; x=1733638729; darn=gnu.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=EB3egTvjubX94DmN3iDbVJPNd/xuUoSy0gXPvITRXdI=; b=C097zSUjyS+x+ZP8usDWzcLMb0Pcm64LkyZVjR2WBYLg7FSq5ZVD818Vu3Z2XBuqaZ Z2IlG6PYuVQZ/hwY/ZAzzNvt9wmLCiiP2mmJHmhxUGmWV/34xus4ZmCXeitR+YZFQFjf I4+f9yWOKsNZIGfs6rtZ26E7L06S3rZJMnqdLur3cuHXKA3nfkcEqadeOZ7A5q8TJoSu tMpLtLo/aPrjRHVuAdJXH+7c5OtjStbGfD1fzv1Sg1btFykzdfDp4j7S+1Fc1XOZ4OZI 9JLB4y+qOZaOfgY8xyJn7a+iKfgRfcbK9hkFQFFOY57BZw6Fbt+AgBJa3AEkYcz9LhL1 DZ/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733033929; x=1733638729; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=EB3egTvjubX94DmN3iDbVJPNd/xuUoSy0gXPvITRXdI=; b=Es7e6INshkPiKz34xlNQZ9T+qG82DgWOuZ8LazTWxDIB41j8mVO+ZnxpRcFAM0RbbD fKKC7knj3G9CS6IWQxObQKgM86fYFOaP4iLmZ4u+iv1ltDyDzO++jcZgfxgcJu3kkU0a +FQhxd6YxNVJq2/QFoNYb9lD9plyr5oqhnClKuPhCUMkKHSvVTNC8LLhG4Uuk17deZ7I tGOvQMsNZW/xNzkkV286BQYNfprlIUkaDRCPatdgeV+6xtWvb5W/e/a4vY7q941qpH8X BdMkzfze/Nj0QlRIGnNKxLaSlUHTnnU4MboLMsCWivOhe1amHeiZ0o/bxvTDJGKbvOTM 2Ohw== X-Forwarded-Encrypted: i=1; AJvYcCVzec/ZC0k5KhWF4CMUJTJd7fgSrojqW06Fw93RrhKm/IzvQk0q+nJX1b8DJVVRz6pNC/Ppc+lBT5UqsA==@gnu.org X-Gm-Message-State: AOJu0Yw7LDjplk8BkUiPrdM+KoE80ZJdfmJeFzUV8Nxvl006FhWs3S0G eUClPMMViOP1uzIjKWJ0RYxm8FapS993R8ZmHXEphlAjgR5tKVGdfOWqGQ== X-Gm-Gg: ASbGncs1xoDtB4We9uHAIGBPMg5w/8yqAJjGrVQb0LQ5LBm254R4azM/m4I6/kOhV4Z VAC3K5yqZ9NZC+pHpx6/h42T0mVl6Iqi+vzilrrLEgL3bdV/J4S3y/dwyaVwkFSWOvUSttEhJhJ f+3aBIaJURbaMP+tIdsOKZGngsvLG6/krBEIxkoPEkQE7RL4opJEfpwNSuvnPc1j9lSbydFbAmy +gXXu3bTuvmwb/l+hBtibk7JBzkwtK5BKUpA/wCinhCoCr/xleJprWV1t/ANImpxUjY0eBr9Q== X-Google-Smtp-Source: AGHT+IGI6QoNW+J/GiDyx6na9UTz7J4XnqSZd/G21jrNf4AjR6keXaZAi9CDzkGZmk1XZKNjb0x3Ww== X-Received: by 2002:a05:6a20:1593:b0:1e0:c8c5:9b24 with SMTP id adf61e73a8af0-1e0ec8724bdmr21079603637.16.1733033929042; Sat, 30 Nov 2024 22:18:49 -0800 (PST) Original-Received: from smtpclient.apple ([2601:646:8f81:6120:71b7:718f:7faa:8436]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7fc9c385a4csm5538656a12.67.2024.11.30.22.18.47 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 30 Nov 2024 22:18:48 -0800 (PST) In-Reply-To: X-Mailer: Apple Mail (2.3776.700.51) Received-SPF: pass client-ip=2607:f8b0:4864:20::52a; envelope-from=casouri@gmail.com; helo=mail-pg1-x52a.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:325902 Archived-At: > On Nov 28, 2024, at 10:30=E2=80=AFAM, Filippo Argiolas = wrote: >=20 > Eli Zaretskii writes: >=20 >>> From: Bj=C3=B6rn Lindqvist >>> Date: Thu, 28 Nov 2024 00:27:17 +0100 >>>=20 >>> I've been trying to get c-ts-mode to indent like I want, but I'm >>> running into problems related to preprocessor directives. >>=20 >> Preprocessor directives are difficult because the tree-sitter C/C++ >> grammars include only partial support for them. >>=20 >>> For >>> example, consider a type definition nested in two #ifdefs: >>>=20 >>> #ifdef X >>> #ifdef Y >>> typedef int foo; >>> #endif >>> #endif >>>=20 >>> Since both the parent and grand parent of the type_definition is a >>> preproc_ifdef no rule matches. >>=20 >> But if you go back (up) the parent-child hierarchy, you will >> eventually find a node which is not a preproc_SOMETHING, and can go >> from there, no? >>=20 >=20 > I believe we might have a bug here, as far as I can tell it does not > match >=20 > ((n-p-gp nil "preproc" "translation_unit") column-0 0) >=20 > Because both parent and grand parent are preproc. So it matches one of > the `c-ts-mode--standalone-parent-skip-preproc' rules right after. >=20 > After skipping preproc nodes parent is translation_unit and indents an = offset > from there. Guess this step could be made smarter to check for > translation_unit and the rule above could be removed? >=20 >>> Another issue is that I want my >>> preprocessor directives kept at column 0, which unfortunately screws >>> up all rules that refer to the parent. E.g.: >>>=20 >>> ((parent-is "if_statement") standalone-parent 4) >>>=20 >>> Doesn't work for >>>=20 >>> int main() { >>> if (true) >>> #ifdef A >>> prutt(); >>> #else >>> fis(); >>> #endif >>> } >>>=20 >>> The rule I'd like to express is "take the indent of the closest >>> *indenting* parent and add one indent". That rule would match = whether >>> that parent is a "while_statement", "if_statement", "for_statement", >>> etc. You can't express such rules with tree-sitter, can you? >>=20 >> Not sure, but Yuan will know. >=20 > This can be worked around as Yuan showed, but isn't it a grammar bug? > problem is with the #ifdef function and if statement become siblings, = without > preproc they have a child-parent relation. >=20 > In my experience c-ts-mode is a bit fragile with preprocessor > statements, probably because the grammar itself is fragile (see > e.g. [1]) and the problem is an hard one. Right. > Yuan, do you think c-ts-mode could some way benefit from LSP knowledge > about inactive preprocessor branches? Idea is that we would at least > have a good syntax tree in the active branches while allowing some > errors in the inactive ones. Maybe. Technically you can create a parser and sets its range to only = included the active branches. But for it to work end-to-end would = require some major effort. I=E2=80=99m not sure if it=E2=80=99s worth it = (in terms of code complexity and maintenance cost). Yuan=