all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* CC Mode - syntactical can of worms.
@ 2008-01-31 11:45 Alan Mackenzie
  2008-01-31 12:53 ` Andreas Schwab
  2008-01-31 15:00 ` Stefan Monnier
  0 siblings, 2 replies; 5+ messages in thread
From: Alan Mackenzie @ 2008-01-31 11:45 UTC (permalink / raw)
  To: emacs-devel, bug-cc-mode

Hi, Emacs and CC Mode!

Following up from: [orium69@gmail.com: Bug in emacs 22.1.1 (cosmetic
bug)].

This bug was in C Mode, with the following source line:

    #warning for isn't a keyword here.

This fouls up the fontification, because the syntactic fontification
recognises the apostrophe as a string opener.  It can get pretty bad -
here is a syntactically correct C function:

1   #warning for isn't a keyword here. (
2   //#warning for isnt a keyword here.
3   void foo (bar)
4   {
5   #error Brace yourself! }
6       printf ("Hello, world!\n") ; /* the famous one liner! */
7   }

The apostrophe in L1 fouls up the entire fontification.  The pseudo
brace in L5 spuriously matches the real one on L4.  The brace on L7
mismatches the pseudo parenthesis on L1.

The root of the problem is that CPP lines are allowed to mingle
syntactically with ordinary lines.  Somehow, CPP lines need to be
"commented out" from the Emacs's syntactic routines.  However, turning
# and EOL into another pair of comment delimiters is NOT the Right
Thing.

I think I need a hook function somewhere (after-change, but
before-font-lock, maybe) which should analyse the innards of CPP lines
for unbalanced string quotes or unbalanced parens/braces/brackets, or
the like.

It feels like another syntax category is wanted for such things, a kind
of "open-enclosure / close-enclosure (or close-enopener? ;-)" pair,
whose purpose is syntactically to isolate its contents from the rest of
the file.

Would anybody like to give me any ideas here?

-- 
Alan Mackenzie (Nuremberg, Germany).

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/


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

* Re: CC Mode - syntactical can of worms.
  2008-01-31 11:45 CC Mode - syntactical can of worms Alan Mackenzie
@ 2008-01-31 12:53 ` Andreas Schwab
  2008-01-31 15:00 ` Stefan Monnier
  1 sibling, 0 replies; 5+ messages in thread
From: Andreas Schwab @ 2008-01-31 12:53 UTC (permalink / raw)
  To: Alan Mackenzie; +Cc: bug-cc-mode, emacs-devel

Alan Mackenzie <acm@muc.de> writes:

> Hi, Emacs and CC Mode!
>
> Following up from: [orium69@gmail.com: Bug in emacs 22.1.1 (cosmetic
> bug)].
>
> This bug was in C Mode, with the following source line:
>
>     #warning for isn't a keyword here.

This is not a valid preprocessing directive, it lacks a matching single
quote.

> Would anybody like to give me any ideas here?

I'd just ignore this case.  IMHO it is quite approriate to completely
mess up the fontification so that the error is very visible.

Andreas.

-- 
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."




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

* Re: CC Mode - syntactical can of worms.
  2008-01-31 11:45 CC Mode - syntactical can of worms Alan Mackenzie
  2008-01-31 12:53 ` Andreas Schwab
@ 2008-01-31 15:00 ` Stefan Monnier
  2008-01-31 22:05   ` Alan Mackenzie
  1 sibling, 1 reply; 5+ messages in thread
From: Stefan Monnier @ 2008-01-31 15:00 UTC (permalink / raw)
  To: Alan Mackenzie; +Cc: bug-cc-mode, emacs-devel

> Hi, Emacs and CC Mode!
> Following up from: [orium69@gmail.com: Bug in emacs 22.1.1 (cosmetic
> bug)].

> This bug was in C Mode, with the following source line:

>     #warning for isn't a keyword here.

> This fouls up the fontification, because the syntactic fontification
> recognises the apostrophe as a string opener.  It can get pretty bad -
> here is a syntactically correct C function:

> 1   #warning for isn't a keyword here. (
> 2   //#warning for isnt a keyword here.
> 3   void foo (bar)
> 4   {
> 5   #error Brace yourself! }
> 6       printf ("Hello, world!\n") ; /* the famous one liner! */
> 7   }

You forget other interesting constructs such as

   #define HELLO }
   #define WORLD )
   
   void foo (WORLD
   {
     printf ("Hello\n"WORLD;
   HELLO

and of course, the more common ones like

   #if foo
   void foo (void)
   {
      size_t toto;
   #else
   int foo ()
   {
      unsigned toto;
   #endif

or how 'bout

   #include "hello.h"

   void foo (WORLD
   {
     printf ("Hello\n"WORLD;
   HELLO

CC-mode's job is fundamentally impossible.


        Stefan

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/


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

* Re: CC Mode - syntactical can of worms.
  2008-01-31 15:00 ` Stefan Monnier
@ 2008-01-31 22:05   ` Alan Mackenzie
  2008-01-31 22:05     ` Lennart Borgman (gmail)
  0 siblings, 1 reply; 5+ messages in thread
From: Alan Mackenzie @ 2008-01-31 22:05 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: bug-cc-mode, emacs-devel

Hi, Stefan!

On Thu, Jan 31, 2008 at 10:00:07AM -0500, Stefan Monnier wrote:
> > Hi, Emacs and CC Mode!
> > Following up from: [orium69@gmail.com: Bug in emacs 22.1.1 (cosmetic
> > bug)].

> > This bug was in C Mode, with the following source line:

> >     #warning for isn't a keyword here.

> > This fouls up the fontification, because the syntactic fontification
> > recognises the apostrophe as a string opener.  It can get pretty bad -
> > here is a syntactically correct C function:

> > 1   #warning for isn't a keyword here. (
> > 2   //#warning for isnt a keyword here.
> > 3   void foo (bar)
> > 4   {
> > 5   #error Brace yourself! }
> > 6       printf ("Hello, world!\n") ; /* the famous one liner! */
> > 7   }

> You forget other interesting constructs such as

>    #define HELLO }
>    #define WORLD )

>    void foo (WORLD
>    {
>      printf ("Hello\n"WORLD;
>    HELLO

Well, anybody who writes anything like this will get what she deserves.

> and of course, the more common ones like

>    #if foo
>    void foo (void)
>    {
>       size_t toto;
>    #else
>    int foo ()
>    {
>       unsigned toto;
>    #endif

Yes, this has been on my TODO list for a long time.  It shouldn't be that
difficult to fix.  I'm intending a fix in CC Mode 5.32.

> CC-mode's job is fundamentally impossible.

Hey, don't be like that!  ;-)  Yes, people can deliberately break it with
the likes of HELLO and WORLD, but the more modest aim of CC Mode, to deal
properly with anything reasonable, is doable.

I think I can fix this by setting a neutral syntax-table property on
anything obtrusive in a CPP construct.  Here's a first shot at doing
that:


;; Set syntax table properties on a CPP (logical) line, so that it becomes
;; "syntactically neutral".  This means that lines such as:
;;
;;     #warning for isn't a keyword.
;;                     ^
;; and
;;
;;     #define RBRACE }
;;                    ^
;; won't interact syntactically with the rest of the file.
(defun c-neutralize-CPP-line (beg end)
  (let (s)
    (while
	(progn
	  (setq s (parse-partial-sexp beg end -1))
	  (cond
	   ((< (nth 0 s) 0)		; found an unmated ),},]
	    (c-put-char-property (1- (point)) 'syntax-table '(1)) ; "punctuation".
	    t)
	   ((or (nth 3 s) (nth 4 s))	; In a string or comment.
	    (c-put-char-property (nth 8 s) 'syntax-table '(1))
	    t)
	   ((> (nth 0 s) 0)		; In a (,{,[
	    (c-put-char-property (nth 1 s) 'syntax-table '(1))
	    t)
	   (t nil))))))


>         Stefan

-- 
Alan Mackenzie (Nuremberg, Germany).




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

* Re: CC Mode - syntactical can of worms.
  2008-01-31 22:05   ` Alan Mackenzie
@ 2008-01-31 22:05     ` Lennart Borgman (gmail)
  0 siblings, 0 replies; 5+ messages in thread
From: Lennart Borgman (gmail) @ 2008-01-31 22:05 UTC (permalink / raw)
  To: Alan Mackenzie; +Cc: Stefan Monnier, emacs-devel

Alan Mackenzie wrote:
>> CC-mode's job is fundamentally impossible.
> 
> Hey, don't be like that!  ;-)  Yes, people can deliberately break it with
> the likes of HELLO and WORLD, but the more modest aim of CC Mode, to deal
> properly with anything reasonable, is doable.
> 
> I think I can fix this by setting a neutral syntax-table property on
> anything obtrusive in a CPP construct.  Here's a first shot at doing
> that:
> 
> 
> ;; Set syntax table properties on a CPP (logical) line, so that it becomes
> ;; "syntactically neutral".  This means that lines such as:
> ;;
> ;;     #warning for isn't a keyword.
> ;;                     ^
> ;; and
> ;;
> ;;     #define RBRACE }
> ;;                    ^
> ;; won't interact syntactically with the rest of the file.
> (defun c-neutralize-CPP-line (beg end)
>   (let (s)
>     (while
> 	(progn
> 	  (setq s (parse-partial-sexp beg end -1))
> 	  (cond
> 	   ((< (nth 0 s) 0)		; found an unmated ),},]
> 	    (c-put-char-property (1- (point)) 'syntax-table '(1)) ; "punctuation".
> 	    t)
> 	   ((or (nth 3 s) (nth 4 s))	; In a string or comment.
> 	    (c-put-char-property (nth 8 s) 'syntax-table '(1))
> 	    t)
> 	   ((> (nth 0 s) 0)		; In a (,{,[
> 	    (c-put-char-property (nth 1 s) 'syntax-table '(1))
> 	    t)
> 	   (t nil))))))
> 


This is like watching a game of chess - without understanding the moves, 
but knowning that the players do.




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

end of thread, other threads:[~2008-01-31 22:05 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-01-31 11:45 CC Mode - syntactical can of worms Alan Mackenzie
2008-01-31 12:53 ` Andreas Schwab
2008-01-31 15:00 ` Stefan Monnier
2008-01-31 22:05   ` Alan Mackenzie
2008-01-31 22:05     ` Lennart Borgman (gmail)

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.