KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > parsec > Map2Parser


1 /**
2  *
3  */

4 package jfun.parsec;
5
6 final class Map2Parser<R, A, B> extends Parser<R> {
7   private final Map2<? super A, ? super B, R> m2;
8
9   private final Parser<A> p1;
10
11   private final Parser<B> p2;
12
13   Map2Parser(String JavaDoc n, Map2<? super A, ? super B, R> m2, Parser<A> p1, Parser<B> p2) {
14     super(n);
15     this.m2 = m2;
16     this.p1 = p1;
17     this.p2 = p2;
18   }
19
20   boolean apply(final ParseContext ctxt) {
21     final boolean r1 = p1.parse(ctxt);
22     if (!r1)
23       return false;
24     final A o1 = p1.getReturn(ctxt);
25     final boolean r2 = p2.parse(ctxt);
26     if (!r2)
27       return false;
28     final B o2 = p2.getReturn(ctxt);
29     ctxt.setReturn(m2.map(o1, o2));
30     return true;
31   }
32 }
Popular Tags