unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: Miles Bader <miles@gnu.org>
Cc: Michael Welsh Duggan <md5i@md5i.com>, emacs-devel@gnu.org
Subject: Re: good "modern" example code for a programming-language mode?
Date: Wed, 16 Feb 2011 09:56:05 -0500	[thread overview]
Message-ID: <jwvaahwjb2w.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <87wrl01dts.fsf@engster.org> (David Engster's message of "Wed, 16 Feb 2011 11:19:43 +0100")

> Regarding indentation: this is much more a matter of taste than
> semantics or syntax. If there was a direct connection between semantical
> analysis and indentation, why were all those wars fought for the
> "correct" indentation of C, for instance?

OTOH there is a clear connection between syntax and indentation.
My experience (which lead me to write SMIE) is that all indentation
styles for all languages share a lot of common principles, so that
indentation can be (partly) automatically derived from the language's
grammar.  And indeed, if you give a valid grammar to SMIE, it will give
you a valid indentation function in return.  Of course, the indentation
style it uses will most likely not fully match the user's expectations,
so SMIE provides a hook to tweak the default indentation rules for the
various possible situations.

E.g. for Prolog, the tweaks I've needed until now are limited to:

(defun prolog-smie-rules (kind token)
  (pcase (cons kind token)
    (`(:elem . basic) prolog-indent-width)
    (`(:after . ".") 0) ;; To work around smie-closer-alist.
    (`(:after . ,(or `":-" `"->" `"-->")) prolog-indent-width))

and for /bin/sh it currently looks like:

(defun sh-smie-rules (kind token)
  (pcase (cons kind token)
    (`(:elem . basic) sh-indentation)
    (`(:after . "case-)") (or sh-indentation smie-indent-basic))
    (`(:before . ,(or `"(" `"{" `"["))
     (if (smie-rule-hanging-p) (smie-rule-parent)))
    ;; FIXME: Maybe this handling of ;; should be made into
    ;; a smie-rule-terminator function that takes the substitute ";" as arg.
    (`(:before . ,(or `";;" `";&" `";;&"))
     (if (and (smie-rule-bolp) (looking-at ";;?&?[ \t]*\\(#\\|$\\)"))
         (cons 'column (smie-indent-keyword ";"))
       (smie-rule-separator kind)))
    (`(:after . ,(or `";;" `";&" `";;&"))
     (with-demoted-errors
       (smie-backward-sexp token)
       (cons 'column (current-column))))))

You can check what's used in Modula-2 and Octave by looking for
"smie-rules".

For SML it is a good bit more complex, for some reason (even though SML
was my original inspiration for SMIE).  Maybe it's because the
language's syntax tends to introduce a lot more indentation than is
practical, so people have come up with all kinds of conventions for
cases where indentation can be reduced without losing the property that
indentation reflects internal structure.

> Just looking at c-style-alist will make that clear.

Indeed, for some reason C indentation is a lot more contentious than
most other languages.  I haven't yet tried to use SMIE for C-like
languages, so I'm not sure how well it would accommodate all the
nitty-gritty details of each one's personal favorite indentation style.


        Stefan



  parent reply	other threads:[~2011-02-16 14:56 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-02-15  9:11 good "modern" example code for a programming-language mode? Miles Bader
2011-02-15 17:43 ` Stefan Monnier
2011-02-16  5:16   ` Miles Bader
2011-02-15 22:22 ` Michael Welsh Duggan
2011-02-16  0:47   ` Miles Bader
2011-02-16 10:19     ` David Engster
2011-02-16 13:59       ` Andy Moreton
2011-02-16 15:00         ` David Kastrup
2011-02-16 14:56       ` Stefan Monnier [this message]
2011-02-16 15:54         ` Tassilo Horn
2011-02-16 21:00           ` Stefan Monnier
2011-02-16 16:58         ` David Engster
2011-02-16 21:07           ` Stefan Monnier
2011-02-17  3:42       ` Miles Bader

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=jwvaahwjb2w.fsf-monnier+emacs@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --cc=emacs-devel@gnu.org \
    --cc=md5i@md5i.com \
    --cc=miles@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.
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).