Back to index

wims  3.65+svn20090927
Public Member Functions | Static Public Member Functions | Private Attributes
fr.ove.openmath.jome.ctrl.linear.LinearParser Class Reference

A bean which parses, more precisely tokenizes, what we can call the linear form of an expression. More...

Collaboration diagram for fr.ove.openmath.jome.ctrl.linear.LinearParser:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 LinearParser ()
 The constructor.
synchronized void addLinearParserListener (LinearParserListener linearParserListener)
 Registers another listener of the instance.
synchronized void removeLinearParserListener (LinearParserListener linearParserListener)
 Removes a listener.
void fireLinearParserEvent (LinearParserEvent linearParserEvent)
 Fires a LinearParserEvent event to registered listeners.
void parse (String expression)
 Parses the specified linear form of the formula.

Static Public Member Functions

static void main (String args[])

Private Attributes

ResourcesManager resourcesManager
 The parser resources manager.
Vector listeners = new Vector()
 The list of listeners of the instance.
LinearParserEvent linearParserEvent = new LinearParserEvent(this)
 The event sent to all the listeners of the instance during the parsing.

Detailed Description

A bean which parses, more precisely tokenizes, what we can call the linear form of an expression.


For each specific token encountered, an event (

See also:
LinearParserEvent) is fired to all the listeners (
LinearParserListener) of the instance.
The event fired is made of 2 parts :
  • the token identifier (for example, for + (addition) the associated identifier is LinearParserEvent.ADDITION)
  • the value of the token, if needed. (for example, when the digit 2 is encountered, the identifier has the LinearParserEvent.INTEGER value, and the token value is represented by the
String "2") When the parsing starts, an event whith the identifier set to LinearParserEvent.START_EXPRESSION is fired. Similarly, when the parsing ends (the end of the linear form has been reached), an event whith the identifier set to LinearParserEvent.END_EXPRESSION is fired.
In order to make distinctions between what should be variables and special keywords, a resources file (LinearParserResources.properties) is attached to the parser. This file contains the user defined keywords.
To add your own keywords, edit this properties file with respect to the used format (see this file for further details).
Author:
© 1999 DIRAT Laurent
Version:
1.0 27/09/1999

Definition at line 57 of file LinearParser.java.


Constructor & Destructor Documentation

The constructor.

Definition at line 78 of file LinearParser.java.

                          {
        resourcesManager = new ResourcesManager("fr.ove.openmath.jome.ctrl.linear.LinearParserResources");
    }

Here is the caller graph for this function:


Member Function Documentation

synchronized void fr.ove.openmath.jome.ctrl.linear.LinearParser.addLinearParserListener ( LinearParserListener  linearParserListener) [inline]

Registers another listener of the instance.

Parameters:
linearParserListenerthe listener to add.

Definition at line 86 of file LinearParser.java.

                                                                                                {
        listeners.addElement(linearParserListener);
    }

Here is the caller graph for this function:

void fr.ove.openmath.jome.ctrl.linear.LinearParser.fireLinearParserEvent ( LinearParserEvent  linearParserEvent) [inline]

Fires a LinearParserEvent event to registered listeners.

Parameters:
linearParserEventevent encapsulating relevant information.

Definition at line 102 of file LinearParser.java.

                                                                           {
        for (int i = 0; i < listeners.size(); i++)
          ((LinearParserListener) listeners.elementAt(i)).consumeLinearParserEvent(linearParserEvent);
    }

Here is the caller graph for this function:

static void fr.ove.openmath.jome.ctrl.linear.LinearParser.main ( String  args[]) [inline, static]

Definition at line 288 of file LinearParser.java.

                                           {
        LinearParserListener l = new LinearParserListener() {
            public void consumeLinearParserEvent(LinearParserEvent linearParserEvent) {
                System.out.println(linearParserEvent.toString());
            }
        };
        
        LinearParser lp = new LinearParser();
        lp.addLinearParserListener(l);
        lp.parse("1+2+23+C+in+pi+x_8");
    }

Here is the call graph for this function:

void fr.ove.openmath.jome.ctrl.linear.LinearParser.parse ( String  expression) [inline]

Parses the specified linear form of the formula.

Parameters:
expressionthe linear form of the formula.

Definition at line 111 of file LinearParser.java.

                                         {
        // Le nombre de tokens utilisés .
        int nbTokenUsed = 0;
        // On spécifie ici quels sont les tokens qui vont permettre de découper
        // l'expression passée en argument.
        StringTokenizer theTokens = new StringTokenizer(expression, "+-/*^_()=<>;, {}\\?!\u2192\u21D4\u211E\u2027", true);
        // Des tokens temporaires pour travailler sur le token suivant le token courant.
        StringTokenizer tmpTokens = null;
        // Le token courant
        String token = null;
        // Le token suivant le token courant
        String nextToken = null;
        
        // On commence le parsing
        linearParserEvent.setToken(LinearParserEvent.START_EXPRESSION, null);
        fireLinearParserEvent(linearParserEvent);
        
        while (theTokens.hasMoreTokens()) {
            token = theTokens.nextToken();
            nbTokenUsed++;
           
            //if (token.equals("[")){System.out.println(linearParserEvent.toString());}

            if (token.equals(" "))
                continue;
              //linearParserEvent.setToken(LinearParserEvent.RESERVED,token);
            else if (token.equals("\u2192"))
              linearParserEvent.setToken(LinearParserEvent.RESERVED, "\u2192");
            else if (token.equals("\u21D4"))
              linearParserEvent.setToken(LinearParserEvent.RESERVED, "\u21D4");
            else if (token.equals("\u211E"))
              linearParserEvent.setToken(LinearParserEvent.RESERVED, "\u211E");
           else if (token.equals("+"))
              linearParserEvent.setToken(LinearParserEvent.ADDITION, token);
            else if (token.equals("-"))
              linearParserEvent.setToken(LinearParserEvent.SUBSTRACTION, token);
            else if (token.equals("*"))
              linearParserEvent.setToken(LinearParserEvent.MULTIPLICATION, "\u2027");
            else if (token.equals("\u2027"))
              linearParserEvent.setToken(LinearParserEvent.RESERVED, "\u2027");
            else if (token.equals("/"))
              linearParserEvent.setToken(LinearParserEvent.DIVISION, token);
            else if (token.equals("^"))
              linearParserEvent.setToken(LinearParserEvent.POWER, token);
            else if (token.equals("("))
              linearParserEvent.setToken(LinearParserEvent.OPEN_PAREN, token);
            else if (token.equals(")"))
              linearParserEvent.setToken(LinearParserEvent.CLOSE_PAREN, token);
            else if (token.equals("{"))
              linearParserEvent.setToken(LinearParserEvent.OPEN_CURLY, token);
            else if (token.equals("}"))
                linearParserEvent.setToken(LinearParserEvent.CLOSE_CURLY, token);
           else if (token.equals(","))                                                                               
               linearParserEvent.setToken(LinearParserEvent.SEPARATOR, null); 
// no check on [] because  n-th root syntax: sqrt[3](123) --> sqrt*[*3*]*(123)
//            else if (token.equals("["))
//                linearParserEvent.setToken(LinearParserEvent.OPEN_BRACKET, token);
//                //linearParserEvent.setToken(LinearParserEvent.RESERVED, token);
//            else if (token.equals("]"))
//            linearParserEvent.setToken(LinearParserEvent.CLOSE_BRACKET, token);
//                //linearParserEvent.setToken(LinearParserEvent.RESERVED, token);
            else if (token.equals("_"))
               linearParserEvent.setToken(LinearParserEvent.UNDERSCRIPT, token);
           else if (token.equals("<") || token.equals(">")) {
                // On re-tokenize l'expression pour récupérer l'élement suivant le token
                // (à défaut d'autre chose.... :o( )
                tmpTokens = new StringTokenizer(expression, "+-/*^_()=<>;, {}\\?!\u2192\u21D4\u211E\u2027", true );
                for (int i = 0; i < nbTokenUsed + 1; i++) {
                    try {
                        nextToken = tmpTokens.nextToken();
                    }
                    catch (NoSuchElementException  e) {
                        nextToken = null;
                    }
                }

                if (token.equals("<")) {
                    if (nextToken != null ) {
                        if (nextToken.equals("=")) {
                            theTokens.nextToken();
                            nbTokenUsed++;
                            linearParserEvent.setToken(LinearParserEvent.LESSEQUAL, "<=");
                        }
                        else if (nextToken.equals(">")) {
                            theTokens.nextToken();
                            nbTokenUsed++;
                            linearParserEvent.setToken(LinearParserEvent.UNEQUAL, "<>");
                        }
                        else
                            linearParserEvent.setToken(LinearParserEvent.LESS, token);
                    }
                    else
                        linearParserEvent.setToken(LinearParserEvent.LESS, token);
                }
                else {
                    if (nextToken != null ) {
                        if (nextToken.equals("=")) {
                            theTokens.nextToken();
                            nbTokenUsed++;
                            linearParserEvent.setToken(LinearParserEvent.GREATEREQUAL, ">=");
                        }
                        else
                            linearParserEvent.setToken(LinearParserEvent.GREATER, token);
                    }
                    else
                        linearParserEvent.setToken(LinearParserEvent.GREATER, token);
                }
            }
            else if (token.equals("="))
                linearParserEvent.setToken(LinearParserEvent.EQUAL, token);
            else if (token.equals("\\"))
                linearParserEvent.setToken(LinearParserEvent.SPECIALIZED, token);
            else if (token.equals("?"))
                linearParserEvent.setToken(LinearParserEvent.SPECIALIZED, token);
            else if (token.equals("!"))
                linearParserEvent.setToken(LinearParserEvent.SPECIALIZED, "fact");
            else {
                if (NumberUtils.isNumber(token)) {
                    if (NumberUtils.isFloat(token))
                        linearParserEvent.setToken(LinearParserEvent.FLOAT, token);
                    else
                        linearParserEvent.setToken(LinearParserEvent.INTEGER, token);
                }
                else {
                    // On a saisi des lettres. Il faut qu'on regarde si à la suite de ces
                    // lettres, il n'y a pas comme token suivant une parenthèse. Si tel est le cas,
                    // c'est que l'on a voulu saisir une fonction.

                    // On re-tokenize l'expression pour récupérer l'élement suivant le token
                    // (à défaut d'autre chose.... :o( )
                    tmpTokens = new StringTokenizer(expression, "+-/*^_()=<>;, {}\\?!\u2192\u21D4\u211E\u2027", true );
                    for (int i = 0; i < nbTokenUsed + 1; i++) {
                        try {
                            nextToken = tmpTokens.nextToken();
                        }
                        catch (NoSuchElementException  e) {
                            nextToken = null;
                        }
                    }

                    if (nextToken != null) {
                        if (nextToken.equals("(")) {
                            // C'est une fonction
                            linearParserEvent.setToken(LinearParserEvent.FUNCTION , token);
                            // On passe outre le token suivant, i.e. la parenthèse ouvrante.
                            theTokens.nextToken();
                            nbTokenUsed++;
                        }
                        else {
                            String str1 = resourcesManager.getResourceString(token);
                            if (str1 == null)
                                // C'est une variable.
                                linearParserEvent.setToken(LinearParserEvent.VARIABLE, token);
                            else
                                linearParserEvent.setToken(LinearParserEvent.RESERVED, token);
                        }
                    }
                    else {
                            String str2 = resourcesManager.getResourceString(token);
                            if (str2 == null)
                                // C'est une variable.
                                linearParserEvent.setToken(LinearParserEvent.VARIABLE, token);
                            else
                                linearParserEvent.setToken(LinearParserEvent.RESERVED,token);
                    
                  }
                }
            }
            
            fireLinearParserEvent(linearParserEvent);
        }
        
        // On a fini le parsing
        linearParserEvent.setToken(LinearParserEvent.END_EXPRESSION, null);
        fireLinearParserEvent(linearParserEvent);
    }

Here is the call graph for this function:

Here is the caller graph for this function:

synchronized void fr.ove.openmath.jome.ctrl.linear.LinearParser.removeLinearParserListener ( LinearParserListener  linearParserListener) [inline]

Removes a listener.

Parameters:
linearParserListenerthe listener to remove.

Definition at line 94 of file LinearParser.java.

                                                                                                   {
        listeners.removeElement(linearParserListener);
    }

Member Data Documentation

LinearParserEvent fr.ove.openmath.jome.ctrl.linear.LinearParser.linearParserEvent = new LinearParserEvent(this) [private]

The event sent to all the listeners of the instance during the parsing.

Definition at line 73 of file LinearParser.java.

The list of listeners of the instance.

Definition at line 68 of file LinearParser.java.

The parser resources manager.


Reads into the properties file (LinearParserResources.properties) to get the user defined specific keywords.

Definition at line 63 of file LinearParser.java.


The documentation for this class was generated from the following file: