all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Alan Mackenzie <acm@muc.de>
To: Daniel Colascione <dancol@dancol.org>
Cc: Emacs developers <emacs-devel@gnu.org>
Subject: Re: "enum class" supports for cc-mode
Date: Wed, 29 Oct 2014 16:49:15 +0000	[thread overview]
Message-ID: <20141029164915.GC2953@acm.acm> (raw)
In-Reply-To: <544E9099.7030409@dancol.org>

Hi, Daniel.

On Mon, Oct 27, 2014 at 06:36:09PM +0000, Daniel Colascione wrote:
> Here's a small patch that teaches cc-mode about C++11 "enum class" syntax.
> See http://msdn.microsoft.com/en-us/library/2dzy4k6e.aspx

> Alan, can you take a look?

Thanks for this, it looks good.  I'll definitely install it (with proper
attribution).

Just one or two little details:
(i) You've used `eql' rather than `eq' for comparing things in a few
  places.  Is there any reason for this?
(ii) c-after-brace-list-key will be "\\<\\>" (not nil) for non-C++
  languages, I think.  It's definition in cc-langs.el needs tweaking a
  bit to make it nil.
(iii) There's a question as to whether the backward searching in
  c-backward-colon-prefixed-type should have an optional search limit.
  But the existing c-backward-over-enum-header doesn't, so that's
  something new to think about.

Now, I'll go and try actually merging and running it.

> === modified file 'lisp/progmodes/cc-engine.el'
> --- lisp/progmodes/cc-engine.el	2014-10-18 10:02:59 +0000
> +++ lisp/progmodes/cc-engine.el	2014-10-27 18:00:51 +0000
> @@ -8460,28 +8460,41 @@
>  	   (cond
>  	    ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t)
>  	     (goto-char (setq kwd-start (match-beginning 0)))
> -	     (or
> -
> -	      ;; Found a keyword that can't be a type?
> -	      (match-beginning 1)
> -
> -	      ;; Can be a type too, in which case it's the return type of a
> -	      ;; function (under the assumption that no declaration level
> -	      ;; block construct starts with a type).
> -	      (not (c-forward-type))
> -
> -	      ;; Jumped over a type, but it could be a declaration keyword
> -	      ;; followed by the declared identifier that we've jumped over
> -	      ;; instead (e.g. in "class Foo {").  If it indeed is a type
> -	      ;; then we should be at the declarator now, so check for a
> -	      ;; valid declarator start.
> -	      ;;
> -	      ;; Note: This doesn't cope with the case when a declared
> -	      ;; identifier is followed by e.g. '(' in a language where '('
> -	      ;; also might be part of a declarator expression.  Currently
> -	      ;; there's no such language.
> -	      (not (or (looking-at c-symbol-start)
> -		       (looking-at c-type-decl-prefix-key)))))
> +             (and
> +              ;; Exclude cases where we matched what would ordinarily
> +              ;; be a block declaration keyword, except where it's not
> +              ;; legal because it's part of a "compound keyword" like
> +              ;; "enum class".  Of course, if c-after-brace-list-key
> +              ;; is nil, we can skip the test.
> +              (or (null c-after-brace-list-key)
> +                  (save-match-data
> +                    (save-excursion
> +                      (not
> +                       (and
> +                        (looking-at c-after-brace-list-key)
> +                        (= (c-backward-token-2 1 t) 0)
> +                        (looking-at c-brace-list-key))))))
> +              (or
> +               ;; Found a keyword that can't be a type?
> +               (match-beginning 1)
> +
> +               ;; Can be a type too, in which case it's the return type of a
> +               ;; function (under the assumption that no declaration level
> +               ;; block construct starts with a type).
> +               (not (c-forward-type))
> +
> +               ;; Jumped over a type, but it could be a declaration keyword
> +               ;; followed by the declared identifier that we've jumped over
> +               ;; instead (e.g. in "class Foo {").  If it indeed is a type
> +               ;; then we should be at the declarator now, so check for a
> +               ;; valid declarator start.
> +               ;;
> +               ;; Note: This doesn't cope with the case when a declared
> +               ;; identifier is followed by e.g. '(' in a language where '('
> +               ;; also might be part of a declarator expression.  Currently
> +               ;; there's no such language.
> +               (not (or (looking-at c-symbol-start)
> +                        (looking-at c-type-decl-prefix-key))))))
>  
>  	    ;; In Pike a list of modifiers may be followed by a brace
>  	    ;; to make them apply to many identifiers.  Note that the
> @@ -8586,9 +8599,35 @@
>  		      (not (looking-at "=")))))
>        b-pos)))
>  
> +(defun c-backward-colon-prefixed-type ()
> +  ;; We're after what might be a type prefixed with a colon.  Try
> +  ;; moving backward over this type and the colon.  On success, return
> +  ;; t and leave point before colon, on falure, leave point unchanged.
> +  ;; Will clobber match data.
> +  (let ((colon-pos nil))
> +    (save-excursion
> +      (while
> +          (and (eql (c-backward-token-2) 0)
> +               (or (not (looking-at "\\s)"))
> +                   (c-go-up-list-backward))
> +               (cond
> +                 ((eql (char-after) ?:)
> +                  (setq colon-pos (point))
> +                  nil)
> +                 ((eql (char-after) ?\()
> +                  t)
> +                 ((looking-at c-symbol-key)
> +                  t)
> +                 (t nil)))))
> +    (when colon-pos
> +      (goto-char colon-pos)
> +      t)))
> +
>  (defun c-backward-over-enum-header ()
>    ;; We're at a "{".  Move back to the enum-like keyword that starts this
>    ;; declaration and return t, otherwise don't move and return nil.
> +  (when c-recognize-post-brace-list-type-p
> +    (c-backward-colon-prefixed-type))
>    (let ((here (point))
>  	up-sexp-pos before-identifier)
>      (while
> @@ -8596,21 +8635,22 @@
>  	 (eq (c-backward-token-2) 0)
>  	 (or (not (looking-at "\\s)"))
>  	     (c-go-up-list-backward))
> -	 (cond
> -	  ((and (looking-at c-symbol-key) (c-on-identifier)
> -		(not before-identifier))
> -	   (setq before-identifier t))
> -	  ((and before-identifier
> -		(or (eq (char-after) ?,)
> -		    (looking-at c-postfix-decl-spec-key)))
> -	   (setq before-identifier nil)
> -	   t)
> -	  ((looking-at c-brace-list-key) nil)
> -	  ((and c-recognize-<>-arglists
> -		(eq (char-after) ?<)
> -		(looking-at "\\s("))
> -	   t)
> -	  (t nil))))
> +         (cond
> +           ((and (looking-at c-symbol-key) (c-on-identifier)
> +                 (not before-identifier))
> +            (setq before-identifier t))
> +           ((and before-identifier
> +                 (or (eql (char-after) ?,)
> +                     (looking-at c-postfix-decl-spec-key)))
> +            (setq before-identifier nil)
> +            t)
> +           ((looking-at c-after-brace-list-key) t)
> +           ((looking-at c-brace-list-key) nil)
> +           ((and c-recognize-<>-arglists
> +                 (eq (char-after) ?<)
> +                 (looking-at "\\s("))
> +            t)
> +           (t nil))))
>      (or (looking-at c-brace-list-key)
>  	(progn (goto-char here) nil))))
>  
> 
> === modified file 'lisp/progmodes/cc-langs.el'
> --- lisp/progmodes/cc-langs.el	2014-10-12 20:09:15 +0000
> +++ lisp/progmodes/cc-langs.el	2014-10-27 17:34:41 +0000
> @@ -1805,8 +1805,31 @@
>    ;; Regexp matching the start of declarations where the following
>    ;; block is a brace list.
>    t (c-make-keywords-re t (c-lang-const c-brace-list-decl-kwds)))
> +
>  (c-lang-defvar c-brace-list-key (c-lang-const c-brace-list-key))
>  
> +(c-lang-defconst c-after-brace-list-decl-kwds
> +  "Keywords that might follow keywords in `c-brace-list-decl-kwds'
> +and precede the opening brace."
> +  t    nil
> +  c++  '("class" "struct"))
> +
> +(c-lang-defconst c-after-brace-list-key
> +  ;; Regexp matching keywords that can fall between a brace-list
> +  ;; keyword and the associated brace list.
> +  t (c-make-keywords-re t (c-lang-const c-after-brace-list-decl-kwds)))
> +
> +(c-lang-defvar c-after-brace-list-key (c-lang-const c-after-brace-list-key))
> +
> +(c-lang-defconst c-recognize-post-brace-list-type-p
> +  ;; Recognize a colon and then a type after an enum, e.g.,
> +  ;; enum foo : int { A, B, C };
> +  t nil
> +  c++ t)
> +
> +(c-lang-defvar c-recognize-post-brace-list-type-p
> +               (c-lang-const c-recognize-post-brace-list-type-p))
> +
>  (c-lang-defconst c-other-block-decl-kwds
>    "Keywords where the following block (if any) contains another
>  declaration level that should not be considered a class.  For every
> @@ -2000,6 +2023,7 @@
>    ;; prefixes.
>    t (delete-duplicates (append (c-lang-const c-class-decl-kwds)
>  			       (c-lang-const c-brace-list-decl-kwds)
> +                               (c-lang-const c-after-brace-list-decl-kwds)
>  			       (c-lang-const c-other-block-decl-kwds)
>  			       (c-lang-const c-typedef-decl-kwds)
>  			       (c-lang-const c-typeless-decl-kwds)


-- 
Alan Mackenzie (Nuremberg, Germany).



  reply	other threads:[~2014-10-29 16:49 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-27 18:36 "enum class" supports for cc-mode Daniel Colascione
2014-10-29 16:49 ` Alan Mackenzie [this message]
2014-10-29 17:02   ` Daniel Colascione
2014-10-30 16:21     ` Alan Mackenzie
2014-10-30 18:56       ` Daniel Colascione
2014-10-31 20:35       ` Glenn Morris
2014-11-01 10:39         ` Alan Mackenzie

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

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

  git send-email \
    --in-reply-to=20141029164915.GC2953@acm.acm \
    --to=acm@muc.de \
    --cc=dancol@dancol.org \
    --cc=emacs-devel@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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.