syntax.py
Go to the documentation of this file.
1 # syntax.py
2 
3 import sys
4 
5 from python_qt_binding.QtCore import QRegExp, Qt
6 from python_qt_binding.QtGui import QColor, QTextCharFormat, QFont, QSyntaxHighlighter
7 
8 def format(color, style=''):
9  """Return a QTextCharFormat with the given attributes.
10  """
11  _color = QColor()
12  if type(color) == QColor:
13  _color = color
14  else:
15  _color.setNamedColor(color)
16 
17  _format = QTextCharFormat()
18  _format.setForeground(_color)
19  if 'bold' in style:
20  _format.setFontWeight(QFont.Bold)
21  if 'italic' in style:
22  _format.setFontItalic(True)
23 
24  return _format
25 
26 reformat = format
27 
28 
29 # Syntax styles that can be shared by all languages
30 BRIGHT_STYLES = {
31  'defaults': format('black'),
32  'keyword': format('blue'),
33  'operator': format('red'),
34  'brace': format('black'),
35  'defclass': format('black', 'bold'),
36  'string': format('darkmagenta'),
37  'string2': format('green'),
38  'comment': format('darkGray', 'italic'),
39  'self': format('black', 'italic'),
40  'numbers': format('brown'),
41 }
42 
43 DARK_STYLES = {
44  'defaults': format('white'),
45  'keyword': format('lightBlue', 'bold'),
46  'operator': format('orange'),
47  'brace': format('white'),
48  'defclass': format('lightGreen', 'bold'),
49  'string': format('darkmagenta'),
50  'string2': format('green'),
51  'comment': format('darkGray', 'italic'),
52  'self': format('white', 'italic'),
53  'numbers': format('brown'),
54 }
55 
56 
57 class PythonHighlighter (QSyntaxHighlighter):
58  """Syntax highlighter for the Python language.
59  """
60 
61  # defaults
62  defaults = [
63  ':', '\.'
64  ]
65 
66  # Python keywords
67  keywords = [
68  'and', 'assert', 'break', 'class', 'continue', 'def',
69  'del', 'elif', 'else', 'except', 'exec', 'finally',
70  'for', 'from', 'global', 'if', 'import', 'in',
71  'is', 'lambda', 'not', 'or', 'pass', 'print',
72  'raise', 'return', 'try', 'while', 'yield',
73  'None', 'True', 'False', 'str', 'as'
74  ]
75 
76  # Python operators
77  operators = [
78  '=',
79  # Comparison
80  '==', '!=', '<', '<=', '>', '>=',
81  # Arithmetic
82  '\+', '-', '\*', '/', '//', '\%', '\*\*',
83  # In-place
84  '\+=', '-=', '\*=', '/=', '\%=',
85  # Bitwise
86  '\^', '\|', '\&', '\~', '>>', '<<',
87  ]
88 
89  # Python braces
90  braces = [
91  '\{', '\}', '\(', '\)', '\[', '\]',
92  ]
93  def __init__(self, document, is_dark = False, default = None):
94  QSyntaxHighlighter.__init__(self, document)
95 
96  def contents_changed():
97  self.highlightBlock("document.toPlainText()")
98 
99  document.contentsChanged.connect(contents_changed)
100 
101  self.is_dark = is_dark
102  self.default = default
103 
104  global reformat
105 
106  if is_dark:
107  STYLES = DARK_STYLES
108  else:
109  STYLES = BRIGHT_STYLES
110 
111  if default != None:
112  STYLES['defaults'] = reformat(*default)
113 
114  # Multi-line strings (expression, flag, style)
115  # FIXME: The triple-quotes in these two lines will mess up the
116  # syntax highlighting from this point onward
117  self.tri_single = (QRegExp("'''"), 1, STYLES['string2'])
118  self.tri_double = (QRegExp('"""'), 2, STYLES['string2'])
119 
120  rules = []
121 
122  rules += [(r'%s' % w, 0, STYLES['defaults'])
123  for w in PythonHighlighter.defaults]
124  # Keyword, operator, and brace rules
125  rules += [(r'\b%s\b' % w, 0, STYLES['keyword'])
126  for w in PythonHighlighter.keywords]
127  rules += [(r'%s' % o, 0, STYLES['operator'])
128  for o in PythonHighlighter.operators]
129  rules += [(r'%s' % b, 0, STYLES['brace'])
130  for b in PythonHighlighter.braces]
131 
132  # All other rules
133  rules += [
134  # 'self'
135  (r'\bself\b', 0, STYLES['self']),
136 
137  # Double-quoted string, possibly containing escape sequences
138  (r'"[^"\\]*(\\.[^"\\]*)*"', 0, STYLES['string']),
139  # Single-quoted string, possibly containing escape sequences
140  (r"'[^'\\]*(\\.[^'\\]*)*'", 0, STYLES['string']),
141 
142  # 'def' followed by an identifier
143  (r'\bdef\b\s*(\w+)', 1, STYLES['defclass']),
144  # 'class' followed by an identifier
145  (r'\bclass\b\s*(\w+)', 1, STYLES['defclass']),
146 
147  # From '#' until a newline
148  (r'#[^\n]*', 0, STYLES['comment']),
149 
150  # Numeric literals
151  (r'\b[+-]?[0-9]+[lL]?\b', 0, STYLES['numbers']),
152  (r'\b[+-]?0[xX][0-9A-Fa-f]+[lL]?\b', 0, STYLES['numbers']),
153  (r'\b[+-]?[0-9]+(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\b', 0, STYLES['numbers']),
154  ]
155 
156  # Build a QRegExp for each pattern
157  self.rules = [(QRegExp(pat), index, fmt)
158  for (pat, index, fmt) in rules]
159 
160 
161 
162  def highlightBlock(self, text):
163  """Apply syntax highlighting to the given block of text.
164  """
165  # print "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&7"
166 
167  global reformat
168 
169  if self.is_dark:
170  STYLES = DARK_STYLES
171  else:
172  STYLES = BRIGHT_STYLES
173 
174  if self.default != None:
175  STYLES['defaults'] = reformat(*self.default)
176 
177  # Do other syntax formatting
178  self.setFormat( 0, len(text), STYLES['defaults'])
179 
180 
181  for expression, nth, format in self.rules:
182  index = expression.indexIn(text, 0)
183 
184  while index >= 0:
185  # We actually want the index of the nth match
186  index = expression.pos(nth)
187  # length = expression.cap(nth).length()
188  length = len(expression.cap(nth))
189  self.setFormat(index, length, format)
190  index = expression.indexIn(text, index + length)
191 
192  self.setCurrentBlockState(0)
193 
194  # Do multi-line strings
195  in_multiline = self.match_multiline(text, *self.tri_single)
196  if not in_multiline:
197  in_multiline = self.match_multiline(text, *self.tri_double)
198 
199 
200  def match_multiline(self, text, delimiter, in_state, style):
201  """Do highlighting of multi-line strings. ``delimiter`` should be a
202  ``QRegExp`` for triple-single-quotes or triple-double-quotes, and
203  ``in_state`` should be a unique integer to represent the corresponding
204  state changes when inside those strings. Returns True if we're still
205  inside a multi-line string when this function is finished.
206  """
207  # If inside triple-single quotes, start at 0
208  if self.previousBlockState() == in_state:
209  start = 0
210  add = 0
211  # Otherwise, look for the delimiter on this line
212  else:
213  start = delimiter.indexIn(text)
214  # Move past this match
215  add = delimiter.matchedLength()
216 
217  # As long as there's a delimiter match on this line...
218  while start >= 0:
219  # Look for the ending delimiter
220  end = delimiter.indexIn(text, start + add)
221  # Ending delimiter on this line?
222  if end >= add:
223  length = end - start + add + delimiter.matchedLength()
224  self.setCurrentBlockState(0)
225  # No; multi-line string
226  else:
227  self.setCurrentBlockState(in_state)
228  length = len(text) - start + add
229  # Apply formatting
230  self.setFormat(start, length, style)
231  # Look for the next match
232  start = delimiter.indexIn(text, start + length)
233 
234  # Return True if still inside a multi-line string, False otherwise
235  if self.currentBlockState() == in_state:
236  return True
237  else:
238  return False
def match_multiline(self, text, delimiter, in_state, style)
Definition: syntax.py:200
def __init__(self, document, is_dark=False, default=None)
Definition: syntax.py:93
def format(color, style='')
Definition: syntax.py:8


rqt_dyn_tune
Author(s):
autogenerated on Mon Jun 10 2019 14:52:08