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