File indexing completed on 2024-04-21 05:51:38

0001 /*
0002  *  SPDX-FileCopyrightText: 2002-2004 Jesper K. Pedersen <blackie@kde.org>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.0-only
0005  **/
0006 
0007 #include "regexpconverter.h"
0008 
0009 #include <QTextEdit>
0010 
0011 #include "altnregexp.h"
0012 #include "compoundregexp.h"
0013 #include "concregexp.h"
0014 #include "dotregexp.h"
0015 #include "lookaheadregexp.h"
0016 #include "positionregexp.h"
0017 #include "regexphighlighter.h"
0018 #include "repeatregexp.h"
0019 #include "textrangeregexp.h"
0020 #include "textregexp.h"
0021 
0022 RegExpConverter *RegExpConverter::_current = nullptr;
0023 RegExp *RegExpConverter::parse(const QString &, bool *ok)
0024 {
0025     *ok = false;
0026     return new DotRegExp(false); // This method should never be called.
0027 }
0028 
0029 // This function needs to be called toStr rather than toString, as it is not possible to
0030 // over load function across inheritance!
0031 QString RegExpConverter::toStr(RegExp *regexp, bool markSelection)
0032 {
0033     switch (regexp->type()) {
0034     case RegExp::CONC:
0035         return toString(static_cast<ConcRegExp *>(regexp), markSelection);
0036     case RegExp::TEXT:
0037         return toString(static_cast<TextRegExp *>(regexp), markSelection);
0038     case RegExp::DOT:
0039         return toString(static_cast<DotRegExp *>(regexp), markSelection);
0040     case RegExp::POSITION:
0041         return toString(static_cast<PositionRegExp *>(regexp), markSelection);
0042     case RegExp::REPEAT:
0043         return toString(static_cast<RepeatRegExp *>(regexp), markSelection);
0044     case RegExp::ALTN:
0045         return toString(static_cast<AltnRegExp *>(regexp), markSelection);
0046     case RegExp::COMPOUND:
0047         return toString(static_cast<CompoundRegExp *>(regexp), markSelection);
0048     case RegExp::LOOKAHEAD:
0049         return toString(static_cast<LookAheadRegExp *>(regexp), markSelection);
0050     case RegExp::TEXTRANGE:
0051         return toString(static_cast<TextRangeRegExp *>(regexp), markSelection);
0052     }
0053     qWarning("We shouldn't get here!");
0054     return QString();
0055 }
0056 
0057 QString RegExpConverter::escape(const QString &text, const QList<QChar> &chars, QChar escapeChar) const
0058 {
0059     QString res;
0060     for (int i = 0; i < text.length(); i++) {
0061         for (int j = 0; j < chars.count(); j++) {
0062             if (text.at(i) == (chars[j])) {
0063                 res.append(escapeChar);
0064                 break;
0065             }
0066         }
0067         res.append(text.at(i));
0068     }
0069 
0070     return res;
0071 }
0072 
0073 /**
0074    Returns a QSyntaxHighlighter to be used in the verifyer widget.
0075 */
0076 RegexpHighlighter *RegExpConverter::highlighter(QTextEdit *)
0077 {
0078     return nullptr;
0079 }
0080 
0081 RegExpConverter *RegExpConverter::current()
0082 {
0083     return _current;
0084 }
0085 
0086 void RegExpConverter::setCurrent(RegExpConverter *converter)
0087 {
0088     _current = converter;
0089 }