unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Re: Emacs-devel Digest, Vol 148, Issue 12
       [not found] <mailman.5130.1465262749.1213.emacs-devel@gnu.org>
@ 2016-06-07 14:27 ` andy22286
  2016-06-07 18:59   ` Stefan Monnier
  0 siblings, 1 reply; 4+ messages in thread
From: andy22286 @ 2016-06-07 14:27 UTC (permalink / raw)
  To: emacs-devel, monnier

[-- Attachment #1: Type: text/plain, Size: 3034 bytes --]

Somewhat related: here is an example I am having trouble with:

sml-smie.el:

(require 'smie)

(defconst sml-smie-grammar
  (smie-prec2->grammar
   (smie-bnf->prec2
    '((exp
       (var)
       ("local" exp "in" exp "end"))
      (var))
   )))

(defun sml-smie-rules (kind token)
  (pcase (cons kind token)
    (`(:elem . basic) 4)
    (`(:elem . args) 2)
    ))

(define-derived-mode my-sml-mode sml-mode "My SML"
  (smie-setup
   sml-smie-grammar
   #'sml-smie-rules))

indented test.sml:

local
  local
  a
in
    a
end
in
    a
end

I set the basic and args indentation to different values to show the
problem I am running in to.  I am not sure how this is being interpreted.
The desired interpretation would be something like

(local
  (local
   a
   in
   a
   end)
 in
 a
 end)

though I am not sure if I am thinking about this properly.  I am not sure
when SMIE interprets entries as function arguments.  In the indentation of
test.sml, it appears that the second occurrence of "local" and the first
occurrence of "a" are interpreted as arguments to the first occurrence of
"local".

Also, in general:

Does the equivalent S-expression representing the parse tree have terminals
in order, or are they moved to the front - i.e. does 1 + 2 for grammar
(number "+" number) become (1 + 2) or (+ 1 2)?

Is there any special behavior that occurs when generating the parse tree
(either actually generating it or acting on an implicit parse tree)?

What does smie-rule-parent-p use to determine the parent of a token?  Is it
the first token above the S-expression the current token is contained in,
e.g. in ("a" "b" ("c" "d" "e") "f"), the parent of "d" is "b"?

Thank you for all of the help!

Andy

On Mon, Jun 6, 2016 at 9:25 PM, <emacs-devel-request@gnu.org> wrote:
>
> Date: Mon, 06 Jun 2016 20:54:48 -0400
> From: Stefan Monnier <monnier@iro.umontreal.ca>
> To: emacs-devel@gnu.org
> Subject: Re: Debugging SMIE by printing an S-expression
> Message-ID: <jwv37ophlhi.fsf-monnier+gmane.emacs.devel@gnu.org>
> Content-Type: text/plain
>
> > I am having some trouble understanding the indentation I'm getting for a
> > simple SMIE grammar and set of indentation rules.  It seems like it would
> > be useful to be able to print out an S-expression of what is being
> parsed -
> > the buffer, whatever has been reduced, really, anything at all just to
> give
> > me some insight as to what I want to match against in the indentation
> rules
> > - but really, to make sure what I think the grammar should produce is
> being
> > produced.  Is there an easy way to do this?
>
> Not really, no.  I don't think it would necessarily require changes to
> the existing code, but it would require a second implementation of
> parsing which additionally builds an s-exp of what it parsed.
>
> Instead, the usual way I use to find out how things are parsed is with
> C-M-f and C-M-b, which is somewhat crude but works fairly well in my
> experience once you learn to figure out how to interpret its behavior.
>
>
>         Stefan
>
>
>
>
>
>

[-- Attachment #2: Type: text/html, Size: 5369 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Emacs-devel Digest, Vol 148, Issue 12
  2016-06-07 14:27 ` Emacs-devel Digest, Vol 148, Issue 12 andy22286
@ 2016-06-07 18:59   ` Stefan Monnier
  2016-06-07 19:01     ` andy22286
  0 siblings, 1 reply; 4+ messages in thread
From: Stefan Monnier @ 2016-06-07 18:59 UTC (permalink / raw)
  To: andy22286@gmail.com; +Cc: emacs-devel

> (require 'smie)
> (defconst sml-smie-grammar
>   (smie-prec2->grammar
>    (smie-bnf->prec2
>     '((exp
>        (var)
>        ("local" exp "in" exp "end"))
>       (var))
>    )))
> (defun sml-smie-rules (kind token)
>   (pcase (cons kind token)
>     (`(:elem . basic) 4)
>     (`(:elem . args) 2)
>     ))
> (define-derived-mode my-sml-mode sml-mode "My SML"
>   (smie-setup
>    sml-smie-grammar
>    #'sml-smie-rules))
>
> indented test.sml:
>
> local
>   local
>   a
> in
>     a
> end
> in
>     a
> end

Hmm... this seems to be clearly an interaction with sml-mode.

I just tried it with my local copy of sml-mode (straight from elpa.git)
and don't see the above indentation behavior.  I also tried it after
replacing `sml-mode' with `prog-mode' as the parent, and that doesn't
show the problematic behavior either.

> Does the equivalent S-expression representing the parse tree have terminals
> in order, or are they moved to the front - i.e. does 1 + 2 for grammar
> (number "+" number) become (1 + 2) or (+ 1 2)?

The tree is never created, so the question doesn't really have an answer.
The grammar is only used for navigation (e.g. to jump over the "+ 2 * z"
in "x + 2 * z + y" when moving forward).

> What does smie-rule-parent-p use to determine the parent of a token?  Is it
> the first token above the S-expression the current token is contained in,
> e.g. in ("a" "b" ("c" "d" "e") "f"), the parent of "d" is "b"?

I think it would be "c".


        Stefan



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Emacs-devel Digest, Vol 148, Issue 12
  2016-06-07 18:59   ` Stefan Monnier
@ 2016-06-07 19:01     ` andy22286
  2016-06-07 19:07       ` Stefan Monnier
  0 siblings, 1 reply; 4+ messages in thread
From: andy22286 @ 2016-06-07 19:01 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1878 bytes --]

Thank you for the answers.  You are right about the interaction with
sml-mode.  I just tried reloading the file and then restarting the derived
minor mode manually (M-x my-sml-mode), and the problem is gone.

On Tue, Jun 7, 2016 at 2:59 PM, Stefan Monnier <monnier@iro.umontreal.ca>
wrote:

> > (require 'smie)
> > (defconst sml-smie-grammar
> >   (smie-prec2->grammar
> >    (smie-bnf->prec2
> >     '((exp
> >        (var)
> >        ("local" exp "in" exp "end"))
> >       (var))
> >    )))
> > (defun sml-smie-rules (kind token)
> >   (pcase (cons kind token)
> >     (`(:elem . basic) 4)
> >     (`(:elem . args) 2)
> >     ))
> > (define-derived-mode my-sml-mode sml-mode "My SML"
> >   (smie-setup
> >    sml-smie-grammar
> >    #'sml-smie-rules))
> >
> > indented test.sml:
> >
> > local
> >   local
> >   a
> > in
> >     a
> > end
> > in
> >     a
> > end
>
> Hmm... this seems to be clearly an interaction with sml-mode.
>
> I just tried it with my local copy of sml-mode (straight from elpa.git)
> and don't see the above indentation behavior.  I also tried it after
> replacing `sml-mode' with `prog-mode' as the parent, and that doesn't
> show the problematic behavior either.
>
> > Does the equivalent S-expression representing the parse tree have
> terminals
> > in order, or are they moved to the front - i.e. does 1 + 2 for grammar
> > (number "+" number) become (1 + 2) or (+ 1 2)?
>
> The tree is never created, so the question doesn't really have an answer.
> The grammar is only used for navigation (e.g. to jump over the "+ 2 * z"
> in "x + 2 * z + y" when moving forward).
>
> > What does smie-rule-parent-p use to determine the parent of a token?  Is
> it
> > the first token above the S-expression the current token is contained in,
> > e.g. in ("a" "b" ("c" "d" "e") "f"), the parent of "d" is "b"?
>
> I think it would be "c".
>
>
>         Stefan
>

[-- Attachment #2: Type: text/html, Size: 2852 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Emacs-devel Digest, Vol 148, Issue 12
  2016-06-07 19:01     ` andy22286
@ 2016-06-07 19:07       ` Stefan Monnier
  0 siblings, 0 replies; 4+ messages in thread
From: Stefan Monnier @ 2016-06-07 19:07 UTC (permalink / raw)
  To: andy22286@gmail.com; +Cc: emacs-devel

> Thank you for the answers.  You are right about the interaction with
> sml-mode.  I just tried reloading the file and then restarting the derived
> minor mode manually (M-x my-sml-mode), and the problem is gone.

I think part of the problem is a name conflict.  You defined:

>> > (defun sml-smie-rules (kind token)

but that function is also defined in sml-mode, so you'll get whichever
was loaded last.  Same thing for sml-smie-grammar.

Better use names of the form "my-sml-..." to avoid those conflicts.


        Stefan



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2016-06-07 19:07 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.5130.1465262749.1213.emacs-devel@gnu.org>
2016-06-07 14:27 ` Emacs-devel Digest, Vol 148, Issue 12 andy22286
2016-06-07 18:59   ` Stefan Monnier
2016-06-07 19:01     ` andy22286
2016-06-07 19:07       ` Stefan Monnier

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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).