KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > parsec > OperatorTable


1 /*****************************************************************************
2  * Copyright (C) Zephyr Business Solutions Corp. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  *****************************************************************************/

8 /*
9  * Created on Nov 21, 2004
10  *
11  * Author Ben Yu
12  */

13 package jfun.parsec;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collections JavaDoc;
16 /**
17  * This class is used to describe operator information.
18  * Operators have precedences. the higher the precedence number, the higher the precedence.
19  * For the same precedence, prefix > postfix > left infix > right infix > non-associative infix.
20  * @author Ben Yu
21  *
22  * Nov 21, 2004
23  */

24 public final class OperatorTable<E> implements java.io.Serializable JavaDoc{
25   static final int PREFIX = 0;
26   static final int POSTFIX = 1;
27   static final int LASSOC = 2;
28   static final int RASSOC = 3;
29   static final int NASSOC = 4;
30   private final ArrayList JavaDoc ops = new ArrayList JavaDoc();
31   static final class Operator implements Comparable JavaDoc<Operator>{
32     private final Parser op;
33     private final int prec;
34     private final int kind;
35     Operator(final Parser op, final int prec, final int k){
36       this.op = op;
37       this.prec = prec;
38       this.kind = k;
39     }
40     Parser getOp(){return op;}
41     int getPrecedence(){return prec;}
42     int getKind(){return kind;}
43     public int compareTo(final Operator other){
44       final Operator op2 = (Operator)other;
45       if(prec > op2.prec) return -1;
46       else if(prec < op2.prec) return 1;
47       else if(kind < op2.kind) return -1;
48       else if(kind > op2.kind) return 1;
49       return 0;
50     }
51   }
52   /**
53    * Adds a prefix unary operator.
54    * @param p the parser for the operator.
55    * @param precedence the precedence number.
56    * @return this.
57    */

58   public OperatorTable<E> prefix(final Parser<? extends Map<? super E, ? extends E>> p, final int precedence){
59     ops.add(new Operator(p,precedence, PREFIX));
60     return this;
61   }
62   /**
63    * Adds a postfix unary operator.
64    * @param p the parser for the operator.
65    * @param precedence the precedence number.
66    * @return this.
67    */

68   public OperatorTable<E> postfix(final Parser<? extends Map<? super E, ? extends E>> p, final int precedence){
69     ops.add(new Operator(p,precedence, POSTFIX));
70     return this;
71   }
72   /**
73    * Adds a infix left-associative binary operator.
74    * @param p the parser for the operator.
75    * @param precedence the precedence number.
76    * @return this.
77    */

78   public OperatorTable<E> infixl(final Parser<? extends Map2<? super E, ? super E, ? extends E>> p,
79       final int precedence){
80     ops.add(new Operator(p,precedence, LASSOC));
81     return this;
82   }
83   /**
84    * Adds a infix right-associative binary operator.
85    * @param p the parser for the operator.
86    * @param precedence the precedence number.
87    * @return this.
88    */

89   public OperatorTable<E> infixr(final Parser<? extends Map2<? super E, ? super E, ? extends E>> p,
90       final int precedence){
91     ops.add(new Operator(p,precedence, RASSOC));
92     return this;
93   }
94   /**
95    * Adds a infix non-associative binary operator.
96    * @param p the parser for the operator.
97    * @param precedence the precedence number.
98    * @return this.
99    */

100   public OperatorTable<E> infixn(final Parser<? extends Map2<? super E, ? super E, ? extends E>> p,
101       final int precedence){
102     ops.add(new Operator(p,precedence, NASSOC));
103     return this;
104   }
105   Operator[] getOperators(){
106     Collections.sort(ops);
107     final Operator[] ret = new Operator[ops.size()];
108     ops.toArray(ret);
109     return ret;
110   }
111 }
112
Popular Tags