[prev in list] [next in list] [prev in thread] [next in thread] 

List:       vim-mac
Subject:    Objective-C++ syntax files
From:       Anthony Hodsdon <ahodsdon () fastmail ! fm>
Date:       2003-01-29 7:27:06
[Download RAW message or body]

Hi. I'd like to solicit some advice from you ObjC programmers out there.

I've been doing a little work to get syntax highlighting working for 
ObjC++ files (ObjC++ is Apple's way of letting C++ programmers use 
their ObjC API). For the most part, this is pretty straightforward; 
however, there are some nitty-gritty details that require a little care.

The main issue is that some keywords in ObjC++ are context-sensitive. 
For instance, "class" is obviously a C++ keyword, but it can (and is) 
also used as a method name in ObjC classes (for instance, NSObject uses 
it as such). Technically, "class" shouldn't be highlighted when it is 
used as a method name.

The following is my attempt at a solution (with some much appreciated 
guidance from Bram):

objcpp.vim:
---------------

" Vim syntax file
" Language:     ObjC++
" Maintainer:   ?
" Last change:  2003 January 28

" For version 5.x: Clear all syntax items
" For version 6.x: Quit when a syntax file was already loaded
if version < 600
     syntax clear
elseif exists("b:current_syntax")
     finish
endif

" Read in C++ and ObjC syntax files
if version < 600
     so <sfile>:p:h/cpp.vim
     so <sflie>:p:h/objc.vim
else
     runtime! syntax/cpp.vim
     unlet b:current_syntax
     runtime! syntax/objc.vim
     unlet b:current_syntax
endif

" If version < 508, we can't override old highlights,
" so we give up
if version >= 508
     syn region objCFunc start="^[\t\s]*[-+]"  end="$" 
contains=ALLBUT,cErrInParen,cErrInBracket
     syn keyword objCppNonStructure      class contained
   endif

let b:current_syntax = "objcpp"

<EOF>



The effect of this is to source both the cpp.vim and objc.vim syntax 
files (NOTE: I take care in the ObjC syntax file to ensure that c.vim 
only gets sourced once) and then do some tweaking to clear up the 
"class" keyword problem.

I've invented the term objCppNonStructure to indicate that "class" is 
in a context where it is not actually a keyword. I also declare the 
region objCFunc (the definition or declaration of a method) as just 
such a context.

This actually works amazingly well (as far as I can tell). Aside from 
just method declarations, we also get stuff like

	[objc_class]
	@SEL("class")

for free (the first because it is contained in the bracketed region, 
and the latter because it's contained in the quoted region).

There are some other ObjC++ keywords that are also case-sensitive 
("new", "delete", and "template", for instance), but they don't seem as 
much of a problem (anyone using "template" in an objC class is asking 
for trouble).

Does this look like a decent syntax file? Can anyone see any special 
cases that should be dealt with? I'd appreciate any input you have.

Thanks,

--Anthony Hodsdon

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic