summaryrefslogtreecommitdiffstats
path: root/kregexpeditor/concregexp.cpp
blob: cefbac474e6229448d71b8eaab7e073867c904e2 (plain)
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
/*
 *  Copyright (c) 2002-2003 Jesper K. Pedersen <[email protected]>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License version 2 as published by the Free Software Foundation.
 *
 *  This library 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
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 **/
#include "concregexp.h"
#include "widgetfactory.h"
#include "compoundregexp.h"

ConcRegExp::ConcRegExp( bool selected ) :RegExp( selected )
{
}

void ConcRegExp::addRegExp( RegExp *regExp )
{
	list.append(regExp);
    addChild( regExp );
}

RegExpList ConcRegExp::children()
{
    return list;
}


bool ConcRegExp::check( ErrorMap& map, bool first, bool last)
{
    bool f = first;
    bool possibleEmpty = true;
    for ( RegExpListIt it(list); *it; ++it ) {
        possibleEmpty = (*it)->check( map, f, last && it.atLast() ) && possibleEmpty;
        if ( ! possibleEmpty )
            f = false;
    }
    return possibleEmpty;
}

RegExp* ConcRegExp::lastRegExp()
{
    if ( list.count() == 0)
        return 0;
    else
        return list.at( list.count()-1);
}

TQDomNode ConcRegExp::toXml( TQDomDocument* doc ) const
{
    TQDomElement top = doc->createElement( TQString::fromLocal8Bit("Concatenation") );
    for ( RegExpListIt it(list); *it; ++it ) {
        top.appendChild( (*it)->toXml( doc ) );
    }
    return top;
}

bool ConcRegExp::load( TQDomElement top, const TQString& version )
{
    Q_ASSERT( top.tagName() == TQString::fromLocal8Bit( "Concatenation" ) );

    for ( TQDomNode child = top.firstChild(); !child.isNull(); child = child.nextSibling() ) {
        if ( ! child.isElement() )
            continue; // User might have added a comment.

        RegExp* regexp = WidgetFactory::createRegExp( child.toElement(), version );
        if ( regexp == 0 )
            return false;
        addRegExp( regexp );
    }
    return true;
}

bool ConcRegExp::operator==( const RegExp& other ) const
{
    // TODO: Merge with AltnRegExp::operator==
    if ( list.count() == 1 )
        return ( other == *(const_cast< TQPtrList<RegExp>& >(list).at(0)) );

    if ( other.type() != type() )
        return false;

    const ConcRegExp& theOther = dynamic_cast<const ConcRegExp&>( other );

    if ( list.count() != theOther.list.count() )
        return false;

    RegExpListIt it1( list );
    RegExpListIt it2( theOther.list );

    for ( ; *it1 && *it2 ; ) {
        if ( ! (**it1 == **it2) )
            return false;
        ++it1;
        ++it2;
    }
    return true;
}


void ConcRegExp::replacePart( CompoundRegExp* replacement )
{
    RegExp* otherChild = replacement->child();
    ConcRegExp* otherConc = dynamic_cast<ConcRegExp*>( otherChild );
    if ( ! otherConc ) {
        // Watch out for garbage here!
        otherConc = new ConcRegExp( false );
        otherConc->addRegExp( otherChild );
    }

    RegExpList newList;
    RegExpListIt it1( list );
    while ( *it1 ) {
        (*it1)->replacePart( replacement );
        RegExpListIt it2 = it1;
        RegExpListIt it3( otherConc->list );
        bool match = true;
        int count = 0;

        // See if replacement is a sublist of list starting from what it1 points at
        for ( ; *it2 && * it3 && match ; ) {
            if (! ( **it2 == **it3 ) )
                match = false;
            ++it2;
            ++it3;
            ++count;
        }

        if ( match && ! *it3) {
            // I found a match, delete the element in it1, which is common with it3
            while ( *it1 != *it2 ) {
                RegExp* item = *it1;
                ++it1;
                delete item;
            }
            RegExp* theClone = replacement->clone();
            newList.append( theClone );
            addChild( theClone );
        }
        else {
            newList.append( *it1 );
            ++it1;
        }
    }
    list = newList;
}