blob: 8ef5bb89d0ddd21689ce166a6750b77e913bf970 [file] [log] [blame]
Brett Cannona4fe1822006-02-25 14:52:53 +00001import keyword
2import exceptions
3import __builtin__
4from string import Template
5
6comment_header = """" Auto-generated Vim syntax file for Python
7"
8" To use: copy or symlink to ~/.vim/syntax/python.vim"""
9
10statement_header = """
11if exists("b:current_syntax")
12 finish
13endif"""
14
15statement_footer = '''
16" Uncomment the 'minlines' statement line and comment out the 'maxlines'
17" statement line; changes behaviour to look at least 2000 lines previously for
18" syntax matches instead of at most 200 lines
19syn sync match pythonSync grouphere NONE "):$"
20syn sync maxlines=200
21"syn sync minlines=2000
22
23let b:current_syntax = "python"'''
24
25looping = ('for', 'while')
26conditionals = ('if', 'elif', 'else')
27boolean_ops = ('and', 'in', 'is', 'not', 'or')
28import_stmts = ('import', 'from')
29object_defs = ('def', 'class')
30
31exception_names = frozenset(exc for exc in dir(exceptions)
32 if not exc.startswith('__'))
33
34# Need to include functions that start with '__' (e.g., __import__), but
35# nothing that comes with modules (e.g., __name__), so just exclude anything in
36# the 'exceptions' module since we want to ignore exceptions *and* what any
37# module would have
38builtin_names = frozenset(builtin for builtin in dir(__builtin__)
39 if builtin not in dir(exceptions))
40
41escapes = (r'+\\[abfnrtv\'"\\]+', r'"\\\o\{1,3}"', r'"\\x\x\{2}"',
42 r'"\(\\u\x\{4}\|\\U\x\{8}\)"', r'"\\$"')
43
44todos = ("TODO", "FIXME", "XXX")
45
46# XXX codify?
47numbers = (r'"\<0x\x\+[Ll]\=\>"', r'"\<\d\+[LljJ]\=\>"',
48 '"\.\d\+\([eE][+-]\=\d\+\)\=[jJ]\=\>"',
49 '"\<\d\+\.\([eE][+-]\=\d\+\)\=[jJ]\=\>"',
50 '"\<\d\+\.\d\+\([eE][+-]\=\d\+\)\=[jJ]\=\>"')
51
52contained = lambda x: "%s contained" % x
53
54def str_regexes():
55 """Generator to yield various combinations of strings regexes"""
56 regex_template = Template('matchgroup=Normal ' +
57 'start=+[uU]\=${raw}${sep}+ ' +
58 'end=+${sep}+ ' +
59 '${skip} ' +
60 '${contains}')
61 skip_regex = Template(r'skip=+\\\\\|\\${sep}+')
62 for raw in ('', '[rR]'):
63 for separator in ("'", '"', '"""', "'''"):
64 if len(separator) == 1:
65 skip = skip_regex.substitute(sep=separator)
66 else:
67 skip = ''
Brett Cannonacde7342006-03-01 04:28:00 +000068 contains = 'contains=pythonEscape' if not raw else ''
Brett Cannona4fe1822006-02-25 14:52:53 +000069 yield regex_template.substitute(raw=raw, sep=separator, skip=skip,
70 contains = contains)
71
72space_errors = (r'excludenl "\S\s\+$"ms=s+1', r'" \+\t"', r'"\t\+ "')
73
74statements = (
75 ('',
76 # XXX Might need to change pythonStatement since have
77 # specific Repeat, Conditional, Operator, etc. for 'while',
78 # etc.
79 [("Statement", "pythonStatement", "keyword",
80 (kw for kw in keyword.kwlist
81 if kw not in (looping + conditionals + boolean_ops +
82 import_stmts + object_defs))
83 ),
84 ("Statement", "pythonStatement", "keyword",
85 (' '.join(object_defs) +
86 ' nextgroup=pythonFunction skipwhite')),
87 ("Function","pythonFunction", "match",
88 contained('"[a-zA-Z_][a-zA-Z0-9_]*"')),
89 ("Repeat", "pythonRepeat", "keyword", looping),
90 ("Conditional", "pythonConditional", "keyword",
91 conditionals),
92 ("Operator", "pythonOperator", "keyword", boolean_ops),
93 ("PreCondit", "pythonPreCondit", "keyword", import_stmts),
94 ("Comment", "pythonComment", "match",
95 '"#.*$" contains=pythonTodo'),
96 ("Todo", "pythonTodo", "keyword",
97 contained(' '.join(todos))),
98 ("String", "pythonString", "region", str_regexes()),
99 ("Special", "pythonEscape", "match",
100 (contained(esc) for esc in escapes
101 if not '$' in esc)),
102 ("Special", "pythonEscape", "match", r'"\\$"'),
103 ]
104 ),
105 ("python_highlight_numbers",
106 [("Number", "pythonNumber", "match", numbers)]
107 ),
108 ("python_highlight_builtins",
109 [("Function", "pythonBuiltin", "keyword", builtin_names)]
110 ),
111 ("python_highlight_exceptions",
112 [("Exception", "pythonException", "keyword",
113 exception_names)]
114 ),
115 ("python_highlight_space_errors",
116 [("Error", "pythonSpaceError", "match",
117 ("display " + err for err in space_errors))]
118 )
119 )
120
121def syn_prefix(type_, kind):
122 return 'syn %s %s ' % (type_, kind)
123
124def fill_stmt(iterable, fill_len):
125 """Yield a string that fills at most fill_len characters with strings
126 returned by 'iterable' and separated by a space"""
127 # Deal with trailing char to handle ' '.join() calculation
Tim Petersd6e7e732006-02-26 04:21:50 +0000128 fill_len += 1
Brett Cannona4fe1822006-02-25 14:52:53 +0000129 overflow = None
130 it = iter(iterable)
131 while True:
132 buffer_ = []
133 total_len = 0
134 if overflow:
135 buffer_.append(overflow)
136 total_len += len(overflow) + 1
137 overflow = None
138 while total_len < fill_len:
139 try:
140 new_item = it.next()
141 buffer_.append(new_item)
142 total_len += len(new_item) + 1
143 except StopIteration:
144 if buffer_:
145 break
146 if not buffer_ and overflow:
147 yield buffer_
148 return
149 else:
150 return
151 if total_len > fill_len:
152 overflow = buffer_.pop()
153 total_len -= len(overflow) - 1
154 ret = ' '.join(buffer_)
155 assert len(ret) <= fill_len
156 yield ret
157
158FILL = 80
159
160def main(file_path):
161 FILE = open(file_path, 'w')
162 try:
163 # Comment for file
164 print>>FILE, comment_header
165 print>>FILE, ''
166 # Statements at start of file
167 print>>FILE, statement_header
168 print>>FILE, ''
169 # Generate case for python_highlight_all
170 print>>FILE, 'if exists("python_highlight_all")'
171 for statement_var, statement_parts in statements:
172 if statement_var:
173 print>>FILE, ' let %s = 1' % statement_var
174 else:
175 print>>FILE, 'endif'
176 print>>FILE, ''
177 # Generate Python groups
178 for statement_var, statement_parts in statements:
179 if statement_var:
180 print>>FILE, 'if exists("%s")' % statement_var
181 indent = ' '
182 else:
183 indent = ''
184 for colour_group, group, type_, arguments in statement_parts:
185 if not isinstance(arguments, basestring):
186 prefix = syn_prefix(type_, group)
187 if type_ == 'keyword':
188 stmt_iter = fill_stmt(arguments,
189 FILL - len(prefix) - len(indent))
190 try:
191 while True:
192 print>>FILE, indent + prefix + stmt_iter.next()
193 except StopIteration:
194 print>>FILE, ''
195 else:
196 for argument in arguments:
197 print>>FILE, indent + prefix + argument
198 else:
199 print>>FILE, ''
200
201 else:
202 print>>FILE, indent + syn_prefix(type_, group) + arguments
203 print>>FILE, ''
204 else:
205 if statement_var:
206 print>>FILE, 'endif'
207 print>>FILE, ''
208 print>>FILE, ''
209 # Associating Python group with Vim colour group
210 for statement_var, statement_parts in statements:
211 if statement_var:
212 print>>FILE, ' if exists("%s")' % statement_var
213 indent = ' '
214 else:
215 indent = ' '
216 for colour_group, group, type_, arguments in statement_parts:
217 print>>FILE, (indent + "hi def link %s %s" %
218 (group, colour_group))
219 else:
220 if statement_var:
221 print>>FILE, ' endif'
222 print>>FILE, ''
223 # Statements at the end of the file
224 print>>FILE, statement_footer
225 finally:
226 FILE.close()
227
228if __name__ == '__main__':
229 main("python.vim")