ยปCore Development>Code coverage>Misc/Vim/vim_syntax.py

Python code coverage for Misc/Vim/vim_syntax.py

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