Back to index

wims  3.65+svn20090927
LinearParser.java
Go to the documentation of this file.
00001 /*
00002 $Id: LinearParser.java,v 1.3 2003/02/18 11:48:46 sander Exp $
00003 */
00004 
00005 /*
00006 Copyright (C) 2001-2002 Mainline Project (I3S - ESSI - CNRS -UNSA)
00007 
00008 This library is free software; you can redistribute it and/or
00009 modify it under the terms of the GNU Lesser General Public
00010 License as published by the Free Software Foundation; either
00011 version 2.1 of the License, or (at your option) any later version.
00012 
00013 This library is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 Lesser General Public License for more details.
00017 
00018 You should have received a copy of the GNU Lesser General Public
00019 License along with this library; if not, write to the Free Software
00020 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 
00022 For further information on the GNU Lesser General Public License,
00023 see: http://www.gnu.org/copyleft/lesser.html
00024 For further information on this library, contact: mainline@essi.fr
00025 */
00026 
00027 
00028 package fr.ove.openmath.jome.ctrl.linear;
00029 
00030 import java.util.*;
00031 import fr.ove.openmath.jome.ctrl.linear.events.*;
00032 import fr.ove.utils.*;
00033 
00057 public class LinearParser implements java.io.Serializable {
00063     private ResourcesManager resourcesManager;
00064     
00068     private Vector listeners = new Vector();
00069 
00073     private LinearParserEvent linearParserEvent = new LinearParserEvent(this);
00074     
00078     public LinearParser() {
00079         resourcesManager = new ResourcesManager("fr.ove.openmath.jome.ctrl.linear.LinearParserResources");
00080     }
00081     
00086     public synchronized void addLinearParserListener(LinearParserListener linearParserListener) {
00087         listeners.addElement(linearParserListener);
00088     }
00089 
00094     public synchronized void removeLinearParserListener(LinearParserListener linearParserListener) {
00095         listeners.removeElement(linearParserListener);
00096     }
00097 
00102     public void fireLinearParserEvent(LinearParserEvent linearParserEvent) {
00103         for (int i = 0; i < listeners.size(); i++)
00104           ((LinearParserListener) listeners.elementAt(i)).consumeLinearParserEvent(linearParserEvent);
00105     }
00106 
00111     public void parse(String expression) {
00112         // Le nombre de tokens utilisés .
00113         int nbTokenUsed = 0;
00114         // On spécifie ici quels sont les tokens qui vont permettre de découper
00115         // l'expression passée en argument.
00116         StringTokenizer theTokens = new StringTokenizer(expression, "+-/*^_()=<>;, {}\\?!\u2192\u21D4\u211E\u2027", true);
00117         // Des tokens temporaires pour travailler sur le token suivant le token courant.
00118         StringTokenizer tmpTokens = null;
00119         // Le token courant
00120         String token = null;
00121         // Le token suivant le token courant
00122         String nextToken = null;
00123         
00124         // On commence le parsing
00125         linearParserEvent.setToken(LinearParserEvent.START_EXPRESSION, null);
00126         fireLinearParserEvent(linearParserEvent);
00127         
00128         while (theTokens.hasMoreTokens()) {
00129             token = theTokens.nextToken();
00130             nbTokenUsed++;
00131            
00132             //if (token.equals("[")){System.out.println(linearParserEvent.toString());}
00133 
00134             if (token.equals(" "))
00135                 continue;
00136               //linearParserEvent.setToken(LinearParserEvent.RESERVED,token);
00137             else if (token.equals("\u2192"))
00138               linearParserEvent.setToken(LinearParserEvent.RESERVED, "\u2192");
00139             else if (token.equals("\u21D4"))
00140               linearParserEvent.setToken(LinearParserEvent.RESERVED, "\u21D4");
00141             else if (token.equals("\u211E"))
00142               linearParserEvent.setToken(LinearParserEvent.RESERVED, "\u211E");
00143            else if (token.equals("+"))
00144               linearParserEvent.setToken(LinearParserEvent.ADDITION, token);
00145             else if (token.equals("-"))
00146               linearParserEvent.setToken(LinearParserEvent.SUBSTRACTION, token);
00147             else if (token.equals("*"))
00148               linearParserEvent.setToken(LinearParserEvent.MULTIPLICATION, "\u2027");
00149             else if (token.equals("\u2027"))
00150               linearParserEvent.setToken(LinearParserEvent.RESERVED, "\u2027");
00151             else if (token.equals("/"))
00152               linearParserEvent.setToken(LinearParserEvent.DIVISION, token);
00153             else if (token.equals("^"))
00154               linearParserEvent.setToken(LinearParserEvent.POWER, token);
00155             else if (token.equals("("))
00156               linearParserEvent.setToken(LinearParserEvent.OPEN_PAREN, token);
00157             else if (token.equals(")"))
00158               linearParserEvent.setToken(LinearParserEvent.CLOSE_PAREN, token);
00159             else if (token.equals("{"))
00160               linearParserEvent.setToken(LinearParserEvent.OPEN_CURLY, token);
00161             else if (token.equals("}"))
00162                 linearParserEvent.setToken(LinearParserEvent.CLOSE_CURLY, token);
00163            else if (token.equals(","))                                                                               
00164                linearParserEvent.setToken(LinearParserEvent.SEPARATOR, null); 
00165 // no check on [] because  n-th root syntax: sqrt[3](123) --> sqrt*[*3*]*(123)
00166 //            else if (token.equals("["))
00167 //                linearParserEvent.setToken(LinearParserEvent.OPEN_BRACKET, token);
00168 //                //linearParserEvent.setToken(LinearParserEvent.RESERVED, token);
00169 //            else if (token.equals("]"))
00170 //            linearParserEvent.setToken(LinearParserEvent.CLOSE_BRACKET, token);
00171 //                //linearParserEvent.setToken(LinearParserEvent.RESERVED, token);
00172             else if (token.equals("_"))
00173                linearParserEvent.setToken(LinearParserEvent.UNDERSCRIPT, token);
00174            else if (token.equals("<") || token.equals(">")) {
00175                 // On re-tokenize l'expression pour récupérer l'élement suivant le token
00176                 // (à défaut d'autre chose.... :o( )
00177                 tmpTokens = new StringTokenizer(expression, "+-/*^_()=<>;, {}\\?!\u2192\u21D4\u211E\u2027", true );
00178                 for (int i = 0; i < nbTokenUsed + 1; i++) {
00179                     try {
00180                         nextToken = tmpTokens.nextToken();
00181                     }
00182                     catch (NoSuchElementException  e) {
00183                         nextToken = null;
00184                     }
00185                 }
00186 
00187                 if (token.equals("<")) {
00188                     if (nextToken != null ) {
00189                         if (nextToken.equals("=")) {
00190                             theTokens.nextToken();
00191                             nbTokenUsed++;
00192                             linearParserEvent.setToken(LinearParserEvent.LESSEQUAL, "<=");
00193                         }
00194                         else if (nextToken.equals(">")) {
00195                             theTokens.nextToken();
00196                             nbTokenUsed++;
00197                             linearParserEvent.setToken(LinearParserEvent.UNEQUAL, "<>");
00198                         }
00199                         else
00200                             linearParserEvent.setToken(LinearParserEvent.LESS, token);
00201                     }
00202                     else
00203                         linearParserEvent.setToken(LinearParserEvent.LESS, token);
00204                 }
00205                 else {
00206                     if (nextToken != null ) {
00207                         if (nextToken.equals("=")) {
00208                             theTokens.nextToken();
00209                             nbTokenUsed++;
00210                             linearParserEvent.setToken(LinearParserEvent.GREATEREQUAL, ">=");
00211                         }
00212                         else
00213                             linearParserEvent.setToken(LinearParserEvent.GREATER, token);
00214                     }
00215                     else
00216                         linearParserEvent.setToken(LinearParserEvent.GREATER, token);
00217                 }
00218             }
00219             else if (token.equals("="))
00220                 linearParserEvent.setToken(LinearParserEvent.EQUAL, token);
00221             else if (token.equals("\\"))
00222                 linearParserEvent.setToken(LinearParserEvent.SPECIALIZED, token);
00223             else if (token.equals("?"))
00224                 linearParserEvent.setToken(LinearParserEvent.SPECIALIZED, token);
00225             else if (token.equals("!"))
00226                 linearParserEvent.setToken(LinearParserEvent.SPECIALIZED, "fact");
00227             else {
00228                 if (NumberUtils.isNumber(token)) {
00229                     if (NumberUtils.isFloat(token))
00230                         linearParserEvent.setToken(LinearParserEvent.FLOAT, token);
00231                     else
00232                         linearParserEvent.setToken(LinearParserEvent.INTEGER, token);
00233                 }
00234                 else {
00235                     // On a saisi des lettres. Il faut qu'on regarde si à la suite de ces
00236                     // lettres, il n'y a pas comme token suivant une parenthèse. Si tel est le cas,
00237                     // c'est que l'on a voulu saisir une fonction.
00238 
00239                     // On re-tokenize l'expression pour récupérer l'élement suivant le token
00240                     // (à défaut d'autre chose.... :o( )
00241                     tmpTokens = new StringTokenizer(expression, "+-/*^_()=<>;, {}\\?!\u2192\u21D4\u211E\u2027", true );
00242                     for (int i = 0; i < nbTokenUsed + 1; i++) {
00243                         try {
00244                             nextToken = tmpTokens.nextToken();
00245                         }
00246                         catch (NoSuchElementException  e) {
00247                             nextToken = null;
00248                         }
00249                     }
00250 
00251                     if (nextToken != null) {
00252                         if (nextToken.equals("(")) {
00253                             // C'est une fonction
00254                             linearParserEvent.setToken(LinearParserEvent.FUNCTION , token);
00255                             // On passe outre le token suivant, i.e. la parenthèse ouvrante.
00256                             theTokens.nextToken();
00257                             nbTokenUsed++;
00258                         }
00259                         else {
00260                             String str1 = resourcesManager.getResourceString(token);
00261                             if (str1 == null)
00262                                 // C'est une variable.
00263                                 linearParserEvent.setToken(LinearParserEvent.VARIABLE, token);
00264                             else
00265                                 linearParserEvent.setToken(LinearParserEvent.RESERVED, token);
00266                         }
00267                     }
00268                     else {
00269                             String str2 = resourcesManager.getResourceString(token);
00270                             if (str2 == null)
00271                                 // C'est une variable.
00272                                 linearParserEvent.setToken(LinearParserEvent.VARIABLE, token);
00273                             else
00274                                 linearParserEvent.setToken(LinearParserEvent.RESERVED,token);
00275                     
00276                   }
00277                 }
00278             }
00279             
00280             fireLinearParserEvent(linearParserEvent);
00281         }
00282         
00283         // On a fini le parsing
00284         linearParserEvent.setToken(LinearParserEvent.END_EXPRESSION, null);
00285         fireLinearParserEvent(linearParserEvent);
00286     }
00287     
00288     public static void main(String args[]) {
00289         LinearParserListener l = new LinearParserListener() {
00290             public void consumeLinearParserEvent(LinearParserEvent linearParserEvent) {
00291                 System.out.println(linearParserEvent.toString());
00292             }
00293         };
00294         
00295         LinearParser lp = new LinearParser();
00296         lp.addLinearParserListener(l);
00297         lp.parse("1+2+23+C+in+pi+x_8");
00298     }
00299 }