lib Library API Documentation

sequenceparser.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2001 Andrea Rizzi <rizzi@kde.org>
00003                   Ulrich Kuettler <ulrich.kuettler@mailbox.tu-dresden.de>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018    Boston, MA 02111-1307, USA.
00019 */
00020 
00021 #include "basicelement.h"
00022 #include "elementtype.h"
00023 #include "sequenceparser.h"
00024 #include "symboltable.h"
00025 #include "textelement.h"
00026 
00027 
00028 KFORMULA_NAMESPACE_BEGIN
00029 
00030 
00031 SequenceParser::SequenceParser( const SymbolTable& t )
00032         : tokenStart( 0 ), tokenEnd( 0 ), type( SEQUENCE ),
00033           binOpAllowed( false ), table( t )
00034 {
00035 }
00036 
00037 
00038 void SequenceParser::setElementType( uint pos, ElementType* type )
00039 {
00040     list.at( pos )->setElementType( type );
00041 }
00042 
00043 
00044 ElementType* SequenceParser::parse( QPtrList<BasicElement>& elements )
00045 {
00046     list = elements;
00047     return new SequenceType( this );
00048 }
00049 
00050 
00051 void SequenceParser::nextToken()
00052 {
00053     tokenStart = tokenEnd;
00054     if ( tokenStart >= list.count() ) {
00055         type = END;
00056         return;
00057     }
00058     tokenEnd++;
00059     BasicElement* element = list.at( tokenStart );
00060     type = element->getTokenType();
00061     if ( type == SEPARATOR ) {
00062         if ( tokenEnd < list.count() ) {
00063             QChar ch = getEndChar();
00064             switch ( ch ) {
00065             case ',':
00066             case '>':
00067             case ';':
00068                 type = NAME;
00069                 tokenEnd++;
00070                 break;
00071             default:
00072                 readText();
00073             }
00074         }
00075     }
00076     else if ( type == ORDINARY ) {
00077         readText();
00078     }
00079     else if ( type == NUMBER ) {
00080         readNumber();
00081     }
00082     if ( !binOpAllowed && ( type == BINOP ) ) {
00083         type = ORDINARY;
00084     }
00085     binOpAllowed = ( type == ORDINARY ) || ( type == NUMBER ) || ( type == NAME ) ||
00086           ( type == ELEMENT ) || ( type == BRACKET ) || ( type == INNER );
00087 
00088     //cerr << "SequenceParser::nextToken(): " << type << " "
00089     //     << tokenStart << " " << tokenEnd << endl;
00090 }
00091 
00092 
00093 void SequenceParser::readNumber()
00094 {
00095     type = NUMBER;
00096     readDigits();
00097     if ( tokenEnd < list.count()-1 ) {
00098         QChar ch = getEndChar();
00099 
00100         // Look for a dot.
00101         if ( ch == '.' ) {
00102             tokenEnd++;
00103             ch = getEndChar();
00104             if ( ch.isNumber() ) {
00105                 readDigits();
00106             }
00107 //             else {
00108 //                 tokenEnd--;
00109 //                 return;
00110 //             }
00111         }
00112 
00113         // there might as well be an exponent
00114         if ( tokenEnd < list.count()-1 ) {
00115             BasicElement* element = list.at(tokenEnd);
00116             ch = getEndChar();
00117             if ( ( element->getTokenType() == ORDINARY ) &&
00118                  ( ( ch == 'E' ) || ( ch == 'e' ) ) ) {
00119                 tokenEnd++;
00120                 ch = getEndChar();
00121 
00122                 // signs are allowed after the exponent
00123                 if ( ( ( ch == '+' ) || ( ch == '-' ) ) &&
00124                      ( tokenEnd < list.count()-1 ) ) {
00125                     tokenEnd++;
00126                     ch = getEndChar();
00127                     if ( ch.isNumber() ) {
00128                         readDigits();
00129                     }
00130                     else {
00131                         tokenEnd -= 2;
00132                         return;
00133                     }
00134                 }
00135                 else if ( ch.isNumber() ) {
00136                     readDigits();
00137                 }
00138                 else {
00139                     tokenEnd--;
00140                 }
00141             }
00142         }
00143     }
00144 }
00145 
00146 
00147 void SequenceParser::readDigits()
00148 {
00149     for ( ; tokenEnd < list.count(); tokenEnd++ ) {
00150         QChar ch = getEndChar();
00151         if ( !ch.isNumber() ) {
00152             break;
00153         }
00154     }
00155 }
00156 
00157 
00158 void SequenceParser::readText()
00159 {
00160     BasicElement* element = list.at( tokenStart );
00161     TextElement* beginText = static_cast<TextElement*>( element );
00162     if ( beginText->isSymbol() ||
00163         ( beginText->getCharacter() == '/' ) ) {
00164         return;
00165     }
00166     char format = beginText->format();
00167     type = ORDINARY;
00168     for ( ; tokenEnd < list.count(); tokenEnd++ ) {
00169         element = list.at( tokenEnd );
00170         TokenType tt = element->getTokenType();
00171         if ( ( ( tt != ORDINARY ) ||
00172                ( element->getCharacter() == '/' ) ) &&
00173              ( tt != NUMBER ) ) {
00174             return;
00175         }
00176         if ( static_cast<TextElement*>( element )->format() != format ) {
00177             return;
00178         }
00179         if ( static_cast<TextElement*>( element )->isSymbol() ) {
00180             return;
00181         }
00182     }
00183 }
00184 
00185 QChar SequenceParser::getEndChar()
00186 {
00187     BasicElement* element = list.at( tokenEnd );
00188     return element->getCharacter();
00189 }
00190 
00191 
00192 ElementType* SequenceParser::getPrimitive()
00193 {
00194     //cerr << "SequenceParser::getPrimitive(): " << type << " "
00195     //     << tokenStart << " " << tokenEnd << endl;
00196     switch ( type ) {
00197     case ORDINARY: {
00198 //         QString text = getText();
00199 //         if ( table.contains( text ) || ( text == "\\quad" ) ) {
00200 //             return new NameType( this, text );
00201 //         }
00202 //         else {
00203             return new TextType( this );
00204 //         }
00205     }
00206     case NAME:
00207         return new NameType( this );
00208     case NUMBER:
00209         return new NumberType( this );
00210     case ELEMENT:
00211         return new ComplexElementType( this );
00212     case INNER:
00213         return new InnerElementType( this );
00214     case BINOP:
00215         return new OperatorType( this );
00216     case RELATION:
00217         return new RelationType( this );
00218     case PUNCTUATION:
00219         return new PunctuationType( this );
00220     case BRACKET:
00221         return new BracketType( this );
00222     case SEQUENCE:
00223     case SEPARATOR:
00224     case END:
00225         return 0;
00226     }
00227     return 0;
00228 }
00229 
00230 
00231 QString SequenceParser::text()
00232 {
00233     QString text;
00234     for ( uint i = tokenStart; i < tokenEnd; i++ ) {
00235         BasicElement* element = list.at( i );
00236         text.append( element->getCharacter() );
00237     }
00238     return text;
00239 }
00240 
00241 KFORMULA_NAMESPACE_END
KDE Logo
This file is part of the documentation for lib Library Version 1.3.5.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Nov 17 13:19:28 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2003