KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > scheduler > BaseParser


1 package hudson.scheduler;
2
3 import antlr.ANTLRException;
4 import antlr.LLkParser;
5 import antlr.ParserSharedInputState;
6 import antlr.SemanticException;
7 import antlr.Token;
8 import antlr.TokenBuffer;
9 import antlr.TokenStream;
10 import antlr.TokenStreamException;
11
12 /**
13  * @author Kohsuke Kawaguchi
14  */

15 abstract class BaseParser extends LLkParser {
16     private static final int[] LOWER_BOUNDS = new int[] {0,0,1,0,0};
17     private static final int[] UPPER_BOUNDS = new int[] {59,23,31,12,7};
18
19     protected BaseParser(int i) {
20         super(i);
21     }
22
23     protected BaseParser(ParserSharedInputState parserSharedInputState, int i) {
24         super(parserSharedInputState, i);
25     }
26
27     protected BaseParser(TokenBuffer tokenBuffer, int i) {
28         super(tokenBuffer, i);
29     }
30
31     protected BaseParser(TokenStream tokenStream, int i) {
32         super(tokenStream, i);
33     }
34
35     protected long doRange(int start, int end, int step, int field) throws ANTLRException {
36         rangeCheck(start, field);
37         rangeCheck(end, field);
38         if (step <= 0)
39             error("step must be positive, but found " + step);
40         if (start>end)
41             error("You mean "+end+'-'+start+'?');
42
43         long bits = 0;
44         for (int i = start; i <= end; i += step) {
45             bits |= 1L << i;
46         }
47         return bits;
48     }
49
50     protected long doRange( int step, int field ) throws ANTLRException {
51         return doRange( LOWER_BOUNDS[field], UPPER_BOUNDS[field], step, field );
52     }
53
54     protected void rangeCheck(int value, int field) throws ANTLRException {
55         if( value<LOWER_BOUNDS[field] || UPPER_BOUNDS[field]<value ) {
56             error(value +" is an invalid value. Must be within "+
57             LOWER_BOUNDS[field]+" and "+UPPER_BOUNDS[field]);
58         }
59     }
60
61     private void error(String JavaDoc msg) throws TokenStreamException, SemanticException {
62         Token token = LT(0);
63         throw new SemanticException(
64             msg,
65             token.getFilename(),
66             token.getLine(),
67             token.getColumn()
68         );
69     }
70 }
71
Popular Tags