1 " Description: Omni completion tokenizer
2 " Maintainer: Vissale NEANG
3 " Last Change: 26 sept. 2007
4 " TODO: Generic behaviour for Tokenize()
7 let s:cppKeyword = ['asm', 'auto', 'bool', 'break', 'case', 'catch', 'char', 'class', 'const', 'const_cast', 'continue', 'default', 'delete', 'do', 'double', 'dynamic_cast', 'else', 'enum', 'explicit', 'export', 'extern', 'false', 'float', 'for', 'friend', 'goto', 'if', 'inline', 'int', 'long', 'mutable', 'namespace', 'new', 'operator', 'private', 'protected', 'public', 'register', 'reinterpret_cast', 'return', 'short', 'signed', 'sizeof', 'static', 'static_cast', 'struct', 'switch', 'template', 'this', 'throw', 'true', 'try', 'typedef', 'typeid', 'typename', 'union', 'unsigned', 'using', 'virtual', 'void', 'volatile', 'wchar_t', 'while', 'and', 'and_eq', 'bitand', 'bitor', 'compl', 'not', 'not_eq', 'or', 'or_eq', 'xor', 'xor_eq']
9 let s:reCppKeyword = '\C\<'.join(s:cppKeyword, '\>\|\<').'\>'
11 " The order of items in this list is very important because we use this list to build a regular
12 " expression (see below) for tokenization
13 let s:cppOperatorPunctuator = ['->*', '->', '--', '-=', '-', '!=', '!', '##', '#', '%:%:', '%=', '%>', '%:', '%', '&&', '&=', '&', '(', ')', '*=', '*', ',', '...', '.*', '.', '/=', '/', '::', ':>', ':', ';', '?', '[', ']', '^=', '^', '{', '||', '|=', '|', '}', '~', '++', '+=', '+', '<<=', '<%', '<:', '<<', '<=', '<', '==', '=', '>>=', '>>', '>=', '>']
15 " We build the regexp for the tokenizer
16 let s:reCComment = '\/\*\|\*\/'
17 let s:reCppComment = '\/\/'
18 let s:reComment = s:reCComment.'\|'.s:reCppComment
19 let s:reCppOperatorOrPunctuator = escape(join(s:cppOperatorPunctuator, '\|'), '*./^~[]')
23 " a token is dictionary where keys are:
24 " - kind = cppKeyword|cppWord|cppOperatorPunctuator|unknown|cComment|cppComment|cppDigit
25 " - value = 'something'
26 " Note: a cppWord is any word that is not a cpp keyword
27 function! omni#cpp#tokenizer#Tokenize(szCode)
30 " The regexp to find a token, a token is a keyword, word or
31 " c++ operator or punctuator. To work properly we have to put
32 " spaces and tabs to our regexp.
33 let reTokenSearch = '\(\w\+\)\|\s\+\|'.s:reComment.'\|'.s:reCppOperatorOrPunctuator
34 " eg: 'using namespace std;'
38 let endPos = matchend(a:szCode, reTokenSearch)
39 let len = endPos-startPos
41 " eg: 'using namespace std;'
45 " We also remove space and tabs
46 let token = substitute(strpart(a:szCode, startPos, len), '\s', '', 'g')
48 " eg: 'using namespace std;'
52 let endPos = matchend(a:szCode, reTokenSearch, startPos)
53 let len = endPos-startPos
55 " It the token is empty we continue
61 let resultToken = {'kind' : 'unknown', 'value' : token}
66 let resultToken.kind = 'cppDigit'
67 elseif token=~'^\w\+$'
69 let resultToken.kind = 'cppWord'
71 " But maybe it's a c++ keyword
72 if match(token, s:reCppKeyword)>=0
73 let resultToken.kind = 'cppKeyword'
76 if match(token, s:reComment)>=0
77 if index(['/*','*/'],token)>=0
78 let resultToken.kind = 'cComment'
80 let resultToken.kind = 'cppComment'
84 let resultToken.kind = 'cppOperatorPunctuator'
88 " We have our token, let's add it to the result list
89 call extend(result, [resultToken])