1 module antlr.v4.runtime.FailedPredicateException;
2 
3 import std.format;
4 import antlr.v4.runtime.RecognitionException;
5 import antlr.v4.runtime.Token;
6 import antlr.v4.runtime.Parser;
7 import antlr.v4.runtime.atn.ATNState;
8 import antlr.v4.runtime.atn.AbstractPredicateTransition;
9 import antlr.v4.runtime.atn.PredicateTransition;
10 
11 /**
12  * TODO add class description
13  */
14 class FailedPredicateException : RecognitionException
15 {
16 
17     private int ruleIndex;
18 
19     private int predicateIndex;
20 
21     private string predicate;
22 
23     public this(Parser recognizer)
24     {
25 	this(recognizer, null);
26     }
27 
28     public this(Parser recognizer, string predicate)
29     {
30         this(recognizer, predicate, null);
31     }
32 
33     public this(Parser recognizer, string predicate, string message)
34     {
35 	super(formatMessage(predicate, message), recognizer, recognizer.getInputStream(), recognizer.ctx_);
36         ATNState s = recognizer.getInterpreter().atn.states[recognizer.getState];
37 
38         AbstractPredicateTransition trans = cast(AbstractPredicateTransition)s.transition(0);
39         if (trans.classinfo == PredicateTransition.classinfo) {
40             this.ruleIndex = (cast(PredicateTransition)trans).ruleIndex;
41             this.predicateIndex = (cast(PredicateTransition)trans).predIndex;
42         }
43         else {
44             this.ruleIndex = 0;
45             this.predicateIndex = 0;
46         }
47 
48         this.predicate = predicate;
49         this.setOffendingToken(recognizer.getCurrentToken());
50 
51     }
52 
53     public int getRuleIndex()
54     {
55 	return ruleIndex;
56     }
57 
58     public int getPredIndex()
59     {
60         return predicateIndex;
61     }
62 
63     public string getPredicate()
64     {
65         return predicate;
66     }
67 
68     private static string formatMessage(string predicate, string message)
69     {
70         if (message !is null) {
71             return message;
72         }
73         return format("failed predicate: {%s}?", predicate);
74     }
75 
76 }