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