11package twg2 .parser .codeParser .analytics ;
22
33import java .io .IOException ;
4- import java .util .EnumMap ;
4+ import java .util .ArrayList ;
5+ import java .util .Collection ;
6+ import java .util .List ;
57import java .util .Map ;
68
79import twg2 .io .files .FileUtil ;
810import twg2 .io .json .stringify .JsonStringify ;
11+ import twg2 .parser .condition .text .CharParser ;
912import twg2 .parser .output .JsonWritableSig ;
1013import twg2 .parser .output .WriteSettings ;
1114import twg2 .text .stringEscape .StringEscapeJson ;
1215import twg2 .text .stringUtils .StringPad ;
13- import twg2 .text .tokenizer .analytics .ParserAction ;
14- import twg2 .text .tokenizer .analytics .TypedLogger ;
16+ import twg2 .text .tokenizer .CharParserFactory ;
17+ import twg2 .text .tokenizer .CharParserMatchableFactory ;
18+ import twg2 .text .tokenizer .analytics .TokenizationLogger ;
1519
1620/**
1721 * @author TeamworkGuy2
1822 * @since 2016-09-11
1923 */
20- public class ParserActionLogger implements TypedLogger <ParserAction , WriteSettings >, JsonWritableSig {
21-
22- static class CountAndDurationLog {
23- String msg ;
24- long count ;
25- double durationMilliseconds ;
26-
27-
28- @ Override
29- public String toString () {
30- return (this .msg != null ? "msg: " + this .msg + ", " : "" ) +
31- (this .count != 0 ? "count: " + this .count + ", " : "" ) +
32- (this .durationMilliseconds != 0 ? "durationMillis: " + this .durationMilliseconds : "" );
33- }
34-
35-
36- public void toJson (Appendable dst , WriteSettings st ) throws IOException {
37- var json = JsonStringify .inst ;
38-
39- json .append ("{ " , dst );
40-
41- if (this .msg != null ) {
42- json .comma (dst ).toProp ("message" , this .msg , dst );
43- }
44-
45- if (this .count != 0 ) {
46- json .comma (dst ).toProp ("count" , this .count , dst );
47- }
48-
49- if (this .durationMilliseconds != 0 ) {
50- json .comma (dst ).toProp ("durationMillis" , this .durationMilliseconds , dst );
51- }
52-
53- json .append (" }" , dst );
54- }
55-
56- }
57-
58-
59- Map <ParserAction , CountAndDurationLog > actions ;
24+ public class ParserActionLogger implements TokenizationLogger , JsonWritableSig {
25+ public int countCompoundCharParserMatch ;
26+ public int countCompoundCharParserAcceptNext ;
27+ public int countCreateParser ;
28+ public int countTextFragmentsConsumed ;
29+ public int totalParserReuseCount ;
30+ public List <CharParserMatchableFactory .Reusable <CharParser >> reusableParserFactories ;
6031
6132
6233 public ParserActionLogger () {
63- this .actions = new EnumMap <>(ParserAction .class );
64- }
65-
66-
67- public CountAndDurationLog getLog (ParserAction action ) {
68- return this .actions .get (action );
6934 }
7035
7136
72- public String getLogMsg (ParserAction action ) {
73- var res = this .actions .get (action );
74- return res != null ? res .msg : null ;
75- }
76-
77-
78- public long getLogCount (ParserAction action ) {
79- var res = this .actions .get (action );
80- return res != null ? res .count : 0 ;
37+ @ Override
38+ public void logCountCompoundCharParserMatch (int count ) {
39+ countCompoundCharParserMatch += count ;
8140 }
8241
8342
84- public double getLogDuration ( ParserAction action ) {
85- var res = this . actions . get ( action );
86- return res != null ? res . durationMilliseconds : 0 ;
43+ @ Override
44+ public void logCountCompoundCharParserAcceptNext ( int count ) {
45+ countCompoundCharParserAcceptNext += count ;
8746 }
8847
8948
9049 @ Override
91- public void logMsg ( ParserAction action , String msg ) {
92- getTypedAction ( action ). msg = msg ;
50+ public void logCountCreateParser ( int count ) {
51+ countCreateParser += count ;
9352 }
9453
9554
9655 @ Override
97- public void logCount ( ParserAction action , long count ) {
98- getTypedAction ( action ). count += count ;
56+ public void logCountTextFragmentsConsumed ( int count ) {
57+ countTextFragmentsConsumed += count ;
9958 }
10059
10160
102- @ Override
103- public void logDuration (ParserAction action , double durationMilliseconds ) {
104- getTypedAction (action ).durationMilliseconds += durationMilliseconds ;
61+ public void logCharParserFactoryReuse (Collection <? extends CharParserFactory > charParserFactories ) {
62+ this .reusableParserFactories = new ArrayList <>();
63+
64+ for (var parserFactory : charParserFactories ) {
65+ if (parserFactory instanceof CharParserMatchableFactory .Reusable ) {
66+ @ SuppressWarnings ("unchecked" )
67+ var parserFactoryReusable = (CharParserMatchableFactory .Reusable <CharParser >)parserFactory ;
68+ reusableParserFactories .add (parserFactoryReusable );
69+ totalParserReuseCount += parserFactoryReusable .getReuseCount ();
70+ }
71+ }
10572 }
10673
10774
@@ -111,7 +78,6 @@ public void toJson(Appendable dst, WriteSettings st) throws IOException {
11178 }
11279
11380
114- @ Override
11581 public void toJson (String srcName , boolean includeSurroundingBrackets , Appendable dst , WriteSettings st ) throws IOException {
11682 if (includeSurroundingBrackets ) { dst .append ("{\n " ); }
11783 if (srcName != null ) {
@@ -120,16 +86,20 @@ public void toJson(String srcName, boolean includeSurroundingBrackets, Appendabl
12086 dst .append ("\" " );
12187 }
12288
123- boolean first = true ;
124- for (ParserAction action : ParserAction .values ()) {
125- var data = this .actions .get (action );
126- if (data != null ) {
127- if (!first || srcName != null ) { dst .append (",\n " ); }
128- String name = action .name ();
129- dst .append ("\t \" " ).append (name ).append ("\" : " );
130- data .toJson (dst , st );
131- first = false ;
132- }
89+ if (countCompoundCharParserMatch > 0 ) {
90+ dst .append ("\t \" compoundCharParserMatch\" : " ).append (Integer .toString (countCompoundCharParserMatch ));
91+ }
92+ if (countCompoundCharParserAcceptNext > 0 ) {
93+ dst .append (",\n " );
94+ dst .append ("\t \" compoundCharParserAcceptNext\" : " ).append (Integer .toString (countCompoundCharParserAcceptNext ));
95+ }
96+ if (countCreateParser > 0 ) {
97+ dst .append (",\n " );
98+ dst .append ("\t \" createParser\" : " ).append (Integer .toString (countCreateParser ));
99+ }
100+ if (countTextFragmentsConsumed > 0 ) {
101+ dst .append (",\n " );
102+ dst .append ("\t \" textFragmentsConsumed\" : " ).append (Integer .toString (countTextFragmentsConsumed ));
133103 }
134104
135105 if (includeSurroundingBrackets ) { dst .append ("\n }" ); }
@@ -142,7 +112,6 @@ public String toString() {
142112 }
143113
144114
145- @ Override
146115 public String toString (String srcName , boolean includeClassName ) {
147116 var dst = new StringBuilder ();
148117 if (includeClassName ) {
@@ -152,40 +121,38 @@ public String toString(String srcName, boolean includeClassName) {
152121 dst .append ("file: " + srcName );
153122 }
154123
155- boolean first = true ;
156- for (ParserAction action : ParserAction .values ()) {
157- var data = this .actions .get (action );
158- if (data != null ) {
159- if (!first || srcName != null ) { dst .append (", " ); }
160- String name = action .name ();
161- dst .append ("\" " ).append (name ).append ("\" : " ).append (data .toString ());
162- first = false ;
163- }
124+ if (countCompoundCharParserMatch > 0 ) {
125+ dst .append ("compoundCharParserMatch: " ).append (Integer .toString (countCompoundCharParserMatch ));
126+ }
127+ if (countCompoundCharParserAcceptNext > 0 ) {
128+ dst .append (", " );
129+ dst .append ("compoundCharParserAcceptNext: " ).append (Integer .toString (countCompoundCharParserAcceptNext ));
130+ }
131+ if (countCreateParser > 0 ) {
132+ dst .append (", " );
133+ dst .append ("createParser: " ).append (Integer .toString (countCreateParser ));
164134 }
135+ if (countTextFragmentsConsumed > 0 ) {
136+ dst .append (", " );
137+ dst .append ("textFragmentsConsumed: " ).append (Integer .toString (countTextFragmentsConsumed ));
138+ }
139+
165140 if (includeClassName ) {
166141 dst .append ("\n }" );
167142 }
168143 return dst .toString ();
169144 }
170145
171146
172- private CountAndDurationLog getTypedAction (ParserAction action ) {
173- var data = actions .get (action );
174- if (data == null ) {
175- data = new CountAndDurationLog ();
176- actions .put (action , data );
177- }
178- return data ;
179- }
180-
181-
182147 public static void toJsons (Map <String , ParserActionLogger > fileParserDetails , boolean includeSurroundingBrackets , Appendable dst , WriteSettings st ) throws IOException {
183148 if (includeSurroundingBrackets ) { dst .append ("[\n " ); }
149+
184150 JsonStringify .inst .joinConsume (fileParserDetails .entrySet (), ",\n " , dst , (entry ) -> {
185151 var stat = entry .getValue ();
186152 String fileName = FileUtil .getFileNameWithoutExtension (entry .getKey ());
187153 stat .toJson (fileName , includeSurroundingBrackets , dst , st );
188154 });
155+
189156 if (includeSurroundingBrackets ) { dst .append ("\n ]" ); }
190157 }
191158
0 commit comments