1 /*
2  * Copyright (c) 2012-2019 The ANTLR Project. All rights reserved.
3  * Use of this file is governed by the BSD 3-clause license that
4  * can be found in the LICENSE.txt file in the project root.
5  */
6 
7 module antlr.v4.runtime.atn.DecisionInfo;
8 
9 import std.conv;
10 import antlr.v4.runtime.atn.LookaheadEventInfo;
11 import antlr.v4.runtime.atn.ContextSensitivityInfo;
12 import antlr.v4.runtime.atn.AmbiguityInfo;
13 import antlr.v4.runtime.atn.ErrorInfo;
14 import antlr.v4.runtime.atn.PredicateEvalInfo;
15 
16 /**
17  * This class contains profiling gathered for a particular decision.
18  *
19  * <p>
20  * Parsing performance in ANTLR 4 is heavily influenced by both static factors
21  * (e.g. the form of the rules in the grammar) and dynamic factors (e.g. the
22  * choice of input and the state of the DFA cache at the time profiling
23  * operations are started). For best results, gather and use aggregate
24  * statistics from a large sample of inputs representing the inputs expected in
25  * production before using the results to make changes in the grammar.</p>
26  *
27  * @since 4.3
28  */
29 class DecisionInfo
30 {
31 
32     /**
33      * @uml
34      * The decision number, which is an index into {@link ATN#decisionToState}.
35      */
36     public int decision;
37 
38     public long invocations;
39 
40     /**
41      * The total time spent in {@link ParserATNSimulator#adaptivePredict} for
42      * this decision, in nanoseconds.
43      *
44      * <p>
45      * The value of this field contains the sum of differential results obtained
46      * by {@link System#nanoTime()}, and is not adjusted to compensate for JIT
47      * and/or garbage collection overhead. For best accuracy, use a modern JVM
48      * implementation that provides precise results from
49      * {@link System#nanoTime()}, and perform profiling in a separate process
50      * which is warmed up by parsing the input prior to profiling. If desired,
51      * call {@link ATNSimulator#clearDFA} to reset the DFA cache to its initial
52      * state before starting the profiling measurement pass.</p>
53      */
54     public long timeInPrediction;
55 
56     /**
57      * The sum of the lookahead required for SLL prediction for this decision.
58      * Note that SLL prediction is used before LL prediction for performance
59      * reasons even when {@link PredictionMode#LL} or
60      * {@link PredictionMode#LL_EXACT_AMBIG_DETECTION} is used.
61      */
62     public long SLL_TotalLook;
63 
64     /**
65      * Gets the minimum lookahead required for any single SLL prediction to
66      * complete for this decision, by reaching a unique prediction, reaching an
67      * SLL conflict state, or encountering a syntax error.
68      */
69     public long SLL_MinLook;
70 
71     /**
72      * Gets the maximum lookahead required for any single SLL prediction to
73      * complete for this decision, by reaching a unique prediction, reaching an
74      * SLL conflict state, or encountering a syntax error.
75      */
76     public long SLL_MaxLook;
77 
78     /**
79      * Gets the {@link LookaheadEventInfo} associated with the event where the
80      * {@link #SLL_MaxLook} value was set.
81      */
82     public LookaheadEventInfo SLL_MaxLookEvent;
83 
84     /**
85      * The sum of the lookahead required for LL prediction for this decision.
86      * Note that LL prediction is only used when SLL prediction reaches a
87      * conflict state.
88      */
89     public long LL_TotalLook;
90 
91     /**
92      * Gets the minimum lookahead required for any single LL prediction to
93      * complete for this decision. An LL prediction completes when the algorithm
94      * reaches a unique prediction, a conflict state (for
95      * {@link PredictionMode#LL}), an ambiguity state (for
96      * {@link PredictionMode#LL_EXACT_AMBIG_DETECTION}), or a syntax error.
97      */
98     public long LL_MinLook;
99 
100     /**
101      * Gets the maximum lookahead required for any single LL prediction to
102      * complete for this decision. An LL prediction completes when the algorithm
103      * reaches a unique prediction, a conflict state (for
104      * {@link PredictionMode#LL}), an ambiguity state (for
105      * {@link PredictionMode#LL_EXACT_AMBIG_DETECTION}), or a syntax error.
106      */
107     public long LL_MaxLook;
108 
109     public LookaheadEventInfo LL_MaxLookEvent;
110 
111     public ContextSensitivityInfo[] contextSensitivities;
112 
113     public ErrorInfo[] errors;
114 
115     public AmbiguityInfo[] ambiguities;
116 
117     public PredicateEvalInfo[] predicateEvals;
118 
119     /**
120      * The total number of ATN transitions required during SLL prediction for
121      * this decision. An ATN transition is determined by the number of times the
122      * DFA does not contain an edge that is required for prediction, resulting
123      * in on-the-fly computation of that edge.
124      *
125      * <p>
126      * If DFA caching of SLL transitions is employed by the implementation, ATN
127      * computation may cache the computed edge for efficient lookup during
128      * future parsing of this decision. Otherwise, the SLL parsing algorithm
129      * will use ATN transitions exclusively.</p>
130      *
131      * @see #SLL_ATNTransitions
132      * @see ParserATNSimulator#computeTargetState
133      * @see LexerATNSimulator#computeTargetState
134      */
135     public long SLL_ATNTransitions;
136 
137     /**
138      * The total number of DFA transitions required during SLL prediction for
139      * this decision.
140      *
141      * <p>If the ATN simulator implementation does not use DFA caching for SLL
142      * transitions, this value will be 0.</p>
143      *
144      * @see ParserATNSimulator#getExistingTargetState
145      * @see LexerATNSimulator#getExistingTargetState
146      */
147     public long SLL_DFATransitions;
148 
149     /**
150      * Gets the total number of times SLL prediction completed in a conflict
151      * state, resulting in fallback to LL prediction.
152      *
153      * <p>Note that this value is not related to whether or not
154      * {@link PredictionMode#SLL} may be used successfully with a particular
155      * grammar. If the ambiguity resolution algorithm applied to the SLL
156      * conflicts for this decision produce the same result as LL prediction for
157      * this decision, {@link PredictionMode#SLL} would produce the same overall
158      * parsing result as {@link PredictionMode#LL}.</p>
159      */
160     public long LL_Fallback;
161 
162     /**
163      * The total number of ATN transitions required during LL prediction for
164      * this decision. An ATN transition is determined by the number of times the
165      * DFA does not contain an edge that is required for prediction, resulting
166      * in on-the-fly computation of that edge.
167      *
168      * <p>
169      * If DFA caching of LL transitions is employed by the implementation, ATN
170      * computation may cache the computed edge for efficient lookup during
171      * future parsing of this decision. Otherwise, the LL parsing algorithm will
172      * use ATN transitions exclusively.</p>
173      *
174      * @see #LL_DFATransitions
175      * @see ParserATNSimulator#computeTargetState
176      * @see LexerATNSimulator#computeTargetState
177      */
178     public long LL_ATNTransitions;
179 
180     /**
181      * The total number of DFA transitions required during LL prediction for
182      * this decision.
183      *
184      * <p>If the ATN simulator implementation does not use DFA caching for LL
185      * transitions, this value will be 0.</p>
186      *
187      * @see ParserATNSimulator#getExistingTargetState
188      * @see LexerATNSimulator#getExistingTargetState
189      */
190     public long LL_DFATransitions;
191 
192     /**
193      * Constructs a new instance of the {@link DecisionInfo} class to contain
194      * statistics for a particular decision.
195      *
196      * @param decision The decision number
197      */
198     public this(int decision)
199     {
200 	this.decision = decision;
201     }
202 
203     /**
204      * @uml
205      * @override
206      */
207     public override string toString()
208     {
209 	return "{" ~
210             "decision=" ~ to!string(decision) ~
211             ", contextSensitivities=" ~ to!string(contextSensitivities.length) ~
212             ", errors=" ~ to!string(errors.length) ~
213             ", ambiguities=" ~ to!string(ambiguities.length) ~
214             ", SLL_lookahead=" ~ to!string(SLL_TotalLook) ~
215             ", SLL_ATNTransitions=" ~ to!string(SLL_ATNTransitions) ~
216             ", SLL_DFATransitions=" ~ to!string(SLL_DFATransitions) ~
217             ", LL_Fallback=" ~ to!string(LL_Fallback) ~
218             ", LL_lookahead=" ~ to!string(LL_TotalLook) ~
219             ", LL_ATNTransitions=" ~ to!string(LL_ATNTransitions) ~
220             '}';
221     }
222 
223 }
224 
225 unittest
226 {
227     auto decisionInfo = new DecisionInfo(0);
228 }