1 /*
2  * [The "BSD license"]
3  *  Copyright (c) 2014 Terence Parr
4  *  Copyright (c) 2014 Sam Harwell
5  *  All rights reserved.
6  *
7  *  Redistribution and use in source and binary forms, with or without
8  *  modification, are permitted provided that the following conditions
9  *  are met:
10  *
11  *  1. Redistributions of source code must retain the above copyright
12  *     notice, this list of conditions and the following disclaimer.
13  *  2. Redistributions in binary form must reproduce the above copyright
14  *     notice, this list of conditions and the following disclaimer in the
15  *     documentation and/or other materials provided with the distribution.
16  *  3. The name of the author may not be used to endorse or promote products
17  *     derived from this software without specific prior written permission.
18  *
19  *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 module antlr.v4.runtime.atn.DecisionInfo;
32 
33 import std.conv;
34 import antlr.v4.runtime.atn.LookaheadEventInfo;
35 import antlr.v4.runtime.atn.ContextSensitivityInfo;
36 import antlr.v4.runtime.atn.AmbiguityInfo;
37 import antlr.v4.runtime.atn.ErrorInfo;
38 import antlr.v4.runtime.atn.PredicateEvalInfo;
39 
40 // Class DecisionInfo
41 /**
42  * This class contains profiling gathered for a particular decision.
43  *
44  * <p>
45  * Parsing performance in ANTLR 4 is heavily influenced by both static factors
46  * (e.g. the form of the rules in the grammar) and dynamic factors (e.g. the
47  * choice of input and the state of the DFA cache at the time profiling
48  * operations are started). For best results, gather and use aggregate
49  * statistics from a large sample of inputs representing the inputs expected in
50  * production before using the results to make changes in the grammar.</p>
51  *
52  * @since 4.3
53  */
54 class DecisionInfo
55 {
56 
57     /**
58      * @uml
59      * The decision number, which is an index into {@link ATN#decisionToState}.
60      */
61     public int decision;
62 
63     public long invocations;
64 
65     /**
66      * The total time spent in {@link ParserATNSimulator#adaptivePredict} for
67      * this decision, in nanoseconds.
68      *
69      * <p>
70      * The value of this field contains the sum of differential results obtained
71      * by {@link System#nanoTime()}, and is not adjusted to compensate for JIT
72      * and/or garbage collection overhead. For best accuracy, use a modern JVM
73      * implementation that provides precise results from
74      * {@link System#nanoTime()}, and perform profiling in a separate process
75      * which is warmed up by parsing the input prior to profiling. If desired,
76      * call {@link ATNSimulator#clearDFA} to reset the DFA cache to its initial
77      * state before starting the profiling measurement pass.</p>
78      */
79     public long timeInPrediction;
80 
81     /**
82      * The sum of the lookahead required for SLL prediction for this decision.
83      * Note that SLL prediction is used before LL prediction for performance
84      * reasons even when {@link PredictionMode#LL} or
85      * {@link PredictionMode#LL_EXACT_AMBIG_DETECTION} is used.
86      */
87     public long SLL_TotalLook;
88 
89     /**
90      * Gets the minimum lookahead required for any single SLL prediction to
91      * complete for this decision, by reaching a unique prediction, reaching an
92      * SLL conflict state, or encountering a syntax error.
93      */
94     public long SLL_MinLook;
95 
96     /**
97      * Gets the maximum lookahead required for any single SLL prediction to
98      * complete for this decision, by reaching a unique prediction, reaching an
99      * SLL conflict state, or encountering a syntax error.
100      */
101     public long SLL_MaxLook;
102 
103     /**
104      * Gets the {@link LookaheadEventInfo} associated with the event where the
105      * {@link #SLL_MaxLook} value was set.
106      */
107     public LookaheadEventInfo SLL_MaxLookEvent;
108 
109     /**
110      * The sum of the lookahead required for LL prediction for this decision.
111      * Note that LL prediction is only used when SLL prediction reaches a
112      * conflict state.
113      */
114     public long LL_TotalLook;
115 
116     /**
117      * Gets the minimum lookahead required for any single LL prediction to
118      * complete for this decision. An LL prediction completes when the algorithm
119      * reaches a unique prediction, a conflict state (for
120      * {@link PredictionMode#LL}, an ambiguity state (for
121      * {@link PredictionMode#LL_EXACT_AMBIG_DETECTION}, or a syntax error.
122      */
123     public long LL_MinLook;
124 
125     /**
126      * Gets the maximum lookahead required for any single LL prediction to
127      * complete for this decision. An LL prediction completes when the algorithm
128      * reaches a unique prediction, a conflict state (for
129      * {@link PredictionMode#LL}, an ambiguity state (for
130      * {@link PredictionMode#LL_EXACT_AMBIG_DETECTION}, or a syntax error.
131      */
132     public long LL_MaxLook;
133 
134     public LookaheadEventInfo LL_MaxLookEvent;
135 
136     public ContextSensitivityInfo[] contextSensitivities;
137 
138     public ErrorInfo[] errors;
139 
140     public AmbiguityInfo[] ambiguities;
141 
142     public PredicateEvalInfo[] predicateEvals;
143 
144     /**
145      * The total number of ATN transitions required during SLL prediction for
146      * this decision. An ATN transition is determined by the number of times the
147      * DFA does not contain an edge that is required for prediction, resulting
148      * in on-the-fly computation of that edge.
149      *
150      * <p>
151      * If DFA caching of SLL transitions is employed by the implementation, ATN
152      * computation may cache the computed edge for efficient lookup during
153      * future parsing of this decision. Otherwise, the SLL parsing algorithm
154      * will use ATN transitions exclusively.</p>
155      *
156      * @see #SLL_ATNTransitions
157      * @see ParserATNSimulator#computeTargetState
158      * @see LexerATNSimulator#computeTargetState
159      */
160     public long SLL_ATNTransitions;
161 
162     /**
163      * The total number of DFA transitions required during SLL prediction for
164      * this decision.
165      *
166      * <p>If the ATN simulator implementation does not use DFA caching for SLL
167      * transitions, this value will be 0.</p>
168      *
169      * @see ParserATNSimulator#getExistingTargetState
170      * @see LexerATNSimulator#getExistingTargetState
171      */
172     public long SLL_DFATransitions;
173 
174     /**
175      * Gets the total number of times SLL prediction completed in a conflict
176      * state, resulting in fallback to LL prediction.
177      *
178      * <p>Note that this value is not related to whether or not
179      * {@link PredictionMode#SLL} may be used successfully with a particular
180      * grammar. If the ambiguity resolution algorithm applied to the SLL
181      * conflicts for this decision produce the same result as LL prediction for
182      * this decision, {@link PredictionMode#SLL} would produce the same overall
183      * parsing result as {@link PredictionMode#LL}.</p>
184      */
185     public long LL_Fallback;
186 
187     /**
188      * The total number of ATN transitions required during LL prediction for
189      * this decision. An ATN transition is determined by the number of times the
190      * DFA does not contain an edge that is required for prediction, resulting
191      * in on-the-fly computation of that edge.
192      *
193      * <p>
194      * If DFA caching of LL transitions is employed by the implementation, ATN
195      * computation may cache the computed edge for efficient lookup during
196      * future parsing of this decision. Otherwise, the LL parsing algorithm will
197      * use ATN transitions exclusively.</p>
198      *
199      * @see #LL_DFATransitions
200      * @see ParserATNSimulator#computeTargetState
201      * @see LexerATNSimulator#computeTargetState
202      */
203     public long LL_ATNTransitions;
204 
205     /**
206      * The total number of DFA transitions required during LL prediction for
207      * this decision.
208      *
209      * <p>If the ATN simulator implementation does not use DFA caching for LL
210      * transitions, this value will be 0.</p>
211      *
212      * @see ParserATNSimulator#getExistingTargetState
213      * @see LexerATNSimulator#getExistingTargetState
214      */
215     public long LL_DFATransitions;
216 
217     /**
218      * Constructs a new instance of the {@link DecisionInfo} class to contain
219      * statistics for a particular decision.
220      *
221      * @param decision The decision number
222      */
223     public this(int decision)
224     {
225 	this.decision = decision;
226     }
227 
228     /**
229      * @uml
230      * @override
231      */
232     public override string toString()
233     {
234 	return "{" ~
235             "decision=" ~ to!string(decision) ~
236             ", contextSensitivities=" ~ to!string(contextSensitivities.length) ~
237             ", errors=" ~ to!string(errors.length) ~
238             ", ambiguities=" ~ to!string(ambiguities.length) ~
239             ", SLL_lookahead=" ~ to!string(SLL_TotalLook) ~
240             ", SLL_ATNTransitions=" ~ to!string(SLL_ATNTransitions) ~
241             ", SLL_DFATransitions=" ~ to!string(SLL_DFATransitions) ~
242             ", LL_Fallback=" ~ to!string(LL_Fallback) ~
243             ", LL_lookahead=" ~ to!string(LL_TotalLook) ~
244             ", LL_ATNTransitions=" ~ to!string(LL_ATNTransitions) ~
245             '}';
246     }
247 
248 }
249 
250 unittest
251 {
252     auto decisionInfo = new DecisionInfo(0);
253 }