xml_syntax_highlighter.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2022, Bielefeld University, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Bielefeld University nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Robert Haschke */
36 
37 #include "xml_syntax_highlighter.h"
38 #include <assert.h>
39 
40 XmlSyntaxHighlighter::XmlSyntaxHighlighter(QTextDocument* parent) : QSyntaxHighlighter(parent)
41 {
42 }
43 
44 void XmlSyntaxHighlighter::addTag(const QString& tag, const QTextCharFormat& format, const QString& parent)
45 {
46  const QString start_pattern("<%1.*?/?>");
47  Rule rule;
48  rule.start = QRegularExpression(start_pattern.arg(tag));
49  rule.end = QRegularExpression(QString("</%1>|<%1[^>]*?/>").arg(tag));
50  rule.format = format;
51  if (!parent.isEmpty())
52  {
53  QString parent_start = start_pattern.arg(parent);
54  rule.parent = std::find_if(rules.begin(), rules.end(), [&](const std::pair<int, Rule>& rule) {
55  return rule.second.start.pattern() == parent_start;
56  });
57  }
58  else
59  rule.parent = rules.end();
60 
61  rules.insert(std::make_pair(rules.size(), rule));
62 }
63 
64 XmlSyntaxHighlighter::Rules::const_iterator
65 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
66 XmlSyntaxHighlighter::highlight(Rules::const_iterator active, QStringView text, int start, bool search_end, int& end)
67 #else
68 XmlSyntaxHighlighter::highlight(Rules::const_iterator active, QStringRef text, int start, bool search_end, int& end)
69 #endif
70 {
71  int offset = end; // when passed, end indicates the end of the opening expression
72  auto next = active; // return value: active rule at end of text
73 
74  if (search_end) // find end of active rule
75  {
76  auto match = active->second.end.match(text);
77  // when returned, end indicates the end of the closing expression
78  end = match.hasMatch() ? match.capturedEnd() : text.size();
79  setFormat(start, end, active->second.format);
80  if (match.hasMatch())
81  {
82  text = text.left(match.capturedStart()); // drop text after (and including) closing expression
83  next = active->second.parent;
84  }
85  }
86  text = text.mid(offset); // skip opening expression
87  start += offset; // adjust start by skipped offset
88  if (text.isEmpty())
89  return next; // early return
90 
91  // highlight remaining text using active's children's rules
92  for (auto it = rules.begin(); it != rules.end(); ++it)
93  {
94  const auto& rule = it->second;
95  if (rule.parent != active)
96  continue; // skip wrong rules
97 
98  offset = 0; // (re)start at beginning of (clipped) text
99  while (true) // process all matches of rule
100  {
101  auto match = rule.start.match(text, offset);
102  if (!match.hasMatch())
103  break;
104 
105  offset = match.capturedEnd() - match.capturedStart(); // mark end of opening expression in passed text
106  auto result = highlight(it, text.mid(match.capturedStart()), start + match.capturedStart(), true, offset);
107  // returned offset is w.r.t. beginning of _passed_ text: add passed start offset to yield offset w.r.t. text
108  offset += match.capturedStart();
109  if (result == it) // text is ending with this rule
110  {
111  assert(next == active || next == active->second.parent);
112  assert(offset == text.size()); // end should mark the end of the text
113  next = result; // remember return value: active rule at end of text
114  break;
115  }
116  }
117  }
118 
119  return next;
120 }
121 
122 void XmlSyntaxHighlighter::highlightBlock(const QString& text)
123 {
124  Rules::const_iterator active = previousBlockState() < 0 ? rules.end() : rules.find(previousBlockState());
125  int unused = 0;
126 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
127  active = highlight(active, QStringView(text), 0, active != rules.cend(), unused);
128 #else
129  active = highlight(active, QStringRef(&text, 0, text.size()), 0, active != rules.cend(), unused);
130 #endif
131  setCurrentBlockState(active != rules.cend() ? active->first : -1);
132 }
XmlSyntaxHighlighter::Rule::parent
std::map< int, Rule >::const_iterator parent
Definition: xml_syntax_highlighter.h:91
XmlSyntaxHighlighter::addTag
void addTag(const QString &tag, const QTextCharFormat &format, const QString &parent=QString())
Definition: xml_syntax_highlighter.cpp:44
XmlSyntaxHighlighter::Rule::format
QTextCharFormat format
Definition: xml_syntax_highlighter.h:90
next
EndPoint * next[3]
XmlSyntaxHighlighter::Rule::start
QRegularExpression start
Definition: xml_syntax_highlighter.h:88
xml_syntax_highlighter.h
text
text
XmlSyntaxHighlighter::Rule::end
QRegularExpression end
Definition: xml_syntax_highlighter.h:89
XmlSyntaxHighlighter::rules
Rules rules
Definition: xml_syntax_highlighter.h:94
start
ROSCPP_DECL void start()
XmlSyntaxHighlighter::Rule
Definition: xml_syntax_highlighter.h:86
XmlSyntaxHighlighter::highlight
Rules::const_iterator highlight(Rules::const_iterator active, QStringView text, int start, bool search_end, int &end)
Definition: xml_syntax_highlighter.cpp:66
assert.h
XmlSyntaxHighlighter::highlightBlock
void highlightBlock(const QString &text) override
Definition: xml_syntax_highlighter.cpp:122
XmlSyntaxHighlighter::XmlSyntaxHighlighter
XmlSyntaxHighlighter(QTextDocument *parent=nullptr)
Definition: xml_syntax_highlighter.cpp:40


moveit_setup_assistant
Author(s): Dave Coleman
autogenerated on Sat May 3 2025 02:28:05