1 /*
2  * [The "BSD license"]
3  *  Copyright (c) 2014 Terence Parr
4  *  Copyright (c) 2014 Sam Harwell
5  *  Copyright (c) 2017 Egbert Voigt
6  *  All rights reserved.
7  *
8  *  Redistribution and use in source and binary forms, with or without
9  *  modification, are permitted provided that the following conditions
10  *  are met:
11  *
12  *  1. Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *  2. Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *  3. The name of the author may not be used to endorse or promote products
18  *     derived from this software without specific prior written permission.
19  *
20  *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 module antlr.v4.runtime.atn.ParseInfo;
33 
34 import std.conv;
35 import antlr.v4.runtime.dfa.DFA;
36 import antlr.v4.runtime.atn.DecisionInfo;
37 import antlr.v4.runtime.atn.ProfilingATNSimulator;
38 
39 // Class ParseInfo
40 /**
41  * This class provides access to specific and aggregate statistics gathered
42  * during profiling of a parser.
43  */
44 class ParseInfo
45 {
46 
47     protected ProfilingATNSimulator atnSimulator;
48 
49     public this(ProfilingATNSimulator atnSimulator)
50     {
51 	this.atnSimulator = atnSimulator;
52     }
53 
54     /**
55      * Gets an array of {@link DecisionInfo} instances containing the profiling
56      * information gathered for each decision in the ATN.
57      *
58      * @return An array of {@link DecisionInfo} instances, indexed by decision
59      * number.
60      */
61     public DecisionInfo[] getDecisionInfo()
62     {
63 	return atnSimulator.getDecisionInfo();
64     }
65 
66     /**
67      * Gets the decision numbers for decisions that required one or more
68      * full-context predictions during parsing. These are decisions for which
69      * {@link DecisionInfo#LL_Fallback} is non-zero.
70      *
71      * @return A list of decision numbers which required one or more
72      * full-context predictions during parsing.
73      */
74     public int[] getLLDecisions()
75     {
76 	DecisionInfo[] decisions = atnSimulator.getDecisionInfo();
77         int[] LL;
78         for (int i=0; i<decisions.length; i++) {
79             long fallBack = decisions[i].LL_Fallback;
80             if ( fallBack>0 ) LL ~= i;
81         }
82         return LL;
83     }
84 
85     /**
86      * Gets the total time spent during prediction across all decisions made
87      * during parsing. This value is the sum of
88      * {@link DecisionInfo#timeInPrediction} for all decisions.
89      */
90     public long getTotalTimeInPrediction()
91     {
92 	DecisionInfo[] decisions = atnSimulator.getDecisionInfo();
93         long t = 0;
94         for (int i=0; i<decisions.length; i++) {
95             t += decisions[i].timeInPrediction;
96         }
97         return t;
98     }
99 
100     /**
101      * Gets the total number of SLL lookahead operations across all decisions
102      * made during parsing. This value is the sum of
103      * {@link DecisionInfo#SLL_TotalLook} for all decisions.
104      */
105     public long getTotalSLLLookaheadOps()
106     {
107 
108         DecisionInfo[] decisions = atnSimulator.getDecisionInfo();
109         long k = 0;
110         for (int i = 0; i < decisions.length; i++) {
111             k += decisions[i].SLL_TotalLook;
112         }
113         return k;
114     }
115 
116     /**
117      * Gets the total number of LL lookahead operations across all decisions
118      * made during parsing. This value is the sum of
119      * {@link DecisionInfo#LL_TotalLook} for all decisions.
120      */
121     public long getTotalLLLookaheadOps()
122     {
123         DecisionInfo[] decisions = atnSimulator.getDecisionInfo();
124         long k = 0;
125         for (int i = 0; i < decisions.length; i++) {
126             k += decisions[i].LL_TotalLook;
127         }
128         return k;
129     }
130 
131     /**
132      * Gets the total number of ATN lookahead operations for SLL prediction
133      * across all decisions made during parsing.
134      */
135     public long getTotalSLLATNLookaheadOps()
136     {
137         DecisionInfo[] decisions = atnSimulator.getDecisionInfo();
138         long k = 0;
139         for (int i = 0; i < decisions.length; i++) {
140             k += decisions[i].SLL_ATNTransitions;
141         }
142         return k;
143     }
144 
145     /**
146      * Gets the total number of ATN lookahead operations for LL prediction
147      * across all decisions made during parsing.
148      */
149     public long getTotalLLATNLookaheadOps()
150     {
151 	DecisionInfo[] decisions = atnSimulator.getDecisionInfo();
152         long k = 0;
153         for (int i = 0; i < decisions.length; i++) {
154             k += decisions[i].LL_ATNTransitions;
155         }
156         return k;
157     }
158 
159     /**
160      * Gets the total number of ATN lookahead operations for SLL and LL
161      * prediction across all decisions made during parsing.
162      *
163      * <p>
164      * This value is the sum of {@link #getTotalSLLATNLookaheadOps} and
165      * {@link #getTotalLLATNLookaheadOps}.</p>
166      */
167     public long getTotalATNLookaheadOps()
168     {
169 	DecisionInfo[] decisions = atnSimulator.getDecisionInfo();
170         long k = 0;
171         for (int i = 0; i < decisions.length; i++) {
172             k += decisions[i].SLL_ATNTransitions;
173             k += decisions[i].LL_ATNTransitions;
174         }
175         return k;
176     }
177 
178     /**
179      * Gets the total number of DFA states stored in the DFA cache for all
180      * decisions in the ATN.
181      */
182     public int getDFASize()
183     {
184 	int n = 0;
185         DFA[] decisionToDFA = atnSimulator.decisionToDFA;
186         for (int i = 0; i < decisionToDFA.length; i++) {
187             n += getDFASize(i);
188         }
189         return n;
190     }
191 
192     /**
193      * Gets the total number of DFA states stored in the DFA cache for a
194      * particular decision.
195      */
196     public int getDFASize(int decision)
197     {
198         DFA decisionToDFA = atnSimulator.decisionToDFA[decision];
199         return to!int(decisionToDFA.states.length);
200     }
201 
202 }