55
55
56
56
public class CppTypes {
57
57
58
- private static final Pattern ATOM_DEF = Pattern .compile ("^GK_ATOM\\ (([^,]+),\\ s*\" ([^\" ]*)\" \\ ).*$" );
58
+ /* Please note we aren't looking for the following Atom definitions:
59
+ PseudoElementAtom or NonInheritingAnonBoxAtom or InheritingAnonBoxAtom */
60
+ private static final Pattern ATOM_DEF = Pattern .compile ("^\\ s*Atom\\ (\" ([^,]+)\" ,\\ s*\" ([^\" ]*)\" \\ ).*$" );
59
61
60
62
private static Set <String > reservedWords = new HashSet <String >();
61
63
@@ -122,17 +124,15 @@ public class CppTypes {
122
124
123
125
private final Writer atomWriter ;
124
126
125
- private StringBuilder manualAtoms = new StringBuilder ();
126
-
127
- public CppTypes (File atomList ) {
127
+ public CppTypes (File atomList , File generatedAtomFile ) {
128
128
if (atomList == null ) {
129
129
atomWriter = null ;
130
130
} else {
131
131
try {
132
132
ingestAtoms (atomList );
133
133
atomWriter = new OutputStreamWriter (new FileOutputStream (
134
- atomList ), "utf-8" );
135
- atomWriter . append ( manualAtoms );
134
+ generatedAtomFile ), "utf-8" );
135
+ this . start ( );
136
136
} catch (IOException e ) {
137
137
throw new RuntimeException (e );
138
138
}
@@ -145,13 +145,19 @@ private void ingestAtoms(File atomList) throws IOException {
145
145
new InputStreamReader (new FileInputStream (atomList ), "utf-8" ));
146
146
try {
147
147
String line ;
148
+ boolean startedParsing = false ;
148
149
while ((line = atomReader .readLine ()) != null ) {
149
- manualAtoms .append (line );
150
- manualAtoms .append ('\n' );
151
- if (line .startsWith ("// BEGIN GENERATED" )) {
150
+ // only start parsing lines after this comment
151
+ if (line .trim ().startsWith ("# START ATOMS" )) {
152
+ startedParsing = true ;
153
+ } else if (!startedParsing ) {
154
+ continue ;
155
+ }
156
+ // stop parsing lines after this comment
157
+ if (line .trim ().startsWith ("# END ATOMS" )) {
152
158
return ;
153
159
}
154
- if (!line .startsWith ("GK_ATOM " )) {
160
+ if (!line .trim (). startsWith ("Atom " )) {
155
161
continue ;
156
162
}
157
163
Matcher m = ATOM_DEF .matcher (line );
@@ -167,10 +173,23 @@ private void ingestAtoms(File atomList) throws IOException {
167
173
}
168
174
}
169
175
176
+ public void start () {
177
+ try {
178
+
179
+ if (atomWriter != null ) {
180
+ atomWriter .write ("# THIS FILE IS GENERATED BY THE HTML PARSER TRANSLATOR AND WILL BE OVERWRITTEN!\n " );
181
+ atomWriter .write ("from Atom import Atom\n \n " );
182
+ atomWriter .write ("HTML_PARSER_ATOMS = [\n " );
183
+ }
184
+ } catch (IOException e ) {
185
+ throw new RuntimeException (e );
186
+ }
187
+ }
188
+
170
189
public void finished () {
171
190
try {
172
191
if (atomWriter != null ) {
173
- atomWriter .write ("// END GENERATED ATOMS, DO NOT ADD CODE BELOW THIS LINE \n " );
192
+ atomWriter .write ("] \n " );
174
193
atomWriter .flush ();
175
194
atomWriter .close ();
176
195
}
@@ -303,8 +322,8 @@ public String localForLiteral(String literal) {
303
322
atomMap .put (literal , atom );
304
323
if (atomWriter != null ) {
305
324
try {
306
- atomWriter .write ("// ATOM GENERATED BY HTML PARSER TRANSLATOR (WILL BE AUTOMATICALLY OVERWRITTEN):\n GK_ATOM(" + atom + ", \" " + literal
307
- + "\" )\n " );
325
+ atomWriter .write (" # ATOM GENERATED BY HTML PARSER TRANSLATOR (WILL BE AUTOMATICALLY OVERWRITTEN):\n Atom( \" " + atom + " \ " , \" " + literal
326
+ + "\" ), \n " );
308
327
} catch (IOException e ) {
309
328
throw new RuntimeException (e );
310
329
}
0 commit comments