1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
/**************************************************************************
* *
* Regina - A Normal Surface Theory Calculator *
* Qt User Interface *
* *
* Copyright (c) 1999-2025, Ben Burton *
* For further details contact Ben Burton (bab@debian.org). *
* *
* This file is modified from the KDE syntax-highlighting framework, *
* which is copyright (C) 2016 Volker Krause <vkrause@kde.org> *
* and is distributed under the GNU Library General Public License as *
* published by the Free Software Foundation; either version 2 of the *
* License, or (at your option) any later version. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 2 of the *
* License, or (at your option) any later version. *
* *
* As an exception, when this program is distributed through (i) the *
* App Store by Apple Inc.; (ii) the Mac App Store by Apple Inc.; or *
* (iii) Google Play by Google Inc., then that store may impose any *
* digital rights management, device limits and/or redistribution *
* restrictions that are required by its terms of service. *
* *
* This program is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
**************************************************************************/
#include "syntax/rule.h"
#include "syntax/definition_p.h"
#include <cassert>
#include <iostream>
#include "utilities/stringutils.h"
#include "file/xml/xmlreader.h"
namespace regina::syntax {
Rule::Rule() :
m_column(-1),
m_firstNonSpace(false),
m_lookAhead(false)
{
}
Rule::~Rule()
{
}
Definition Rule::definition() const
{
return m_def.definition();
}
void Rule::setDefinition(const Definition &def)
{
m_def = def;
}
const std::string& Rule::attribute() const
{
return m_attribute;
}
const ContextSwitch& Rule::context() const
{
return m_context;
}
bool Rule::isLookAhead() const
{
return m_lookAhead;
}
bool Rule::firstNonSpace() const
{
return m_firstNonSpace;
}
int Rule::requiredColumn() const
{
return m_column;
}
bool Rule::load(xmlTextReaderPtr reader)
{
m_attribute = regina::xml::xmlString(xmlTextReaderGetAttribute(reader, (const xmlChar*)"attribute"));
if (regina::xml::xmlString(xmlTextReaderName(reader)) != "IncludeRules") // IncludeRules uses this with a different semantic
m_context.parse(regina::xml::xmlString(xmlTextReaderGetAttribute(reader, (const xmlChar*)"context")));
regina::valueOf(regina::xml::xmlString(xmlTextReaderGetAttribute(reader, (const xmlChar*)"firstNonSpace")), m_firstNonSpace);
regina::valueOf(regina::xml::xmlString(xmlTextReaderGetAttribute(reader, (const xmlChar*)"lookAhead")), m_lookAhead);
if (! regina::valueOf(regina::xml::xmlString(xmlTextReaderGetAttribute(reader, (const xmlChar*)"column")), m_column))
m_column = -1;
auto result = doLoad(reader);
if (m_lookAhead && m_context.isStay())
result = false;
if (xmlTextReaderIsEmptyElement(reader))
return result;
if (xmlTextReaderRead(reader) != 1)
return result;
while (true) {
switch (xmlTextReaderNodeType(reader)) {
case 1 /* start element */:
{
auto rule = Rule::create(regina::xml::xmlString(xmlTextReaderName(reader)));
if (rule) {
rule->setDefinition(m_def.definition());
if (rule->load(reader)) {
m_subRules.push_back(rule);
if (xmlTextReaderRead(reader) != 1)
return result;
}
} else {
// Skip current element.
if (xmlTextReaderNext(reader) != 1)
return result;
}
break;
}
case 15 /* end element */:
return result;
default:
if (xmlTextReaderRead(reader) != 1)
return result;
break;
}
}
return result;
}
void Rule::resolveContext()
{
m_context.resolve(m_def.definition());
for (const auto &rule : m_subRules)
rule->resolveContext();
}
bool Rule::doLoad(xmlTextReaderPtr)
{
return true;
}
MatchResult Rule::match(Matcher& m, int offset)
{
assert(! m.textEmpty());
const auto result = doMatch(m, offset);
// Hmm. Currently matches use int but the text size can be size_t.
// Having said this, Regina only uses this code with Qt, which current
// stores most things using ints anyway. So we acknowledge the problem
// but cast things silently down to int for now.
if (result.offset() == offset ||
result.offset() == static_cast<int>(m.textSize()))
return result;
for (const auto &subRule : m_subRules) {
const auto subResult = subRule->match(m, result.offset());
if (subResult.offset() > result.offset())
return MatchResult(subResult.offset());
}
return result;
}
Rule::Ptr Rule::create(const std::string& name)
{
Rule *rule = nullptr;
if (name == "AnyChar")
rule = new AnyChar;
else if (name == "DetectChar")
rule = new DetectChar;
else if (name == "Detect2Chars")
rule = new Detect2Char;
else if (name == "DetectIdentifier")
rule = new DetectIdentifier;
else if (name == "DetectSpaces")
rule = new DetectSpaces;
else if (name == "Float")
rule = new Float;
else if (name == "Int")
rule = new Int;
else if (name == "HlCChar")
rule = new HlCChar;
else if (name == "HlCHex")
rule = new HlCHex;
else if (name == "HlCOct")
rule = new HlCOct;
else if (name == "HlCStringChar")
rule = new HlCStringChar;
else if (name == "IncludeRules")
rule = new IncludeRules;
else if (name == "keyword")
rule = new KeywordListRule;
else if (name == "LineContinue")
rule = new LineContinue;
else if (name == "RangeDetect")
rule = new RangeDetect;
else if (name == "RegExpr")
rule = new RegExpr;
else if (name == "StringDetect")
rule = new StringDetect;
else if (name == "WordDetect")
rule = new WordDetect;
else
std::cerr << "Unknown rule type: " << name << std::endl;
return Ptr(rule);
}
bool AnyChar::doLoad(xmlTextReaderPtr reader)
{
m_chars = regina::xml::xmlString(xmlTextReaderGetAttribute(reader, (const xmlChar*)"String"));
if (m_chars.length() == 1)
std::cerr << "AnyChar rule with just one char: use DetectChar instead." << std::endl;
return !m_chars.empty();
}
bool DetectChar::doLoad(xmlTextReaderPtr reader)
{
const auto s = regina::xml::xmlString(xmlTextReaderGetAttribute(reader, (const xmlChar*)"char"));
if (s.empty())
return false;
m_char = s.front();
return true;
}
bool Detect2Char::doLoad(xmlTextReaderPtr reader)
{
const auto s1 = regina::xml::xmlString(xmlTextReaderGetAttribute(reader, (const xmlChar*)"char"));
const auto s2 = regina::xml::xmlString(xmlTextReaderGetAttribute(reader, (const xmlChar*)"char1"));
if (s1.empty() || s2.empty())
return false;
m_char1 = s1.front();
m_char2 = s2.front();
return true;
}
const std::string& IncludeRules::contextName() const
{
return m_contextName;
}
const std::string& IncludeRules::definitionName() const
{
return m_defName;
}
bool IncludeRules::includeAttribute() const
{
return m_includeAttribute;
}
bool IncludeRules::doLoad(xmlTextReaderPtr reader)
{
const auto s = regina::xml::xmlString(xmlTextReaderGetAttribute(reader, (const xmlChar*)"context"));
if (s.empty())
return false;
auto pos = s.find("##");
if (pos != std::string::npos) {
m_contextName = s.substr(0, pos);
m_defName = s.substr(pos + 2);
} else {
m_contextName = s;
}
regina::valueOf(regina::xml::xmlString(xmlTextReaderGetAttribute(reader, (const xmlChar*)"includeAttrib")), m_includeAttribute);
return !m_contextName.empty() || !m_defName.empty();
}
bool KeywordListRule::doLoad(xmlTextReaderPtr reader)
{
m_listName = regina::xml::xmlString(xmlTextReaderGetAttribute(reader, (const xmlChar*)"String"));
auto attr = xmlTextReaderGetAttribute(reader, (const xmlChar*)"insensitive");
if (attr) {
bool cs;
regina::valueOf(regina::xml::xmlString(attr), cs);
m_caseSensitivityOverride = ! cs;
m_hasCaseSensitivityOverride = true;
} else {
m_hasCaseSensitivityOverride = false;
}
return !m_listName.empty();
}
const KeywordList* KeywordListRule::keywordList() {
if (! m_keywordList) {
const auto def = definition();
assert(def.isValid());
auto defData = DefinitionData::get(def);
m_keywordList = &defData->keywordList(m_listName);
}
return m_keywordList;
}
bool LineContinue::doLoad(xmlTextReaderPtr reader)
{
const auto s = regina::xml::xmlString(xmlTextReaderGetAttribute(reader, (const xmlChar*)"char"));
if (s.empty())
m_char = '\\';
else
m_char = s.front();
return true;
}
bool RangeDetect::doLoad(xmlTextReaderPtr reader)
{
const auto s1 = regina::xml::xmlString(xmlTextReaderGetAttribute(reader, (const xmlChar*)"char"));
const auto s2 = regina::xml::xmlString(xmlTextReaderGetAttribute(reader, (const xmlChar*)"char1"));
if (s1.empty() || s2.empty())
return false;
m_begin = s1.front();
m_end = s2.front();
return true;
}
bool RegExpr::doLoad(xmlTextReaderPtr reader)
{
m_pattern = regina::xml::xmlString(xmlTextReaderGetAttribute(reader, (const xmlChar*)"String"));
regina::valueOf(regina::xml::xmlString(xmlTextReaderGetAttribute(reader, (const xmlChar*)"minimal")), m_minimal);
regina::valueOf(regina::xml::xmlString(xmlTextReaderGetAttribute(reader, (const xmlChar*)"insensitive")), m_caseInsensitive);
return !m_pattern.empty(); // m_regex.isValid() would be better, but parses the regex and thus is way too expensive
}
bool StringDetect::doLoad(xmlTextReaderPtr reader)
{
m_string = regina::xml::xmlString(xmlTextReaderGetAttribute(reader, (const xmlChar*)"String"));
bool cs;
regina::valueOf(regina::xml::xmlString(xmlTextReaderGetAttribute(reader, (const xmlChar*)"insensitive")), cs);
m_caseSensitivity = ! cs;
return !m_string.empty();
}
bool WordDetect::doLoad(xmlTextReaderPtr reader)
{
m_word = regina::xml::xmlString(xmlTextReaderGetAttribute(reader, (const xmlChar*)"String"));
return !m_word.empty();
}
} // namespace regina::syntax
|