KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > parsec > RepeatArrayParser


1 /**
2  *
3  */

4 package jfun.parsec;
5
6 final class RepeatArrayParser<R> extends Parser<R[]> {
7   private final ArrayFactory<R> af;
8
9   private final int n;
10
11   private final Parser<? extends R> p;
12
13   RepeatArrayParser(String JavaDoc name, ArrayFactory<R> af, int n, Parser<? extends R> p) {
14     super(name);
15     this.af = af;
16     this.n = n;
17     this.p = p;
18   }
19
20   boolean apply(final ParseContext ctxt) {
21     final R[] ret = af.createArray(n);
22     for (int i = 0; i < n; i++) {
23       if (!p.parse(ctxt))
24         return false;
25       ret[i] = p.getReturn(ctxt);
26     }
27     ctxt.setReturn(ret);
28     return true;
29   }
30 }
Popular Tags