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 /**
40  * This class provides access to specific and aggregate statistics gathered
41  * during profiling of a parser.
42  */
43 class ParseInfo
44 {
45 
46     protected ProfilingATNSimulator atnSimulator;
47 
48     public this(ProfilingATNSimulator atnSimulator)
49     {
50 	this.atnSimulator = atnSimulator;
51     }
52 
53     /**
54      * Gets an array of {@link DecisionInfo} instances containing the profiling
55      * information gathered for each decision in the ATN.
56      *
57      * @return An array of {@link DecisionInfo} instances, indexed by decision
58      * number.
59      */
60     public DecisionInfo[] getDecisionInfo()
61     {
62 	return atnSimulator.getDecisionInfo();
63     }
64 
65     /**
66      * Gets the decision numbers for decisions that required one or more
67      * full-context predictions during parsing. These are decisions for which
68      * {@link DecisionInfo#LL_Fallback} is non-zero.
69      *
70      * @return A list of decision numbers which required one or more
71      * full-context predictions during parsing.
72      */
73     public int[] getLLDecisions()
74     {
75 	DecisionInfo[] decisions = atnSimulator.getDecisionInfo();
76         int[] LL;
77         for (int i=0; i<decisions.length; i++) {
78             long fallBack = decisions[i].LL_Fallback;
79             if ( fallBack>0 ) LL ~= i;
80         }
81         return LL;
82     }
83 
84     /**
85      * Gets the total time spent during prediction across all decisions made
86      * during parsing. This value is the sum of
87      * {@link DecisionInfo#timeInPrediction} for all decisions.
88      */
89     public long getTotalTimeInPrediction()
90     {
91 	DecisionInfo[] decisions = atnSimulator.getDecisionInfo();
92         long t = 0;
93         for (int i=0; i<decisions.length; i++) {
94             t += decisions[i].timeInPrediction;
95         }
96         return t;
97     }
98 
99     /**
100      * Gets the total number of SLL lookahead operations across all decisions
101      * made during parsing. This value is the sum of
102      * {@link DecisionInfo#SLL_TotalLook} for all decisions.
103      */
104     public long getTotalSLLLookaheadOps()
105     {
106 
107         DecisionInfo[] decisions = atnSimulator.getDecisionInfo();
108         long k = 0;
109         for (int i = 0; i < decisions.length; i++) {
110             k += decisions[i].SLL_TotalLook;
111         }
112         return k;
113     }
114 
115     /**
116      * Gets the total number of LL lookahead operations across all decisions
117      * made during parsing. This value is the sum of
118      * {@link DecisionInfo#LL_TotalLook} for all decisions.
119      */
120     public long getTotalLLLookaheadOps()
121     {
122         DecisionInfo[] decisions = atnSimulator.getDecisionInfo();
123         long k = 0;
124         for (int i = 0; i < decisions.length; i++) {
125             k += decisions[i].LL_TotalLook;
126         }
127         return k;
128     }
129 
130     /**
131      * Gets the total number of ATN lookahead operations for SLL prediction
132      * across all decisions made during parsing.
133      */
134     public long getTotalSLLATNLookaheadOps()
135     {
136         DecisionInfo[] decisions = atnSimulator.getDecisionInfo();
137         long k = 0;
138         for (int i = 0; i < decisions.length; i++) {
139             k += decisions[i].SLL_ATNTransitions;
140         }
141         return k;
142     }
143 
144     /**
145      * Gets the total number of ATN lookahead operations for LL prediction
146      * across all decisions made during parsing.
147      */
148     public long getTotalLLATNLookaheadOps()
149     {
150 	DecisionInfo[] decisions = atnSimulator.getDecisionInfo();
151         long k = 0;
152         for (int i = 0; i < decisions.length; i++) {
153             k += decisions[i].LL_ATNTransitions;
154         }
155         return k;
156     }
157 
158     /**
159      * Gets the total number of ATN lookahead operations for SLL and LL
160      * prediction across all decisions made during parsing.
161      *
162      * <p>
163      * This value is the sum of {@link #getTotalSLLATNLookaheadOps} and
164      * {@link #getTotalLLATNLookaheadOps}.</p>
165      */
166     public long getTotalATNLookaheadOps()
167     {
168 	DecisionInfo[] decisions = atnSimulator.getDecisionInfo();
169         long k = 0;
170         for (int i = 0; i < decisions.length; i++) {
171             k += decisions[i].SLL_ATNTransitions;
172             k += decisions[i].LL_ATNTransitions;
173         }
174         return k;
175     }
176 
177     /**
178      * Gets the total number of DFA states stored in the DFA cache for all
179      * decisions in the ATN.
180      */
181     public int getDFASize()
182     {
183 	int n = 0;
184         DFA[] decisionToDFA = atnSimulator.decisionToDFA;
185         for (int i = 0; i < decisionToDFA.length; i++) {
186             n += getDFASize(i);
187         }
188         return n;
189     }
190 
191     /**
192      * Gets the total number of DFA states stored in the DFA cache for a
193      * particular decision.
194      */
195     public int getDFASize(int decision)
196     {
197         DFA decisionToDFA = atnSimulator.decisionToDFA[decision];
198         return to!int(decisionToDFA.states.length);
199     }
200 
201 }