summaryrefslogtreecommitdiffstats
path: root/lib/kformula/sequenceparser.cc
blob: 9a4ee8a26bdbadba5182d8b64999f52cf63b9348 (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
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
/* This file is part of the KDE project
   Copyright (C) 2001 Andrea Rizzi <rizzi@kde.org>
	              Ulrich Kuettler <ulrich.kuettler@mailbox.tu-dresden.de>

   This library is free software; you can redistribute it and/or
   modify it under the terms of 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 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 "basicelement.h"
#include "elementtype.h"
#include "sequenceparser.h"
#include "symboltable.h"
#include "textelement.h"


KFORMULA_NAMESPACE_BEGIN


SequenceParser::SequenceParser( const SymbolTable& t )
        : tokenStart( 0 ), tokenEnd( 0 ), type( SEQUENCE ),
          binOpAllowed( false ), table( t )
{
}


void SequenceParser::setElementType( uint pos, ElementType* type )
{
    list.at( pos )->setElementType( type );
}


ElementType* SequenceParser::parse( TQPtrList<BasicElement>& elements )
{
    list = elements;
    return new SequenceType( this );
}


void SequenceParser::nextToken()
{
    tokenStart = tokenEnd;
    if ( tokenStart >= list.count() ) {
        type = END;
        return;
    }
    tokenEnd++;
    BasicElement* element = list.at( tokenStart );
    type = element->getTokenType();
    if ( type == SEPARATOR ) {
        if ( tokenEnd < list.count() ) {
            TQChar ch = getEndChar();
            switch ( ch ) {
            case ',':
            case '>':
            case ';':
                type = NAME;
                tokenEnd++;
                break;
            default:
                readText();
            }
        }
    }
    else if ( type == ORDINARY ) {
        readText();
    }
    else if ( type == NUMBER ) {
        readNumber();
    }
    if ( !binOpAllowed && ( type == BINOP ) ) {
        type = ORDINARY;
    }
    binOpAllowed = ( type == ORDINARY ) || ( type == NUMBER ) || ( type == NAME ) ||
          ( type == ELEMENT ) || ( type == BRACKET ) || ( type == INNER );

    //cerr << "SequenceParser::nextToken(): " << type << " "
    //     << tokenStart << " " << tokenEnd << endl;
}


void SequenceParser::readNumber()
{
    type = NUMBER;
    readDigits();
    if ( tokenEnd < list.count()-1 ) {
        TQChar ch = getEndChar();

        // Look for a dot.
        if ( ch == '.' ) {
            tokenEnd++;
            ch = getEndChar();
            if ( ch.isNumber() ) {
                readDigits();
            }
//             else {
//                 tokenEnd--;
//                 return;
//             }
        }

        // there might as well be an exponent
        if ( tokenEnd < list.count()-1 ) {
            BasicElement* element = list.at(tokenEnd);
            ch = getEndChar();
            if ( ( element->getTokenType() == ORDINARY ) &&
                 ( ( ch == 'E' ) || ( ch == 'e' ) ) ) {
                tokenEnd++;
                ch = getEndChar();

                // signs are allowed after the exponent
                if ( ( ( ch == '+' ) || ( ch == '-' ) ) &&
                     ( tokenEnd < list.count()-1 ) ) {
                    tokenEnd++;
                    ch = getEndChar();
                    if ( ch.isNumber() ) {
                        readDigits();
                    }
                    else {
                        tokenEnd -= 2;
                        return;
                    }
                }
                else if ( ch.isNumber() ) {
                    readDigits();
                }
                else {
                    tokenEnd--;
                }
            }
        }
    }
}


void SequenceParser::readDigits()
{
    for ( ; tokenEnd < list.count(); tokenEnd++ ) {
        TQChar ch = getEndChar();
        if ( !ch.isNumber() ) {
            break;
        }
    }
}


void SequenceParser::readText()
{
    BasicElement* element = list.at( tokenStart );
    TextElement* beginText = static_cast<TextElement*>( element );
    if ( beginText->isSymbol() ||
        ( beginText->getCharacter() == '/' ) ) {
        return;
    }
    char format = beginText->format();
    type = ORDINARY;
    for ( ; tokenEnd < list.count(); tokenEnd++ ) {
        element = list.at( tokenEnd );
        TokenType tt = element->getTokenType();
        if ( ( ( tt != ORDINARY ) ||
               ( element->getCharacter() == '/' ) ) &&
             ( tt != NUMBER ) ) {
            return;
        }
        if ( static_cast<TextElement*>( element )->format() != format ) {
            return;
        }
        if ( static_cast<TextElement*>( element )->isSymbol() ) {
            return;
        }
    }
}

TQChar SequenceParser::getEndChar()
{
    BasicElement* element = list.at( tokenEnd );
    return element->getCharacter();
}


ElementType* SequenceParser::getPrimitive()
{
    //cerr << "SequenceParser::getPrimitive(): " << type << " "
    //     << tokenStart << " " << tokenEnd << endl;
    switch ( type ) {
    case ORDINARY: {
//         TQString text = getText();
//         if ( table.tqcontains( text ) || ( text == "\\quad" ) ) {
//             return new NameType( this, text );
//         }
//         else {
            return new TextType( this );
//         }
    }
    case NAME:
        return new NameType( this );
    case NUMBER:
        return new NumberType( this );
    case ELEMENT:
        return new ComplexElementType( this );
    case INNER:
        return new InnerElementType( this );
    case BINOP:
        return new OperatorType( this );
    case RELATION:
        return new RelationType( this );
    case PUNCTUATION:
        return new PunctuationType( this );
    case BRACKET:
        return new BracketType( this );
    case SEQUENCE:
    case SEPARATOR:
    case END:
        return 0;
    }
    return 0;
}


TQString SequenceParser::text()
{
    TQString text;
    for ( uint i = tokenStart; i < tokenEnd; i++ ) {
        BasicElement* element = list.at( i );
        text.append( element->getCharacter() );
    }
    return text;
}

KFORMULA_NAMESPACE_END