Debian Codegraph @

modules/pmg_qt/syntax/python.py

raw
1 import re
2 import keyword
3
4 from . import *
5
6 STYLES = {
7 'keyword': textformat('darkOrange'),
8 'builtins': textformat('green'),
9 'decorator': textformat('darkBlue'),
10 'defclass': textformat('darkGreen'),
11 'string': textformat('magenta'),
12 'comment': textformat('blue'),
13 'self': textformat('black', 'bold'),
14 'numbers': textformat('brown'),
15 }
16
17
18 # enum for block state
19 class QUOTESTATE:
20 NONE = 0
21 DOUBLE = 1
22 SINGLE = 2
23 TRIPLEOFFSET = 2
24
25
26 # quotes (QUOTESTATE indices)
27 QUOTES = [None, '"', "'", '"""', "'''"]
28
29
30 def find_first(subs, text, start):
31 '''Index of first occurence of `subs` in `text`. Return None if not found.'''
32 best = None
33 for sub in subs:
34 index = text.find(sub, start, best)
35 if index != -1:
36 best = index
37 return best
38
39
40 python_rules = [
41 (re.compile(pat), index, fmt)
42 for (pat, index, fmt) in [
43 # decorators
44 (r'^\s*@[\w\.]+', 0, STYLES['decorator']),
45
46 # built in names
47 (r'(?<!\.)\b(?:' + '|'.join(__builtins__) + r')\b', 0, STYLES['builtins']),
48
49 # 'self'
50 (r'\bself\b', 0, STYLES['self']),
51
52 # 'def' or 'class' followed by an identifier
53 (r'\b(?:def|class)\s+(\w+)', 1, STYLES['defclass']),
54
55 # Numeric literals
56 (r'\b[+-]?(?:[0-9]+|0[xX][0-9A-Fa-f]+|0[bB][01]+)[lL]?\b', 0, STYLES['numbers']),
57 (r'\b[+-]?[0-9]+(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\b', 0, STYLES['numbers']),
58
59 # keywords
60 (r'\b(?:' + '|'.join(keyword.kwlist) + r')\b', 0, STYLES['keyword']),
61 ]
62 ]
63
64 re_end_of_line = re.compile(r'$', re.M)
65
66
67 def highlightPython(self, state, text, offset=0, end=None):
68 '''
69 @type self: QSyntaxHighlighter
70 @type state: int
71 @type text: str
72 @type offset: int
73 @type end: int or None
74
75 @rtype: int
76 @return current block state
77 '''
78 # support highlighting substrings
79 text = text[offset:end]
80
81 # regular expressions
82 for expression, nth, format in python_rules:
83 for match in expression.finditer(text):
84 start, index = match.span(nth)
85 self.setFormat(start + offset, index - start, format)
86
87 start = 0
88 index = 0
89 L = len(text)
90
91 while index < L:
92 if state == QUOTESTATE.NONE:
93 # find opening quote
94 start = find_first(('"', "'", '#'), text, index)
95
96 if start is None:
97 # nothing found
98 break
99
100 index = start + 1
101
102 if text[start] == '#':
103 # found comment
104 index += re_end_of_line.search(text[index:]).end()
105 self.setFormat(start + offset, index - start,
106 STYLES['comment'])
107 continue
108
109 if text[start] == '"':
110 # found double quote
111 state = QUOTESTATE.DOUBLE
112 else:
113 # found single quote
114 state = QUOTESTATE.SINGLE
115
116 # check for triple quote
117 if text[index:index + 2] == QUOTES[state] * 2:
118 state += QUOTESTATE.TRIPLEOFFSET
119 index += 2
120
121 quote = QUOTES[state]
122 quotelen = len(quote)
123
124 # find closing quote
125 while index < L:
126 if text[index] == '\\':
127 index += 1
128 elif text[index:index + quotelen] == quote:
129 index += quotelen
130 state = QUOTESTATE.NONE
131 break
132 index += 1
133
134 # raw/unicode/bytes prefix
135 if start > 0 and text[start - 1].lower() in 'rub':
136 start -= 1
137
138 self.setFormat(start + offset, index - start, STYLES['string'])
139
140 return state
141
142
143 class Highlighter(QtGui.QSyntaxHighlighter):
144 def highlightBlock(self, text):
145 state = max(0, self.previousBlockState())
146 state = highlightPython(self, state, text)
147 self.setCurrentBlockState(state)