Skip to content

Commit 15f9ede

Browse files
author
Jonathan Kingston
committed
Bug 1483458 - Change HTMLParser to look at .py atom files. r=hsivonen,heycam.
1 parent 6fbd6bd commit 15f9ede

File tree

4 files changed

+38
-17
lines changed

4 files changed

+38
-17
lines changed

translator-src/nu/validator/htmlparser/cpptranslate/CppTypes.java

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@
5555

5656
public class CppTypes {
5757

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*\"([^\"]*)\"\\).*$");
5961

6062
private static Set<String> reservedWords = new HashSet<String>();
6163

@@ -122,17 +124,15 @@ public class CppTypes {
122124

123125
private final Writer atomWriter;
124126

125-
private StringBuilder manualAtoms = new StringBuilder();
126-
127-
public CppTypes(File atomList) {
127+
public CppTypes(File atomList, File generatedAtomFile) {
128128
if (atomList == null) {
129129
atomWriter = null;
130130
} else {
131131
try {
132132
ingestAtoms(atomList);
133133
atomWriter = new OutputStreamWriter(new FileOutputStream(
134-
atomList), "utf-8");
135-
atomWriter.append(manualAtoms);
134+
generatedAtomFile), "utf-8");
135+
this.start();
136136
} catch (IOException e) {
137137
throw new RuntimeException(e);
138138
}
@@ -145,13 +145,19 @@ private void ingestAtoms(File atomList) throws IOException {
145145
new InputStreamReader(new FileInputStream(atomList), "utf-8"));
146146
try {
147147
String line;
148+
boolean startedParsing = false;
148149
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")) {
152158
return;
153159
}
154-
if (!line.startsWith("GK_ATOM")) {
160+
if (!line.trim().startsWith("Atom")) {
155161
continue;
156162
}
157163
Matcher m = ATOM_DEF.matcher(line);
@@ -167,10 +173,23 @@ private void ingestAtoms(File atomList) throws IOException {
167173
}
168174
}
169175

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+
170189
public void finished() {
171190
try {
172191
if (atomWriter != null) {
173-
atomWriter.write("// END GENERATED ATOMS, DO NOT ADD CODE BELOW THIS LINE\n");
192+
atomWriter.write("]\n");
174193
atomWriter.flush();
175194
atomWriter.close();
176195
}
@@ -303,8 +322,8 @@ public String localForLiteral(String literal) {
303322
atomMap.put(literal, atom);
304323
if (atomWriter != null) {
305324
try {
306-
atomWriter.write("// ATOM GENERATED BY HTML PARSER TRANSLATOR (WILL BE AUTOMATICALLY OVERWRITTEN):\nGK_ATOM(" + atom + ", \"" + literal
307-
+ "\")\n");
325+
atomWriter.write(" # ATOM GENERATED BY HTML PARSER TRANSLATOR (WILL BE AUTOMATICALLY OVERWRITTEN):\n Atom(\"" + atom + "\", \"" + literal
326+
+ "\"),\n");
308327
} catch (IOException e) {
309328
throw new RuntimeException(e);
310329
}

translator-src/nu/validator/htmlparser/cpptranslate/GkAtomParser.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@
4747

4848
public class GkAtomParser {
4949

50-
private static final Pattern ATOM = Pattern.compile("^GK_ATOM\\(([^,]+),\\s*\"([^\"]*)\"\\).*$");
50+
/* Please note we aren't looking for the following Atom definitions:
51+
PseudoElementAtom or NonInheritingAnonBoxAtom or InheritingAnonBoxAtom */
52+
private static final Pattern ATOM = Pattern.compile("^Atom\\(\"([^,]+)\",\\s*\"([^\"]*)\"\\).*$");
5153

5254
private final BufferedReader reader;
5355

@@ -59,7 +61,7 @@ public Map<String, String> parse() throws IOException {
5961
Map<String, String> map = new HashMap<String, String>();
6062
String line;
6163
while((line = reader.readLine()) != null) {
62-
Matcher m = ATOM.matcher(line);
64+
Matcher m = ATOM.matcher(line.trim());
6365
if (m.matches()) {
6466
map.put(m.group(2), m.group(1));
6567
}

translator-src/nu/validator/htmlparser/cpptranslate/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public class Main {
8080
* @throws IOException
8181
*/
8282
public static void main(String[] args) throws ParseException, IOException {
83-
CppTypes cppTypes = new CppTypes(new File(args[2]));
83+
CppTypes cppTypes = new CppTypes(new File(args[2]), new File(args[3]));
8484
SymbolTable symbolTable = new SymbolTable();
8585

8686
File javaDirectory = new File(args[0]);

translator-src/nu/validator/htmlparser/generator/GenerateNamedCharactersCpp.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public static void main(String[] args) throws IOException {
141141
}
142142
}
143143

144-
CppTypes cppTypes = new CppTypes(null);
144+
CppTypes cppTypes = new CppTypes(null, null);
145145
File targetDirectory = new File(args[1]);
146146

147147
generateH(targetDirectory, cppTypes, entities);

0 commit comments

Comments
 (0)