1 8 13 package jfun.parsec; 14 import java.util.ArrayList ; 15 import java.util.Collections ; 16 24 public final class OperatorTable<E> implements java.io.Serializable { 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 ops = new ArrayList (); 31 static final class Operator implements Comparable <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 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 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 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 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 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 |