unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: help-gnu-emacs@gnu.org
Subject: Re: SMIE: if-elseif-else constructs
Date: Tue, 06 Oct 2020 10:04:10 -0400	[thread overview]
Message-ID: <jwvy2kjv5eu.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: CAHp59H2D41tK_DV=NRzDNCM9vp1J1jOtnZmxjGB58ZA2FnSBQw@mail.gmail.com

> I think I've found a better way. I revised the grammar and adjusted
> the indentation rules.
> The following seems to work.

It looks OK, now (tho I think it implies that "elsif" is nested within
the right hand side of "then", which I think is wrong in the sense that
it doesn't reflect the natural shape of the abstract syntax tree).
It seems similar to the approach I've used in such cases, except I would
have used something like:

    (inst ("if" if-body "end"))
    (if-body (exp "then" insts)
             (if-body "elseif" if-body)
             (if-body "else" exp))

with an ((assoc "elseif") (nonassoc "else")).  I believe this better
reflects the intended abstract syntax tree, so it should behave a bit
better w.r.t indentation and navigation.

BTW, another way to define the grammar can be:

     [...]
     (inst ("if" exp "then" insts "end")
           ("if" exp "then" insts "else" insts "end")
           ("if" exp "then" insts "elsif" exp "then" insts "else" insts "end")
     [...]

You don't need to list all the (infinite number of) combinations, the
above is sufficient for SMIE to figure out the needed relative
constraints between the precedences of the keywords.  But IIRC this
approach tends to behave less robustly in cases of syntax error (by
which I mean either when the source code is incorrect or when SMIE
parses incorrectly because its grammar or lexer isn't good enough).


        Stefan




      reply	other threads:[~2020-10-06 14:04 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-03 15:16 SMIE: if-elseif-else constructs Andreas Matthias
2020-10-03 16:24 ` Stefan Monnier
2020-10-06 13:29   ` Andreas Matthias
2020-10-06 14:04     ` Stefan Monnier [this message]

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=jwvy2kjv5eu.fsf-monnier+emacs@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --cc=help-gnu-emacs@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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).