View Javadoc

1   package fr.ove.openmath.jome.ctrl.amto;
2   
3   import fr.ove.openmath.jome.ctrl.linear.events.LinearParserEvent;
4   import fr.ove.openmath.jome.ctrl.amto.MapsToNAry;
5   
6   /***
7   * The abstract math tree object of the arithmetic addition.
8   *
9   * @author © 1999 DIRAT Laurent
10  * @version 1.0  17/11/1999
11  */
12  public class Plus extends MapsToNAry {
13      /***
14      * The constructor.
15      */
16      public Plus() {
17          super("plus", LinearParserEvent.ADDITION);
18      }
19  
20      /***
21      * "Flushes" the object as a sequence of events for the building of the model
22      */
23      public void flush() {
24          LinearParserEvent linearParserEvent = new LinearParserEvent(getEventSource());
25          
26          int nbChildren = getNbChildren();
27          if (nbChildren == 0) {
28              linearParserEvent.setToken(token, "+");
29              fireLinearParserEvent(linearParserEvent);
30          }
31          else {
32              AbstractMathTreeObject child = getChild(0);
33              flushChild(child, 0);
34              
35              for (int i = 1; i < nbChildren; i++) {
36                  child = getChild(i);
37                  if (!child.identifier.equals("unaryMinus")) {
38                      linearParserEvent.setToken(token, "+");
39                      fireLinearParserEvent(linearParserEvent);
40                  }
41                  flushChild(child, 0);
42              }
43          }
44      }
45      
46      /***
47      * "Flushes" the specified child.<BR>
48      * According to their father, some children need to be between parenthesis to respect correctness of the
49      * expression. So, when needed, the specified child is "flushed" as if it was between parenthesis.
50      * @param child the child to "flush"
51      * @param index the child index in the list of children of the instance.
52      */
53      protected void flushChild(AbstractMathTreeObject child, int index){
54          if (/*child.identifier.equals("arithMinus") ||*/ child.identifier.equals("plus")) {
55              LinearParserEvent linearParserEvent = new LinearParserEvent(getEventSource());
56              linearParserEvent.setToken(LinearParserEvent.OPEN_PAREN, "(");
57              fireLinearParserEvent(linearParserEvent);
58              child.flush();
59              linearParserEvent.setToken(LinearParserEvent.CLOSE_PAREN, ")");
60              fireLinearParserEvent(linearParserEvent);
61          }
62          else
63              child.flush();
64      }
65  }