QCXXHighlighter.cpp
Go to the documentation of this file.
1 // QCodeEditor
2 #include <QCXXHighlighter>
3 #include <QSyntaxStyle>
4 #include <QLanguage>
5 
6 // Qt
7 #include <QFile>
8 
9 
10 QCXXHighlighter::QCXXHighlighter(QTextDocument* document) :
11  QStyleSyntaxHighlighter(document),
12  m_highlightRules (),
13  m_includePattern (QRegularExpression(R"(^\s*#\s*include\s*([<"][^:?"<>\|]+[">]))")),
14  m_functionPattern (QRegularExpression(R"(\b([_a-zA-Z][_a-zA-Z0-9]*\s+)?((?:[_a-zA-Z][_a-zA-Z0-9]*\s*::\s*)*[_a-zA-Z][_a-zA-Z0-9]*)(?=\s*\())")),
15  m_defTypePattern (QRegularExpression(R"(\b([_a-zA-Z][_a-zA-Z0-9]*)\s+[_a-zA-Z][_a-zA-Z0-9]*\s*[;=])")),
16  m_commentStartPattern(QRegularExpression(R"(/\*)")),
17  m_commentEndPattern (QRegularExpression(R"(\*/)"))
18 {
19  Q_INIT_RESOURCE(qcodeeditor_resources);
20  QFile fl(":/languages/cpp.xml");
21 
22  if (!fl.open(QIODevice::ReadOnly))
23  {
24  return;
25  }
26 
27  QLanguage language(&fl);
28 
29  if (!language.isLoaded())
30  {
31  return;
32  }
33 
34  auto keys = language.keys();
35  for (auto&& key : keys)
36  {
37  auto names = language.names(key);
38  for (auto&& name : names)
39  {
40  m_highlightRules.append({
41  QRegularExpression(QString(R"(\b%1\b)").arg(name)),
42  key
43  });
44  }
45  }
46 
47  // Numbers
48  m_highlightRules.append({
49  QRegularExpression(R"((?<=\b|\s|^)(?i)(?:(?:(?:(?:(?:\d+(?:'\d+)*)?\.(?:\d+(?:'\d+)*)(?:e[+-]?(?:\d+(?:'\d+)*))?)|(?:(?:\d+(?:'\d+)*)\.(?:e[+-]?(?:\d+(?:'\d+)*))?)|(?:(?:\d+(?:'\d+)*)(?:e[+-]?(?:\d+(?:'\d+)*)))|(?:0x(?:[0-9a-f]+(?:'[0-9a-f]+)*)?\.(?:[0-9a-f]+(?:'[0-9a-f]+)*)(?:p[+-]?(?:\d+(?:'\d+)*)))|(?:0x(?:[0-9a-f]+(?:'[0-9a-f]+)*)\.?(?:p[+-]?(?:\d+(?:'\d+)*))))[lf]?)|(?:(?:(?:[1-9]\d*(?:'\d+)*)|(?:0[0-7]*(?:'[0-7]+)*)|(?:0x[0-9a-f]+(?:'[0-9a-f]+)*)|(?:0b[01]+(?:'[01]+)*))(?:u?l{0,2}|l{0,2}u?)))(?=\b|\s|$))"),
50  "Number"
51  });
52 
53  // Strings
54  m_highlightRules.append({
55  QRegularExpression(R"("[^\n"]*")"),
56  "String"
57  });
58 
59  // Define
60  m_highlightRules.append({
61  QRegularExpression(R"(#[a-zA-Z_]+)"),
62  "Preprocessor"
63  });
64 
65  // Single line
66  m_highlightRules.append({
67  QRegularExpression(R"(//[^\n]*)"),
68  "Comment"
69  });
70 }
71 
72 void QCXXHighlighter::highlightBlock(const QString& text)
73 {
74  // Checking for include
75  {
76  auto matchIterator = m_includePattern.globalMatch(text);
77 
78  while (matchIterator.hasNext())
79  {
80  auto match = matchIterator.next();
81 
82  setFormat(
83  match.capturedStart(),
84  match.capturedLength(),
85  syntaxStyle()->getFormat("Preprocessor")
86  );
87 
88  setFormat(
89  match.capturedStart(1),
90  match.capturedLength(1),
91  syntaxStyle()->getFormat("String")
92  );
93  }
94  }
95  // Checking for function
96  {
97  auto matchIterator = m_functionPattern.globalMatch(text);
98 
99  while (matchIterator.hasNext())
100  {
101  auto match = matchIterator.next();
102 
103  setFormat(
104  match.capturedStart(),
105  match.capturedLength(),
106  syntaxStyle()->getFormat("Type")
107  );
108 
109  setFormat(
110  match.capturedStart(2),
111  match.capturedLength(2),
112  syntaxStyle()->getFormat("Function")
113  );
114  }
115  }
116  {
117  auto matchIterator = m_defTypePattern.globalMatch(text);
118 
119  while (matchIterator.hasNext())
120  {
121  auto match = matchIterator.next();
122 
123  setFormat(
124  match.capturedStart(1),
125  match.capturedLength(1),
126  syntaxStyle()->getFormat("Type")
127  );
128  }
129  }
130 
131  for (auto& rule : m_highlightRules)
132  {
133  auto matchIterator = rule.pattern.globalMatch(text);
134 
135  while (matchIterator.hasNext())
136  {
137  auto match = matchIterator.next();
138 
139  setFormat(
140  match.capturedStart(),
141  match.capturedLength(),
142  syntaxStyle()->getFormat(rule.formatName)
143  );
144  }
145  }
146 
147  setCurrentBlockState(0);
148 
149  int startIndex = 0;
150  if (previousBlockState() != 1)
151  {
152  startIndex = text.indexOf(m_commentStartPattern);
153  }
154 
155  while (startIndex >= 0)
156  {
157  auto match = m_commentEndPattern.match(text, startIndex);
158 
159  int endIndex = match.capturedStart();
160  int commentLength = 0;
161 
162  if (endIndex == -1)
163  {
164  setCurrentBlockState(1);
165  commentLength = text.length() - startIndex;
166  }
167  else
168  {
169  commentLength = endIndex - startIndex + match.capturedLength();
170  }
171 
172  setFormat(
173  startIndex,
174  commentLength,
175  syntaxStyle()->getFormat("Comment")
176  );
177  startIndex = text.indexOf(m_commentStartPattern, startIndex + commentLength);
178  }
179 }
QCXXHighlighter::m_commentEndPattern
QRegularExpression m_commentEndPattern
Definition: QCXXHighlighter.hpp:40
QCXXHighlighter::m_functionPattern
QRegularExpression m_functionPattern
Definition: QCXXHighlighter.hpp:36
QCXXHighlighter::highlightBlock
void highlightBlock(const QString &text) override
Definition: QCXXHighlighter.cpp:72
QCXXHighlighter::m_highlightRules
QVector< QHighlightRule > m_highlightRules
Definition: QCXXHighlighter.hpp:33
arg
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
Definition: core.h:1875
QCXXHighlighter::m_defTypePattern
QRegularExpression m_defTypePattern
Definition: QCXXHighlighter.hpp:37
QStyleSyntaxHighlighter::syntaxStyle
QSyntaxStyle * syntaxStyle() const
Method for getting syntax style.
Definition: QStyleSyntaxHighlighter.cpp:16
QCXXHighlighter::QCXXHighlighter
QCXXHighlighter(QTextDocument *document=nullptr)
Constructor.
QCXXHighlighter::m_includePattern
QRegularExpression m_includePattern
Definition: QCXXHighlighter.hpp:35
QStyleSyntaxHighlighter
Class, that descrubes highlighter with syntax style.
Definition: QStyleSyntaxHighlighter.hpp:12
QLanguage
Definition: QLanguage.hpp:14
match
static const char * match(MatchState *ms, const char *s, const char *p)
Definition: lstrlib.c:570
QCXXHighlighter::m_commentStartPattern
QRegularExpression m_commentStartPattern
Definition: QCXXHighlighter.hpp:39
nlohmann::detail::parse_event_t::key
@ key
the parser read a key of a value in an object


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:45