help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: SMIE: if-elseif-else constructs


From: Stefan Monnier
Subject: Re: SMIE: if-elseif-else constructs
Date: Tue, 06 Oct 2020 10:04:10 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

> 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 via email to

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