001    // $ANTLR 2.7.5 (20050128): "../src/csheets/ext/assertion/AssertionParser.g" -> "AssertionParser.java"$
002    package csheets.ext.assertion;
003    import java.util.List;
004    
005    import antlr.NoViableAltException;
006    import antlr.ParserSharedInputState;
007    import antlr.RecognitionException;
008    import antlr.Token;
009    import antlr.TokenBuffer;
010    import antlr.TokenStream;
011    import antlr.TokenStreamException;
012    public class AssertionParser extends antlr.LLkParser       implements AssertionParserTokenTypes
013     {
014    
015    
016    protected AssertionParser(TokenBuffer tokenBuf, int k) {
017      super(tokenBuf,k);
018      tokenNames = _tokenNames;
019    }
020    
021    public AssertionParser(TokenBuffer tokenBuf) {
022      this(tokenBuf,1);
023    }
024    
025    protected AssertionParser(TokenStream lexer, int k) {
026      super(lexer,k);
027      tokenNames = _tokenNames;
028    }
029    
030    public AssertionParser(TokenStream lexer) {
031      this(lexer,1);
032    }
033    
034    public AssertionParser(ParserSharedInputState state) {
035      super(state,1);
036      tokenNames = _tokenNames;
037    }
038    
039            public final void assertion(
040                    USAssertion ass, List<Interval> orIntervals, List<Interval> exceptIntervals
041            ) throws RecognitionException, TokenStreamException, AssertionException {
042                    
043                    
044                                    ass.isInteger = false;
045                            
046                    
047                    {
048                    switch ( LA(1)) {
049                    case LBRACK:
050                    case RBRACK:
051                    case NUMBER:
052                    case GT:
053                    case LT:
054                    case GTEQ:
055                    case LTEQ:
056                    {
057                            or_expr(orIntervals);
058                            break;
059                    }
060                    case EOF:
061                    case EXCEPT:
062                    case INTEGER:
063                    {
064                            break;
065                    }
066                    default:
067                    {
068                            throw new NoViableAltException(LT(1), getFilename());
069                    }
070                    }
071                    }
072                    {
073                    switch ( LA(1)) {
074                    case EXCEPT:
075                    {
076                            except_clause(exceptIntervals);
077                            break;
078                    }
079                    case EOF:
080                    case INTEGER:
081                    {
082                            break;
083                    }
084                    default:
085                    {
086                            throw new NoViableAltException(LT(1), getFilename());
087                    }
088                    }
089                    }
090                    {
091                    switch ( LA(1)) {
092                    case INTEGER:
093                    {
094                            is_integer(ass);
095                            break;
096                    }
097                    case EOF:
098                    {
099                            break;
100                    }
101                    default:
102                    {
103                            throw new NoViableAltException(LT(1), getFilename());
104                    }
105                    }
106                    }
107                    match(Token.EOF_TYPE);
108            }
109            
110            public final void or_expr(
111                    List<Interval> lst
112            ) throws RecognitionException, TokenStreamException, AssertionException {
113                    
114                    
115                    term(lst);
116                    {
117                    _loop10:
118                    do {
119                            if ((LA(1)==OR)) {
120                                    match(OR);
121                                    term(lst);
122                            }
123                            else {
124                                    break _loop10;
125                            }
126                            
127                    } while (true);
128                    }
129            }
130            
131            public final void except_clause(
132                    List<Interval> lst
133            ) throws RecognitionException, TokenStreamException, AssertionException {
134                    
135                    
136                    match(EXCEPT);
137                    term(lst);
138                    {
139                    _loop7:
140                    do {
141                            if ((LA(1)==COMMA)) {
142                                    match(COMMA);
143                                    term(lst);
144                            }
145                            else {
146                                    break _loop7;
147                            }
148                            
149                    } while (true);
150                    }
151            }
152            
153            public final void is_integer(
154                    USAssertion ass
155            ) throws RecognitionException, TokenStreamException {
156                    
157                    
158                    match(INTEGER);
159                    
160                                    ass.isInteger = true;
161                            
162            }
163            
164            public final void term(
165                    List<Interval> lst
166            ) throws RecognitionException, TokenStreamException, AssertionException {
167                    
168                    
169                                    double c; 
170                            
171                    
172                    switch ( LA(1)) {
173                    case LBRACK:
174                    case RBRACK:
175                    {
176                            interval(lst);
177                            break;
178                    }
179                    case NUMBER:
180                    {
181                            c=constant();
182                            
183                                                    try {
184                                                            lst.add(new Interval(c));
185                                                    } catch (IllegalArgumentException iae) {
186                                                            // This should never happen since c should be a good value.
187                                                            throw new AssertionException("Invalid constant specified in assertion");
188                                                    }
189                                            
190                            break;
191                    }
192                    case GT:
193                    case LT:
194                    case GTEQ:
195                    case LTEQ:
196                    {
197                            os_interval(lst);
198                            break;
199                    }
200                    default:
201                    {
202                            throw new NoViableAltException(LT(1), getFilename());
203                    }
204                    }
205            }
206            
207            public final void interval(
208                    List<Interval> lst
209            ) throws RecognitionException, TokenStreamException, AssertionException {
210                    
211                    Token  ltok = null;
212                    Token  rtok = null;
213                    
214                                    double lower = 0.0, upper = 0.0;
215                                    boolean lclosed = false, rclosed = false;
216                            
217                    
218                    {
219                    switch ( LA(1)) {
220                    case LBRACK:
221                    {
222                            ltok = LT(1);
223                            match(LBRACK);
224                            lclosed = true;
225                            break;
226                    }
227                    case RBRACK:
228                    {
229                            rtok = LT(1);
230                            match(RBRACK);
231                            break;
232                    }
233                    default:
234                    {
235                            throw new NoViableAltException(LT(1), getFilename());
236                    }
237                    }
238                    }
239                    lower=constant();
240                    match(TO);
241                    upper=constant();
242                    {
243                    switch ( LA(1)) {
244                    case LBRACK:
245                    {
246                            match(LBRACK);
247                            break;
248                    }
249                    case RBRACK:
250                    {
251                            match(RBRACK);
252                            rclosed=true;
253                            break;
254                    }
255                    default:
256                    {
257                            throw new NoViableAltException(LT(1), getFilename());
258                    }
259                    }
260                    }
261                    
262                                            Token tok;
263                                            if (lclosed)
264                                                    tok = ltok;
265                                            else    
266                                                    tok = rtok;
267                                            try {
268                                                    lst.add(new Interval(lower, upper, lclosed, rclosed));
269                                            }
270                                            catch (IllegalArgumentException iae) {
271                                                    
272                                                    throw new AssertionException(tok.getLine(), tok.getColumn(), iae.getMessage());
273                                            }
274                                                    
275                                    
276            }
277            
278            public final double  constant() throws RecognitionException, TokenStreamException {
279                    double value;
280                    
281                    Token  r = null;
282                    
283                                    value = 0;
284                            
285                    
286                    r = LT(1);
287                    match(NUMBER);
288                    
289                                            value = Double.parseDouble(r.getText());
290                                    
291                    return value;
292            }
293            
294            public final void os_interval(
295                    List<Interval> lst
296            ) throws RecognitionException, TokenStreamException, AssertionException {
297                    
298                    Token  gttok = null;
299                    Token  lttok = null;
300                    Token  gteqtok = null;
301                    Token  lteqtok = null;
302                    
303                                    double c = 0.0;
304                                    boolean eq = false, gt = true;
305                            
306                    
307                    {
308                    switch ( LA(1)) {
309                    case GT:
310                    {
311                            gttok = LT(1);
312                            match(GT);
313                            break;
314                    }
315                    case LT:
316                    {
317                            lttok = LT(1);
318                            match(LT);
319                            gt = false;
320                            break;
321                    }
322                    case GTEQ:
323                    {
324                            gteqtok = LT(1);
325                            match(GTEQ);
326                            eq = true;
327                            break;
328                    }
329                    case LTEQ:
330                    {
331                            lteqtok = LT(1);
332                            match(LTEQ);
333                            eq=true;gt=false;
334                            break;
335                    }
336                    default:
337                    {
338                            throw new NoViableAltException(LT(1), getFilename());
339                    }
340                    }
341                    }
342                    c=constant();
343                    
344                                    Token tok = (gt ? (eq ? gteqtok : gttok) : (eq ? lteqtok : lttok));
345                                    try {
346                                            if (gt) {
347                                                    lst.add(new Interval(c, Double.POSITIVE_INFINITY, eq, false));
348                                            } else {
349                                                    lst.add(new Interval(Double.NEGATIVE_INFINITY, c, false, eq));
350                                            }
351                                    } catch (IllegalArgumentException iae) {
352                                            throw new AssertionException(tok.getLine(), tok.getColumn(), iae.toString());
353                                    }
354                            
355            }
356            
357            
358            public static final String[] _tokenNames = {
359                    "<0>",
360                    "EOF",
361                    "<2>",
362                    "NULL_TREE_LOOKAHEAD",
363                    "EXCEPT",
364                    "','",
365                    "OR",
366                    "'['",
367                    "']'",
368                    "TO",
369                    "a numeric constant",
370                    "'>'",
371                    "'<'",
372                    "'>='",
373                    "'<='",
374                    "INTEGER",
375                    "end of line",
376                    "whitespace",
377                    "DIGIT",
378                    "'+'",
379                    "'-'",
380                    "EXPONENT"
381            };
382            
383            
384            }