1
1
package twg2 .parser .codeParser .analytics ;
2
2
3
3
import java .io .IOException ;
4
- import java .util .EnumMap ;
4
+ import java .util .ArrayList ;
5
+ import java .util .Collection ;
6
+ import java .util .List ;
5
7
import java .util .Map ;
6
8
7
9
import twg2 .io .files .FileUtil ;
8
10
import twg2 .io .json .stringify .JsonStringify ;
11
+ import twg2 .parser .condition .text .CharParser ;
9
12
import twg2 .parser .output .JsonWritableSig ;
10
13
import twg2 .parser .output .WriteSettings ;
11
14
import twg2 .text .stringEscape .StringEscapeJson ;
12
15
import 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 ;
15
19
16
20
/**
17
21
* @author TeamworkGuy2
18
22
* @since 2016-09-11
19
23
*/
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 ;
60
31
61
32
62
33
public ParserActionLogger () {
63
- this .actions = new EnumMap <>(ParserAction .class );
64
- }
65
-
66
-
67
- public CountAndDurationLog getLog (ParserAction action ) {
68
- return this .actions .get (action );
69
34
}
70
35
71
36
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 ;
81
40
}
82
41
83
42
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 ;
87
46
}
88
47
89
48
90
49
@ Override
91
- public void logMsg ( ParserAction action , String msg ) {
92
- getTypedAction ( action ). msg = msg ;
50
+ public void logCountCreateParser ( int count ) {
51
+ countCreateParser += count ;
93
52
}
94
53
95
54
96
55
@ Override
97
- public void logCount ( ParserAction action , long count ) {
98
- getTypedAction ( action ). count += count ;
56
+ public void logCountTextFragmentsConsumed ( int count ) {
57
+ countTextFragmentsConsumed += count ;
99
58
}
100
59
101
60
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
+ }
105
72
}
106
73
107
74
@@ -111,7 +78,6 @@ public void toJson(Appendable dst, WriteSettings st) throws IOException {
111
78
}
112
79
113
80
114
- @ Override
115
81
public void toJson (String srcName , boolean includeSurroundingBrackets , Appendable dst , WriteSettings st ) throws IOException {
116
82
if (includeSurroundingBrackets ) { dst .append ("{\n " ); }
117
83
if (srcName != null ) {
@@ -120,16 +86,20 @@ public void toJson(String srcName, boolean includeSurroundingBrackets, Appendabl
120
86
dst .append ("\" " );
121
87
}
122
88
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 ));
133
103
}
134
104
135
105
if (includeSurroundingBrackets ) { dst .append ("\n }" ); }
@@ -142,7 +112,6 @@ public String toString() {
142
112
}
143
113
144
114
145
- @ Override
146
115
public String toString (String srcName , boolean includeClassName ) {
147
116
var dst = new StringBuilder ();
148
117
if (includeClassName ) {
@@ -152,40 +121,38 @@ public String toString(String srcName, boolean includeClassName) {
152
121
dst .append ("file: " + srcName );
153
122
}
154
123
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 ));
164
134
}
135
+ if (countTextFragmentsConsumed > 0 ) {
136
+ dst .append (", " );
137
+ dst .append ("textFragmentsConsumed: " ).append (Integer .toString (countTextFragmentsConsumed ));
138
+ }
139
+
165
140
if (includeClassName ) {
166
141
dst .append ("\n }" );
167
142
}
168
143
return dst .toString ();
169
144
}
170
145
171
146
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
-
182
147
public static void toJsons (Map <String , ParserActionLogger > fileParserDetails , boolean includeSurroundingBrackets , Appendable dst , WriteSettings st ) throws IOException {
183
148
if (includeSurroundingBrackets ) { dst .append ("[\n " ); }
149
+
184
150
JsonStringify .inst .joinConsume (fileParserDetails .entrySet (), ",\n " , dst , (entry ) -> {
185
151
var stat = entry .getValue ();
186
152
String fileName = FileUtil .getFileNameWithoutExtension (entry .getKey ());
187
153
stat .toJson (fileName , includeSurroundingBrackets , dst , st );
188
154
});
155
+
189
156
if (includeSurroundingBrackets ) { dst .append ("\n ]" ); }
190
157
}
191
158
0 commit comments